| 1 |
#!/usr/bin/env python3 |
| 2 |
import os.path |
| 3 |
import shlex |
| 4 |
import sys |
| 5 |
try: |
| 6 |
from setuptools import setup, Extension |
| 7 |
except ImportError: |
| 8 |
from distutils.core import setup, Extension |
| 9 |
try: |
| 10 |
from distutils import sysconfig |
| 11 |
except ImportError: |
| 12 |
import sysconfig |
| 13 |
|
| 14 |
|
| 15 |
# C++ is only supported on Python 3.6 and newer |
| 16 |
TEST_CXX = (sys.version_info >= (3, 6)) |
| 17 |
|
| 18 |
SRC_DIR = os.path.normpath(os.path.join(os.path.dirname(__file__), '..')) |
| 19 |
|
| 20 |
# Windows uses MSVC compiler |
| 21 |
MSVC = (os.name == "nt") |
| 22 |
|
| 23 |
COMMON_FLAGS = [ |
| 24 |
'-I' + SRC_DIR, |
| 25 |
] |
| 26 |
if not MSVC: |
| 27 |
# C compiler flags for GCC and clang |
| 28 |
COMMON_FLAGS.extend(( |
| 29 |
# Treat warnings as error |
| 30 |
'-Werror', |
| 31 |
# Enable all warnings |
| 32 |
'-Wall', '-Wextra', |
| 33 |
# Extra warnings |
| 34 |
'-Wconversion', |
| 35 |
# /usr/lib64/pypy3.7/include/pyport.h:68:20: error: redefinition of typedef |
| 36 |
# 'Py_hash_t' is a C11 feature |
| 37 |
'-Wno-typedef-redefinition', |
| 38 |
# Formatting checks |
| 39 |
'-Wformat', |
| 40 |
'-Wformat-nonliteral', |
| 41 |
'-Wformat-security', |
| 42 |
)) |
| 43 |
CFLAGS = COMMON_FLAGS + [ |
| 44 |
# Use C99 for pythoncapi_compat.c which initializes PyModuleDef with a |
| 45 |
# mixture of designated and non-designated initializers |
| 46 |
'-std=c99', |
| 47 |
] |
| 48 |
else: |
| 49 |
# C compiler flags for MSVC |
| 50 |
COMMON_FLAGS.extend(( |
| 51 |
# Treat all compiler warnings as compiler errors |
| 52 |
'/WX', |
| 53 |
)) |
| 54 |
CFLAGS = list(COMMON_FLAGS) |
| 55 |
CXXFLAGS = list(COMMON_FLAGS) |
| 56 |
|
| 57 |
|
| 58 |
def main(): |
| 59 |
# gh-105776: When "gcc -std=11" is used as the C++ compiler, -std=c11 |
| 60 |
# option emits a C++ compiler warning. Remove "-std11" option from the |
| 61 |
# CC command. |
| 62 |
cmd = (sysconfig.get_config_var('CC') or '') |
| 63 |
if cmd: |
| 64 |
cmd = shlex.split(cmd) |
| 65 |
cmd = [arg for arg in cmd if not arg.startswith('-std=')] |
| 66 |
if (sys.version_info >= (3, 8)): |
| 67 |
cmd = shlex.join(cmd) |
| 68 |
elif (sys.version_info >= (3, 3)): |
| 69 |
cmd = ' '.join(shlex.quote(arg) for arg in cmd) |
| 70 |
else: |
| 71 |
# Python 2.7 |
| 72 |
import pipes |
| 73 |
cmd = ' '.join(pipes.quote(arg) for arg in cmd) |
| 74 |
# CC env var overrides sysconfig CC variable in setuptools |
| 75 |
os.environ['CC'] = cmd |
| 76 |
|
| 77 |
# C extension |
| 78 |
c_ext = Extension( |
| 79 |
'test_pythoncapi_compat_cext', |
| 80 |
sources=['test_pythoncapi_compat_cext.c'], |
| 81 |
extra_compile_args=CFLAGS) |
| 82 |
extensions = [c_ext] |
| 83 |
|
| 84 |
if TEST_CXX: |
| 85 |
# C++ extension |
| 86 |
|
| 87 |
# MSVC has /std flag but doesn't support /std:c++11 |
| 88 |
if not MSVC: |
| 89 |
versions = [ |
| 90 |
('test_pythoncapi_compat_cpp03ext', ['-std=c++03']), |
| 91 |
('test_pythoncapi_compat_cpp11ext', ['-std=c++11']), |
| 92 |
] |
| 93 |
else: |
| 94 |
versions = [ |
| 95 |
('test_pythoncapi_compat_cppext', None), |
| 96 |
('test_pythoncapi_compat_cpp14ext', ['/std:c++14', '/Zc:__cplusplus']), |
| 97 |
] |
| 98 |
for name, std_flags in versions: |
| 99 |
flags = list(CXXFLAGS) |
| 100 |
if std_flags is not None: |
| 101 |
flags.extend(std_flags) |
| 102 |
cpp_ext = Extension( |
| 103 |
name, |
| 104 |
sources=['test_pythoncapi_compat_cppext.cpp'], |
| 105 |
extra_compile_args=flags, |
| 106 |
language='c++') |
| 107 |
extensions.append(cpp_ext) |
| 108 |
|
| 109 |
setup(name="test_pythoncapi_compat", |
| 110 |
ext_modules=extensions) |
| 111 |
|
| 112 |
|
| 113 |
if __name__ == "__main__": |
| 114 |
main() |