build.cmd 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. REM Copyright The OpenTelemetry Authors
  2. REM SPDX-License-Identifier: Apache-2.0
  3. @echo off
  4. REM ##########################################################################################
  5. REM # Build SDK with (msvc or clang) + CMake + (MSBuild or Ninja). #
  6. REM # #
  7. REM # CMake arguments may be passed as parameters to this script. #
  8. REM # If Visual Studio is not installed, then this script falls back to LLVM-CLang, #
  9. REM # Emscripten or any other C++ compiler of your choice. #
  10. REM # #
  11. REM ##########################################################################################
  12. REM # #
  13. REM # Options passed as environment variables: #
  14. REM # #
  15. REM # BUILDTOOLS_VERSION - specify build tools version. See `vcvars.cmd` for details. #
  16. REM # CMAKE_GEN - specify CMake generator. #
  17. REM # VCPKG_ROOT - path to vcpkg root #
  18. REM # ARCH - architecture to build for (default: x64) #
  19. REM # #
  20. REM ##########################################################################################
  21. set "PATH=%PATH%;%ProgramFiles%\CMake\bin"
  22. pushd %~dp0
  23. setlocal enableextensions
  24. setlocal enabledelayedexpansion
  25. if not defined BUILDTOOLS_VERSION (
  26. set BUILDTOOLS_VERSION=vs2019
  27. )
  28. REM ##########################################################################################
  29. REM Set up CMake generator. Use Ninja if available.
  30. REM ##########################################################################################
  31. for /f "tokens=*" %%F in ('where ninja') do (
  32. set NINJA=%%F
  33. )
  34. if defined VCPKG_ROOT (
  35. if not defined NINJA (
  36. for /f "tokens=*" %%F in ('where /R %VCPKG_ROOT%\vcpkg\downloads\tools ninja') do (
  37. set NINJA=%%F
  38. )
  39. popd
  40. )
  41. )
  42. if not defined NINJA (
  43. for /f "tokens=*" %%F in ('where /R %CD%\vcpkg\downloads\tools ninja') do (
  44. set NINJA=%%F
  45. )
  46. )
  47. if defined NINJA (
  48. echo Found ninja: !NINJA!
  49. if not defined CMAKE_GEN (
  50. set CMAKE_GEN=Ninja
  51. )
  52. )
  53. if not defined CMAKE_GEN (
  54. set CMAKE_GEN=Visual Studio 16 2019
  55. )
  56. set "ROOT=%~dp0\.."
  57. if not defined ARCH (
  58. set ARCH=x64
  59. )
  60. REM ##########################################################################################
  61. REM Use preinstalled vcpkg from %VCPKG_ROOT% if installed or use our local snapshot of it.
  62. REM ##########################################################################################
  63. if defined VCPKG_ROOT (
  64. set "VCPKG_CMAKE=%VCPKG_ROOT%\scripts\buildsystems\vcpkg.cmake"
  65. ) else (
  66. set "VCPKG_CMAKE=%CD%\vcpkg\scripts\buildsystems\vcpkg.cmake"
  67. )
  68. REM ##########################################################################################
  69. REM Setup Microsoft Visual C++ compiler environment (if found, if not - fallback to alternate)
  70. REM ##########################################################################################
  71. call "%~dp0\vcvars.cmd"
  72. REM Prefer Visual Studio C++ compiler if found
  73. for /f "tokens=*" %%F in ('where cl.exe') do (
  74. set CONFIG=!CONFIG! -DCMAKE_C_COMPILER:FILEPATH="%%F" -DCMAKE_CXX_COMPILER:FILEPATH="%%F"
  75. echo !CONFIG!
  76. )
  77. REM ##########################################################################################
  78. REM The following two configurations are built below:
  79. REM - nostd - build with OpenTelemetry C++ Template library
  80. REM - stl - build with Standard Template Library
  81. REM ##########################################################################################
  82. REM Build with nostd implementation.
  83. REM ##########################################################################################
  84. set CONFIG=-DWITH_STL:BOOL=OFF %*
  85. set "OUTDIR=%ROOT%\out\%BUILDTOOLS_VERSION%\nostd"
  86. call :build_config
  87. REM ##########################################################################################
  88. REM Build with STL implementation. This option does not yield benefits for vs2015 build.
  89. REM ##########################################################################################
  90. if "%BUILDTOOLS_VERSION%" neq "vs2015" (
  91. set CONFIG=-DWITH_STL:BOOL=ON %*
  92. set "OUTDIR=%ROOT%\out\%BUILDTOOLS_VERSION%\stl"
  93. call :build_config
  94. )
  95. popd
  96. exit
  97. REM ##########################################################################################
  98. REM Function that allows to build given build configuration with MSBuild or Ninja
  99. REM ##########################################################################################
  100. :build_config
  101. REM TODO: consider rmdir for clean builds
  102. if not exist "%OUTDIR%" mkdir "%OUTDIR%"
  103. cd "%OUTDIR%"
  104. REM Prefer ninja if available
  105. if "!CMAKE_GEN!" == "Ninja" (
  106. call :build_config_ninja
  107. exit /b
  108. )
  109. if "!BUILDTOOLS_VERSION!" == "vs2015" (
  110. cmake -G "!CMAKE_GEN!" -A !ARCH! -DCMAKE_TOOLCHAIN_FILE="!VCPKG_CMAKE!" !CONFIG! "!ROOT!"
  111. call :build_msbuild
  112. exit /b
  113. )
  114. if "!BUILDTOOLS_VERSION!" == "vs2017" (
  115. cmake -G "!CMAKE_GEN!" -A !ARCH! -DCMAKE_TOOLCHAIN_FILE="!VCPKG_CMAKE!" !CONFIG! "!ROOT!"
  116. call :build_msbuild
  117. exit /b
  118. )
  119. if "!BUILDTOOLS_VERSION!" == "vs2019" (
  120. cmake -G "!CMAKE_GEN!" -A !ARCH! -DCMAKE_TOOLCHAIN_FILE="!VCPKG_CMAKE!" !CONFIG! "!ROOT!"
  121. call :build_msbuild
  122. exit /b
  123. )
  124. REM ##########################################################################################
  125. REM Exotic CMake generators, like MSYS and MinGW MAY work, but untested
  126. REM ##########################################################################################
  127. cmake -G "!CMAKE_GEN!" -DCMAKE_TOOLCHAIN_FILE="!VCPKG_CMAKE!" !CONFIG! "!ROOT!"
  128. :build_msbuild
  129. set "SOLUTION=%OUTDIR%\opentelemetry-cpp.sln"
  130. msbuild "%SOLUTION%" /p:Configuration=Release /p:VcpkgEnabled=true
  131. exit /b
  132. REM ##########################################################################################
  133. REM Build using CMake+ninja: vs2019 is known to work well. vs2017 was not tested.
  134. REM ##########################################################################################
  135. REM
  136. REM Optional parameters may be passed to `build.cmd ARG1 ARG2 .. ARGN`.
  137. REM
  138. REM These arguments get appended to CONFIG and passed to CMake.
  139. REM
  140. REM To build for Debug:
  141. REM -DCMAKE_BUILD_TYPE:STRING="Debug"
  142. REM
  143. REM To specify alternate installation path:
  144. REM -DCMAKE_INSTALL_PREFIX:PATH=C:\path\to\install
  145. REM
  146. REM To specify alternate toolchain version:
  147. REM -DCMAKE_C_COMPILER:FILEPATH="C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe"
  148. REM -DCMAKE_CXX_COMPILER:FILEPATH="C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe"
  149. REM
  150. REM To specify alternate version of Ninja.exe:
  151. REM -DCMAKE_MAKE_PROGRAM="C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\ENTERPRISE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe"
  152. REM
  153. REM ##########################################################################################
  154. :build_config_ninja
  155. cmake -G "Ninja" -DCMAKE_MAKE_PROGRAM="!NINJA!" -DCMAKE_TOOLCHAIN_FILE="!VCPKG_CMAKE!" !CONFIG! "!ROOT!"
  156. "%NINJA%"
  157. exit /b