install_protobuf.sh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. # Copyright The OpenTelemetry Authors
  3. # SPDX-License-Identifier: Apache-2.0
  4. set -e
  5. [ -z "${PROTOBUF_VERSION}" ] && export PROTOBUF_VERSION="21.12"
  6. #
  7. # Note
  8. #
  9. # protobuf uses two release number schemes,
  10. # for example 3.21.12 and 21.12,
  11. # and both tags corresponds to the same commit:
  12. #
  13. # commit f0dc78d7e6e331b8c6bb2d5283e06aa26883ca7c (HEAD -> release-3.21.12, tag: v3.21.12, tag: v21.12)
  14. # Author: Protobuf Team Bot <[email protected]>
  15. # Date: Mon Dec 12 16:03:12 2022 -0800
  16. #
  17. # Updating version.json and repo version numbers to: 21.12
  18. #
  19. # tag v21.12 corresponds to the 'protoc version', or repo version
  20. # tag v3.21.12 corresponds to the 'cpp version'
  21. #
  22. # protobuf-cpp-3.21.12.tar.gz:
  23. # - is provided under releases/download/v21.12
  24. # - is no longer provided under releases/download/v3.21.12,
  25. #
  26. # Use the "repo version number" (PROTOBUF_VERSION=21.12)
  27. # when calling this script
  28. #
  29. CPP_PROTOBUF_BUILD_OPTIONS=(
  30. "-DCMAKE_POSITION_INDEPENDENT_CODE=ON"
  31. "-Dprotobuf_BUILD_TESTS=OFF"
  32. "-Dprotobuf_BUILD_EXAMPLES=OFF"
  33. )
  34. if [ ! -z "${CXX_STANDARD}" ]; then
  35. CPP_PROTOBUF_BUILD_OPTIONS+=("-DCMAKE_CXX_STANDARD=${CXX_STANDARD}")
  36. CPP_PROTOBUF_BUILD_OPTIONS+=("-DCMAKE_CXX_STANDARD_REQUIRED=ON")
  37. CPP_PROTOBUF_BUILD_OPTIONS+=("-DCMAKE_CXX_EXTENSIONS=OFF")
  38. fi
  39. # After protobuf 22/4.22, protobuf depends on absl and we can use
  40. # "-Dprotobuf_ABSL_PROVIDER=package" to tell protobuf to find absl from the
  41. # system. Otherwise, it will build absl from source.
  42. # 4.XX.YY and 3.XX.YY are alias of XX.YY, and source pacakges are moved into the
  43. # tag of XX.YY and without -cpp suffix from protobuf v22.
  44. if [[ ${PROTOBUF_VERSION/.*/} -ge 22 ]]; then
  45. export CPP_PROTOBUF_VERSION="${PROTOBUF_VERSION}"
  46. CPP_PROTOBUF_PACKAGE_NAME="protobuf-${CPP_PROTOBUF_VERSION}"
  47. CPP_PROTOBUF_BUILD_OPTIONS=(${CPP_PROTOBUF_BUILD_OPTIONS[@]} "-Dprotobuf_ABSL_PROVIDER=package")
  48. else
  49. export CPP_PROTOBUF_VERSION="3.${PROTOBUF_VERSION}"
  50. CPP_PROTOBUF_PACKAGE_NAME="protobuf-cpp-${CPP_PROTOBUF_VERSION}"
  51. fi
  52. cd /tmp
  53. wget https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/${CPP_PROTOBUF_PACKAGE_NAME}.tar.gz
  54. tar zxf ${CPP_PROTOBUF_PACKAGE_NAME}.tar.gz --no-same-owner
  55. echo "Building protobuf ${CPP_PROTOBUF_VERSION}"
  56. echo "CMake build options:" "${CPP_PROTOBUF_BUILD_OPTIONS[@]}"
  57. mkdir protobuf-${CPP_PROTOBUF_VERSION}/build && pushd protobuf-${CPP_PROTOBUF_VERSION}/build
  58. if [ -e "../CMakeLists.txt" ]; then
  59. cmake .. "${CPP_PROTOBUF_BUILD_OPTIONS[@]}"
  60. else
  61. cmake ../cmake "${CPP_PROTOBUF_BUILD_OPTIONS[@]}"
  62. fi
  63. make -j $(nproc)
  64. make install
  65. popd
  66. ldconfig