Skip to content

Commit 36e5727

Browse files
committed
Support local builds with Ninja and ccache
Add a --local option (and EPPG_SCREENSHOT_LOCAL env) to use Ninja, ccache and a portable CPU-count for faster local builds. Introduce argument parsing to separate flags passed to the test binary, detect --update-references robustly, and add build_parallel_jobs() to choose a sensible parallelism across platforms. Configure CMake with generator and ccache launchers when available, warn when ninja/ccache are missing, and clear the build dir when switching generators so Ninja can be used. Also simplify/configure the cmake invocation and ensure arguments are forwarded to the screenshot_tests executable.
1 parent 1e792f7 commit 36e5727

1 file changed

Lines changed: 94 additions & 17 deletions

File tree

test/test_screenshots/build_and_run.sh

Lines changed: 94 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#!/bin/bash
22
# Build and run the LVGL screenshot tests
3-
# Usage: ./test/test_screenshots/build_and_run.sh [--update-references]
3+
# Usage:
4+
# ./test/test_screenshots/build_and_run.sh [--local] [--update-references]
45
#
5-
# Prerequisites: cmake, g++ (or clang++)
6+
# --local Use Ninja (if installed), ccache (if installed), and a portable CPU
7+
# count for parallel builds. If the build dir was configured without
8+
# --local, it is cleared once so the generator can switch to Ninja.
9+
# You can also set EPPG_SCREENSHOT_LOCAL=1 instead of the flag.
10+
#
11+
# Prerequisites: cmake, g++ (or clang++), optional: ninja, ccache
612
# LVGL and GoogleTest are fetched automatically if not cached.
713

814
set -e
@@ -11,42 +17,113 @@ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
1117
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
1218
BUILD_DIR="$PROJECT_ROOT/build-screenshot"
1319

20+
LOCAL_BUILD=0
21+
if [ "${EPPG_SCREENSHOT_LOCAL:-}" = 1 ]; then
22+
LOCAL_BUILD=1
23+
fi
24+
25+
PASS_TO_TEST=()
26+
for arg in "$@"; do
27+
case "$arg" in
28+
--local) LOCAL_BUILD=1 ;;
29+
*) PASS_TO_TEST+=("$arg") ;;
30+
esac
31+
done
32+
33+
UPDATE_REFS=0
34+
for arg in "${PASS_TO_TEST[@]}"; do
35+
if [ "$arg" = "--update-references" ]; then
36+
UPDATE_REFS=1
37+
break
38+
fi
39+
done
40+
41+
# Parallel jobs: full portable count only with --local; CI keeps nproc || 4 (Linux).
42+
build_parallel_jobs() {
43+
if [ "$LOCAL_BUILD" != 1 ]; then
44+
nproc 2>/dev/null || echo 4
45+
return
46+
fi
47+
if [ -n "${EPPG_SCREENSHOT_BUILD_JOBS:-}" ]; then
48+
echo "$EPPG_SCREENSHOT_BUILD_JOBS"
49+
return
50+
fi
51+
nproc 2>/dev/null && return
52+
getconf _NPROCESSORS_ONLN 2>/dev/null && return
53+
sysctl -n hw.ncpu 2>/dev/null && return
54+
echo 4
55+
}
56+
57+
CMAKE_GEN_ARGS=()
58+
CMAKE_CCACHE_ARGS=()
59+
if [ "$LOCAL_BUILD" = 1 ]; then
60+
if command -v ninja >/dev/null 2>&1; then
61+
CMAKE_GEN_ARGS=(-G Ninja)
62+
else
63+
echo "Warning: --local: 'ninja' not found; using CMake's default generator." >&2
64+
fi
65+
if command -v ccache >/dev/null 2>&1; then
66+
CMAKE_CCACHE_ARGS=(
67+
-DCMAKE_C_COMPILER_LAUNCHER=ccache
68+
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache
69+
)
70+
else
71+
echo "Warning: --local: 'ccache' not found; building without compiler cache." >&2
72+
fi
73+
fi
74+
1475
echo "=== LVGL Screenshot Tests ==="
1576
echo "Project root: $PROJECT_ROOT"
1677
echo "Build dir: $BUILD_DIR"
78+
if [ "$LOCAL_BUILD" = 1 ]; then
79+
ninja_status="default"
80+
[ "${#CMAKE_GEN_ARGS[@]}" -gt 0 ] && ninja_status="Ninja"
81+
ccache_status="off"
82+
[ "${#CMAKE_CCACHE_ARGS[@]}" -gt 0 ] && ccache_status="on"
83+
echo "Local fast path: generator=${ninja_status}, ccache=${ccache_status}, jobs=$(build_parallel_jobs)"
84+
fi
1785

18-
# Create build directory
1986
mkdir -p "$BUILD_DIR"
20-
cd "$BUILD_DIR"
2187

22-
# Configure with CMake (FetchContent will download LVGL and GoogleTest if needed)
23-
if [ ! -f "Makefile" ] && [ ! -f "build.ninja" ]; then
24-
echo ""
25-
echo "--- Configuring CMake ---"
26-
cmake "$SCRIPT_DIR" \
27-
-DCMAKE_BUILD_TYPE=Debug \
28-
${LVGL_DIR:+-DLVGL_DIR="$LVGL_DIR"} \
29-
${GTEST_DIR:+-DGTEST_DIR="$GTEST_DIR"}
88+
# Switching to --local with Ninja: clear a Unix Makefiles (or other) cache once.
89+
if [ "$LOCAL_BUILD" = 1 ] && [ "${#CMAKE_GEN_ARGS[@]}" -gt 0 ]; then
90+
if [ -f "$BUILD_DIR/CMakeCache.txt" ]; then
91+
if ! grep -q '^CMAKE_GENERATOR:INTERNAL=Ninja$' "$BUILD_DIR/CMakeCache.txt" 2>/dev/null; then
92+
echo "Local build: clearing $BUILD_DIR to switch CMake generator to Ninja..."
93+
rm -rf "$BUILD_DIR"
94+
mkdir -p "$BUILD_DIR"
95+
fi
96+
fi
3097
fi
3198

32-
# Build
99+
cd "$BUILD_DIR"
100+
101+
echo ""
102+
echo "--- Configuring CMake ---"
103+
# shellcheck disable=SC2086
104+
cmake "$SCRIPT_DIR" \
105+
-DCMAKE_BUILD_TYPE=Debug \
106+
"${CMAKE_GEN_ARGS[@]}" \
107+
"${CMAKE_CCACHE_ARGS[@]}" \
108+
${LVGL_DIR:+-DLVGL_DIR="$LVGL_DIR"} \
109+
${GTEST_DIR:+-DGTEST_DIR="$GTEST_DIR"}
110+
33111
echo ""
34112
echo "--- Building ---"
35-
cmake --build . --parallel "$(nproc 2>/dev/null || echo 4)"
113+
cmake --build . --parallel "$(build_parallel_jobs)"
36114

37-
# Run tests from project root (paths are relative)
38115
echo ""
39116
echo "--- Running tests ---"
40117
cd "$PROJECT_ROOT"
41118
mkdir -p test/test_screenshots/output
42119
mkdir -p test/test_screenshots/reference
43120

44-
if [ "$1" = "--update-references" ]; then
121+
if [ "$UPDATE_REFS" = 1 ]; then
45122
echo "Updating reference screenshots..."
46123
rm -rf test/test_screenshots/reference/*.bmp
47124
fi
48125

49-
"$BUILD_DIR/screenshot_tests" "$@"
126+
"$BUILD_DIR/screenshot_tests" "${PASS_TO_TEST[@]}"
50127

51128
echo ""
52129
echo "--- Screenshots ---"

0 commit comments

Comments
 (0)