Skip to content

Commit 0135fbe

Browse files
jeongseok-metameta-codesync[bot]
authored andcommitted
Refactor pyproject-pypi.toml.j2: Extract CMake args into Jinja2 macros (#923)
Summary: Reduce duplication in PyPI build template by extracting common CMake arguments into reusable Jinja2 macros. This improves maintainability by centralizing build configuration that was previously duplicated across multiple platform overrides. ## Changes - Add `cmake_base_args()` macro for 6 shared CMake flags - Add `cmake_linux_args()` macro for manylinux-specific config - Add `cmake_macos_args()` macro for Darwin-specific config - Add `cmake_windows_args()` macro for Windows/VS config - Add `cibw_linux_before_all()` macro for pixi setup script - Fix typo: `python_add_library` → `python3_add_library` in comment ## Impact - **Template reduction**: 429 → 418 lines (-11 lines) - **Generated output**: Semantically equivalent, all TOML valid - **Future maintenance**: CMake flag changes only need one edit ## Testing - Generated `pyproject-pypi-cpu-py312.toml`, `pyproject-pypi-cpu-py313.toml`, `pyproject-pypi-gpu.toml` using `pixi run generate_pyproject` - Validated all generated files parse as valid TOML - Compared generated output with original - only cosmetic whitespace differences and one comment typo fix Pull Request resolved: #923 Reviewed By: fbogo Differential Revision: D90646192 Pulled By: jeongseok-meta fbshipit-source-id: fc750bc3e7fe110d2c89b67748d1d3187d0bf10d
1 parent 6410ece commit 0135fbe

File tree

1 file changed

+87
-99
lines changed

1 file changed

+87
-99
lines changed

pyproject-pypi.toml.j2

Lines changed: 87 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,79 @@
1+
{#- Jinja2 macros for DRY CMake configuration -#}
2+
3+
{#- Base CMake args shared by all platforms -#}
4+
{%- macro cmake_base_args() %}
5+
"-DBUILD_SHARED_LIBS=OFF",
6+
"-DMOMENTUM_BUILD_PYMOMENTUM=ON",
7+
"-DMOMENTUM_BUILD_EXAMPLES=OFF",
8+
"-DMOMENTUM_BUILD_TESTING=OFF",
9+
"-DMOMENTUM_BUILD_RENDERER=OFF",
10+
"-DMOMENTUM_ENABLE_SIMD=OFF",
11+
{%- endmacro %}
12+
13+
{#- Linux-specific args for manylinux compatibility -#}
14+
{%- macro cmake_linux_args() %}
15+
{{ cmake_base_args() }}
16+
"-DCMAKE_CXX_SCAN_FOR_MODULES=OFF",
17+
"-DMOMENTUM_USE_SYSTEM_RERUN_CPP_SDK=ON",
18+
# Use bundled pybind11 to avoid CMake FindPython Development.Module requirement
19+
# The pixi environment's pybind11 uses pybind11NewTools which requires python3_add_library
20+
# from CMake's FindPython, but manylinux Python only has Interpreter component
21+
"-DMOMENTUM_USE_SYSTEM_PYBIND11=OFF",
22+
# Use manylinux container's compiler (gcc-toolset-12) instead of pixi's GCC 14.3
23+
# to ensure manylinux_2_28 compatibility (requires GLIBCXX <= 3.4.24, CXXABI <= 1.3.11)
24+
"-DCMAKE_CXX_FLAGS=-static-libstdc++ -static-libgcc",
25+
"-DCMAKE_C_FLAGS=-static-libgcc",
26+
{%- endmacro %}
27+
28+
{#- macOS-specific args -#}
29+
{%- macro cmake_macos_args() %}
30+
{{ cmake_base_args() }}
31+
"-DCMAKE_CXX_SCAN_FOR_MODULES=OFF",
32+
"-DMOMENTUM_USE_SYSTEM_RERUN_CPP_SDK=OFF",
33+
"-DMOMENTUM_USE_SYSTEM_PYBIND11=ON",
34+
{%- endmacro %}
35+
36+
{#- Windows-specific args (maintains original order with -G after SHARED_LIBS) -#}
37+
{%- macro cmake_windows_args() %}
38+
"-DBUILD_SHARED_LIBS=OFF",
39+
"-G Visual Studio 17 2022",
40+
"-DMOMENTUM_BUILD_PYMOMENTUM=ON",
41+
"-DMOMENTUM_BUILD_EXAMPLES=OFF",
42+
"-DMOMENTUM_BUILD_TESTING=OFF",
43+
"-DMOMENTUM_BUILD_RENDERER=OFF",
44+
"-DMOMENTUM_ENABLE_SIMD=OFF",
45+
"-DMOMENTUM_USE_SYSTEM_RERUN_CPP_SDK=ON",
46+
"-DMOMENTUM_USE_SYSTEM_PYBIND11=ON",
47+
{% if variant == "cpu" %}
48+
"-DCMAKE_CXX_SCAN_FOR_MODULES=OFF",
49+
{% endif %}
50+
{%- endmacro %}
51+
52+
{#- Common cibuildwheel before-all script for Linux -#}
53+
{%- macro cibw_linux_before_all() %}
54+
# Use local pixi binary if available to avoid network issues
55+
if [ -f /project/pixi_bin ]; then
56+
echo "Using local pixi binary"
57+
mkdir -p $HOME/.pixi/bin
58+
cp /project/pixi_bin $HOME/.pixi/bin/pixi
59+
chmod +x $HOME/.pixi/bin/pixi
60+
else
61+
echo "Downloading pixi"
62+
curl -fsSL https://pixi.sh/install.sh | bash
63+
fi
64+
export PATH=$HOME/.pixi/bin:$PATH
65+
66+
# Install dependencies in a separate directory to avoid conflicts with host's .pixi folder
67+
mkdir -p /tmp/build_env
68+
cp {project}/pixi.toml {project}/pixi.lock {project}/README.md {project}/LICENSE /tmp/build_env/
69+
cd /tmp/build_env
70+
pixi install -e default
71+
{%- endmacro %}
72+
73+
{#- ============================================================================ -#}
74+
{#- MAIN TEMPLATE CONTENT -#}
75+
{#- ============================================================================ -#}
76+
177
[build-system]
278
requires = ["scikit-build-core", "pybind11", "setuptools-scm"]
379
build-backend = "scikit_build_core.build"
@@ -82,12 +158,7 @@ Changelog = "https://github.com/facebookresearch/momentum/releases"
82158
[tool.scikit-build]
83159
build-dir = "build/{wheel_tag}"
84160
cmake.args = [
85-
"-DBUILD_SHARED_LIBS=OFF",
86-
"-DMOMENTUM_BUILD_PYMOMENTUM=ON",
87-
"-DMOMENTUM_BUILD_EXAMPLES=OFF",
88-
"-DMOMENTUM_BUILD_TESTING=OFF",
89-
"-DMOMENTUM_BUILD_RENDERER=OFF",
90-
"-DMOMENTUM_ENABLE_SIMD=OFF",
161+
{{ cmake_base_args() }}
91162
"-DCMAKE_CXX_SCAN_FOR_MODULES=OFF",
92163
"-DMOMENTUM_USE_SYSTEM_RERUN_CPP_SDK=OFF",
93164
"-DMOMENTUM_USE_SYSTEM_PYBIND11=ON",
@@ -111,37 +182,14 @@ wheel.exclude = ["geometry_test_helper.*"]
111182
[[tool.scikit-build.overrides]]
112183
if.platform-system = "^darwin"
113184
cmake.args = [
114-
"-DBUILD_SHARED_LIBS=OFF",
115-
"-DMOMENTUM_BUILD_PYMOMENTUM=ON",
116-
"-DMOMENTUM_BUILD_EXAMPLES=OFF",
117-
"-DMOMENTUM_BUILD_TESTING=OFF",
118-
"-DMOMENTUM_BUILD_RENDERER=OFF",
119-
"-DMOMENTUM_ENABLE_SIMD=OFF",
120-
"-DCMAKE_CXX_SCAN_FOR_MODULES=OFF",
121-
"-DMOMENTUM_USE_SYSTEM_RERUN_CPP_SDK=OFF",
122-
"-DMOMENTUM_USE_SYSTEM_PYBIND11=ON",
185+
{{ cmake_macos_args() }}
123186
]
124187
cmake.define.CMAKE_PREFIX_PATH = {env = "CMAKE_PREFIX_PATH"}
125188

126189
[[tool.scikit-build.overrides]]
127190
if.platform-system = "^linux"
128191
cmake.args = [
129-
"-DBUILD_SHARED_LIBS=OFF",
130-
"-DMOMENTUM_BUILD_PYMOMENTUM=ON",
131-
"-DMOMENTUM_BUILD_EXAMPLES=OFF",
132-
"-DMOMENTUM_BUILD_TESTING=OFF",
133-
"-DMOMENTUM_BUILD_RENDERER=OFF",
134-
"-DMOMENTUM_ENABLE_SIMD=OFF",
135-
"-DCMAKE_CXX_SCAN_FOR_MODULES=OFF",
136-
"-DMOMENTUM_USE_SYSTEM_RERUN_CPP_SDK=ON",
137-
# Use bundled pybind11 to avoid CMake FindPython Development.Module requirement
138-
# The pixi environment's pybind11 uses pybind11NewTools which requires python3_add_library
139-
# from CMake's FindPython, but manylinux Python only has Interpreter component
140-
"-DMOMENTUM_USE_SYSTEM_PYBIND11=OFF",
141-
# Use manylinux container's compiler (gcc-toolset-12) instead of pixi's GCC 14.3
142-
# to ensure manylinux_2_28 compatibility (requires GLIBCXX <= 3.4.24, CXXABI <= 1.3.11)
143-
"-DCMAKE_CXX_FLAGS=-static-libstdc++ -static-libgcc",
144-
"-DCMAKE_C_FLAGS=-static-libgcc",
192+
{{ cmake_linux_args() }}
145193
]
146194
cmake.define.CMAKE_PREFIX_PATH = {env = "CMAKE_PREFIX_PATH"}
147195
{% endif %}
@@ -150,41 +198,15 @@ cmake.define.CMAKE_PREFIX_PATH = {env = "CMAKE_PREFIX_PATH"}
150198
[[tool.scikit-build.overrides]]
151199
if.platform-system = "^linux"
152200
cmake.args = [
153-
"-DBUILD_SHARED_LIBS=OFF",
154-
"-DMOMENTUM_BUILD_PYMOMENTUM=ON",
155-
"-DMOMENTUM_BUILD_EXAMPLES=OFF",
156-
"-DMOMENTUM_BUILD_TESTING=OFF",
157-
"-DMOMENTUM_BUILD_RENDERER=OFF",
158-
"-DMOMENTUM_ENABLE_SIMD=OFF",
159-
"-DCMAKE_CXX_SCAN_FOR_MODULES=OFF",
160-
"-DMOMENTUM_USE_SYSTEM_RERUN_CPP_SDK=ON",
161-
# Use bundled pybind11 to avoid CMake FindPython Development.Module requirement
162-
# The pixi environment's pybind11 uses pybind11NewTools which requires python_add_library
163-
# from CMake's FindPython, but manylinux Python only has Interpreter component
164-
"-DMOMENTUM_USE_SYSTEM_PYBIND11=OFF",
165-
# Use manylinux container's compiler (gcc-toolset-12) instead of pixi's GCC 14.3
166-
# to ensure manylinux_2_28 compatibility (requires GLIBCXX <= 3.4.24, CXXABI <= 1.3.11)
167-
"-DCMAKE_CXX_FLAGS=-static-libstdc++ -static-libgcc",
168-
"-DCMAKE_C_FLAGS=-static-libgcc",
201+
{{ cmake_linux_args() }}
169202
]
170203
cmake.define.CMAKE_PREFIX_PATH = {env = "CMAKE_PREFIX_PATH"}
171204
{% endif %}
172205

173206
[[tool.scikit-build.overrides]]
174207
if.platform-system = "^win32"
175208
cmake.args = [
176-
"-DBUILD_SHARED_LIBS=OFF",
177-
"-G Visual Studio 17 2022",
178-
"-DMOMENTUM_BUILD_PYMOMENTUM=ON",
179-
"-DMOMENTUM_BUILD_EXAMPLES=OFF",
180-
"-DMOMENTUM_BUILD_TESTING=OFF",
181-
"-DMOMENTUM_BUILD_RENDERER=OFF",
182-
"-DMOMENTUM_ENABLE_SIMD=OFF",
183-
"-DMOMENTUM_USE_SYSTEM_RERUN_CPP_SDK=ON",
184-
"-DMOMENTUM_USE_SYSTEM_PYBIND11=ON",
185-
{% if variant == "cpu" %}
186-
"-DCMAKE_CXX_SCAN_FOR_MODULES=OFF",
187-
{% endif %}
209+
{{ cmake_windows_args() }}
188210
]
189211
cmake.define.CMAKE_PREFIX_PATH = {env = "CMAKE_PREFIX_PATH"}
190212

@@ -263,23 +285,7 @@ fi
263285

264286
[tool.cibuildwheel.linux]
265287
before-all = """
266-
# Use local pixi binary if available to avoid network issues
267-
if [ -f /project/pixi_bin ]; then
268-
echo "Using local pixi binary"
269-
mkdir -p $HOME/.pixi/bin
270-
cp /project/pixi_bin $HOME/.pixi/bin/pixi
271-
chmod +x $HOME/.pixi/bin/pixi
272-
else
273-
echo "Downloading pixi"
274-
curl -fsSL https://pixi.sh/install.sh | bash
275-
fi
276-
export PATH=$HOME/.pixi/bin:$PATH
277-
278-
# Install dependencies in a separate directory to avoid conflicts with host's .pixi folder
279-
mkdir -p /tmp/build_env
280-
cp {project}/pixi.toml {project}/pixi.lock {project}/README.md {project}/LICENSE /tmp/build_env/
281-
cd /tmp/build_env
282-
pixi install -e default
288+
{{ cibw_linux_before_all() }}
283289
"""
284290
# Use manylinux container's gcc-toolset-12 compiler for manylinux_2_28 compatibility
285291
# Pixi provides dependencies (headers/libs) but we use the container's compiler
@@ -439,30 +445,12 @@ before-all = """
439445
echo ">>> Disk space at start:"
440446
df -h /
441447

442-
# Use local pixi binary if available to avoid network issues
443-
if [ -f /project/pixi_bin ]; then
444-
echo "Using local pixi binary"
445-
mkdir -p $HOME/.pixi/bin
446-
cp /project/pixi_bin $HOME/.pixi/bin/pixi
447-
chmod +x $HOME/.pixi/bin/pixi
448-
else
449-
echo "Downloading pixi"
450-
curl -fsSL https://pixi.sh/install.sh | bash
451-
fi
452-
export PATH=$HOME/.pixi/bin:$PATH
453-
454-
# Install dependencies using the minimal-build environment
455-
# The minimal-build environment is ~2GB smaller than default, saving disk space for PyTorch CUDA
456-
mkdir -p /tmp/build_env
457-
cp {project}/pixi.toml {project}/pixi.lock {project}/README.md {project}/LICENSE /tmp/build_env/
458-
cd /tmp/build_env
459-
pixi install -e minimal-build
448+
# Set up CUDA environment
449+
export CUDA_HOME=/usr/local/cuda-12.8
450+
export PATH=$CUDA_HOME/bin:$PATH
451+
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH
460452

461-
# Clean up pixi cache to free space
462-
rm -rf $HOME/.cache/rattler 2>/dev/null || true
463-
464-
echo ">>> Disk space after pixi install:"
465-
df -h /
453+
{{ cibw_linux_before_all() }}
466454
"""
467455
# Use manylinux container's gcc-toolset-12 compiler for manylinux_2_28 compatibility
468456
# Pixi provides dependencies (headers/libs) but we use the container's compiler

0 commit comments

Comments
 (0)