|
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 | +# ------------------------------------------------------------------------------ |
2 | 29 |
|
3 | 30 | # Initialize an empty string |
4 | 31 | flags="" |
5 | 32 |
|
| 33 | +# Track TRICE_OFF=1 explicitly (used later for conditional insert/build) |
| 34 | +triceOFF="0" |
| 35 | + |
6 | 36 | # Loop through all arguments |
7 | 37 | 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 |
10 | 41 | fi |
11 | | - flags+="-D$arg " |
| 42 | + flags="${flags}-D${arg} " |
12 | 43 | done |
13 | 44 |
|
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. |
16 | 53 |
|
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 |
19 | 57 | fi |
20 | 58 |
|
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. |
23 | 75 |
|
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 | +# ------------------------------------------------------------------------------ |
25 | 79 |
|
26 | 80 | # Additionally we again translate the triceOff case without IDs and expect no warnings. |
27 | | -if [ "$triceOFF" == "1" ]; then |
| 81 | +if [ "${triceOFF}" = "1" ]; then |
28 | 82 | make clean |
29 | | - make $MAKE_JOBS TRICE_FLAGS="$flags" gcc |
| 83 | + make ${MAKE_JOBS} TRICE_FLAGS="${flags}" gcc |
30 | 84 | fi |
0 commit comments