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.
|
|
|
|
#
|
2020-04-03 17:07:46 +00:00
|
|
|
# TODO: ROOTS, FOUND, etc., as arrays (since they fail on paths with spaces)
|
|
|
|
# TODO: revert to Bash-only regex (the grep ones being too slow)
|
2019-09-02 16:02:50 +00:00
|
|
|
#
|
2020-04-03 18:50:54 +00:00
|
|
|
# Author: Carl Woffenden, Numfum GmbH (this script is released under a CC0 license/Public Domain)
|
2019-08-23 16:43:29 +00:00
|
|
|
|
|
|
|
# Common file roots
|
2020-04-03 17:07:46 +00:00
|
|
|
ROOTS="."
|
|
|
|
|
|
|
|
# -x option excluded includes
|
|
|
|
XINCS=""
|
|
|
|
|
|
|
|
# -k option includes to keep as include directives
|
|
|
|
KINCS=""
|
2019-08-23 16:43:29 +00:00
|
|
|
|
|
|
|
# Files previously visited
|
|
|
|
FOUND=""
|
|
|
|
|
2019-08-25 20:49:01 +00:00
|
|
|
# Optional destination file (empty string to write to stdout)
|
|
|
|
DESTN=""
|
|
|
|
|
2020-04-03 17:07:46 +00:00
|
|
|
# Whether the "#pragma once" directives should be written to the output
|
|
|
|
PONCE=0
|
|
|
|
|
2019-08-23 16:43:29 +00:00
|
|
|
# Prints the script usage then exits
|
2019-08-28 17:20:42 +00:00
|
|
|
usage() {
|
2020-04-03 17:07:46 +00:00
|
|
|
echo "Usage: $0 [-r <path>] [-x <header>] [-k <header>] [-o <outfile>] infile"
|
|
|
|
echo " -r file root search path"
|
|
|
|
echo " -x file to completely exclude from inlining"
|
|
|
|
echo " -k file to exclude from inlining but keep the include directive"
|
|
|
|
echo " -p keep any '#pragma once' directives (removed by default)"
|
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)
|
2020-05-05 17:58:22 +00:00
|
|
|
test_deps() {
|
2020-04-03 17:07:46 +00:00
|
|
|
if ! echo '#include "foo"' | grep -Eq '^\s*#\s*include\s*".+"'; then
|
|
|
|
echo "Aborting: the grep implementation fails to parse include lines"
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-05-05 18:54:04 +00:00
|
|
|
if ! echo '"foo.h"' | sed -E 's/"([^"]+)"/\1/' | grep -Eq '^foo\.h$'; then
|
2020-05-05 17:58:22 +00:00
|
|
|
echo "Aborting: sed is unavailable or non-functional"
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-09-02 16:02:50 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:58:22 +00:00
|
|
|
log_line() {
|
|
|
|
echo $@ >&2
|
|
|
|
}
|
|
|
|
|
2020-05-01 21:56:51 +00:00
|
|
|
# Find this file!
|
|
|
|
resolve_include() {
|
|
|
|
local srcdir=$1
|
|
|
|
local inc=$2
|
|
|
|
for root in $srcdir $ROOTS; do
|
|
|
|
if [ -f "$root/$inc" ]; then
|
2020-05-05 17:18:45 +00:00
|
|
|
# Try to reduce the file path into a canonical form (so that multiple)
|
|
|
|
# includes of the same file are successfully deduplicated, even if they
|
|
|
|
# are expressed differently.
|
2020-05-05 19:25:18 +00:00
|
|
|
local relpath="$(realpath --relative-to . "$root/$inc" 2>/dev/null)"
|
2020-05-05 17:18:45 +00:00
|
|
|
if [ "$relpath" != "" ]; then # not all realpaths support --relative-to
|
|
|
|
echo "$relpath"
|
|
|
|
return 0
|
2020-05-04 22:59:47 +00:00
|
|
|
fi
|
2020-05-05 19:25:18 +00:00
|
|
|
local relpath="$(realpath "$root/$inc" 2>/dev/null)"
|
2020-05-05 17:18:45 +00:00
|
|
|
if [ "$relpath" != "" ]; then # not all distros have realpath...
|
|
|
|
echo "$relpath"
|
|
|
|
return 0
|
|
|
|
fi
|
2020-05-18 08:12:02 +00:00
|
|
|
# Fallback on Python to reduce the path if the above fails.
|
|
|
|
local relpath=$(python -c "import os,sys; print os.path.relpath(sys.argv[1])" "$root/$inc" 2>/dev/null)
|
|
|
|
if [ "$relpath" != "" ]; then # not all distros have realpath...
|
|
|
|
echo "$relpath"
|
|
|
|
return 0
|
|
|
|
fi
|
2020-05-05 17:18:45 +00:00
|
|
|
# Worst case, fall back to just the root + relative include path. The
|
|
|
|
# problem with this is that it is possible to emit multiple different
|
|
|
|
# resolved paths to the same file, depending on exactly how its included.
|
|
|
|
# Since the main loop below keeps a list of the resolved paths it's
|
|
|
|
# already included, in order to avoid repeated includes, this failure to
|
|
|
|
# produce a canonical/reduced path can lead to multiple inclusions of the
|
|
|
|
# same file. But it seems like the resulting single file library still
|
|
|
|
# works (hurray include guards!), so I guess it's ok.
|
|
|
|
echo "$root/$inc"
|
2020-05-01 21:56:51 +00:00
|
|
|
return 0
|
2020-04-03 17:07:46 +00:00
|
|
|
fi
|
|
|
|
done
|
2020-05-01 21:56:51 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# Adds the contents of $1 with any of its includes inlined
|
|
|
|
add_file() {
|
|
|
|
local file=$1
|
2019-08-25 20:49:01 +00:00
|
|
|
if [ -n "$file" ]; then
|
2020-05-05 17:58:22 +00:00
|
|
|
log_line "Processing: $file"
|
2020-05-05 17:18:45 +00:00
|
|
|
# Get directory of the current so we can resolve relative includes
|
2020-05-01 21:56:51 +00:00
|
|
|
local srcdir="$(dirname "$file")"
|
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
|
2020-05-05 18:54:04 +00:00
|
|
|
local inc=$(echo "$line" | grep -Eo '".*"' | sed -E 's/"([^"]+)"/\1/' | head -1)
|
2020-05-01 21:56:51 +00:00
|
|
|
local res_inc="$(resolve_include "$srcdir" "$inc")"
|
2020-04-03 17:07:46 +00:00
|
|
|
if list_has_item "$XINCS" "$inc"; then
|
|
|
|
# The file was excluded so error if the source attempts to use it
|
|
|
|
write_line "#error Using excluded file: $inc"
|
2020-05-05 19:25:18 +00:00
|
|
|
log_line "Excluding: $inc"
|
2019-08-23 16:43:29 +00:00
|
|
|
else
|
2020-05-01 21:56:51 +00:00
|
|
|
if ! list_has_item "$FOUND" "$res_inc"; then
|
2020-04-03 17:07:46 +00:00
|
|
|
# The file was not previously encountered
|
2020-05-01 21:56:51 +00:00
|
|
|
FOUND="$FOUND $res_inc"
|
2020-04-03 17:07:46 +00:00
|
|
|
if list_has_item "$KINCS" "$inc"; then
|
|
|
|
# But the include was flagged to keep as included
|
|
|
|
write_line "/**** *NOT* inlining $inc ****/"
|
|
|
|
write_line "$line"
|
2020-05-05 19:25:18 +00:00
|
|
|
log_line "Not Inlining: $inc"
|
2020-04-03 17:07:46 +00:00
|
|
|
else
|
|
|
|
# The file was neither excluded nor seen before so inline it
|
|
|
|
write_line "/**** start inlining $inc ****/"
|
2020-05-01 21:56:51 +00:00
|
|
|
add_file "$res_inc"
|
2020-04-03 17:07:46 +00:00
|
|
|
write_line "/**** ended inlining $inc ****/"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
write_line "/**** skipping file: $inc ****/"
|
|
|
|
fi
|
2019-08-23 16:43:29 +00:00
|
|
|
fi
|
|
|
|
else
|
2020-04-03 17:07:46 +00:00
|
|
|
# Skip any 'pragma once' directives, otherwise write the source line
|
|
|
|
local write=$PONCE
|
|
|
|
if [ $write -eq 0 ]; then
|
|
|
|
if echo "$line" | grep -Eqv '^\s*#\s*pragma\s*once\s*'; then
|
|
|
|
write=1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ $write -ne 0 ]; then
|
|
|
|
write_line "$line"
|
|
|
|
fi
|
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\""
|
2020-05-05 17:58:22 +00:00
|
|
|
log_line "Error: Unable to find: \"$1\""
|
2019-08-23 16:43:29 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-04-03 17:07:46 +00:00
|
|
|
while getopts ":r:x:k:po:" opts; do
|
2019-08-23 16:43:29 +00:00
|
|
|
case $opts in
|
|
|
|
r)
|
2020-04-03 17:07:46 +00:00
|
|
|
ROOTS="$ROOTS $OPTARG"
|
|
|
|
;;
|
|
|
|
x)
|
|
|
|
XINCS="$XINCS $OPTARG"
|
|
|
|
;;
|
|
|
|
k)
|
|
|
|
KINCS="$KINCS $OPTARG"
|
|
|
|
;;
|
|
|
|
p)
|
|
|
|
PONCE=1
|
2019-08-25 20:49:01 +00:00
|
|
|
;;
|
|
|
|
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
|
2020-05-05 17:58:22 +00:00
|
|
|
test_deps
|
2020-05-01 21:56:51 +00:00
|
|
|
add_file "$1"
|
2019-08-25 20:49:01 +00:00
|
|
|
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
|