build-single-header.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. }
  53. cd $(dirname $0)/../..
  54. INCLUDED=()
  55. process src/ArduinoJson.h true > ../ArduinoJson-$TAG.h
  56. simplify_namespaces ../ArduinoJson-$TAG.h
  57. g++ -x c++ -c -o ../smoketest.o - <<END
  58. #include "../ArduinoJson-$TAG.h"
  59. int main() {
  60. StaticJsonDocument<300> doc;
  61. deserializeJson(doc, "{}");
  62. }
  63. END
  64. INCLUDED=()
  65. process src/ArduinoJson.hpp true > ../ArduinoJson-$TAG.hpp
  66. simplify_namespaces ../ArduinoJson-$TAG.hpp
  67. g++ -x c++ -c -o ../smoketest.o - <<END
  68. #include "../ArduinoJson-$TAG.hpp"
  69. int main() {
  70. ArduinoJson::StaticJsonDocument<300> doc;
  71. ArduinoJson::deserializeJson(doc, "{}");
  72. }
  73. END