| 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 |
|
| 24 |
CPPFLAGS_PEDANTIC = [ |
| 25 |
# Ask for strict(er) compliance with the standard. |
| 26 |
# We cannot do this for c++03 unlimited API, since several headers in |
| 27 |
# Include/cpython/ use commas at end of `enum` declarations, a C++11 |
| 28 |
# feature for which GCC has no narrower option than -Wpedantic itself. |
| 29 |
'-pedantic-errors', |
| 30 |
|
| 31 |
# We also use `long long`, a C++11 feature we can enable individually. |
| 32 |
'-Wno-long-long', |
| 33 |
] |
| 34 |
else: |
| 35 |
# MSVC compiler flags |
| 36 |
CPPFLAGS = [ |
| 37 |
# Display warnings level 1 to 4 |
| 38 |
'/W4', |
| 39 |
# Treat all compiler warnings as compiler errors |
| 40 |
'/WX', |
| 41 |
] |
| 42 |
CPPFLAGS_PEDANTIC = [] |
| 43 |
|
| 44 |
|
| 45 |
def main(): |
| 46 |
cppflags = list(CPPFLAGS) |
| 47 |
std = os.environ.get("CPYTHON_TEST_CPP_STD", "") |
| 48 |
module_name = os.environ["CPYTHON_TEST_EXT_NAME"] |
| 49 |
limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", "")) |
| 50 |
internal = bool(int(os.environ.get("TEST_INTERNAL_C_API", "0"))) |
| 51 |
|
| 52 |
cppflags = list(CPPFLAGS) |
| 53 |
cppflags.append(f'-DMODULE_NAME={module_name}') |
| 54 |
|
| 55 |
# Add -std=STD or /std:STD (MSVC) compiler flag |
| 56 |
if std: |
| 57 |
if support.MS_WINDOWS: |
| 58 |
cppflags.append(f'/std:{std}') |
| 59 |
else: |
| 60 |
cppflags.append(f'-std={std}') |
| 61 |
|
| 62 |
if limited or (std != 'c++03') and not internal: |
| 63 |
# See CPPFLAGS_PEDANTIC docstring |
| 64 |
cppflags.extend(CPPFLAGS_PEDANTIC) |
| 65 |
|
| 66 |
# gh-105776: When "gcc -std=11" is used as the C++ compiler, -std=c11 |
| 67 |
# option emits a C++ compiler warning. Remove "-std11" option from the |
| 68 |
# CC command. |
| 69 |
cmd = (sysconfig.get_config_var('CC') or '') |
| 70 |
if cmd is not None: |
| 71 |
if support.MS_WINDOWS: |
| 72 |
std_prefix = '/std' |
| 73 |
else: |
| 74 |
std_prefix = '-std' |
| 75 |
cmd = shlex.split(cmd) |
| 76 |
cmd = [arg for arg in cmd if not arg.startswith(std_prefix)] |
| 77 |
cmd = shlex.join(cmd) |
| 78 |
# CC env var overrides sysconfig CC variable in setuptools |
| 79 |
os.environ['CC'] = cmd |
| 80 |
|
| 81 |
# Define Py_LIMITED_API macro |
| 82 |
if limited: |
| 83 |
version = sys.hexversion |
| 84 |
cppflags.append(f'-DPy_LIMITED_API={version:#x}') |
| 85 |
|
| 86 |
if internal: |
| 87 |
cppflags.append('-DTEST_INTERNAL_C_API=1') |
| 88 |
|
| 89 |
extra_cflags = os.environ.get("CPYTHON_TEST_EXTRA_CFLAGS", "") |
| 90 |
if extra_cflags: |
| 91 |
cppflags.extend(shlex.split(extra_cflags)) |
| 92 |
|
| 93 |
# On Windows, add PCbuild\amd64\ to include and library directories |
| 94 |
include_dirs = [] |
| 95 |
library_dirs = [] |
| 96 |
if support.MS_WINDOWS: |
| 97 |
srcdir = sysconfig.get_config_var('srcdir') |
| 98 |
machine = platform.uname().machine |
| 99 |
pcbuild = os.path.join(srcdir, 'PCbuild', machine) |
| 100 |
if os.path.exists(pcbuild): |
| 101 |
# pyconfig.h is generated in PCbuild\amd64\ |
| 102 |
include_dirs.append(pcbuild) |
| 103 |
# python313.lib is generated in PCbuild\amd64\ |
| 104 |
library_dirs.append(pcbuild) |
| 105 |
print(f"Add PCbuild directory: {pcbuild}") |
| 106 |
|
| 107 |
# Display information to help debugging |
| 108 |
for env_name in ('CC', 'CXX', 'CFLAGS', 'CPPFLAGS', 'CXXFLAGS'): |
| 109 |
if env_name in os.environ: |
| 110 |
print(f"{env_name} env var: {os.environ[env_name]!r}") |
| 111 |
else: |
| 112 |
print(f"{env_name} env var: <missing>") |
| 113 |
print(f"extra_compile_args: {cppflags!r}") |
| 114 |
|
| 115 |
ext = Extension( |
| 116 |
module_name, |
| 117 |
sources=[SOURCE], |
| 118 |
language='c++', |
| 119 |
extra_compile_args=cppflags, |
| 120 |
include_dirs=include_dirs, |
| 121 |
library_dirs=library_dirs) |
| 122 |
setup(name=f'internal_{module_name}', |
| 123 |
version='0.0', |
| 124 |
ext_modules=[ext]) |
| 125 |
|
| 126 |
|
| 127 |
if __name__ == "__main__": |
| 128 |
main() |