| 1 |
"""Build script for mypyc C runtime library and C API unit tests. |
| 2 |
|
| 3 |
The tests are written in C++ and use the Google Test framework. |
| 4 |
""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import os |
| 9 |
import subprocess |
| 10 |
import sys |
| 11 |
from distutils import ccompiler, sysconfig |
| 12 |
from typing import Any |
| 13 |
|
| 14 |
# we'll import stuff from the source tree, let's ensure is on the sys path |
| 15 |
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) |
| 16 |
|
| 17 |
import build_setup # noqa: F401 |
| 18 |
from setuptools import Extension, setup |
| 19 |
from setuptools.command.build_ext import build_ext |
| 20 |
|
| 21 |
C_APIS_TO_TEST = [ |
| 22 |
"init.c", |
| 23 |
"int_ops.c", |
| 24 |
"float_ops.c", |
| 25 |
"list_ops.c", |
| 26 |
"exc_ops.c", |
| 27 |
"generic_ops.c", |
| 28 |
"pythonsupport.c", |
| 29 |
] |
| 30 |
|
| 31 |
|
| 32 |
class BuildExtGtest(build_ext): |
| 33 |
def get_library_names(self) -> list[str]: |
| 34 |
return ["gtest"] |
| 35 |
|
| 36 |
def run(self) -> None: |
| 37 |
# Build Google Test, the C++ framework we use for testing C code. |
| 38 |
# The source code for Google Test is copied to this repository. |
| 39 |
gtest_dir = os.path.abspath( |
| 40 |
os.path.join(os.path.dirname(__file__), "..", "external", "googletest") |
| 41 |
) |
| 42 |
os.makedirs(self.build_temp, exist_ok=True) |
| 43 |
subprocess.check_call( |
| 44 |
["make", "-f", os.path.join(gtest_dir, "make", "Makefile"), f"GTEST_DIR={gtest_dir}"], |
| 45 |
cwd=self.build_temp, |
| 46 |
) |
| 47 |
self.library_dirs = [self.build_temp] |
| 48 |
return build_ext.run(self) |
| 49 |
|
| 50 |
|
| 51 |
if "--run-capi-tests" in sys.argv: |
| 52 |
sys.argv.pop() |
| 53 |
|
| 54 |
kwargs: dict[str, Any] |
| 55 |
if sys.platform == "darwin": |
| 56 |
kwargs = {"language": "c++"} |
| 57 |
compile_args: list[str] = [] |
| 58 |
else: |
| 59 |
kwargs = {} |
| 60 |
compile_args = ["--std=c++11"] |
| 61 |
|
| 62 |
setup( |
| 63 |
name="test_capi", |
| 64 |
version="0.1", |
| 65 |
ext_modules=[ |
| 66 |
Extension( |
| 67 |
"test_capi", |
| 68 |
["test_capi.cc"] + C_APIS_TO_TEST, |
| 69 |
depends=["CPy.h", "mypyc_util.h", "pythonsupport.h"], |
| 70 |
extra_compile_args=["-Wno-unused-function", "-Wno-sign-compare"] + compile_args, |
| 71 |
libraries=["gtest"], |
| 72 |
include_dirs=["../external/googletest", "../external/googletest/include"], |
| 73 |
**kwargs, |
| 74 |
) |
| 75 |
], |
| 76 |
cmdclass={"build_ext": BuildExtGtest}, |
| 77 |
) |
| 78 |
else: |
| 79 |
compiler = ccompiler.new_compiler() |
| 80 |
sysconfig.customize_compiler(compiler) |
| 81 |
cflags: list[str] = [] |
| 82 |
if compiler.compiler_type == "unix": # type: ignore[attr-defined] |
| 83 |
cflags += ["-O3", "-Wno-unused-function"] |
| 84 |
elif compiler.compiler_type == "msvc": # type: ignore[attr-defined] |
| 85 |
cflags += ["/O2"] |
| 86 |
|
| 87 |
setup( |
| 88 |
ext_modules=[ |
| 89 |
Extension( |
| 90 |
"librt.internal", |
| 91 |
[ |
| 92 |
"internal/librt_internal.c", |
| 93 |
"init.c", |
| 94 |
"int_ops.c", |
| 95 |
"exc_ops.c", |
| 96 |
"pythonsupport.c", |
| 97 |
"getargsfast.c", |
| 98 |
], |
| 99 |
include_dirs=[".", "internal"], |
| 100 |
extra_compile_args=cflags, |
| 101 |
), |
| 102 |
Extension( |
| 103 |
"librt.strings", |
| 104 |
[ |
| 105 |
"strings/librt_strings.c", |
| 106 |
"init.c", |
| 107 |
"int_ops.c", |
| 108 |
"exc_ops.c", |
| 109 |
"pythonsupport.c", |
| 110 |
"getargsfast.c", |
| 111 |
], |
| 112 |
include_dirs=[".", "strings"], |
| 113 |
extra_compile_args=cflags, |
| 114 |
), |
| 115 |
Extension( |
| 116 |
"librt.base64", |
| 117 |
[ |
| 118 |
"base64/librt_base64.c", |
| 119 |
"base64/lib.c", |
| 120 |
"base64/codec_choose.c", |
| 121 |
"base64/tables/tables.c", |
| 122 |
"base64/arch/generic/codec.c", |
| 123 |
"base64/arch/ssse3/codec.c", |
| 124 |
"base64/arch/sse41/codec.c", |
| 125 |
"base64/arch/sse42/codec.c", |
| 126 |
"base64/arch/avx/codec.c", |
| 127 |
"base64/arch/avx2/codec.c", |
| 128 |
"base64/arch/avx512/codec.c", |
| 129 |
"base64/arch/neon32/codec.c", |
| 130 |
"base64/arch/neon64/codec.c", |
| 131 |
], |
| 132 |
include_dirs=[".", "base64"], |
| 133 |
extra_compile_args=cflags, |
| 134 |
), |
| 135 |
Extension( |
| 136 |
"librt.vecs", |
| 137 |
[ |
| 138 |
"vecs/librt_vecs.c", |
| 139 |
"vecs/vec_i64.c", |
| 140 |
"vecs/vec_i32.c", |
| 141 |
"vecs/vec_i16.c", |
| 142 |
"vecs/vec_u8.c", |
| 143 |
"vecs/vec_float.c", |
| 144 |
"vecs/vec_bool.c", |
| 145 |
"vecs/vec_t.c", |
| 146 |
"vecs/vec_nested.c", |
| 147 |
], |
| 148 |
include_dirs=[".", "vecs"], |
| 149 |
extra_compile_args=cflags, |
| 150 |
), |
| 151 |
Extension( |
| 152 |
"librt.time", ["time/librt_time.c"], include_dirs=["."], extra_compile_args=cflags |
| 153 |
), |
| 154 |
Extension( |
| 155 |
"librt.random", |
| 156 |
[ |
| 157 |
"random/librt_random.c", |
| 158 |
"init.c", |
| 159 |
"int_ops.c", |
| 160 |
"exc_ops.c", |
| 161 |
"pythonsupport.c", |
| 162 |
"getargsfast.c", |
| 163 |
], |
| 164 |
include_dirs=["."], |
| 165 |
extra_compile_args=cflags, |
| 166 |
), |
| 167 |
] |
| 168 |
) |