gen_resources.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env bash
  2. linker="x86_64-w64-mingw32-ld"
  3. # Select output types
  4. for i in {0..1}
  5. do
  6. case "$1" in
  7. --header)
  8. gen_header=1
  9. shift
  10. ;;
  11. --object)
  12. gen_object=1
  13. shift
  14. ;;
  15. esac
  16. done
  17. # Read project directory
  18. proj_dir=`realpath "$1"`
  19. shift
  20. # Read output file destinations and make sure they don't exist
  21. if [ "x${gen_object}" = "x1" ]; then
  22. resources_o=`realpath "$1"`
  23. shift
  24. rm -f "${resources_h}"
  25. fi
  26. if [ "x${gen_header}" = "x1" ]; then
  27. resources_h=`realpath "$1"`
  28. shift
  29. rm -f "${resources_o}"
  30. fi
  31. # Recomupte relative paths to parameters
  32. idx=0
  33. resource_files=()
  34. for path in "$@"
  35. do
  36. resource_files["${idx}"]=`realpath --relative-to="${proj_dir}" "${path}"`
  37. idx="$(("${idx}" + 1))"
  38. done
  39. if [ "x${gen_object}" = "x1" ]; then
  40. # Create the object file
  41. pushd "${proj_dir}" >> /dev/null
  42. $linker -r -b binary -o "${resources_o}" "${resource_files[@]}"
  43. popd >> /dev/null
  44. fi
  45. if [ "x${gen_header}" = "x1" ]; then
  46. # Include stddef.h in the resources header (for size_t)
  47. echo "#include <stddef.h>" >> "${resources_h}"
  48. for resource in "${resource_files[@]}"
  49. do
  50. # Use relative path to the resource as the variable name
  51. var_name="_binary_${resource}"
  52. # Replace all non-alphanumeric characters with underscores
  53. var_name=`printf "${var_name}" | sed "s/[^a-zA-Z0-9]/_/g"`
  54. # Define externs in the header
  55. echo "extern void *${var_name}_start;" >> "${resources_h}"
  56. echo "extern void *${var_name}_size;" >> "${resources_h}"
  57. echo "" >> "${resources_h}"
  58. done
  59. fi