|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | +# |
| 4 | +# Compile every portable wolfCert source for a bare-metal ARM target: no POSIX |
| 5 | +# headers, no built-in transport, no POSIX store, no test server. This keeps |
| 6 | +# src/http.c, src/csr.c and src/store.c free of syscalls, so wolfCert can run on |
| 7 | +# a non-BSD-sockets stack through a caller-supplied WolfCertTransport. |
| 8 | +# |
| 9 | +# Compile-only, so it wants a wolfSSL checkout for headers rather than a build, |
| 10 | +# and finishes in seconds. Feature set: freestanding-user_settings.h alongside. |
| 11 | +# |
| 12 | +# Usage: |
| 13 | +# compile-freestanding.sh --wolfssl-src DIR [--cc CC] [--cpu MCPU] |
| 14 | +# |
| 15 | +# Defaults: --cc arm-none-eabi-gcc, --cpu cortex-m4 (CC_BIN / CPU also override). |
| 16 | + |
| 17 | +set -euo pipefail |
| 18 | + |
| 19 | +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 20 | +ROOT="$(cd "$HERE/../.." && pwd)" |
| 21 | + |
| 22 | +CC_BIN="${CC_BIN:-arm-none-eabi-gcc}" |
| 23 | +CPU="${CPU:-cortex-m4}" |
| 24 | +WOLFSSL_SRC="" |
| 25 | + |
| 26 | +while [ "$#" -gt 0 ]; do |
| 27 | + case "$1" in |
| 28 | + --wolfssl-src) WOLFSSL_SRC="$2"; shift 2 ;; |
| 29 | + --cc) CC_BIN="$2"; shift 2 ;; |
| 30 | + --cpu) CPU="$2"; shift 2 ;; |
| 31 | + -h|--help) grep '^#' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; |
| 32 | + *) echo "ERROR: unknown flag '$1'." >&2; exit 2 ;; |
| 33 | + esac |
| 34 | +done |
| 35 | + |
| 36 | +if [ -z "$WOLFSSL_SRC" ]; then |
| 37 | + echo "ERROR: --wolfssl-src is required." >&2 |
| 38 | + exit 2 |
| 39 | +fi |
| 40 | +if [ ! -d "$WOLFSSL_SRC/wolfssl" ]; then |
| 41 | + echo "ERROR: '$WOLFSSL_SRC' does not look like a wolfSSL checkout." >&2 |
| 42 | + exit 2 |
| 43 | +fi |
| 44 | + |
| 45 | +STAGE="$(mktemp -d)" |
| 46 | +trap 'rm -rf "$STAGE"' EXIT |
| 47 | +mkdir -p "$STAGE/inc" "$STAGE/obj" |
| 48 | + |
| 49 | +# Never a configured tree's own options.h: it describes that build, not the set |
| 50 | +# pinned here. The stub replacing it is what --enable-usersettings generates, |
| 51 | +# which check_config.h and memory.h need to resolve <wolfssl/options.h>. |
| 52 | +cp -R "$WOLFSSL_SRC/wolfssl" "$STAGE/inc/wolfssl" |
| 53 | +rm -f "$STAGE/inc/wolfssl/options.h" |
| 54 | +cat > "$STAGE/inc/wolfssl/options.h" <<'EOF' |
| 55 | +#ifndef WOLFSSL_OPTIONS_H |
| 56 | +#define WOLFSSL_OPTIONS_H |
| 57 | +#include <user_settings.h> |
| 58 | +#endif |
| 59 | +EOF |
| 60 | + |
| 61 | +cp "$HERE/freestanding-user_settings.h" "$STAGE/inc/user_settings.h" |
| 62 | + |
| 63 | +# From CMakeLists.txt, so a new src/*.c joins the gate automatically. Skipped: |
| 64 | +# the POSIX transport and the test server, both POSIX by design. |
| 65 | +SKIP_RE='^src/(net_posix|server|ca_issue|est/est_server|scep/scep_server)\.c$' |
| 66 | +SRCS="$(grep -oE 'src/[A-Za-z0-9_/]+\.c' "$ROOT/CMakeLists.txt" \ |
| 67 | + | sort -u | grep -Ev "$SKIP_RE" || true)" |
| 68 | + |
| 69 | +if [ -z "$SRCS" ]; then |
| 70 | + echo "ERROR: no sources extracted from CMakeLists.txt" >&2 |
| 71 | + exit 1 |
| 72 | +fi |
| 73 | +n_srcs="$(printf '%s\n' "$SRCS" | wc -l | tr -d ' ')" |
| 74 | + |
| 75 | +# -Wno-error=cpp keeps wolfSSL's own #warning non-fatal; ours stay fatal. |
| 76 | +CFLAGS=(-c -mcpu="$CPU" -mthumb -ffreestanding |
| 77 | + -Wall -Wextra -Werror -Wno-error=cpp |
| 78 | + -DWOLFSSL_USER_SETTINGS -DWOLFCERT_USER_SETTINGS |
| 79 | + -I"$ROOT" -I"$STAGE/inc") |
| 80 | + |
| 81 | +echo "Compiling $n_srcs sources with $CC_BIN (-mcpu=$CPU), no POSIX:" |
| 82 | +status=0 |
| 83 | +# Fed by here-doc rather than an array: macOS ships bash 3.2, which has no |
| 84 | +# mapfile. |
| 85 | +while IFS= read -r s; do |
| 86 | + obj="$STAGE/obj/$(echo "$s" | tr '/' '_').o" |
| 87 | + if "$CC_BIN" "${CFLAGS[@]}" -o "$obj" "$ROOT/$s" 2> "$STAGE/err.log"; then |
| 88 | + printf ' ok %s\n' "$s" |
| 89 | + else |
| 90 | + printf ' FAIL %s\n' "$s" |
| 91 | + sed 's/^/ /' "$STAGE/err.log" |
| 92 | + status=1 |
| 93 | + fi |
| 94 | +done <<EOF |
| 95 | +$SRCS |
| 96 | +EOF |
| 97 | + |
| 98 | +if [ "$status" -eq 0 ]; then |
| 99 | + echo "freestanding compile OK: $n_srcs sources, no POSIX headers." |
| 100 | +fi |
| 101 | +exit "$status" |
0 commit comments