build-single-header.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/bash
  2. set -e
  3. TAG=$(git describe)
  4. RE_RELATIVE_INCLUDE='^#include[[:space:]]*"(.*)"'
  5. RE_ABSOLUTE_INCLUDE='^#include[[:space:]]*<(ArduinoJson/.*)>'
  6. RE_SYSTEM_INCLUDE='^#include[[:space:]]*<(.*)>'
  7. RE_EMPTY='^(#pragma[[:space:]]+once)?[[:space:]]*(//.*)?$'
  8. SRC_DIRECTORY="$(realpath "$(dirname $0)/../../src")"
  9. declare -A INCLUDED
  10. process()
  11. {
  12. local PARENT=$1
  13. local FOLDER=$(dirname $1)
  14. local SHOW_COMMENT=$2
  15. while IFS= read -r LINE; do
  16. if [[ $LINE =~ $RE_ABSOLUTE_INCLUDE ]]; then
  17. local CHILD=${BASH_REMATCH[1]}
  18. local CHILD_PATH
  19. CHILD_PATH=$(realpath "$SRC_DIRECTORY/$CHILD")
  20. echo "$PARENT -> $CHILD" >&2
  21. if [[ ! ${INCLUDED[$CHILD_PATH]} ]]; then
  22. INCLUDED[$CHILD_PATH]=true
  23. process "$CHILD" false
  24. fi
  25. elif [[ $LINE =~ $RE_RELATIVE_INCLUDE ]]; then
  26. local CHILD=${BASH_REMATCH[1]}
  27. pushd "$FOLDER" > /dev/null
  28. local CHILD_PATH
  29. CHILD_PATH=$(realpath "$CHILD")
  30. echo "$PARENT -> $CHILD" >&2
  31. if [[ ! ${INCLUDED[$CHILD_PATH]} ]]; then
  32. INCLUDED[$CHILD_PATH]=true
  33. process "$CHILD" false
  34. fi
  35. popd > /dev/null
  36. elif [[ $LINE =~ $RE_SYSTEM_INCLUDE ]]; then
  37. local CHILD=${BASH_REMATCH[1]}
  38. echo "$PARENT -> <$CHILD>" >&2
  39. if [[ ! ${INCLUDED[$CHILD]} ]]; then
  40. echo "#include <$CHILD>"
  41. INCLUDED[$CHILD]=true
  42. fi
  43. elif [[ "${SHOW_COMMENT}" = "true" ]] ; then
  44. echo "$LINE"
  45. elif [[ ! $LINE =~ $RE_EMPTY ]]; then
  46. echo "$LINE"
  47. fi
  48. done < $PARENT
  49. }
  50. simplify_namespaces() {
  51. perl -p0i -e 's|\} // namespace ARDUINOJSON_NAMESPACE\r?\nnamespace ARDUINOJSON_NAMESPACE \{\r?\n||igs' "$1"
  52. rm -f "$1.bak"
  53. }
  54. cd $(dirname $0)/../..
  55. INCLUDED=()
  56. process src/ArduinoJson.h true > ../ArduinoJson-$TAG.h
  57. simplify_namespaces ../ArduinoJson-$TAG.h
  58. g++ -x c++ -c -o ../smoketest.o - <<END
  59. #include "../ArduinoJson-$TAG.h"
  60. int main() {
  61. StaticJsonDocument<300> doc;
  62. deserializeJson(doc, "{}");
  63. }
  64. END
  65. INCLUDED=()
  66. process src/ArduinoJson.hpp true > ../ArduinoJson-$TAG.hpp
  67. simplify_namespaces ../ArduinoJson-$TAG.hpp
  68. g++ -x c++ -c -o ../smoketest.o - <<END
  69. #include "../ArduinoJson-$TAG.hpp"
  70. int main() {
  71. ArduinoJson::StaticJsonDocument<300> doc;
  72. ArduinoJson::deserializeJson(doc, "{}");
  73. }
  74. END