build-single-header.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/bash
  2. set -e
  3. RE_RELATIVE_INCLUDE='^#[[:space:]]*include[[:space:]]*"(.*)"'
  4. RE_ABSOLUTE_INCLUDE='^#[[:space:]]*include[[:space:]]*<(ArduinoJson/.*)>'
  5. RE_SYSTEM_INCLUDE='^#[[:space:]]*include[[:space:]]*<(.*)>'
  6. RE_EMPTY='^(#[[:space:]]*pragma[[:space:]]+once)?[[:space:]]*(//.*)?$'
  7. SRC_DIRECTORY="$(realpath "$(dirname $0)/../../src")"
  8. declare -A INCLUDED
  9. process()
  10. {
  11. local PARENT=$1
  12. local FOLDER=$(dirname $1)
  13. local SHOW_COMMENT=$2
  14. while IFS= read -r LINE; do
  15. if [[ $LINE =~ $RE_ABSOLUTE_INCLUDE ]]; then
  16. local CHILD=${BASH_REMATCH[1]}
  17. local CHILD_PATH
  18. CHILD_PATH=$(realpath "$SRC_DIRECTORY/$CHILD")
  19. echo "$PARENT -> $CHILD" >&2
  20. if [[ ! ${INCLUDED[$CHILD_PATH]} ]]; then
  21. INCLUDED[$CHILD_PATH]=true
  22. process "$CHILD" false
  23. fi
  24. elif [[ $LINE =~ $RE_RELATIVE_INCLUDE ]]; then
  25. local CHILD=${BASH_REMATCH[1]}
  26. pushd "$FOLDER" > /dev/null
  27. local CHILD_PATH
  28. CHILD_PATH=$(realpath "$CHILD")
  29. echo "$PARENT -> $CHILD" >&2
  30. if [[ ! ${INCLUDED[$CHILD_PATH]} ]]; then
  31. INCLUDED[$CHILD_PATH]=true
  32. process "$CHILD" false
  33. fi
  34. popd > /dev/null
  35. elif [[ $LINE =~ $RE_SYSTEM_INCLUDE ]]; then
  36. local CHILD=${BASH_REMATCH[1]}
  37. echo "$PARENT -> <$CHILD>" >&2
  38. if [[ ! ${INCLUDED[$CHILD]} ]]; then
  39. echo "#include <$CHILD>"
  40. INCLUDED[$CHILD]=true
  41. fi
  42. elif [[ "${SHOW_COMMENT}" = "true" ]] ; then
  43. echo "$LINE"
  44. elif [[ ! $LINE =~ $RE_EMPTY ]]; then
  45. echo "$LINE"
  46. fi
  47. done < $PARENT
  48. }
  49. simplify_namespaces() {
  50. perl -p0i -e 's|\} // namespace ARDUINOJSON_NAMESPACE\r?\nnamespace ARDUINOJSON_NAMESPACE \{\r?\n||igs' "$1"
  51. rm -f "$1.bak"
  52. }
  53. INCLUDED=()
  54. INPUT=$1
  55. OUTPUT=$2
  56. process "$INPUT" true > "$OUTPUT"
  57. simplify_namespaces "$OUTPUT"