genresources.bash 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/bin/bash
  2. (set -o igncr) 2>/dev/null && set -o igncr; # this comment is required
  3. set -$-ue${DEBUG+x}
  4. #######################################################################################################################
  5. # This script is designed to process resource files that are necessary for a single component. It converts each
  6. # resource file into a binary object and then stores that into an array in a .c file. The .c file can then be compiled
  7. # with and linked into an application image.
  8. #
  9. # Once all resources files have been converted it will generate a header file that references each of them.
  10. #
  11. # usage:
  12. # genresources.bash <RECIPE_DIR> <SCRIPTS_DIR> <RESOURCE_FILE> <PROJECT_DIR> <TARGET_DIR> <FILESYSTEM|MEM> [s]
  13. #
  14. #######################################################################################################################
  15. RECIPE_DIR=$1 #eg: ./tools
  16. RESOURCE_FILE=$2 #eg: ./generated/temp.cyrsc
  17. PROJECT_DIR=$3 #eg: ./project_mainapp
  18. TARGET_DIR=$4 #eg: ./project_resources
  19. RESOURCE_TYPE=$5 #eg: FILESYSTEM or MEM
  20. echo Script: genresources.bash
  21. echo " 1: Recipe Dir : '"$RECIPE_DIR"'"
  22. echo " 3: Resource Files : '"$RESOURCE_FILE"'"
  23. echo " 4: Project Dir : '"$PROJECT_DIR"'"
  24. echo " 5: Target Dir : '"$TARGET_DIR"'"
  25. echo " 6: Resource Type : '"$RESOURCE_TYPE"'"
  26. #
  27. # File in the target directory
  28. #
  29. RES_FILE="$TARGET_DIR/cy_resources.h"
  30. # array of c source files parsed for declarations to generate resources.h
  31. declare SOURCE_ARRAY=()
  32. #
  33. # Print nice error messages
  34. #
  35. function error() {
  36. echo "ERROR: $1"
  37. shift
  38. while (( $# > 0 )); do
  39. echo " : $1:"
  40. shift
  41. done
  42. echo "—ABORTING--"
  43. exit 1
  44. }
  45. #
  46. # Checks if the value $1 is in the array $element
  47. #
  48. array_contains () {
  49. local seeking=$1; shift
  50. local in=0
  51. for element; do
  52. if [[ $element == $seeking ]]; then
  53. in=1
  54. break
  55. fi
  56. done
  57. echo $in
  58. }
  59. #
  60. # Prepares the resource file for outputing as c-file
  61. #
  62. convert_resource_name() {
  63. local input=$1
  64. local result=${input//\//_DIR_} #replace '/' with '_DIR_'
  65. result=${result//./_} #replace '.' with '_'
  66. result=${result//-/_} #replace '-' with '_'
  67. result=${result//resources_DIR/resources} #replace 'resources_DIR' with 'resources'
  68. echo $result
  69. }
  70. #
  71. # Process the resources listed in the .cyrsc file by converting them to .c and creating
  72. # a list of files for the resource header script
  73. #
  74. processResources() {
  75. local TEXT_FILTERS=(html htm txt eml js css dat cer pem json xml py key)
  76. local BINARY_FILTERS=(jpg jpeg png ico gif bin flac wav clm_blob gz mp3 wmfw)
  77. local TEXT_TO_RES="$RECIPE_DIR/text_to_resource_c.pl"
  78. local BIN_TO_RES="$RECIPE_DIR/bin_to_resource_c.pl"
  79. local resourceList=($(<$1))
  80. # Parse through each element in the .cyrsc file
  81. for ((i = 0; i < ${#resourceList[@]}; i++)); do
  82. # Evaluate the file
  83. local resourceFile="${resourceList[$i]}"
  84. local filename="${resourceFile##*/}"
  85. local extension="${filename##*.}"
  86. # only process the file if it exists
  87. if [ -f "$resourceFile" ]; then
  88. local resourceName=$(convert_resource_name "$resourceFile")
  89. local outputFile="$TARGET_DIR/$(convert_resource_name $filename).c"
  90. SOURCE_ARRAY+=("$TARGET_DIR/$(convert_resource_name $filename).c")
  91. local script
  92. local isText=$(array_contains $extension "${TEXT_FILTERS[@]}")
  93. if [ "1" == "$isText" ]; then
  94. script=$TEXT_TO_RES
  95. fi
  96. local isBinary=$(array_contains $extension "${BINARY_FILTERS[@]}")
  97. if [ "1" == "$isBinary" ]; then
  98. script=$BIN_TO_RES
  99. fi
  100. local outputFileTmp="$TARGET_DIR/$(convert_resource_name $filename).c"
  101. perl "$script" "$RESOURCE_TYPE" "$resourceName" "$resourceFile" > "$outputFileTmp"
  102. else
  103. error "Listed resource $resourceFile does not exist"
  104. fi
  105. done
  106. }
  107. #
  108. # Remove stale files from previous run
  109. #
  110. cleanStale() {
  111. local staleList=($(find $TARGET_DIR -name "*.c"))
  112. local resourceList=($(<$1))
  113. local fileFound=0
  114. for ((j = 0; j < ${#staleList[@]}; j++)); do
  115. for ((i = 0; i < ${#resourceList[@]}; i++)); do
  116. local file="${resourceList[$i]}"
  117. local filename="${file##*/}"
  118. local outputFile="$TARGET_DIR/$(convert_resource_name $filename).c"
  119. if [[ $(basename $outputFile) == $(basename "${staleList[$j]}") ]]; then
  120. fileFound=1
  121. fi
  122. done
  123. if [[ $fileFound == 0 ]]; then
  124. rm -rf "${staleList[$j]}"
  125. fi
  126. fileFound=0
  127. done
  128. }
  129. #
  130. # Call the perl script that creates resources.h
  131. #
  132. generateResourceHeader() {
  133. perl "$RECIPE_DIR/resources_header.pl" ${SOURCE_ARRAY[*]} > "$RES_FILE"
  134. }
  135. #######################################################################################################################
  136. #
  137. # Clean files from previous run that aren't in the current list
  138. #
  139. cleanStale $RESOURCE_FILE
  140. #
  141. # Process all the resources in the cyrsc file
  142. #
  143. processResources $RESOURCE_FILE
  144. #
  145. # Create the resource header
  146. #
  147. generateResourceHeader