build.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/bin/bash
  2. # Copyright The OpenTelemetry Authors
  3. # SPDX-License-Identifier: Apache-2.0
  4. set -e
  5. export PATH=/usr/local/bin:$PATH
  6. ##
  7. ## Install all build tools and dependencies on Mac
  8. ##
  9. function install_mac_tools {
  10. if [ ! -f $BUILDTOOLS_FILE ] ; then
  11. $WORKSPACE_ROOT/tools/setup-buildtools-mac.sh
  12. echo > $BUILDTOOLS_FILE
  13. else
  14. echo "Build tools already installed. Skipping build tools installation."
  15. fi
  16. }
  17. ##
  18. ## Install all build tools and dependencies on Linux
  19. ##
  20. function install_linux_tools {
  21. if [ ! -f $BUILDTOOLS_FILE ] ; then
  22. sudo $WORKSPACE_ROOT/tools/setup-buildtools.sh
  23. echo > $BUILDTOOLS_FILE
  24. else
  25. echo "Build tools already installed. Skipping build tools installation."
  26. fi
  27. }
  28. ##
  29. ## Build dependencies
  30. ##
  31. function build_dependencies {
  32. # Build Google Benchmark
  33. $WORKSPACE_ROOT/tools/build-benchmark.sh
  34. # Build Google Test
  35. $WORKSPACE_ROOT/tools/build-gtest.sh
  36. }
  37. ##
  38. ## Build specific configuration for a given platform
  39. ##
  40. function build {
  41. echo "Build configuration: $BUILD_CONFIG"
  42. cd $WORKSPACE_ROOT
  43. export BUILD_ROOT=`pwd`/out/$PLATFORM_NAME/$BUILD_CONFIG
  44. mkdir -p $BUILD_ROOT
  45. if [ ! -w $BUILD_ROOT ] ; then
  46. echo "Unable to create output directory: $BUILD_ROOT"
  47. exit 1
  48. fi
  49. if [ -z ${USE_VCPKG} ] ; then
  50. # TODO: consider that dependencies may also be coming from OS or brew
  51. build_dependencies
  52. else
  53. echo VCPKG_ROOT=${VCPKG_ROOT}
  54. # Prefer ninja from VCPKG if available
  55. NINJA=$WORKSPACE_ROOT/`find tools/vcpkg -name ninja -type f -print -quit`
  56. if [ -z ${NINJA} ] ; then
  57. NINJA=`which ninja`
  58. fi
  59. fi
  60. # Build OpenTelemetry SDK
  61. pushd $BUILD_ROOT
  62. if [ -z ${NINJA} ] ; then
  63. cmake $BUILD_OPTIONS $WORKSPACE_ROOT
  64. make
  65. else
  66. cmake -G "Ninja" $BUILD_OPTIONS $WORKSPACE_ROOT
  67. echo Building with NINJA=$NINJA
  68. $NINJA
  69. fi
  70. popd
  71. }
  72. function runtests {
  73. pushd $BUILD_ROOT
  74. ctest
  75. popd
  76. }
  77. ##
  78. ## Clean
  79. ##
  80. function clean {
  81. rm -f CMakeCache.txt *.cmake
  82. rm -rf out
  83. rm -rf .buildtools
  84. # make clean
  85. }
  86. ##
  87. ## Detect compiler
  88. ##
  89. function detect_compiler {
  90. if [ -z "${CC}" ] ; then
  91. # Compiler autodetection
  92. if [ -z "${APPLE}" ] ; then
  93. # Prefer gcc for non-Apple
  94. if [ -f /usr/bin/gcc ] ; then
  95. echo "gcc version: `gcc --version`"
  96. PLATFORM_NAME=`gcc -dumpmachine`-gcc-`gcc -dumpversion`
  97. fi
  98. else
  99. # Prefer clang on Appple platforms
  100. if [ -f /usr/bin/clang ] ; then
  101. echo "clang version: `clang --version`"
  102. PLATFORM_NAME=`clang -dumpmachine`-clang-`clang -dumpversion`
  103. fi
  104. fi
  105. else
  106. # Use compiler specified by ${CC} environment variable
  107. IFS=- read $PLATFORM_NAME $COMPILER_VERSION <<< "${CC}"
  108. echo "CC version: `${CC} --version`"
  109. PLATFORM_NAME=$PLATFORM_NAME-`${CC} -dumpversion`
  110. fi
  111. if [ -z "${PLATFORM_NAME}" ] ; then
  112. # Default configuration name for unknown compiler
  113. # could be overridden by setting env var explicitly
  114. PLATFORM_NAME=unknown-0
  115. fi
  116. }
  117. ##
  118. ## Detect Host OS, install tools and detect compiler
  119. ##
  120. function install_tools {
  121. # Marker file to signal that the tools have been already installed (save build time for incremental builds)
  122. BUILDTOOLS_FILE=`pwd`/.buildtools
  123. # Host OS detection
  124. OS_NAME=`uname -a`
  125. case "$OS_NAME" in
  126. *Darwin*)
  127. export APPLE=1
  128. # Set target MacOS minver
  129. export MACOSX_DEPLOYMENT_TARGET=10.10
  130. install_mac_tools ;;
  131. *Linux*)
  132. export LINUX=1
  133. [[ -z "$NOROOT" ]] && install_linux_tools || echo "No root. Skipping build tools installation." ;;
  134. *)
  135. echo "WARNING: unsupported OS $OS_NAME. Skipping build tools installation." ;;
  136. esac
  137. detect_compiler
  138. }
  139. ##
  140. ## Parse arguments
  141. ##
  142. function parse_args {
  143. # Build debug build by default
  144. if [ "$1" == "release" ] ; then
  145. BUILD_TYPE="release"
  146. else
  147. BUILD_TYPE="debug"
  148. fi
  149. }
  150. ################################################################################################################
  151. ## Switch to workspace root directory first
  152. DIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
  153. WORKSPACE_ROOT=$DIR/..
  154. cd $WORKSPACE_ROOT
  155. echo "Current directory is `pwd`"
  156. # Parse command line arguments
  157. parse_args
  158. # Install the necessary build tools if needed
  159. [[ -z "$NOROOT" ]] && install_tools || echo "No root: skipping build tools installation."
  160. # Build given configuration. Default configuration is ABI-stable 'nostd::' classes.
  161. # Please refer to CMakeLists.txt for the list of supported build configurations.
  162. BUILD_CONFIG=${1-nostd}
  163. shift
  164. BUILD_OPTIONS="$@"
  165. build
  166. runtests