3 # Tool to bundle multiple C/C++ source files, inlining any includes.
5 # TODO: ROOTS, FOUND, etc., as arrays (since they fail on paths with spaces)
7 # Author: Carl Woffenden, Numfum GmbH (this script is released under a CC0 license/Public Domain)
12 # -x option excluded includes
15 # -k option includes to keep as include directives
18 # Files previously visited
21 # Optional destination file (empty string to write to stdout)
24 # Whether the "#pragma once" directives should be written to the output
27 # Prints the script usage then exits
29 echo "Usage: $0 [-r <path>] [-x <header>] [-k <header>] [-o <outfile>] infile"
30 echo " -r file root search path"
31 echo " -x file to completely exclude from inlining"
32 echo " -k file to exclude from inlining but keep the include directive"
33 echo " -p keep any '#pragma once' directives (removed by default)"
34 echo " -o output file (otherwise stdout)"
35 echo "Example: $0 -r ../my/path - r ../other/path -o out.c in.c"
39 # Tests that the grep implementation works as expected (older OSX grep fails)
41 if ! echo '#include "foo"' | grep -Eq '^\s*#\s*include\s*".+"'; then
42 echo "Aborting: the grep implementation fails to parse include lines"
45 if ! echo '"foo.h"' | sed -E 's/"([^"]+)"/\1/' | grep -Eq '^foo\.h$'; then
46 echo "Aborting: sed is unavailable or non-functional"
51 # Test if glob pattern $1 matches subject $2 (see fnmatch(3))
61 # Test if line $1 is local include directive
63 fnmatch "*#*include*" "$1" || return 1
64 printf "%s\n" "$1" | grep -Eq '^\s*#\s*include\s*".+"'
67 # Test if line $1 is pragma once directive
68 is_pragma_once_line() {
69 fnmatch "*#*pragma*once*" "$1" || return 1
70 printf "%s\n" "$1" | grep -Eq '^\s*#\s*pragma\s*once\s*'
73 # Tests if list $1 has item $2 (returning zero on a match)
74 # (originally used grep -Eq "(^|\s*)$2(\$|\s*))
75 readonly list_FS="$IFS"
77 # Re: escaping glob pattern special characters in item string:
79 # bash (tested 3.2.57, 5.1.4), dash (tested 0.5.10.2), NetBSD /bin/sh
80 # (tested 8.2), and Solaris /bin/sh (tested 11.4) require escaping
81 # backslashes in a bracket expression despite POSIX specifying that
82 # backslash loses significance in a bracket expression.
84 # Conversely, neither FreeBSD /bin/sh (tested 12.2) nor OpenBSD /bin/sh
85 # (tested 7.1) obey backslash-escaping in case statement patterns even
86 # outside bracket expressions, so escape special characters using bracket
89 # Solaris /bin/sh (tested 11.4) requires vertical bar (|) to be escaped.
91 # All accommodations should behave as expected under strict POSIX semantics.
92 if fnmatch "*[\\*?[|]*" "$2"; then
93 set -- "$1" "$(printf '%s\n' "$2" | sed -e 's/[*?[|]/[&]/g; s/[\]/[\\&]/g')"
95 for item_P in "*[$list_FS]$2[$list_FS]*" "*[$list_FS]$2" "$2[$list_FS]*" "$2"; do
96 fnmatch "${item_P}" "$1" && return 0
101 # Adds a new line with the supplied arguments to $DESTN (or stdout)
103 if [ -n "$DESTN" ]; then
104 printf '%s\n' "$@" >> "$DESTN"
118 for root in $srcdir $ROOTS; do
119 if [ -f "$root/$inc" ]; then
120 # Try to reduce the file path into a canonical form (so that multiple)
121 # includes of the same file are successfully deduplicated, even if they
122 # are expressed differently.
123 local relpath="$(realpath --relative-to . "$root/$inc" 2>/dev/null)"
124 if [ "$relpath" != "" ]; then # not all realpaths support --relative-to
128 local relpath="$(realpath "$root/$inc" 2>/dev/null)"
129 if [ "$relpath" != "" ]; then # not all distros have realpath...
133 # Fallback on Python to reduce the path if the above fails.
134 local relpath=$(python -c "import os,sys; print os.path.relpath(sys.argv[1])" "$root/$inc" 2>/dev/null)
135 if [ "$relpath" != "" ]; then # not all distros have realpath...
139 # Worst case, fall back to just the root + relative include path. The
140 # problem with this is that it is possible to emit multiple different
141 # resolved paths to the same file, depending on exactly how its included.
142 # Since the main loop below keeps a list of the resolved paths it's
143 # already included, in order to avoid repeated includes, this failure to
144 # produce a canonical/reduced path can lead to multiple inclusions of the
145 # same file. But it seems like the resulting single file library still
146 # works (hurray include guards!), so I guess it's ok.
154 # Adds the contents of $1 with any of its includes inlined
157 if [ -n "$file" ]; then
158 log_line "Processing: $file"
159 # Get directory of the current so we can resolve relative includes
160 local srcdir="$(dirname "$file")"
163 while IFS= read -r line; do
164 if is_include_line "$line"; then
165 # We have an include directive so strip the (first) file
166 local inc=$(echo "$line" | grep -Eo '".*"' | sed -E 's/"([^"]+)"/\1/' | head -1)
167 local res_inc="$(resolve_include "$srcdir" "$inc")"
168 if list_has_item "$XINCS" "$inc"; then
169 # The file was excluded so error if the source attempts to use it
170 write_line "#error Using excluded file: $inc (re-amalgamate source to fix)"
171 log_line "Excluding: $inc"
173 if ! list_has_item "$FOUND" "$res_inc"; then
174 # The file was not previously encountered
175 FOUND="$FOUND $res_inc"
176 if list_has_item "$KINCS" "$inc"; then
177 # But the include was flagged to keep as included
178 write_line "/**** *NOT* inlining $inc ****/"
180 log_line "Not Inlining: $inc"
182 # The file was neither excluded nor seen before so inline it
183 write_line "/**** start inlining $inc ****/"
185 write_line "/**** ended inlining $inc ****/"
188 write_line "/**** skipping file: $inc ****/"
192 # Skip any 'pragma once' directives, otherwise write the source line
194 if [ $write -eq 0 ]; then
195 if ! is_pragma_once_line "$line"; then
199 if [ $write -ne 0 ]; then
205 write_line "#error Unable to find \"$1\""
206 log_line "Error: Unable to find: \"$1\""
210 while getopts ":r:x:k:po:" opts; do
213 ROOTS="$ROOTS $OPTARG"
216 XINCS="$XINCS $OPTARG"
219 KINCS="$KINCS $OPTARG"
236 if [ -n "$DESTN" ]; then
240 log_line "Processing using the slower shell script; this might take a while"
243 echo "Input file not found: \"$1\""