Rewrote the scripts to sh instead of bash

This commit is contained in:
Carl Woffenden 2019-08-28 19:20:42 +02:00
parent e9c0fc12d2
commit cdf73e915e
2 changed files with 22 additions and 19 deletions

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/sh
# Where to find the sources # Where to find the sources
ZSTD_SRC_ROOT="../../lib" ZSTD_SRC_ROOT="../../lib"
@ -10,6 +10,7 @@ OUT_FILE="tempbin"
OUT_WASM="temp.wasm" OUT_WASM="temp.wasm"
# Amalgamate the sources # Amalgamate the sources
echo "Amalgamating files... this may take a few minutes"
./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" -r "$ZSTD_SRC_ROOT/common" -r "$ZSTD_SRC_ROOT/decompress" -o zstddeclib.c zstddeclib-in.c
# Did combining work? # Did combining work?
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then

View File

@ -1,7 +1,10 @@
#!/bin/bash #!/bin/sh -e
# Tool to bundle multiple C/C++ source files, inlining any includes. # Tool to bundle multiple C/C++ source files, inlining any includes.
# #
# 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.
#
# TODO: ROOTS and FOUND as arrays (since they fail on paths with spaces) # TODO: ROOTS and FOUND as arrays (since they fail on paths with spaces)
# Common file roots # Common file roots
@ -14,7 +17,7 @@ FOUND=""
DESTN="" DESTN=""
# Prints the script usage then exits # Prints the script usage then exits
function usage { usage() {
echo "Usage: $0 [-r <path>] [-o <outfile>] infile" echo "Usage: $0 [-r <path>] [-o <outfile>] infile"
echo " -r file root search paths" echo " -r file root search paths"
echo " -o output file (otherwise stdout)" echo " -o output file (otherwise stdout)"
@ -22,27 +25,26 @@ function usage {
exit 1 exit 1
} }
# Tests if list $1 has item $2 # Tests if list $1 has item $2 (returning zero on a match)
function list_has_item { list_has_item() {
local list="$1" if echo "$1" | grep -Eq "(^|\s*)$2(\$|\s*)"; then
local item="$2"
if [[ $list =~ (^|[[:space:]]*)"$item"($|[[:space:]]*) ]]; then
return 0 return 0
fi else
return 1 return 1
fi
} }
# Adds a new line with the supplied arguments to $DESTN (or stdout) # Adds a new line with the supplied arguments to $DESTN (or stdout)
function write_line { write_line() {
if [ -n "$DESTN" ]; then if [ -n "$DESTN" ]; then
printf "%s\n" "$@" >> "$DESTN" printf '%s\n' "$@" >> "$DESTN"
else else
printf "%s\n" "$@" printf '%s\n' "$@"
fi fi
} }
# Adds the contents of $1 with any of its includes inlined # Adds the contents of $1 with any of its includes inlined
function add_file { add_file() {
# Match the path # Match the path
local file= local file=
if [ -f "$1" ]; then if [ -f "$1" ]; then
@ -56,12 +58,12 @@ function add_file {
fi fi
if [ -n "$file" ]; then if [ -n "$file" ]; then
# Read the file # Read the file
local line local line=
while IFS= read -r line; do while IFS= read -r line; do
if [[ $line =~ ^[[:space:]]*\#[[:space:]]*include[[:space:]]*\"(.*)\".* ]]; then if echo "$line" | grep -Eq '^\s*#\s*include\s*".+"'; then
# We have an include directive # We have an include directive so strip the (first) file
local inc=${BASH_REMATCH[1]} local inc=$(echo "$line" | grep -Eo '".*"' | grep -Eo '\w*(\.?\w+)+' | head -1)
if ! `list_has_item "$FOUND" "$inc"`; then if ! list_has_item "$FOUND" "$inc"; then
# And we've not previously encountered it # And we've not previously encountered it
FOUND="$FOUND $inc" FOUND="$FOUND $inc"
write_line "/**** start inlining $inc ****/" write_line "/**** start inlining $inc ****/"
@ -102,7 +104,7 @@ if [ -n "$1" ]; then
fi fi
add_file $1 add_file $1
else else
echo "Input file not found: '$1'" echo "Input file not found: \"$1\""
exit 1 exit 1
fi fi
else else