jadeite/gen_resources.sh

75 lines
1.7 KiB
Bash
Raw Normal View History

2023-06-05 21:23:08 +00:00
#!/usr/bin/env bash
# I hate this.
2023-06-05 21:23:08 +00:00
linker="x86_64-w64-mingw32-ld"
# Output config (terrible)
if [ "x$1" = "x--header" ]; then
gen_header=1
shift
fi
if [ "x$1" = "x--object" ]; then
gen_object=1
shift
fi
2023-06-05 21:23:08 +00:00
# Read project directory
proj_dir=`realpath "$1"`
shift
# Read output file destinations
if [ "x${gen_object}" = "x1" ]; then
resources_o=`realpath "$1"`
shift
fi
if [ "x${gen_header}" = "x1" ]; then
resources_h=`realpath "$1"`
shift
fi
2023-06-05 21:23:08 +00:00
# Make sure that output files do not exist
if [ "x${gen_header}" = "x1" ]; then
rm -f "${resources_h}"
fi
if [ "x${gen_object}" = "x1" ]; then
rm -f "${resources_o}"
fi
2023-06-05 21:23:08 +00:00
# Recomupte relative paths to parameters
idx=0
resource_files=()
for path in "$@"
do
resource_files["${idx}"]=`realpath --relative-to="${proj_dir}" "${path}"`
idx="$(("${idx}" + 1))"
done
if [ "x${gen_object}" = "x1" ]; then
# Create the object file
pushd "${proj_dir}" >> /dev/null
$linker -r -b binary -o "${resources_o}" "${resource_files[@]}"
popd >> /dev/null
fi
2023-06-05 21:23:08 +00:00
if [ "x${gen_header}" = "x1" ]; then
# Include stddef.h in the resources header (for size_t)
echo "#include <stddef.h>" >> "${resources_h}"
2023-06-05 21:23:08 +00:00
for resource in "${resource_files[@]}"
do
# Use relative path to the resource as the variable name
var_name="_binary_${resource}"
2023-06-05 21:23:08 +00:00
# Replace all non-alphanumeric characters with underscores
var_name=`printf "${var_name}" | sed "s/[^a-zA-Z0-9]/_/g"`
2023-06-05 21:23:08 +00:00
# Define externs in the header
echo "extern void *${var_name}_start;" >> "${resources_h}"
echo "extern void *${var_name}_size;" >> "${resources_h}"
echo "" >> "${resources_h}"
done
fi