setup_testcppext.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # gh-91321: Build a basic C++ test extension to check that the Python C API is
  2. # compatible with C++ and does not emit C++ compiler warnings.
  3. import sys
  4. from test import support
  5. from setuptools import setup, Extension
  6. MS_WINDOWS = (sys.platform == 'win32')
  7. SOURCE = support.findfile('_testcppext.cpp')
  8. if not MS_WINDOWS:
  9. # C++ compiler flags for GCC and clang
  10. CPPFLAGS = [
  11. # gh-91321: The purpose of _testcppext extension is to check that building
  12. # a C++ extension using the Python C API does not emit C++ compiler
  13. # warnings
  14. '-Werror',
  15. ]
  16. else:
  17. # Don't pass any compiler flag to MSVC
  18. CPPFLAGS = []
  19. def main():
  20. cppflags = list(CPPFLAGS)
  21. if '-std=c++03' in sys.argv:
  22. sys.argv.remove('-std=c++03')
  23. std = 'c++03'
  24. name = '_testcpp03ext'
  25. else:
  26. # Python currently targets C++11
  27. std = 'c++11'
  28. name = '_testcpp11ext'
  29. cppflags = [*CPPFLAGS, f'-std={std}']
  30. cpp_ext = Extension(
  31. name,
  32. sources=[SOURCE],
  33. language='c++',
  34. extra_compile_args=cppflags)
  35. setup(name='internal' + name, version='0.0', ext_modules=[cpp_ext])
  36. if __name__ == "__main__":
  37. main()