blob: 98a0c9d3b9aa922c84beeaa3b626d95be8304b5d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#!/usr/bin/env bash
# embed.sh
# a simple script to handle embedding binary file contents into invocations
# of the CATBOOT_EMBED macro within catboot compiler source files.
# invoked with `./embed.sh source-1.c source-2.c header.h`
# outputs a file structure within the ./build/ directory matching the source
# directory, with the correct replacements done.
#
# Copyright (c) 2026, Mel G. <mel@rnrd.eu>
#
# SPDX-License-Identifier: MPL-2.0
set -euo pipefail
build_directory="./build/"
process_file() {
local source_file="$1"
local output_file="$build_directory/$source_file"
mkdir -p $(dirname "$output_file")
cp "$source_file" "$output_file"
local embed_regex='CATBOOT_EMBED\("([^)]+)"\)' # const byte data[] = CATBOOT_EMBED("./file");
local comment_regex='^\s*//' # // the macro CATBOOT_EMBED does...
local define_regex='^\s*#define' # #define CATBOOT_EMBED {0}
while IFS= read -r line; do
# skip lines which are just comments mentioning the macro and
# the line where we define the macro in the first place.
if [[ $line =~ $comment_regex ]] || [[ $line =~ $define_regex ]]; then
continue
fi
if [[ $line =~ $embed_regex ]]; then
local pattern="${BASH_REMATCH[0]}"
local embed_path="${BASH_REMATCH[1]}"
# check that the file we want to embed actually exists
if [ ! -f "$embed_path" ]; then
echo "error: file not found: $embed_path (in $source_file)" >&2
exit 1
fi
# get hexadecimal representation of file contents.
# trim of start and end to get just the files, merge into one line and remove trailing whitespace.
local hex_bytes=$(xxd -i "$embed_path" | tail -n+2 | head -n-2 | tr -d '\n' | xargs)
# add in nil byte at the end of the binary data, for easier
# interoperation with c-type strings
local replacement="{ $hex_bytes, 0x00 }"
local content=$(<"$output_file")
content="${content//$pattern/$replacement}" # replace invocation with binary contents w/ bash.
echo "$content" > "$output_file"
fi
done < "$source_file"
}
for source_file in "$@"; do
process_file "$source_file"
done
|