Try to Fix Single File Library Combiner Script to Handle Relative Includes

This commit is contained in:
W. Felix Handte 2020-05-01 17:56:51 -04:00
parent 7b75d772b1
commit b48f6c7d26
5 changed files with 63 additions and 54 deletions

View File

@ -66,33 +66,43 @@ write_line() {
fi
}
# Adds the contents of $1 with any of its includes inlined
add_file() {
# Match the path
local file=
for root in $ROOTS; do
if [ -f "$root/$1" ]; then
file="$root/$1"
# Find this file!
resolve_include() {
local srcdir=$1
local inc=$2
for root in $srcdir $ROOTS; do
if [ -f "$root/$inc" ]; then
echo "$(realpath --relative-to . "$root/$inc")"
return 0
fi
done
return 1
}
# Adds the contents of $1 with any of its includes inlined
add_file() {
local file=$1
if [ -n "$file" ]; then
if [ -n "$DESTN" ]; then
# Log but only if not writing to stdout
echo "Processing: $file"
fi
# Get directory to resolve relative includes
local srcdir="$(dirname "$file")"
# Read the file
local line=
while IFS= read -r line; do
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)
local inc=$(echo "$line" | grep -Eo '".*"' | sed 's/"\([^"]\+\)"/\1/' | head -1)
local res_inc="$(resolve_include "$srcdir" "$inc")"
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"
else
if ! list_has_item "$FOUND" "$inc"; then
if ! list_has_item "$FOUND" "$res_inc"; then
# The file was not previously encountered
FOUND="$FOUND $inc"
FOUND="$FOUND $res_inc"
if list_has_item "$KINCS" "$inc"; then
# But the include was flagged to keep as included
write_line "/**** *NOT* inlining $inc ****/"
@ -100,7 +110,7 @@ add_file() {
else
# The file was neither excluded nor seen before so inline it
write_line "/**** start inlining $inc ****/"
add_file "$inc"
add_file "$res_inc"
write_line "/**** ended inlining $inc ****/"
fi
else
@ -122,6 +132,10 @@ add_file() {
done < "$file"
else
write_line "#error Unable to find \"$1\""
if [ -n "$DESTN" ]; then
# Log but only if not writing to stdout
echo "Error: Unable to find: \"$1\""
fi
fi
}
@ -155,7 +169,7 @@ if [ -n "$1" ]; then
printf "" > "$DESTN"
fi
test_grep
add_file $1
add_file "$1"
else
echo "Input file not found: \"$1\""
exit 1

View File

@ -5,7 +5,7 @@ ZSTD_SRC_ROOT="../../lib"
# Amalgamate the sources
echo "Amalgamating files... this can take a while"
./combine.sh -r "$ZSTD_SRC_ROOT" -r "$ZSTD_SRC_ROOT/common" -r "$ZSTD_SRC_ROOT/decompress" -o zstddeclib.c zstddeclib-in.c
./combine.sh -r "$ZSTD_SRC_ROOT" -o zstddeclib.c zstddeclib-in.c
# Did combining work?
if [ $? -ne 0 ]; then
echo "Combine script: FAILED"

View File

@ -5,7 +5,7 @@ ZSTD_SRC_ROOT="../../lib"
# Amalgamate the sources
echo "Amalgamating files... this can take a while"
./combine.sh -r "$ZSTD_SRC_ROOT" -r "$ZSTD_SRC_ROOT/common" -r "$ZSTD_SRC_ROOT/compress" -r "$ZSTD_SRC_ROOT/decompress" -k zstd.h -o zstd.c zstd-in.c
./combine.sh -r "$ZSTD_SRC_ROOT" -k zstd.h -o zstd.c zstd-in.c
# Did combining work?
if [ $? -ne 0 ]; then
echo "Combine script: FAILED"

View File

@ -4,7 +4,7 @@
*
* Generate using:
* \code
* combine.sh -r ../../lib -r ../../lib/common -r ../../lib/compress -r ../../lib/decompress -k zstd.h -o zstd.c zstd-in.c
* combine.sh -r ../../lib -k zstd.h -o zstd.c zstd-in.c
* \endcode
*/
/*
@ -46,34 +46,31 @@
#define ZSTD_MULTITHREAD
#endif
/* lib/common */
#include "debug.c"
#include "entropy_common.c"
#include "error_private.c"
#include "fse_decompress.c"
#include "threading.c"
#include "pool.c"
#include "zstd_common.c"
#include "common/debug.c"
#include "common/entropy_common.c"
#include "common/error_private.c"
#include "common/fse_decompress.c"
#include "common/threading.c"
#include "common/pool.c"
#include "common/zstd_common.c"
/* lib/compress */
#include "fse_compress.c"
#include "hist.c"
#include "huf_compress.c"
#include "zstd_compress_literals.c"
#include "zstd_compress_sequences.c"
#include "zstd_compress_superblock.c"
#include "zstd_compress.c"
#include "zstd_double_fast.c"
#include "zstd_fast.c"
#include "zstd_lazy.c"
#include "zstd_ldm.c"
#include "zstd_opt.c"
#include "compress/fse_compress.c"
#include "compress/hist.c"
#include "compress/huf_compress.c"
#include "compress/zstd_compress_literals.c"
#include "compress/zstd_compress_sequences.c"
#include "compress/zstd_compress_superblock.c"
#include "compress/zstd_compress.c"
#include "compress/zstd_double_fast.c"
#include "compress/zstd_fast.c"
#include "compress/zstd_lazy.c"
#include "compress/zstd_ldm.c"
#include "compress/zstd_opt.c"
#ifdef ZSTD_MULTITHREAD
#include "zstdmt_compress.c"
#include "compress/zstdmt_compress.c"
#endif
/* lib/decompress */
#include "huf_decompress.c"
#include "zstd_ddict.c"
#include "zstd_decompress.c"
#include "zstd_decompress_block.c"
#include "decompress/huf_decompress.c"
#include "decompress/zstd_ddict.c"
#include "decompress/zstd_decompress.c"
#include "decompress/zstd_decompress_block.c"

View File

@ -4,7 +4,7 @@
*
* Generate using:
* \code
* combine.sh -r ../../lib -r ../../lib/common -r ../../lib/decompress -o zstddeclib.c zstddeclib-in.c
* combine.sh -r ../../lib -o zstddeclib.c zstddeclib-in.c
* \endcode
*/
/*
@ -42,15 +42,13 @@
#define ZSTD_NOBENCH
#define ZSTD_STRIP_ERROR_STRINGS
/* lib/common */
#include "debug.c"
#include "entropy_common.c"
#include "error_private.c"
#include "fse_decompress.c"
#include "zstd_common.c"
#include "common/debug.c"
#include "common/entropy_common.c"
#include "common/error_private.c"
#include "common/fse_decompress.c"
#include "common/zstd_common.c"
/* lib/decompress */
#include "huf_decompress.c"
#include "zstd_ddict.c"
#include "zstd_decompress.c"
#include "zstd_decompress_block.c"
#include "decompress/huf_decompress.c"
#include "decompress/zstd_ddict.c"
#include "decompress/zstd_decompress.c"
#include "decompress/zstd_decompress_block.c"