generate_protos.sh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/bash
  2. # Copyright 2022 Google LLC
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # Example usage:
  17. # ./build_protos <path to nanopb>
  18. # Dependencies: git, protobuf, python-protobuf, pyinstaller
  19. readonly DIR="$( git rev-parse --show-toplevel )"
  20. # Current release of nanopb being used to build the CCT protos
  21. readonly NANOPB_VERSION="0.3.9.9"
  22. readonly NANOPB_TEMPDIR="${DIR}/scripts/nanopb/nanopb_temp"
  23. readonly PROTO_DIR="$1"
  24. readonly PROTOGEN_DIR="$2"
  25. readonly INCLUDE_PREFIX="$3"
  26. echoColor() {
  27. COLOR='\033[0;35m'
  28. NC='\033[0m'
  29. printf "${COLOR}$1${NC}\n"
  30. }
  31. echoRed() {
  32. RED='\033[0;31m'
  33. NC='\033[0m'
  34. printf "${RED}$1${NC}\n"
  35. }
  36. cleanup_and_exit() {
  37. rm -rf "${NANOPB_TEMPDIR}"
  38. exit
  39. }
  40. rm -rf "${NANOPB_TEMPDIR}"
  41. echoColor "Downloading nanopb..."
  42. git clone --branch "${NANOPB_VERSION}" https://github.com/nanopb/nanopb.git "${NANOPB_TEMPDIR}"
  43. echoColor "Building nanopb..."
  44. pushd "${NANOPB_TEMPDIR}"
  45. ./tools/make_mac_package.sh
  46. GIT_DESCRIPTION=`git describe --always`-macosx-x86
  47. NANOPB_BIN_DIR="dist/${GIT_DESCRIPTION}"
  48. popd
  49. echoColor "Removing existing protos..."
  50. rm -rf "${PROTOGEN_DIR}/*"
  51. echoColor "Generating protos..."
  52. if ! command -v python &> /dev/null
  53. then
  54. echoRed ""
  55. echoRed "'python' command not found."
  56. echoRed "You may be able to resolve this by installing python with homebrew and linking 'python' to 'python3'. Eg. run:"
  57. echoRed ""
  58. echoRed '$ brew install python && cd $(dirname $(which python3)) && ln -s python3 python'
  59. echoRed ""
  60. cleanup_and_exit
  61. fi
  62. python "${DIR}/scripts/nanopb/proto_generator.py" \
  63. --nanopb \
  64. --protos_dir="${PROTO_DIR}" \
  65. --pythonpath="${NANOPB_TEMPDIR}/${NANOPB_BIN_DIR}/generator" \
  66. --output_dir="${PROTOGEN_DIR}" \
  67. --include="${PROTO_DIR}" \
  68. --include_prefix="${INCLUDE_PREFIX}"
  69. RED='\033[0;31m'
  70. NC='\033[0m'
  71. echoRed ""
  72. echoRed ""
  73. echoRed -e "Important: Any new proto fields of type string, repeated, or bytes must be specified in the proto's .options file with type:FT_POINTER"
  74. echoRed ""
  75. echoRed ""
  76. cleanup_and_exit