Skip to content

Commit

Permalink
refactor(.): Use template 'uv.def.in' to capture LDFLAGS
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Mar 12, 2019
1 parent 6c2a1c9 commit 6185a59
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ clean:
uv.klib: uv.def lib/libuv.a
cinterop -compilerOpts '$(CFLAGS)' -linkerOpts '-lpthread' -linkerOpts '$(LDFLAGS)' -def uv.def -o uv

uv.def: uv.def.in
./configure

lib/libuv.a: libuv
./configure
$(MAKE) -C libuv
Expand Down
6 changes: 5 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ if [ -z "$SYSROOT" ]; then
fi

./configure --prefix=$PREFIX "$@"
make install

cd ..
rm -f uv.def
export LDFLAGS="$(pkg-config libuv/libuv.pc --libs)"
cat ./uv.def.in | ./scripts/mush.sh > uv.def
146 changes: 146 additions & 0 deletions scripts/mush.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/bin/bash

mush_version () {
echo "0.0.3"
}

mush_usage () {
echo "usage: mush [-ehV] [-f <file>] [-o <file>]"

if [ "$1" = "1" ]; then
echo
echo "examples:"
echo " $ cat file.ms | FOO=BAR mush"
echo " $ VALUE=123 mush -f file.ms -o file"
echo " $ echo \"Today's date is {{DATE}}\" | DATE=\`date +%D\` mush"
echo " $ cat ./template.ms | VAR=VALUE mush"
echo
echo "options:"
echo " -f, --file <file> file to parse"
echo " -o, --out <file> output file"
echo " -e, --escape escapes html html entities"
echo " -h, --help display this message"
echo " -V, --version output version"
fi
}

mush () {
local SELF="$0"
local NULL=/dev/null
local STDIN=0
local STDOUT=1
local STDERR=2
local LEFT_DELIM="{{"
local RIGHT_DELIM="}}"
local INDENT_LEVEL=" "
local ESCAPE=0
local ENV="`env`"
local out=">&$STDOUT"

## parse opts
while true; do
arg="$1"

if [ "" = "$1" ]; then
break;
fi

if [ "${arg:0:1}" != "-" ]; then
shift
continue
fi

case $arg in
-f|--file)
file="$2";
shift 2;
;;
-o|--out)
out="> $2";
shift 2;
;;
-e|--escape)
ESCAPE=1
shift
;;
-h|--help)
mush_usage 1
exit 1
;;
-V|--version)
mush_version
exit 0
;;
*)
{
echo "unknown option \`$arg'"
} >&$STDERR
mush_usage
exit 1
;;
esac
done

## read each line
while IFS= read -r line; do
printf '%q\n' "${line}" | {
## read each ENV variable
echo "$ENV" | {
while read var; do
## split each ENV variable by '='
## and parse the line replacing
## occurrence of the key with
## guarded by the values of
## `LEFT_DELIM' and `RIGHT_DELIM'
## with the value of the variable
case "$var" in
(*"="*)
key=${var%%"="*}
val=${var#*"="*}
;;

(*)
key=$var
val=
;;
esac

line="${line//${LEFT_DELIM}$key${RIGHT_DELIM}/$val}"
done

if [ "1" = "$ESCAPE" ]; then
line="${line//&/&amp;}"
line="${line//\"/&quot;}"
line="${line//\</&lt;}"
line="${line//\>/&gt;}"
fi

## output to stdout
echo "$line" | {
## parse undefined variables
sed -e "s#${LEFT_DELIM}[A-Za-z]*${RIGHT_DELIM}##g" | \
## parse comments
sed -e "s#${LEFT_DELIM}\!.*${RIGHT_DELIM}##g" | \
## escaping
sed -e 's/\\\"/""/g'
};
}
};
done
}


if [[ ${BASH_SOURCE[0]} != $0 ]]; then
export -f mush
else
if [ ! -t 0 ]; then
eval "mush $out"
elif [ ! -z "$file" ]; then
eval "cat $file | mush $out"
else
mush_usage
exit 1
fi
exit $?
fi

1 change: 1 addition & 0 deletions uv.def
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ headers = uv.h
headerFilter = uv.h uv/**
libraryPaths = lib
staticLibraries = libuv.a
linkerOpts = "-L/home/werle/repos/datkt/uv/lib -luv -lrt -lpthread -lnsl -ldl"
8 changes: 8 additions & 0 deletions uv.def.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# vim: set syntax=make:

package = datkt.uv
headers = uv.h
headerFilter = uv.h uv/**
libraryPaths = lib
staticLibraries = libuv.a
linkerOpts = "{{LDFLAGS}}"

0 comments on commit 6185a59

Please sign in to comment.