Debian Codegraph @

Lib/test/test_cppext/setup.py

raw
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 platform
5 import shlex
6 import sys
7 import sysconfig
8 from test import support
9
10 from setuptools import setup, Extension
11
12
13 SOURCE = 'extension.cpp'
14
15 if not support.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 # MSVC compiler flags
25 CPPFLAGS = [
26 # Display warnings level 1 to 4
27 '/W4',
28 # Treat all compiler warnings as compiler errors
29 '/WX',
30 ]
31
32
33 def main():
34 cppflags = list(CPPFLAGS)
35 std = os.environ.get("CPYTHON_TEST_CPP_STD", "")
36 module_name = os.environ["CPYTHON_TEST_EXT_NAME"]
37 limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", ""))
38
39 cppflags = list(CPPFLAGS)
40 cppflags.append(f'-DMODULE_NAME={module_name}')
41
42 # Add -std=STD or /std:STD (MSVC) compiler flag
43 if std:
44 if support.MS_WINDOWS:
45 cppflags.append(f'/std:{std}')
46 else:
47 cppflags.append(f'-std={std}')
48
49 # gh-105776: When "gcc -std=11" is used as the C++ compiler, -std=c11
50 # option emits a C++ compiler warning. Remove "-std11" option from the
51 # CC command.
52 cmd = (sysconfig.get_config_var('CC') or '')
53 if cmd is not None:
54 if support.MS_WINDOWS:
55 std_prefix = '/std'
56 else:
57 std_prefix = '-std'
58 cmd = shlex.split(cmd)
59 cmd = [arg for arg in cmd if not arg.startswith(std_prefix)]
60 cmd = shlex.join(cmd)
61 # CC env var overrides sysconfig CC variable in setuptools
62 os.environ['CC'] = cmd
63
64 # Define Py_LIMITED_API macro
65 if limited:
66 version = sys.hexversion
67 cppflags.append(f'-DPy_LIMITED_API={version:#x}')
68
69 # On Windows, add PCbuild\amd64\ to include and library directories
70 include_dirs = []
71 library_dirs = []
72 if support.MS_WINDOWS:
73 srcdir = sysconfig.get_config_var('srcdir')
74 machine = platform.uname().machine
75 pcbuild = os.path.join(srcdir, 'PCbuild', machine)
76 if os.path.exists(pcbuild):
77 # pyconfig.h is generated in PCbuild\amd64\
78 include_dirs.append(pcbuild)
79 # python313.lib is generated in PCbuild\amd64\
80 library_dirs.append(pcbuild)
81 print(f"Add PCbuild directory: {pcbuild}")
82
83 # Display information to help debugging
84 for env_name in ('CC', 'CFLAGS', 'CPPFLAGS'):
85 if env_name in os.environ:
86 print(f"{env_name} env var: {os.environ[env_name]!r}")
87 else:
88 print(f"{env_name} env var: <missing>")
89 print(f"extra_compile_args: {cppflags!r}")
90
91 ext = Extension(
92 module_name,
93 sources=[SOURCE],
94 language='c++',
95 extra_compile_args=cppflags,
96 include_dirs=include_dirs,
97 library_dirs=library_dirs)
98 setup(name=f'internal_{module_name}',
99 version='0.0',
100 ext_modules=[ext])
101
102
103 if __name__ == "__main__":
104 main()