Skip to content

Commit 0d0d18e

Browse files
committed
build: improve cmake generator handling, ccache, and build parallelism
- Honour GEN env var (default to Ninja) - Wipe stale build directory on generator mismatch - Only clean build when BUILD_CLEAN=1 - Use ccache when available - Honour CMAKE_BUILD_PARALLEL_LEVEL / PARALLEL for parallelism - Handle artifact location flexibility
1 parent 078029c commit 0d0d18e

1 file changed

Lines changed: 82 additions & 13 deletions

File tree

scripts/build_pybind_from_subdir.sh

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,93 @@ export CCACHE_TEMPDIR
3131

3232
mkdir -p "${CCACHE_TEMPDIR}"
3333

34-
rm -rf "${BUILD_DIR}"
34+
# ---------------------------------------------------------------------------
35+
# Generator selection
36+
# ---------------------------------------------------------------------------
37+
# Honour GEN env var (e.g. GEN=Unix\ Makefiles), otherwise default to Ninja.
38+
if [[ -n "${GEN:-}" ]]; then
39+
GENERATOR="${GEN}"
40+
else
41+
GENERATOR="Ninja"
42+
fi
43+
echo "[pybind] Generator: ${GENERATOR}"
44+
45+
# If the build directory exists with a different generator, wipe it so
46+
# CMake doesn't error out with a generator mismatch.
47+
if [[ -f "${BUILD_DIR}/CMakeCache.txt" ]]; then
48+
cached_gen="$(grep -m1 'CMAKE_GENERATOR:INTERNAL=' "${BUILD_DIR}/CMakeCache.txt" 2>/dev/null | cut -d= -f2)"
49+
if [[ -n "${cached_gen}" && "${cached_gen}" != "${GENERATOR}" ]]; then
50+
echo "[pybind] Generator changed from '${cached_gen}' to '${GENERATOR}' — wiping stale build directory."
51+
rm -rf "${BUILD_DIR}"
52+
fi
53+
fi
54+
55+
# ---------------------------------------------------------------------------
56+
# CMake configuration (always re-runs if CMakeLists.txt changed)
57+
# ---------------------------------------------------------------------------
58+
# Only wipe the build tree when BUILD_CLEAN=1 is set explicitly.
59+
if [[ "${BUILD_CLEAN:-0}" == "1" ]]; then
60+
echo "[pybind] BUILD_CLEAN=1 — removing ${BUILD_DIR}"
61+
rm -rf "${BUILD_DIR}"
62+
fi
63+
64+
cmake_args=(
65+
-S "${ROOT_DIR}"
66+
-B "${BUILD_DIR}"
67+
-G "${GENERATOR}"
68+
-DCMAKE_BUILD_TYPE=Release
69+
-DLBUG_SOURCE_DIR="${LBUG_DIR}"
70+
-DPYTHON_EXECUTABLE="${PYTHON_BIN}"
71+
-DPython_EXECUTABLE="${PYTHON_BIN}"
72+
-DPython3_EXECUTABLE="${PYTHON_BIN}"
73+
-DPYBIND11_PYTHON_VERSION="${PYTHON_VERSION}"
74+
)
75+
76+
# Wire in ccache if it is available (vastly speeds up rebuilds).
77+
if command -v ccache &>/dev/null; then
78+
cmake_args+=(
79+
-DCMAKE_C_COMPILER_LAUNCHER=ccache
80+
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache
81+
)
82+
fi
83+
84+
cmake "${cmake_args[@]}"
85+
86+
# ---------------------------------------------------------------------------
87+
# Build
88+
# ---------------------------------------------------------------------------
89+
# Parallelism: honour CMAKE_BUILD_PARALLEL_LEVEL or PARALLEL first,
90+
# then fall back to the number of logical CPUs.
91+
if [[ -n "${CMAKE_BUILD_PARALLEL_LEVEL:-}" ]]; then
92+
NPROC="${CMAKE_BUILD_PARALLEL_LEVEL}"
93+
elif [[ -n "${PARALLEL:-}" ]]; then
94+
NPROC="${PARALLEL}"
95+
else
96+
NPROC="$(nproc 2>/dev/null || echo 4)"
97+
fi
3598

36-
cmake \
37-
-S "${ROOT_DIR}" \
38-
-B "${BUILD_DIR}" \
39-
-DCMAKE_BUILD_TYPE=Release \
40-
-DLBUG_SOURCE_DIR="${LBUG_DIR}" \
41-
-DPYTHON_EXECUTABLE="${PYTHON_BIN}" \
42-
-DPython_EXECUTABLE="${PYTHON_BIN}" \
43-
-DPython3_EXECUTABLE="${PYTHON_BIN}" \
44-
-DPYBIND11_PYTHON_VERSION="${PYTHON_VERSION}"
99+
build_args=(
100+
--build "${BUILD_DIR}"
101+
--config Release
102+
--target _lbug
103+
--parallel "${NPROC}"
104+
)
45105

46-
cmake --build "${BUILD_DIR}" --config Release --target _lbug
106+
echo "[pybind] Starting build with ${NPROC} parallel job(s) ..."
107+
cmake "${build_args[@]}"
47108

48-
if compgen -G "${ROOT_DIR}/build/ladybug/_lbug*" > /dev/null; then
109+
# ---------------------------------------------------------------------------
110+
# Post-build check
111+
# ---------------------------------------------------------------------------
112+
if compgen -G "${ROOT_DIR}/build/ladybug/_lbug*" &>/dev/null; then
49113
echo "[pybind] Built extension into ${ROOT_DIR}/build/ladybug"
114+
elif compgen -G "${BUILD_DIR}/ladybug/_lbug*" &>/dev/null; then
115+
echo "[pybind] Built extension into ${BUILD_DIR}/ladybug"
116+
# Copy to expected location so Makefile's test target finds it.
117+
mkdir -p "${ROOT_DIR}/build/ladybug"
118+
cp "${BUILD_DIR}"/ladybug/_lbug* "${ROOT_DIR}/build/ladybug/"
50119
else
51120
echo "[pybind] Build finished, but no _lbug extension artifact was found." >&2
52-
echo "Checked: ${ROOT_DIR}/build/ladybug" >&2
121+
echo "Checked: ${ROOT_DIR}/build/ladybug and ${BUILD_DIR}/ladybug" >&2
53122
exit 1
54123
fi

0 commit comments

Comments
 (0)