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 os.path
4 import shlex
5 import sys
6 import sysconfig
7 from test import support
8
9 from setuptools import setup, Extension
10
11
12 SOURCE = os.path.join(os.path.dirname(__file__), 'extension.cpp')
13 if not support.MS_WINDOWS:
14 # C++ compiler flags for GCC and clang
15 CPPFLAGS = [
16 # gh-91321: The purpose of _testcppext extension is to check that building
17 # a C++ extension using the Python C API does not emit C++ compiler
18 # warnings
19 '-Werror',
20 ]
21 else:
22 # Don't pass any compiler flag to MSVC
23 CPPFLAGS = []
24
25
26 def main():
27 cppflags = list(CPPFLAGS)
28 if '-std=c++03' in sys.argv:
29 sys.argv.remove('-std=c++03')
30 std = 'c++03'
31 name = '_testcpp03ext'
32 else:
33 # Python currently targets C++11
34 std = 'c++11'
35 name = '_testcpp11ext'
36
37 cppflags = [*CPPFLAGS, f'-std={std}']
38
39 # gh-105776: When "gcc -std=11" is used as the C++ compiler, -std=c11
40 # option emits a C++ compiler warning. Remove "-std11" option from the
41 # CC command.
42 cmd = (sysconfig.get_config_var('CC') or '')
43 if cmd is not None:
44 cmd = shlex.split(cmd)
45 cmd = [arg for arg in cmd if not arg.startswith('-std=')]
46 cmd = shlex.join(cmd)
47 # CC env var overrides sysconfig CC variable in setuptools
48 os.environ['CC'] = cmd
49
50 cpp_ext = Extension(
51 name,
52 sources=[SOURCE],
53 language='c++',
54 extra_compile_args=cppflags)
55 setup(name='internal' + name, version='0.0', ext_modules=[cpp_ext])
56
57
58 if __name__ == "__main__":
59 main()