Skip to content

Commit 406a4cc

Browse files
committed
Add variant config pre-seeding for BASE_BOARD in src/build
Related to #262 (does not fix the issue, but addresses a related problem). Previously, only dist config and config.local were pre-seeded before generate_board_config.py. If a variant config set BASE_BOARD (e.g. raspberrypiarm64), it was never seen by generate_board_config.py because variants were only sourced later inside src/config. Changes: 1. src/build: Added variant config pre-seeding between dist config and config.local, before generate_board_config.py runs. The variant name comes from $1 (same argument passed to src/config). Resolution logic checks ${DIST_PATH}/variants/$1/config then ${CUSTOM_PI_OS_PATH}/variants/$1/config, matching src/config behavior. The heredoc bash -c check was also updated to include variant sourcing. Priority order (lowest to highest): - dist config - variant config - config.local (always wins) 2. tests/test_config_local_board.sh: Added 3 new test cases (7 total): - Variant sets BASE_BOARD=raspberrypiarm64 → produces BASE_ARCH=arm64 - config.local overrides variant BASE_BOARD (config.local wins) - Variant overrides dist config BASE_BOARD (variant wins)
1 parent 883cc9f commit 406a4cc

File tree

2 files changed

+160
-10
lines changed

2 files changed

+160
-10
lines changed

src/build

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@ set -x
88

99
define(){ IFS='\n' read -r -d '' ${1} || true; }
1010

11-
# Pre-seed: source dist config and config.local to seed generate_board_config.py
12-
# with BASE_BOARD. The full/authoritative config sourcing happens later via
13-
# ${CUSTOM_PI_OS_PATH}/config inside the heredoc.
11+
# Pre-seed: source dist config, variant config, and config.local to seed
12+
# generate_board_config.py with BASE_BOARD. The full/authoritative config
13+
# sourcing happens later via ${CUSTOM_PI_OS_PATH}/config inside the heredoc.
14+
# Priority (lowest to highest): dist config < variant config < config.local
1415
source ${DIST_PATH}/config
16+
17+
# Pre-seed variant config if a non-default variant is specified
18+
_PRESEED_VARIANT="${1:-default}"
19+
if [ "${_PRESEED_VARIANT}" != "default" ]; then
20+
if [ -f "${DIST_PATH}/variants/${_PRESEED_VARIANT}/config" ]; then
21+
source "${DIST_PATH}/variants/${_PRESEED_VARIANT}/config"
22+
elif [ -f "${CUSTOM_PI_OS_PATH}/variants/${_PRESEED_VARIANT}/config" ]; then
23+
source "${CUSTOM_PI_OS_PATH}/variants/${_PRESEED_VARIANT}/config"
24+
fi
25+
fi
26+
1527
if [ -f "${DIST_PATH}/config.local" ]; then
1628
source "${DIST_PATH}/config.local"
1729
fi
@@ -24,16 +36,28 @@ CONFIG_FILE="${DIST_PATH}"/config
2436
2537
CONFIG_LOCAL_FILE="${DIST_PATH}"/config.local
2638
39+
# Resolve variant config path (same logic as src/config)
40+
_VARIANT="${1:-default}"
41+
VARIANT_CONFIG_FILE=""
42+
if [ "${_VARIANT}" != "default" ]; then
43+
if [ -f "${DIST_PATH}/variants/${_VARIANT}/config" ]; then
44+
VARIANT_CONFIG_FILE="${DIST_PATH}/variants/${_VARIANT}/config"
45+
elif [ -f "${CUSTOM_PI_OS_PATH}/variants/${_VARIANT}/config" ]; then
46+
VARIANT_CONFIG_FILE="${CUSTOM_PI_OS_PATH}/variants/${_VARIANT}/config"
47+
fi
48+
fi
49+
2750
if [ -f "${CONFIG_FILE}" ]; then
28-
echo "Found a dist config file, checking for BASE_BOARD in config and config.local..."
51+
echo "Found a dist config file, checking for BASE_BOARD in config, variant, and config.local..."
2952
BASE_BOARD_FROM_CONFIG=$(bash -c "source \"$CONFIG_FILE\" >/dev/null 2>&1; \
53+
[ -n \"$VARIANT_CONFIG_FILE\" ] && [ -f \"$VARIANT_CONFIG_FILE\" ] && source \"$VARIANT_CONFIG_FILE\" >/dev/null 2>&1; \
3054
[ -f \"$CONFIG_LOCAL_FILE\" ] && source \"$CONFIG_LOCAL_FILE\" >/dev/null 2>&1; \
3155
echo \$BASE_BOARD")
3256
if [ -n "$BASE_BOARD_FROM_CONFIG" ]; then
3357
export BASE_BOARD="$BASE_BOARD_FROM_CONFIG"
34-
echo "BASE_BOARD set to ${BASE_BOARD} from dist config/config.local before generating board config."
58+
echo "BASE_BOARD set to ${BASE_BOARD} from dist config/variant/config.local before generating board config."
3559
else
36-
echo "BASE_BOARD not found in ${CONFIG_FILE} or ${CONFIG_LOCAL_FILE}. It will be set from the cli or fallback to raspberrypiarmhf"
60+
echo "BASE_BOARD not found in ${CONFIG_FILE}, variant, or ${CONFIG_LOCAL_FILE}. It will be set from the cli or fallback to raspberrypiarmhf"
3761
fi
3862
else
3963
echo "Config file ${CONFIG_FILE} does not exist. You probably want to create one for your distribution."

tests/test_config_local_board.sh

Lines changed: 130 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,32 @@ print_test_result() {
4343
# $1 - path to dist config file
4444
# $2 - path to dist config.local file (or empty)
4545
# $3 - path to output script
46+
# $4 - path to variant config file (or empty)
4647
run_build_preseed_flow() {
4748
local config_file="$1"
4849
local config_local_file="$2"
4950
local output_script="$3"
51+
local variant_config_file="$4"
5052

5153
# Run in a subshell to avoid polluting the test environment
5254
(
5355
# Unset BASE_BOARD to start clean
5456
unset BASE_BOARD
5557

56-
# Mimic src/build lines 14-17: source config, then config.local
58+
# Mimic src/build pre-seed: config, then variant, then config.local
5759
if [ -f "${config_file}" ]; then
5860
source "${config_file}"
5961
fi
62+
if [ -n "${variant_config_file}" ] && [ -f "${variant_config_file}" ]; then
63+
source "${variant_config_file}"
64+
fi
6065
if [ -n "${config_local_file}" ] && [ -f "${config_local_file}" ]; then
6166
source "${config_local_file}"
6267
fi
6368

64-
# Mimic the heredoc bash -c check
69+
# Mimic the heredoc bash -c check (config < variant < config.local)
6570
BASE_BOARD_FROM_CONFIG=$(bash -c "source \"${config_file}\" >/dev/null 2>&1; \
71+
[ -n \"${variant_config_file}\" ] && [ -f \"${variant_config_file}\" ] && source \"${variant_config_file}\" >/dev/null 2>&1; \
6672
[ -n \"${config_local_file}\" ] && [ -f \"${config_local_file}\" ] && source \"${config_local_file}\" >/dev/null 2>&1; \
6773
echo \$BASE_BOARD")
6874
if [ -n "$BASE_BOARD_FROM_CONFIG" ]; then
@@ -215,15 +221,135 @@ CONF
215221
rm -rf "${tmpdir}"
216222
}
217223

224+
# Test 5: Variant sets BASE_BOARD=raspberrypiarm64, no config.local → BASE_ARCH=arm64
225+
test_variant_sets_arm64() {
226+
local test_name="Variant sets BASE_BOARD=raspberrypiarm64, no config.local"
227+
print_test_header "$test_name"
228+
((test_count++))
229+
230+
local tmpdir=$(mktemp -d)
231+
local config_file="${tmpdir}/config"
232+
local variant_config_file="${tmpdir}/variant_config"
233+
local output_script="${tmpdir}/board_config.sh"
234+
235+
# Dist config does NOT set BASE_BOARD
236+
cat > "${config_file}" <<'CONF'
237+
export SOME_OTHER_VAR="hello"
238+
CONF
239+
240+
# Variant config sets arm64 board
241+
cat > "${variant_config_file}" <<'CONF'
242+
export BASE_BOARD=raspberrypiarm64
243+
CONF
244+
245+
run_build_preseed_flow "${config_file}" "" "${output_script}" "${variant_config_file}"
246+
247+
local is_passed=false
248+
if [ -f "${output_script}" ] && grep -q 'BASE_ARCH="arm64"' "${output_script}"; then
249+
is_passed=true
250+
((tests_passed++))
251+
fi
252+
253+
echo "Generated board config:"
254+
[ -f "${output_script}" ] && cat "${output_script}" || echo "(file not created)"
255+
print_test_result "$test_name" "$is_passed" "Expected BASE_ARCH=\"arm64\" from variant"
256+
$is_passed || failed_tests+=("$test_name")
257+
258+
rm -rf "${tmpdir}"
259+
}
260+
261+
# Test 6: config.local overrides variant's BASE_BOARD
262+
test_config_local_overrides_variant() {
263+
local test_name="config.local BASE_BOARD overrides variant BASE_BOARD"
264+
print_test_header "$test_name"
265+
((test_count++))
266+
267+
local tmpdir=$(mktemp -d)
268+
local config_file="${tmpdir}/config"
269+
local config_local_file="${tmpdir}/config.local"
270+
local variant_config_file="${tmpdir}/variant_config"
271+
local output_script="${tmpdir}/board_config.sh"
272+
273+
# Dist config does NOT set BASE_BOARD
274+
cat > "${config_file}" <<'CONF'
275+
export SOME_OTHER_VAR="hello"
276+
CONF
277+
278+
# Variant sets armhf
279+
cat > "${variant_config_file}" <<'CONF'
280+
export BASE_BOARD=raspberrypiarmhf
281+
CONF
282+
283+
# config.local overrides to arm64
284+
cat > "${config_local_file}" <<'CONF'
285+
export BASE_BOARD=raspberrypiarm64
286+
CONF
287+
288+
run_build_preseed_flow "${config_file}" "${config_local_file}" "${output_script}" "${variant_config_file}"
289+
290+
local is_passed=false
291+
if [ -f "${output_script}" ] && grep -q 'BASE_ARCH="arm64"' "${output_script}"; then
292+
is_passed=true
293+
((tests_passed++))
294+
fi
295+
296+
echo "Generated board config:"
297+
[ -f "${output_script}" ] && cat "${output_script}" || echo "(file not created)"
298+
print_test_result "$test_name" "$is_passed" "Expected BASE_ARCH=\"arm64\" (config.local should win over variant)"
299+
$is_passed || failed_tests+=("$test_name")
300+
301+
rm -rf "${tmpdir}"
302+
}
303+
304+
# Test 7: Variant overrides dist config's BASE_BOARD
305+
test_variant_overrides_config() {
306+
local test_name="Variant BASE_BOARD overrides dist config BASE_BOARD"
307+
print_test_header "$test_name"
308+
((test_count++))
309+
310+
local tmpdir=$(mktemp -d)
311+
local config_file="${tmpdir}/config"
312+
local variant_config_file="${tmpdir}/variant_config"
313+
local output_script="${tmpdir}/board_config.sh"
314+
315+
# Dist config sets armhf
316+
cat > "${config_file}" <<'CONF'
317+
export BASE_BOARD=raspberrypiarmhf
318+
CONF
319+
320+
# Variant overrides to arm64
321+
cat > "${variant_config_file}" <<'CONF'
322+
export BASE_BOARD=raspberrypiarm64
323+
CONF
324+
325+
run_build_preseed_flow "${config_file}" "" "${output_script}" "${variant_config_file}"
326+
327+
local is_passed=false
328+
if [ -f "${output_script}" ] && grep -q 'BASE_ARCH="arm64"' "${output_script}"; then
329+
is_passed=true
330+
((tests_passed++))
331+
fi
332+
333+
echo "Generated board config:"
334+
[ -f "${output_script}" ] && cat "${output_script}" || echo "(file not created)"
335+
print_test_result "$test_name" "$is_passed" "Expected BASE_ARCH=\"arm64\" (variant should win over dist config)"
336+
$is_passed || failed_tests+=("$test_name")
337+
338+
rm -rf "${tmpdir}"
339+
}
340+
218341
# Run all tests
219342
run_tests() {
220-
echo -e "${BLUE}Running config.local BASE_BOARD Tests${NC}"
221-
echo "═══════════════════════════════════════"
343+
echo -e "${BLUE}Running config.local and Variant BASE_BOARD Tests${NC}"
344+
echo "═══════════════════════════════════════════════════"
222345

223346
test_no_config_local_defaults_to_armhf
224347
test_config_local_sets_arm64
225348
test_config_local_overrides_config
226349
test_config_sets_board_no_local
350+
test_variant_sets_arm64
351+
test_config_local_overrides_variant
352+
test_variant_overrides_config
227353

228354
# Print summary
229355
echo

0 commit comments

Comments
 (0)