Skip to content

Commit e400bb0

Browse files
authored
Add env var to disable SIMD in setup.py (#231)
* Add env var to disable SIMD in setup.py - Add `IPCTK_WITH_SIMD` environment variable to control SIMD support in Python builds - Reformat `setup.py` to match standard Python style guidelines * Update IPCTK_WITH_SIMD parsing and CI config - Support "off", "false", and "no" values for IPCTK_WITH_SIMD in setup.py - Re-enable SIMD for macOS and Windows Python CI builds * Add SIMD build instructions to Python README - Document the `IPCTK_WITH_SIMD` environment variable for `pip install` - List accepted falsy values for disabling SIMD optimizations - Add a note on forcing a source build from PyPI to apply the flag
1 parent de41cdb commit e400bb0

3 files changed

Lines changed: 37 additions & 21 deletions

File tree

.github/workflows/python.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ jobs:
7878
ccache --show-stats && ccache --zero-stats
7979
8080
- name: Build and Install
81+
env:
82+
IPCTK_WITH_SIMD: ${{ runner.os == 'Linux' && '0' || '1' }}
8183
run: |
8284
pip install --verbose . && ccache --show-stats
8385

python/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ You can test that the installation was successful by doing
3838
python -c "import ipctk"
3939
```
4040

41+
#### SIMD
42+
43+
SIMD optimizations are enabled by default. To disable them (e.g., for compatibility with older or cross-compiled targets), set `IPCTK_WITH_SIMD=0` before installing:
44+
45+
```sh
46+
IPCTK_WITH_SIMD=0 pip install .
47+
```
48+
49+
Accepted falsy values: `0`, `off`, `false`, `no` (case-insensitive). Any other value keeps SIMD enabled.
50+
51+
:::{note}
52+
Pre-built binary wheels from PyPI have SIMD support baked in at wheel-build time. The `IPCTK_WITH_SIMD` variable only applies when building from source. To force a source build from PyPI, use:
53+
```sh
54+
IPCTK_WITH_SIMD=0 pip install --no-binary ipctk ipctk
55+
```
56+
:::
57+
4158
### CMake Build
4259

4360
Alternatively, you can use `cmake` directly. To do this, use the following commands from the root of the repository:

setup.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,23 @@ def build_extension(self, ext):
3030
out = subprocess.check_output(["cmake", "--version"])
3131
except OSError:
3232
raise RuntimeError(
33-
"CMake must be installed to build the following extensions: " +
34-
", ".join(e.name for e in self.extensions))
33+
"CMake must be installed to build the following extensions: "
34+
+ ", ".join(e.name for e in self.extensions)
35+
)
3536

36-
extdir = os.path.abspath(os.path.dirname(
37-
self.get_ext_fullpath(ext.name)))
37+
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
3838

3939
# required for auto-detection & inclusion of auxiliary "native" libs
4040
if not extdir.endswith(os.path.sep):
4141
extdir += os.path.sep
4242

43-
debug = (int(os.environ.get("DEBUG", 0)) if self.debug is None
44-
else self.debug)
43+
debug = int(os.environ.get("DEBUG", 0)) if self.debug is None else self.debug
4544
cfg = "Debug" if debug else "Release"
4645

46+
# Allow users to disable SIMD via IPCTK_WITH_SIMD=0/off/false/no.
47+
_simd_env = os.environ.get("IPCTK_WITH_SIMD", "1").strip().lower()
48+
with_simd = "OFF" if _simd_env in {"0", "off", "false", "no"} else "ON"
49+
4750
# CMake lets you override the generator - we need to check this.
4851
# Can be set with Conda-Build, for example.
4952
cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
@@ -55,13 +58,13 @@ def build_extension(self, ext):
5558
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
5659
"-DIPC_TOOLKIT_BUILD_TESTS=OFF",
5760
"-DIPC_TOOLKIT_BUILD_PYTHON=ON",
61+
f"-DIPC_TOOLKIT_WITH_SIMD={with_simd}",
5862
]
5963
build_args = []
6064
# Adding CMake arguments set as environment variable
6165
# (needed e.g. to build for ARM OSx on conda-forge)
6266
if "CMAKE_ARGS" in os.environ:
63-
cmake_args += [
64-
item for item in os.environ["CMAKE_ARGS"].split(" ") if item]
67+
cmake_args += [item for item in os.environ["CMAKE_ARGS"].split(" ") if item]
6568

6669
if self.compiler.compiler_type != "msvc":
6770
# Using Ninja-build since it a) is available as a wheel and b)
@@ -73,8 +76,7 @@ def build_extension(self, ext):
7376
try:
7477
import ninja # noqa: F401
7578

76-
ninja_executable_path = os.path.join(
77-
ninja.BIN_DIR, "ninja")
79+
ninja_executable_path = os.path.join(ninja.BIN_DIR, "ninja")
7880
cmake_args += [
7981
"-GNinja",
8082
f"-DCMAKE_MAKE_PROGRAM:FILEPATH={ninja_executable_path}",
@@ -83,10 +85,8 @@ def build_extension(self, ext):
8385
pass
8486

8587
else:
86-
8788
# Single config generators are handled "normally"
88-
single_config = any(
89-
x in cmake_generator for x in {"NMake", "Ninja"})
89+
single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})
9090

9191
# CMake allows an arch-in-generator style for backward compatibility
9292
contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})
@@ -108,8 +108,7 @@ def build_extension(self, ext):
108108
# Cross-compile support for macOS - respect ARCHFLAGS if set
109109
archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", ""))
110110
if archs:
111-
cmake_args += [
112-
"-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))]
111+
cmake_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))]
113112

114113
# Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
115114
# across all generators.
@@ -126,14 +125,12 @@ def build_extension(self, ext):
126125
if not os.path.exists(build_temp):
127126
os.makedirs(build_temp)
128127

129-
subprocess.check_call(["cmake", ext.sourcedir] +
130-
cmake_args, cwd=build_temp)
131-
subprocess.check_call(["cmake", "--build", "."] +
132-
build_args, cwd=build_temp)
128+
subprocess.check_call(["cmake", ext.sourcedir] + cmake_args, cwd=build_temp)
129+
subprocess.check_call(["cmake", "--build", "."] + build_args, cwd=build_temp)
133130

134131

135132
setup(
136-
ext_modules=[CMakeExtension('ipctk')],
133+
ext_modules=[CMakeExtension("ipctk")],
137134
cmdclass={"build_ext": CMakeBuild},
138-
zip_safe=False
135+
zip_safe=False,
139136
)

0 commit comments

Comments
 (0)