Skip to content

Commit 61e3bd3

Browse files
committed
trice lib build scripts hardened for CI
1 parent e9b283b commit 61e3bd3

File tree

8 files changed

+362
-70
lines changed

8 files changed

+362
-70
lines changed

examples/F030_inst/build.sh

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,60 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
2+
#
3+
# Simple and CI-safe build script.
4+
#
5+
# Main idea:
6+
# - Always run relative to the script location (NOT the caller's working directory).
7+
# - Use strict error handling, so CI fails immediately and clearly.
8+
#
29

3-
# Initialize an empty string
4-
flags=""
10+
set -euo pipefail
11+
12+
# ------------------------------------------------------------------------------
13+
# 1) Resolve stable directories
14+
# ------------------------------------------------------------------------------
15+
16+
# Absolute path of the directory containing this build.sh
17+
# - Works regardless of where the script is called from (CI often calls from repo root).
18+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
519

6-
# Loop through all arguments
20+
# Repo root is two levels above: <repo>/examples/<target>/build.sh
21+
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
22+
23+
# ------------------------------------------------------------------------------
24+
# 2) Build TRICE_FLAGS (same logic as your original, but safe and predictable)
25+
# ------------------------------------------------------------------------------
26+
27+
flags=""
728
for arg in "$@"; do
8-
flags+="-D$arg "
29+
flags="${flags}-D${arg} "
930
done
1031

11-
# Trice is called here and not within make, to guarantee, it is finished before any other job starts.
12-
../../trice_cleanIDs_in_examples_and_test_folder.sh # Run this first to trigger the used editor to show the Trice IDs cleaned state.
13-
../../trice_insertIDs_in_examples_and_test_folder.sh
32+
# ------------------------------------------------------------------------------
33+
# 3) Run TRICE scripts (use absolute paths, so CWD does not matter)
34+
# ------------------------------------------------------------------------------
35+
36+
# If these scripts are not executable, call them via bash (CI checkout can be strict).
37+
bash "${REPO_ROOT}/trice_cleanIDs_in_examples_and_test_folder.sh"
38+
bash "${REPO_ROOT}/trice_insertIDs_in_examples_and_test_folder.sh"
39+
40+
# ------------------------------------------------------------------------------
41+
# 4) Load build environment and build
42+
# ------------------------------------------------------------------------------
43+
44+
# Source the environment file from repo root (absolute path).
45+
# shellcheck disable=SC1090
46+
source "${REPO_ROOT}/build_environment.sh"
47+
48+
# Optional: provide a default if build_environment.sh does not set MAKE_JOBS.
49+
# : "${MAKE_JOBS:=-j2}"
50+
51+
# Ensure we build from the example directory (where the Makefile is expected).
52+
cd "${SCRIPT_DIR}"
53+
54+
make ${MAKE_JOBS} TRICE_FLAGS="${flags}"
1455

15-
source ../../build_environment.sh
16-
make $MAKE_JOBS TRICE_FLAGS="$flags"
56+
# ------------------------------------------------------------------------------
57+
# 5) Post-build cleanup (leave repository in cleaned state)
58+
# ------------------------------------------------------------------------------
1759

18-
../../trice_cleanIDs_in_examples_and_test_folder.sh # Run this again to get the Trice IDs cleaned state.
60+
bash "${REPO_ROOT}/trice_cleanIDs_in_examples_and_test_folder.sh"

examples/G0B1_inst/build.sh

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,84 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
2+
#
3+
# Simple and CI-safe build script (pattern-preserving version).
4+
#
5+
# Main idea:
6+
# - Always run relative to the script location (NOT the caller's working directory).
7+
# - Use strict error handling, so CI fails immediately and clearly.
8+
#
9+
10+
set -euo pipefail
11+
12+
# ------------------------------------------------------------------------------
13+
# 1) Resolve stable directories
14+
# ------------------------------------------------------------------------------
15+
16+
# Absolute path of the directory containing this build.sh
17+
# - Works regardless of where the script is called from (CI often calls from repo root).
18+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
19+
20+
# Repo root is two levels above: <repo>/examples/<target>/build.sh
21+
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
22+
23+
# Ensure we build from the example directory (where the Makefile is expected).
24+
cd "${SCRIPT_DIR}"
25+
26+
# ------------------------------------------------------------------------------
27+
# 2) Build TRICE_FLAGS and detect TRICE_OFF
28+
# ------------------------------------------------------------------------------
229

330
# Initialize an empty string
431
flags=""
532

33+
# Track TRICE_OFF=1 explicitly (used later for conditional insert/build)
34+
triceOFF="0"
35+
636
# Loop through all arguments
737
for arg in "$@"; do
8-
if [ $arg = "TRICE_OFF=1" ]; then
9-
export triceOFF="1"
38+
if [ "${arg}" = "TRICE_OFF=1" ]; then
39+
triceOFF="1"
40+
export triceOFF="1" # keep compatibility with existing scripts/env if needed
1041
fi
11-
flags+="-D$arg "
42+
flags="${flags}-D${arg} "
1243
done
1344

14-
# Trice is called here and not within make, to guarantee, it is finished before any other job starts.
15-
../../trice_cleanIDs_in_examples_and_test_folder.sh # Run this first to trigger the used editor to show the Trice IDs cleaned state.
45+
# ------------------------------------------------------------------------------
46+
# 3) Run TRICE scripts (use absolute paths, so CWD does not matter)
47+
# ------------------------------------------------------------------------------
48+
49+
# Trice is called here and not within make, to guarantee, it is finished
50+
# before any other job starts.
51+
bash "${REPO_ROOT}/trice_cleanIDs_in_examples_and_test_folder.sh" \
52+
# Run this first to trigger the used editor to show the Trice IDs cleaned state.
1653

17-
if [ "$triceOFF" != "1" ]; then
18-
../../trice_insertIDs_in_examples_and_test_folder.sh # custom aliases should be excluded or without IDs, when translating with TRICE_OFF=1
54+
if [ "${triceOFF}" != "1" ]; then
55+
bash "${REPO_ROOT}/trice_insertIDs_in_examples_and_test_folder.sh" \
56+
# custom aliases should be excluded or without IDs, when translating with TRICE_OFF=1
1957
fi
2058

21-
source ../../build_environment.sh
22-
make $MAKE_JOBS TRICE_FLAGS="$flags" gcc # We translate here with inserted IDs, even in the triceOff case and expect no warnings.
59+
# ------------------------------------------------------------------------------
60+
# 4) Load build environment and build
61+
# ------------------------------------------------------------------------------
62+
63+
# Source the environment file from repo root (absolute path).
64+
# shellcheck disable=SC1090
65+
source "${REPO_ROOT}/build_environment.sh"
66+
67+
# Optional: provide a default if build_environment.sh does not set MAKE_JOBS.
68+
: "${MAKE_JOBS:=-j2}"
69+
70+
# We translate here with inserted IDs, even in the triceOff case and expect no warnings.
71+
make ${MAKE_JOBS} TRICE_FLAGS="${flags}" gcc
72+
73+
bash "${REPO_ROOT}/trice_cleanIDs_in_examples_and_test_folder.sh" \
74+
# Run this again to get the Trice IDs cleaned state.
2375

24-
../../trice_cleanIDs_in_examples_and_test_folder.sh # Run this again to get the Trice IDs cleaned state.
76+
# ------------------------------------------------------------------------------
77+
# 5) Additional triceOff verification build (without IDs)
78+
# ------------------------------------------------------------------------------
2579

2680
# Additionally we again translate the triceOff case without IDs and expect no warnings.
27-
if [ "$triceOFF" == "1" ]; then
81+
if [ "${triceOFF}" = "1" ]; then
2882
make clean
29-
make $MAKE_JOBS TRICE_FLAGS="$flags" gcc
83+
make ${MAKE_JOBS} TRICE_FLAGS="${flags}" gcc
3084
fi
Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,54 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
2+
#
3+
# Simple and CI-safe clang build script.
4+
#
5+
# Main idea:
6+
# - Always run relative to the script location (NOT the caller's working directory).
7+
# - Use strict error handling, so CI fails immediately and clearly.
8+
#
9+
10+
set -euo pipefail
11+
12+
# ------------------------------------------------------------------------------
13+
# 1) Resolve stable directories
14+
# ------------------------------------------------------------------------------
15+
16+
# Absolute path of the directory containing this script
17+
# - Works regardless of where the script is called from (CI often calls from repo root).
18+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
19+
20+
# Repo root is two levels above: <repo>/examples/<target>/build_with_clang.sh -> <repo>
21+
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
22+
23+
# Ensure we build from the example directory (where the Makefile is expected).
24+
cd "${SCRIPT_DIR}"
25+
26+
# ------------------------------------------------------------------------------
27+
# 2) Run TRICE scripts (use absolute paths, so CWD does not matter)
28+
# ------------------------------------------------------------------------------
229

330
# Trice is called here and not within make, to guarantee, it is finished before any other job starts.
4-
../../trice_cleanIDs_in_examples_and_test_folder.sh # Run this first to trigger the used editor to show the Trice IDs cleaned state.
5-
../../trice_insertIDs_in_examples_and_test_folder.sh
31+
bash "${REPO_ROOT}/trice_cleanIDs_in_examples_and_test_folder.sh" \
32+
# Run this first to trigger the used editor to show the Trice IDs cleaned state.
33+
34+
bash "${REPO_ROOT}/trice_insertIDs_in_examples_and_test_folder.sh"
35+
36+
# ------------------------------------------------------------------------------
37+
# 3) Load build environment and build
38+
# ------------------------------------------------------------------------------
39+
40+
# Source the environment file from repo root (absolute path).
41+
# shellcheck disable=SC1090
42+
source "${REPO_ROOT}/build_environment.sh"
43+
44+
# Optional: provide a default if build_environment.sh does not set MAKE_JOBS.
45+
: "${MAKE_JOBS:=-j2}"
46+
47+
make ${MAKE_JOBS} clang
648

7-
source ../../build_environment.sh
8-
make $MAKE_JOBS clang
49+
# ------------------------------------------------------------------------------
50+
# 4) Post-build cleanup (leave repo in cleaned state)
51+
# ------------------------------------------------------------------------------
952

10-
../../trice_cleanIDs_in_examples_and_test_folder.sh # Run this again to get the Trice IDs cleaned state.
53+
bash "${REPO_ROOT}/trice_cleanIDs_in_examples_and_test_folder.sh" \
54+
# Run this again to get the Trice IDs cleaned state.

examples/L432_bare/build.sh

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,37 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
2+
#
3+
# Simple and CI-safe build script.
4+
#
5+
# Main idea:
6+
# - Always run relative to the script location (NOT the caller's working directory).
7+
# - Use strict error handling, so CI fails immediately and clearly.
8+
#
29

3-
source ../../build_environment.sh
4-
make $MAKE_JOBS
10+
set -euo pipefail
11+
12+
# ------------------------------------------------------------------------------
13+
# 1) Resolve stable directories
14+
# ------------------------------------------------------------------------------
15+
16+
# Absolute path of the directory containing this build.sh
17+
# - Works regardless of where the script is called from (CI often calls from repo root).
18+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
19+
20+
# Repo root is two levels above: <repo>/examples/<target>/build.sh -> <repo>
21+
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
22+
23+
# Ensure we build from the example directory (where the Makefile is expected).
24+
cd "${SCRIPT_DIR}"
25+
26+
# ------------------------------------------------------------------------------
27+
# 2) Load build environment and build
28+
# ------------------------------------------------------------------------------
29+
30+
# Source the environment file from repo root (absolute path).
31+
# shellcheck disable=SC1090
32+
source "${REPO_ROOT}/build_environment.sh"
33+
34+
# Optional: provide a default if build_environment.sh does not set MAKE_JOBS.
35+
# : "${MAKE_JOBS:=-j2}"
36+
37+
make ${MAKE_JOBS}

examples/L432_inst/build.sh

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,71 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
2+
#
3+
# Simple and CI-safe build script (pattern-preserving version).
4+
#
5+
# Main idea:
6+
# - Always run relative to the script location (NOT the caller's working directory).
7+
# - Use strict error handling, so CI fails immediately and clearly.
8+
#
9+
10+
set -euo pipefail
11+
12+
# ------------------------------------------------------------------------------
13+
# 1) Resolve stable directories
14+
# ------------------------------------------------------------------------------
15+
16+
# Absolute path of the directory containing this build.sh
17+
# - Works regardless of where the script is called from (CI often calls from repo root).
18+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
19+
20+
# Repo root is two levels above: <repo>/examples/<target>/build.sh -> <repo>
21+
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
22+
23+
# Ensure we build from the example directory (where the Makefile is expected).
24+
cd "${SCRIPT_DIR}"
25+
26+
# ------------------------------------------------------------------------------
27+
# 2) Build TRICE_FLAGS (same logic as the original, but safe and predictable)
28+
# ------------------------------------------------------------------------------
229

330
# Initialize an empty string
431
flags=""
532

633
# Loop through all arguments
734
for arg in "$@"; do
8-
flags+="-D$arg "
35+
flags="${flags}-D${arg} "
936
done
1037

38+
# ------------------------------------------------------------------------------
39+
# 3) Run TRICE scripts (use absolute paths, so CWD does not matter)
40+
# ------------------------------------------------------------------------------
41+
1142
# Trice is called here and not within make, to guarantee, it is finished before any other job starts.
12-
../../trice_cleanIDs_in_examples_and_test_folder.sh # Run this first to trigger the used editor to show the Trice IDs cleaned state.
13-
../../trice_insertIDs_in_examples_and_test_folder.sh
43+
bash "${REPO_ROOT}/trice_cleanIDs_in_examples_and_test_folder.sh" \
44+
# Run this first to trigger the used editor to show the Trice IDs cleaned state.
45+
46+
bash "${REPO_ROOT}/trice_insertIDs_in_examples_and_test_folder.sh"
47+
48+
# ------------------------------------------------------------------------------
49+
# 4) Load build environment and build
50+
# ------------------------------------------------------------------------------
1451

15-
source ../../build_environment.sh
16-
make $MAKE_JOBS TRICE_FLAGS="$flags" gcc
52+
# Source the environment file from repo root (absolute path).
53+
# shellcheck disable=SC1090
54+
source "${REPO_ROOT}/build_environment.sh"
1755

56+
# Optional: provide a default if build_environment.sh does not set MAKE_JOBS.
57+
: "${MAKE_JOBS:=-j2}"
58+
59+
make ${MAKE_JOBS} TRICE_FLAGS="${flags}" gcc
60+
61+
# Preserve make's exit code exactly (your original behavior).
1862
EXITCODE=$?
1963

20-
../../trice_cleanIDs_in_examples_and_test_folder.sh # Run this again to get the Trice IDs cleaned state.
64+
# ------------------------------------------------------------------------------
65+
# 5) Post-build cleanup and exit
66+
# ------------------------------------------------------------------------------
67+
68+
bash "${REPO_ROOT}/trice_cleanIDs_in_examples_and_test_folder.sh" \
69+
# Run this again to get the Trice IDs cleaned state.
2170

22-
exit $EXITCODE
71+
exit "${EXITCODE}"

0 commit comments

Comments
 (0)