Skip to content

Commit

Permalink
feat(extract-libjade.sh): Refactor extract.sh code to use main functi…
Browse files Browse the repository at this point in the history
…on and set -e
  • Loading branch information
koraa committed Aug 7, 2024
1 parent 65e1f31 commit 459ea51
Showing 1 changed file with 69 additions and 65 deletions.
134 changes: 69 additions & 65 deletions extract-libjade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,96 @@
# ./extract-libjade.sh --list-implementations | while read implementation; do ./extract-libjade.sh --gen-implementation $implementation ../libjade/src/$implementation; done
#

call=$0
top_dir=$(cd "$(dirname "$0")" ; pwd -P) || exit 1

print_usage()
{
print_usage() {
echo "usage:"
echo " $ $call --list-implementations"
echo " $ $call --gen-implementation IMPLEMENTATION DIRECTORY"
}

# if there are no arguments, print usage
if [ $# -eq 0 ]; then
print_usage
exit 1
fi
main() {
call=$0
top_dir=$(cd "$(dirname "$0")" ; pwd -P) || exit 1

# check if --list-implementations
if [ "$1" == "--list-implementations" ]; then
make --no-print-directory -C "$top_dir"/src print-available-implementations
exit 1
fi
# if there are no arguments, print usage
if [ $# -eq 0 ]; then
print_usage
exit 1
fi

# check if --gen-implementation
if [ "$1" == "--gen-implementation" ]; then
# if we are generating an implementation then there should be 3 args
if [ $# -eq 3 ]; then
# check if --list-implementations
if [ "$1" == "--list-implementations" ]; then
make --no-print-directory -C "$top_dir"/src print-available-implementations
exit 1
fi

# start by realpath them (useful to run make)
implementation="$top_dir"/src/$2
directory=$3
# check if --gen-implementation
if [ "$1" == "--gen-implementation" ]; then
# if we are generating an implementation then there should be 3 args
if [ $# -eq 3 ]; then

# test if IMPLEMENTATION directory exists
if [ ! -d "$implementation" ]; then
echo " IMPLEMENTATION: $implementation does not exist."
exit 1;
fi
# start by realpath them (useful to run make)
implementation="$top_dir"/src/$2
directory=$3

# test if libjade DIRECTORY exists
if [ ! -d "$directory" ]; then
echo " DIRECTORY: $directory does not exist."
exit 1;
fi
# test if IMPLEMENTATION directory exists
if [ ! -d "$implementation" ]; then
echo " IMPLEMENTATION: $implementation does not exist."
exit 1;
fi

relative_implementation=$(realpath --relative-to="$top_dir/src" "$implementation")
make --no-print-directory -C "$top_dir"/src/ "$relative_implementation"/preprocess-inplace
# test if libjade DIRECTORY exists
if [ ! -d "$directory" ]; then
echo " DIRECTORY: $directory does not exist."
exit 1;
fi

#############################################################################
# copy the preprocessed files
relative_implementation=$(realpath --relative-to="$top_dir/src" "$implementation")
make --no-print-directory -C "$top_dir"/src/ "$relative_implementation"/preprocess-inplace

jazz_files=$(find "$implementation" -name '*.jazz')
for file in $jazz_files; do
cp "$file" "$directory"/
done
#############################################################################
# copy the preprocessed files

# setup the Makefile
echo -n "SRCS := " > "$directory"/Makefile
for file in $jazz_files; do
echo -n "$(basename "$file")" >> "$directory"/Makefile
done
echo "" >> "$directory"/Makefile
jazz_files=$(find "$implementation" -name '*.jazz')
for file in $jazz_files; do
cp "$file" "$directory"/
done

# NOTE: the following line will need change (or be deleted) once multi-repo libjade is stable
echo "include ../../../../Makefile.common" >> "$directory"/Makefile
# setup the Makefile
echo -n "SRCS := " > "$directory"/Makefile
for file in $jazz_files; do
echo -n "$(basename "$file")" >> "$directory"/Makefile
done
echo "" >> "$directory"/Makefile

# NOTE: the following command will change once there is a PR in libjade to move api.h files out of include/ directories
cp "$implementation"/include/api.h "$directory"/include/api.h
# NOTE: the following line will need change (or be deleted) once multi-repo libjade is stable
echo "include ../../../../Makefile.common" >> "$directory"/Makefile

#
#############################################################################
# NOTE: the following command will change once there is a PR in libjade to move api.h files out of include/ directories
cp "$implementation"/include/api.h "$directory"/include/api.h

# restore implementation state
make --no-print-directory -C "$top_dir"/src/ "$relative_implementation"/revert-preprocess-inplace
#
#############################################################################

exit 1;
# restore implementation state
make --no-print-directory -C "$top_dir"/src/ "$relative_implementation"/revert-preprocess-inplace

else
echo "error: --gen-implementation : number of required arguments 3 : provided $#"
print_usage
exit 1
fi
fi

# with 'good' options this should be unreachable, hence, print usage
print_usage
exit 1
exit 1;

else
echo "error: --gen-implementation : number of required arguments 3 : provided $#"
print_usage
exit 1
fi
fi

# with 'good' options this should be unreachable, hence, print usage
print_usage
exit 1
}

init() {
set -e
main "$@"
}

init "$@"

0 comments on commit 459ea51

Please sign in to comment.