Debian Codegraph @

Lib/test/test_cext/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.c'
14
15 if not support.MS_WINDOWS:
16 # C compiler flags for GCC and clang
17 CFLAGS = [
18 # The purpose of test_cext extension is to check that building a C
19 # extension using the Python C API does not emit C compiler warnings.
20 '-Werror',
21
22 # gh-120593: Check the 'const' qualifier
23 '-Wcast-qual',
24 ]
25 if not support.Py_GIL_DISABLED:
26 CFLAGS.append(
27 # gh-116869: The Python C API must be compatible with building
28 # with the -Werror=declaration-after-statement compiler flag.
29 '-Werror=declaration-after-statement',
30 )
31 else:
32 # MSVC compiler flags
33 CFLAGS = [
34 # Display warnings level 1 to 4
35 '/W4',
36 # Treat all compiler warnings as compiler errors
37 '/WX',
38 ]
39
40
41 def main():
42 std = os.environ.get("CPYTHON_TEST_STD", "")
43 module_name = os.environ["CPYTHON_TEST_EXT_NAME"]
44 limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", ""))
45
46 cflags = list(CFLAGS)
47 cflags.append(f'-DMODULE_NAME={module_name}')
48
49 # Add -std=STD or /std:STD (MSVC) compiler flag
50 if std:
51 if support.MS_WINDOWS:
52 cflags.append(f'/std:{std}')
53 else:
54 cflags.append(f'-std={std}')
55
56 # Remove existing -std or /std options from CC command line.
57 # Python adds -std=c11 option.
58 cmd = (sysconfig.get_config_var('CC') or '')
59 if cmd is not None:
60 if support.MS_WINDOWS:
61 std_prefix = '/std'
62 else:
63 std_prefix = '-std'
64 cmd = shlex.split(cmd)
65 cmd = [arg for arg in cmd if not arg.startswith(std_prefix)]
66 cmd = shlex.join(cmd)
67 # CC env var overrides sysconfig CC variable in setuptools
68 os.environ['CC'] = cmd
69
70 # Define Py_LIMITED_API macro
71 if limited:
72 version = sys.hexversion
73 cflags.append(f'-DPy_LIMITED_API={version:#x}')
74
75 # On Windows, add PCbuild\amd64\ to include and library directories
76 include_dirs = []
77 library_dirs = []
78 if support.MS_WINDOWS:
79 srcdir = sysconfig.get_config_var('srcdir')
80 machine = platform.uname().machine
81 pcbuild = os.path.join(srcdir, 'PCbuild', machine)
82 if os.path.exists(pcbuild):
83 # pyconfig.h is generated in PCbuild\amd64\
84 include_dirs.append(pcbuild)
85 # python313.lib is generated in PCbuild\amd64\
86 library_dirs.append(pcbuild)
87 print(f"Add PCbuild directory: {pcbuild}")
88
89 # Display information to help debugging
90 for env_name in ('CC', 'CFLAGS'):
91 if env_name in os.environ:
92 print(f"{env_name} env var: {os.environ[env_name]!r}")
93 else:
94 print(f"{env_name} env var: <missing>")
95 print(f"extra_compile_args: {cflags!r}")
96
97 ext = Extension(
98 module_name,
99 sources=[SOURCE],
100 extra_compile_args=cflags,
101 include_dirs=include_dirs,
102 library_dirs=library_dirs)
103 setup(name=f'internal_{module_name}',
104 version='0.0',
105 ext_modules=[ext])
106
107
108 if __name__ == "__main__":
109 main()