2019-08-28 17:20:42 +00:00
|
|
|
#!/bin/sh -e
|
2019-08-23 16:43:29 +00:00
|
|
|
|
|
|
|
# Tool to bundle multiple C/C++ source files, inlining any includes.
|
2019-08-25 20:49:01 +00:00
|
|
|
#
|
2019-08-28 17:20:42 +00:00
|
|
|
# Note: this POSIX-compliant script is many times slower than the original bash
|
|
|
|
# implementation (due to the grep calls) but it runs and works everywhere.
|
|
|
|
#
|
2019-08-25 20:49:01 +00:00
|
|
|
# TODO: ROOTS and FOUND as arrays (since they fail on paths with spaces)
|
2019-09-02 16:02:50 +00:00
|
|
|
#
|
|
|
|
# Author: Carl Woffenden, Numfum GmbH (released under a CC0 license)
|
2019-08-23 16:43:29 +00:00
|
|
|
|
|
|
|
# Common file roots
|
|
|
|
ROOTS="./"
|
|
|
|
|
|
|
|
# Files previously visited
|
|
|
|
FOUND=""
|
|
|
|
|
2019-08-25 20:49:01 +00:00
|
|
|
# Optional destination file (empty string to write to stdout)
|
|
|
|
DESTN=""
|
|
|
|
|
2019-08-23 16:43:29 +00:00
|
|
|
# Prints the script usage then exits
|
2019-08-28 17:20:42 +00:00
|
|
|
usage() {
|
2019-08-25 20:49:01 +00:00
|
|
|
echo "Usage: $0 [-r <path>] [-o <outfile>] infile"
|
2019-08-23 16:43:29 +00:00
|
|
|
echo " -r file root search paths"
|
2019-08-25 20:49:01 +00:00
|
|
|
echo " -o output file (otherwise stdout)"
|
|
|
|
echo "Example: $0 -r ../my/path - r ../other/path -o out.c in.c"
|
2019-08-23 16:43:29 +00:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2019-09-02 16:02:50 +00:00
|
|
|
# Tests that the grep implementation works as expected (older OSX grep fails)
|
|
|
|
test_grep() {
|
|
|
|
if ! echo '#include "foo"' | grep -Eq '^\s*#\s*include\s*".+"'; then
|
|
|
|
echo "Aborting: the grep implementation fails to parse include lines"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-08-28 17:20:42 +00:00
|
|
|
# Tests if list $1 has item $2 (returning zero on a match)
|
|
|
|
list_has_item() {
|
|
|
|
if echo "$1" | grep -Eq "(^|\s*)$2(\$|\s*)"; then
|
2019-08-23 16:43:29 +00:00
|
|
|
return 0
|
2019-08-28 17:20:42 +00:00
|
|
|
else
|
|
|
|
return 1
|
2019-08-23 16:43:29 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-08-25 20:49:01 +00:00
|
|
|
# Adds a new line with the supplied arguments to $DESTN (or stdout)
|
2019-08-28 17:20:42 +00:00
|
|
|
write_line() {
|
2019-08-25 20:49:01 +00:00
|
|
|
if [ -n "$DESTN" ]; then
|
2019-08-28 17:20:42 +00:00
|
|
|
printf '%s\n' "$@" >> "$DESTN"
|
2019-08-25 20:49:01 +00:00
|
|
|
else
|
2019-08-28 17:20:42 +00:00
|
|
|
printf '%s\n' "$@"
|
2019-08-25 20:49:01 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-08-23 16:43:29 +00:00
|
|
|
# Adds the contents of $1 with any of its includes inlined
|
2019-08-28 17:20:42 +00:00
|
|
|
add_file() {
|
2019-08-23 16:43:29 +00:00
|
|
|
# Match the path
|
|
|
|
local file=
|
2019-08-26 05:48:57 +00:00
|
|
|
if [ -f "$1" ]; then
|
|
|
|
file="$1"
|
|
|
|
else
|
|
|
|
for root in $ROOTS; do
|
|
|
|
if test -f "$root/$1"; then
|
|
|
|
file="$root/$1"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2019-08-25 20:49:01 +00:00
|
|
|
if [ -n "$file" ]; then
|
2019-08-23 16:43:29 +00:00
|
|
|
# Read the file
|
2019-08-28 17:20:42 +00:00
|
|
|
local line=
|
2019-08-23 16:43:29 +00:00
|
|
|
while IFS= read -r line; do
|
2019-08-28 17:20:42 +00:00
|
|
|
if echo "$line" | grep -Eq '^\s*#\s*include\s*".+"'; then
|
|
|
|
# We have an include directive so strip the (first) file
|
|
|
|
local inc=$(echo "$line" | grep -Eo '".*"' | grep -Eo '\w*(\.?\w+)+' | head -1)
|
|
|
|
if ! list_has_item "$FOUND" "$inc"; then
|
2019-08-23 16:43:29 +00:00
|
|
|
# And we've not previously encountered it
|
|
|
|
FOUND="$FOUND $inc"
|
2019-08-25 20:49:01 +00:00
|
|
|
write_line "/**** start inlining $inc ****/"
|
2019-08-23 21:09:13 +00:00
|
|
|
add_file "$inc"
|
2019-08-25 20:49:01 +00:00
|
|
|
write_line "/**** ended inlining $inc ****/"
|
2019-08-23 16:43:29 +00:00
|
|
|
else
|
2019-08-25 20:49:01 +00:00
|
|
|
write_line "/**** skipping file: $inc ****/"
|
2019-08-23 16:43:29 +00:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
# Otherwise write the source line
|
2019-08-25 20:49:01 +00:00
|
|
|
write_line "$line"
|
2019-08-23 16:43:29 +00:00
|
|
|
fi
|
|
|
|
done < "$file"
|
|
|
|
else
|
2019-08-25 20:49:01 +00:00
|
|
|
write_line "#error Unable to find \"$1\""
|
2019-08-23 16:43:29 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-08-25 20:49:01 +00:00
|
|
|
while getopts ":r:o:" opts; do
|
2019-08-23 16:43:29 +00:00
|
|
|
case $opts in
|
|
|
|
r)
|
2019-08-25 20:49:01 +00:00
|
|
|
ROOTS="$OPTARG $ROOTS"
|
|
|
|
;;
|
|
|
|
o)
|
|
|
|
DESTN="$OPTARG"
|
2019-08-23 16:43:29 +00:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
|
2019-08-25 20:49:01 +00:00
|
|
|
if [ -n "$1" ]; then
|
|
|
|
if [ -f "$1" ]; then
|
|
|
|
if [ -n "$DESTN" ]; then
|
|
|
|
printf "" > "$DESTN"
|
|
|
|
fi
|
2019-09-02 16:02:50 +00:00
|
|
|
test_grep
|
2019-08-25 20:49:01 +00:00
|
|
|
add_file $1
|
|
|
|
else
|
2019-08-28 17:20:42 +00:00
|
|
|
echo "Input file not found: \"$1\""
|
2019-08-25 20:49:01 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2019-08-23 16:43:29 +00:00
|
|
|
else
|
|
|
|
usage
|
|
|
|
fi
|
2019-08-25 20:49:01 +00:00
|
|
|
exit 0
|