]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Try to Fix Single File Library Combiner Script to Handle Relative Includes
authorW. Felix Handte <w@felixhandte.com>
Fri, 1 May 2020 21:56:51 +0000 (17:56 -0400)
committerW. Felix Handte <w@felixhandte.com>
Mon, 4 May 2020 19:20:26 +0000 (15:20 -0400)
contrib/single_file_libs/combine.sh
contrib/single_file_libs/create_single_file_decoder.sh
contrib/single_file_libs/create_single_file_library.sh
contrib/single_file_libs/zstd-in.c
contrib/single_file_libs/zstddeclib-in.c

index ea99717f87ced92e3a1949f00f0fffcfb358bb5e..a98eef28396579e8dd1e63d43b88d3233de6706f 100755 (executable)
@@ -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
index ad249ccd79981de1d90a1db8bf0ad9139c644a93..b5f5613ae2e37f7c3331be94ad3deb288893d9c5 100755 (executable)
@@ -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"
index 68bbfbd27294104043aa87d8bf69aebcd0af0d21..6cbff459577a02312df2e51545d67eed48ef4b0d 100755 (executable)
@@ -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"
index c3551226411358987c274ade398b249a49a806e3..aa197a682c0899ce8442f62e773b2f3faaf04bb4 100644 (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
  */
 /*
 #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"
index c447bc25ca308ef0c7475600ca8404da121245f0..a9083dd34b52275afd27c4e225100d4c825a63da 100755 (executable)
@@ -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
  */
 /*
 #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"