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 BASE_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
23 # C compiler flags for GCC and clang
24 PUBLIC_CFLAGS = [
25 *BASE_CFLAGS,
26
27 # gh-120593: Check the 'const' qualifier
28 '-Wcast-qual',
29
30 # Ask for strict(er) compliance with the standard
31 '-pedantic-errors',
32 ]
33 if not support.Py_GIL_DISABLED:
34 PUBLIC_CFLAGS.append(
35 # gh-116869: The Python C API must be compatible with building
36 # with the -Werror=declaration-after-statement compiler flag.
37 '-Werror=declaration-after-statement',
38 )
39 INTERNAL_CFLAGS = [*BASE_CFLAGS]
40 else:
41 # MSVC compiler flags
42 BASE_CFLAGS = [
43 # Treat all compiler warnings as compiler errors
44 '/WX',
45 ]
46 PUBLIC_CFLAGS = [
47 *BASE_CFLAGS,
48 # Display warnings level 1 to 4
49 '/W4',
50 ]
51 INTERNAL_CFLAGS = [
52 *BASE_CFLAGS,
53 # Display warnings level 1 to 3
54 '/W3',
55 ]
56
57
58 def main():
59 std = os.environ.get("CPYTHON_TEST_STD", "")
60 module_name = os.environ["CPYTHON_TEST_EXT_NAME"]
61 limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", ""))
62 internal = bool(int(os.environ.get("TEST_INTERNAL_C_API", "0")))
63
64 sources = [SOURCE]
65
66 if not internal:
67 cflags = list(PUBLIC_CFLAGS)
68 else:
69 cflags = list(INTERNAL_CFLAGS)
70 cflags.append(f'-DMODULE_NAME={module_name}')
71
72 # Add -std=STD or /std:STD (MSVC) compiler flag
73 if std:
74 if support.MS_WINDOWS:
75 cflags.append(f'/std:{std}')
76 else:
77 cflags.append(f'-std={std}')
78
79 # Remove existing -std or /std options from CC command line.
80 # Python adds -std=c11 option.
81 cmd = (sysconfig.get_config_var('CC') or '')
82 if cmd is not None:
83 if support.MS_WINDOWS:
84 std_prefix = '/std'
85 else:
86 std_prefix = '-std'
87 cmd = shlex.split(cmd)
88 cmd = [arg for arg in cmd if not arg.startswith(std_prefix)]
89 cmd = shlex.join(cmd)
90 # CC env var overrides sysconfig CC variable in setuptools
91 os.environ['CC'] = cmd
92
93 # Define Py_LIMITED_API macro
94 if limited:
95 version = sys.hexversion
96 cflags.append(f'-DPy_LIMITED_API={version:#x}')
97
98 if internal:
99 cflags.append('-DTEST_INTERNAL_C_API=1')
100
101 # On Windows, add PCbuild\amd64\ to include and library directories
102 include_dirs = []
103 library_dirs = []
104 if support.MS_WINDOWS:
105 srcdir = sysconfig.get_config_var('srcdir')
106 machine = platform.uname().machine
107 pcbuild = os.path.join(srcdir, 'PCbuild', machine)
108 if os.path.exists(pcbuild):
109 # pyconfig.h is generated in PCbuild\amd64\
110 include_dirs.append(pcbuild)
111 # python313.lib is generated in PCbuild\amd64\
112 library_dirs.append(pcbuild)
113 print(f"Add PCbuild directory: {pcbuild}")
114
115 # Display information to help debugging
116 for env_name in ('CC', 'CFLAGS', 'CPPFLAGS'):
117 if env_name in os.environ:
118 print(f"{env_name} env var: {os.environ[env_name]!r}")
119 else:
120 print(f"{env_name} env var: <missing>")
121 print(f"extra_compile_args: {cflags!r}")
122
123 ext = Extension(
124 module_name,
125 sources=sources,
126 extra_compile_args=cflags,
127 include_dirs=include_dirs,
128 library_dirs=library_dirs)
129 setup(name=f'internal_{module_name}',
130 version='0.0',
131 ext_modules=[ext])
132
133
134 if __name__ == "__main__":
135 main()