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