diff --git a/contrib/declib/BUCK b/contrib/declib/BUCK new file mode 100644 index 00000000..e04f5e82 --- /dev/null +++ b/contrib/declib/BUCK @@ -0,0 +1,11 @@ +sh_test( + name='combine', + visibility=['PUBLIC'], + test='combine.sh', + args=[ + '-r', '../../lib', + '-r', '../../lib/common', + '-r', '../../lib/decompress', + '-o', 'zstddeclib.c', + 'zstddeclib-in.c'] +) diff --git a/contrib/declib/README.md b/contrib/declib/README.md index 727b3e8f..bb22c242 100644 --- a/contrib/declib/README.md +++ b/contrib/declib/README.md @@ -3,6 +3,6 @@ Create the file using the shell script: ``` cd zstd/contrib/declib -./combine.sh -r "../../lib ../../lib/common ../../lib/decompress" zstddeclib-in.c > zstddeclib.c +./combine.sh -r ../../lib -r ../../lib/common -r ../../lib/decompress -o zstddeclib.c zstddeclib-in.c ``` Then add the resulting file to your project (see the [test sources](tests) for examples). diff --git a/contrib/declib/combine.sh b/contrib/declib/combine.sh index ba05028b..b95977e3 100755 --- a/contrib/declib/combine.sh +++ b/contrib/declib/combine.sh @@ -1,6 +1,8 @@ #!/bin/bash # Tool to bundle multiple C/C++ source files, inlining any includes. +# +# TODO: ROOTS and FOUND as arrays (since they fail on paths with spaces) # Common file roots ROOTS="./" @@ -8,11 +10,15 @@ ROOTS="./" # Files previously visited FOUND="" +# Optional destination file (empty string to write to stdout) +DESTN="" + # Prints the script usage then exits function usage { - echo "Usage: $0 [-r ] infile" + echo "Usage: $0 [-r ] [-o ] infile" echo " -r file root search paths" - echo "Example: $0 -r \"../my/path ../my/other\" in.c > out.c" + echo " -o output file (otherwise stdout)" + echo "Example: $0 -r ../my/path - r ../other/path -o out.c in.c" exit 1 } @@ -26,6 +32,15 @@ function list_has_item { return 1 } +# Adds a new line with the supplied arguments to $DESTN (or stdout) +function write_line { + if [ -n "$DESTN" ]; then + printf "%s\n" "$@" >> "$DESTN" + else + printf "%s\n" "$@" + fi +} + # Adds the contents of $1 with any of its includes inlined function add_file { # Match the path @@ -35,7 +50,7 @@ function add_file { file="$root/$1" fi done - if [ "$file" != "" ]; then + if [ -n "$file" ]; then # Read the file local line while IFS= read -r line; do @@ -45,26 +60,29 @@ function add_file { if ! `list_has_item "$FOUND" "$inc"`; then # And we've not previously encountered it FOUND="$FOUND $inc" - echo "/**** start inlining $inc ****/" + write_line "/**** start inlining $inc ****/" add_file "$inc" - echo "/**** ended inlining $inc ****/" + write_line "/**** ended inlining $inc ****/" else - echo "/**** skipping file: $inc ****/" + write_line "/**** skipping file: $inc ****/" fi else # Otherwise write the source line - echo "$line" + write_line "$line" fi done < "$file" else - echo "#error Unable to find \"$1\"" + write_line "#error Unable to find \"$1\"" fi } -while getopts ":r:" opts; do +while getopts ":r:o:" opts; do case $opts in r) - ROOTS="$ROOTS $OPTARG" + ROOTS="$OPTARG $ROOTS" + ;; + o) + DESTN="$OPTARG" ;; *) usage @@ -73,8 +91,17 @@ while getopts ":r:" opts; do done shift $((OPTIND-1)) -if [ "$1" != "" ]; then - add_file $1 +if [ -n "$1" ]; then + if [ -f "$1" ]; then + if [ -n "$DESTN" ]; then + printf "" > "$DESTN" + fi + add_file $1 + else + echo "Input file not found: '$1'" + exit 1 + fi else usage fi +exit 0 diff --git a/contrib/declib/tests/BUCK b/contrib/declib/tests/BUCK new file mode 100644 index 00000000..9e3621ae --- /dev/null +++ b/contrib/declib/tests/BUCK @@ -0,0 +1,5 @@ +cxx_test( + name='simple', + srcs=['simple.c'], + deps=['//contrib/declib:combine'] +) diff --git a/contrib/declib/tests/simple.c b/contrib/declib/tests/simple.c index cb200c93..2dda4b06 100644 --- a/contrib/declib/tests/simple.c +++ b/contrib/declib/tests/simple.c @@ -3385,8 +3385,12 @@ size_t ZSTD_decompress(void* dst, size_t dstLen, const void* src, size_t srcLen) * the binary by 74kB. */ int main() { - size_t bytes = ZSTD_decompress(dstDxt1, sizeof dstDxt1, srcZstd, sizeof srcZstd); - printf("Decompressed size: %ld (expected %ld)\n", bytes, sizeof dstDxt1); - printf("Byte comparison: %s\n", (memcmp(rawDxt1, dstDxt1, sizeof dstDxt1)) ? "failed" : "succeeded"); - return 0; + size_t size = ZSTD_decompress(dstDxt1, sizeof dstDxt1, srcZstd, sizeof srcZstd); + int compare = memcmp(rawDxt1, dstDxt1, sizeof dstDxt1); + printf("Decompressed size: %ld (expected %ld)\n", size, sizeof dstDxt1); + printf("Byte comparison: %s\n", (compare) ? "failed" : "succeeded"); + if (size == sizeof dstDxt1 && compare == 0) { + return EXIT_SUCCESS; + } + return EXIT_FAILURE; } diff --git a/contrib/declib/zstddeclib-in.c b/contrib/declib/zstddeclib-in.c index 73320af7..e0c8d51a 100755 --- a/contrib/declib/zstddeclib-in.c +++ b/contrib/declib/zstddeclib-in.c @@ -4,7 +4,7 @@ * * Generate using: * \code - * combine.sh -r "../../lib ../../lib/common ../../lib/decompress" zstddeclib-in.c > zstddeclib.c + * combine.sh -r ../../lib -r ../../lib/common -r ../../lib/decompress -o zstddeclib.c zstddeclib-in.c * \endcode */ /*