From 076effa650d4cc2130c1b4ff9ed261188ae57aeb Mon Sep 17 00:00:00 2001 From: Max Golovanov Date: Tue, 11 May 2021 13:31:03 -0700 Subject: [PATCH 1/9] Add curl via vcpkg to WIndows builds --- tools/setup-buildtools.cmd | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/setup-buildtools.cmd b/tools/setup-buildtools.cmd index 2c0912ca2e..39270b9c7c 100644 --- a/tools/setup-buildtools.cmd +++ b/tools/setup-buildtools.cmd @@ -54,5 +54,6 @@ vcpkg install abseil:x64-windows vcpkg install protobuf:x64-windows vcpkg install gRPC:x64-windows vcpkg install prometheus-cpp:x64-windows +vcpkg install curl:x64-windows popd exit /b 0 From 840dba238b0a83e8d70d4dd12b3ca44f59c2fdc5 Mon Sep 17 00:00:00 2001 From: Max Golovanov Date: Fri, 14 May 2021 13:02:52 -0700 Subject: [PATCH 2/9] CMake+ninja build process plus clean-up of build warnings associated with that --- CMakeLists.txt | 16 ++- docs/building-with-vs2019.md | 180 ++++++++++++++++++++++++ ext/src/http/client/curl/CMakeLists.txt | 2 +- tools/build.cmd | 164 +++++++++++++++------ tools/setup-buildtools.cmd | 39 +++-- tools/vcvars.cmd | 122 ++++++++++------ 6 files changed, 416 insertions(+), 107 deletions(-) create mode 100644 docs/building-with-vs2019.md diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c414bcbd4..65986fca69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,7 +67,15 @@ if(WITH_STL) # Optimize for speed to reduce the hops if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - set(CMAKE_CXX_FLAGS_SPEED "/O2") + if(CMAKE_BUILD_TYPE MATCHES Debug) + # Turn off optimizations for DEBUG + set(CMAKE_CXX_FLAGS_SPEED "/Od") + else() + STRING(REGEX MATCH "\/O" result ${CMAKE_CXX_FLAGS}) + if(NOT ${result} MATCHES "\/O") + set(CMAKE_CXX_FLAGS_SPEED "/O2") + endif() + endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus ${CMAKE_CXX_FLAGS_SPEED}") endif() @@ -86,6 +94,12 @@ option(WITH_JAEGER "Whether to include the Jaeger exporter" OFF) option(BUILD_TESTING "Whether to enable tests" ON) if(WIN32) + if(BUILD_TESTING) + if(MSVC) + # GTest bug: https://github.com/google/googletest/issues/860 + add_compile_options(/wd4275) + endif() + endif() option(WITH_ETW "Whether to include the ETW Exporter in the SDK" ON) if(WITH_ETW) add_definitions(-DHAVE_MSGPACK) diff --git a/docs/building-with-vs2019.md b/docs/building-with-vs2019.md new file mode 100644 index 0000000000..a752f63743 --- /dev/null +++ b/docs/building-with-vs2019.md @@ -0,0 +1,180 @@ +# Building OpenTelemetry C++ SDK with Visual Studio 2019, CMake and Ninja. + +## Preface + +These instructions are focused on developers and integrators, providing a hassle-free +and FAST option of building OpenTelemetry C++ SDK with Visual Studio on Windows. + +The process is optimized for both scenarios: +- SDK developer experience on developer machine. +- final product CI/CD pipeline. + +## Build System Components + +Visual Studio 2019 is a Full-featured integrated development environment (IDE) for +Android, iOS, Windows, web, and cloud. There are three editions: +- FREE [Community Edition](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community&rel=16) +- [Professional](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Professional&rel=16) +- [Enterprise](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Enterprise&rel=16) + +There is also no-IDE 'headless' set of command line tools available as +`Visual Studio 2019 Build Tools` package. You may install it on Windows +via [Chocolatey package manager](https://community.chocolatey.org/packages/visualstudio2019buildtools). + +You may also use Visual Studio with Windows 10 Subsystem for Linux to cross-compile +the SDK [targeting Linux distributions](https://code.visualstudio.com/docs/cpp/config-wsl). +Linux build process is largely identical to Windows process described below, +with `tools/setup-buildtools.sh` and `tools/build.sh` running on Linux. + +Older versions of Visual Studio, such as 2015 and 2017 are known to work well +with CMake and Ninja. However, these old versions are not covered by instructions +below. If you would like to contribute additional instructions for older versions, +feel free to contribute a Pull Request detailing your build experience with older +versions. + +[CMake](https://cmake.org/) is cross-platform free and open-source software for +build automation, testing, packaging and installation of software by using a +compiler-independent method. CMake is not a build system but rather it generates +another system's build files. + +[MSBuild](https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild?view=vs-2019) +is a default build system used by Visual Studio for loading and building software +projects. + +[Ninja](https://ninja-build.org/) is a small build system with a focus on speed. +It differs from other build systems in two major respects: it is designed to have +its input files generated by a higher-level build system, and it is designed +to run builds as fast as possible. + +[Chocolatey package manager](https://chocolatey.org/) has the largest online registry +of Windows packages. Chocolatey packages encapsulate everything required to manage +a particular piece of software into one deployment artifact by wrapping installers, +executables, zips, and/or scripts into a compiled package file. + +[vcpkg](https://vcpkg.io/en/index.html) is a free C/C++ package manager for acquiring +and managing libraries. Choose from over 1500 open source libraries to download +and build in a single step or add your own private libraries to simplify your +build process. Maintained by the Microsoft C++ team and open source contributors. + +## Installing Prerequisites + +Please install the following software: + +- Install Visual Studio 2019 with C/C++ development tools, including CMake tools. +[This article](https://docs.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=msvc-160) + explains the fundamentals of working with CMake projects in Visual Studio. + +- Install [Git tools for Windows](https://git-scm.com/downloads). + +Setup script below uses Chocolatey to install the following components: +- `vswhere` - utility to auto-discover Visual Studio installation. +- `cmake` +- `git` +- `vcpkg` to download, compile and install 3rd party C++ libraries from source. + +List of C++ dependencies compiled and installed via `vcpkg`: +- [Google Test](https://github.com/google/googletest) +- [Google Benchmark](https://github.com/google/benchmark) +- [Microsoft GSL](https://github.com/microsoft/GSL) +- [nlohmann/json](https://github.com/nlohmann/json) +- [Abseil](https://github.com/abseil/abseil-cpp) +- [Protocol Buffers](https://github.com/protocolbuffers/protobuf) +- [gRPC](https://github.com/grpc/grpc) +- [Prometheus C++ client](https://github.com/jupp0r/prometheus-cpp) +- [cURL](https://github.com/curl/curl) + +## Command Line Build Process + +Start `Command Prompt` as Administrator and execute the following commands: + +```console +git clone --recursive https://github.com/open-telemetry/opentelemetry-cpp +cd opentelemetry-cpp +tools\setup-buildtools.cmd +tools\build.cmd +``` + +Let's dissect the flow: + +```console +git clone --recursive https://github.com/open-telemetry/opentelemetry-cpp +``` + +SDK will be cloned recursively with remote submodule dependencies. + +```console +cd opentelemetry-cpp +tools\setup-buildtools.cmd +``` + +The necessary build tools are installed. This step requires elevation to install +additional tooling, e.g. CMake to `Program Files`. The necessary dependencies are +being built using [vcpkg package manager](https://vcpkg.io/en/index.html). This +one-time step is time-consuming - about up to 5-10 minutes. It has to be done once +once during the initial installation and configuration of build tools and dependencies. + +```console +tools\build.cmd +``` + +The build of all SDK components is done using CMake + ninja in less than couple +minutes. Above script shows you how to build both configurations: +- `nostd` - OpenTelemetry implementation of standard containers. +- `stdlib` - Standard Template Library containers. + +You may execute this workflow in a docker container. Please refer to generic +instructions that detail how to [run build build tools in a docker container](https://docs.microsoft.com/en-us/visualstudio/install/build-tools-container?view=vs-2019). + +## Building in Visual Studio 2019 IDE + +- Run as Administrator: `tools\setup-buildtools.cmd` to install the necessary +build tooling. This builds installs all build tools and builds all 3rd party +dependencies from source using [vcpkg package manager](https://vcpkg.io/en/index.html). +- Launch Visual Studio 2019 IDE. +- Use `Open a local folder` option to open the folder where you cloned the source code. +- Right-click on `CMakeLists.txt` and choose `Generate Cache for opentelemetry-cpp`. +- In the top bar menu - select `Build -> Build All` to build SDK, Exporters and Tests. +- You can use [Google Test Adapter](https://marketplace.visualstudio.com/items?itemName=ChristianSoltenborn.GoogleTestAdapter) +Visual Studio extension to run all SDK and Exporter tests in IDE. +- You can individually select and run only given tests or examples. + +Visual Studio provides an excellent debugging and troubleshooting experience, +with incremental builds using Ninja typically taking just one click to build +and less than a few seconds for the build to be complete. + +## Build time comparison between `MSBuild` and `Ninja` + +After the initial set of 3rd party dependencies have been built via +`tools\setup-buildtools.cmd`, we can benchmark the OpenTelemetry C++ SDK build +times with [MSBuild](https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild?view=vs-2019) +vs with [Ninja](https://ninja-build.org/). [ptime utility](https://community.chocolatey.org/packages/ptime) +may be used to measure the total execution time for two build configurations +built in one run: `nostd-debug` and `stdlib-debug`. + +### MSBuild build timing + +```console +set CMAKE_GEN=Visual Studio 16 2019 +ptime build.cmd +... +``` + +*Execution time: 543.701 s* + +# Ninja build timing + +```console +REM Unset CMAKE_GEN= - default is ninja with autodetection of ninja.exe tool path +set CMAKE_GEN= +ptime build.cmd +... +``` + +*Execution time: 105.158 s* + +It is recommended to built the SDK with *Ninja* since it allows to build the SDK at +least x5 times faster than MSBuild for a full clean build. Incremental builds with +*Ninja* are also considerably faster, taking about 10 seconds total for 2 build +configurations. Absolute time may differ depending on machine being benchmarked. +Relative ratio on most machines would demonstrate that building with *Ninja* build +greatly optimizes your development cycle and is much more energy-friendly. diff --git a/ext/src/http/client/curl/CMakeLists.txt b/ext/src/http/client/curl/CMakeLists.txt index b2265e5d2a..4208de0607 100644 --- a/ext/src/http/client/curl/CMakeLists.txt +++ b/ext/src/http/client/curl/CMakeLists.txt @@ -1,6 +1,6 @@ find_package(CURL) if(CURL_FOUND) - add_library(http_client_curl http_client_factory_curl http_client_curl) + add_library(http_client_curl http_client_factory_curl.cc http_client_curl.cc) set_target_properties(http_client_curl PROPERTIES EXPORT_NAME http_client_curl) diff --git a/tools/build.cmd b/tools/build.cmd index 74ba5db875..5583dfbb35 100644 --- a/tools/build.cmd +++ b/tools/build.cmd @@ -1,57 +1,99 @@ @echo off -REM Currently we require Visual Studio 2019 for C++20 build targeting Release/x64. -REM -REM TODO: allow specifying compiler version as argument. -REM -REM Supported versions for nostd build: -REM - vs2015 (C++11) -REM - vs2017 (C++14) -REM - vs2019 (C++20) -REM -REM Supported versions for STL build: -REM - vs2017 (C++14) -REM - vs2019 (C++20) -REM - -if "%VS_TOOLS_VERSION%" == "" set "VS_TOOLS_VERSION=vs2019" -if "%CMAKE_GEN%" == "" set "CMAKE_GEN=Visual Studio 16 2019" - +REM ########################################################################################## +REM # Build SDK with Visual Studio + CMake + MSBUild or Ninja. # +REM # # +REM # CMake arguments may be passed as parameters to this script. # +REM # If Visual Studio is not installed, then this script falls back to LLVM-CLang, # +REM # Emscripten or any other C++ compiler of your choice. # +REM # # +REM ########################################################################################## +REM # # +REM # Options passed as environment variables: # +REM # # +REM # VS_TOOLS_VERSION - specify visual studio version. See `vcvars.cmd` for details. # +REM # CMAKE_GEN - specify CMake generator. # +REM # VCPKG_ROOT - path to vcpkg root # +REM # ARCH - architecture to build for (default: x64) # +REM # # +REM ########################################################################################## +set "PATH=%PATH%;%ProgramFiles%\CMake\bin" pushd %~dp0 setlocal enableextensions setlocal enabledelayedexpansion -set "ROOT=%~dp0\.." +if not defined VS_TOOLS_VERSION ( + set VS_TOOLS_VERSION=vs2019 +) -if ("%CMAKE_ARCH%"=="") ( - set CMAKE_ARCH=x64 +REM ########################################################################################## +REM Set up CMake generator. Use Ninja if available. +REM ########################################################################################## +if not defined CMAKE_GEN ( + set CMAKE_GEN=Visual Studio 16 2019 + for /f "tokens=*" %%F in ('where ninja') do ( + set NINJA=%%F + ) + if defined VCPKG_ROOT ( + if not defined NINJA ( + for /f "tokens=*" %%F in ('where /R %VCPKG_ROOT%\vcpkg\downloads\tools ninja') do ( + set NINJA=%%F + ) + popd + ) + ) + if not defined NINJA ( + for /f "tokens=*" %%F in ('where /R %CD%\vcpkg\downloads\tools ninja') do ( + set NINJA=%%F + ) + ) + if defined NINJA ( + echo Using ninja at !NINJA! + set CMAKE_GEN=Ninja + ) +) +set "ROOT=%~dp0\.." +if not defined ARCH ( + set ARCH=x64 ) -REM Use preinstalled vcpkg if installed or use our local -if "%VCPKG_ROOT%" neq "" ( +REM ########################################################################################## +REM Use preinstalled vcpkg from %VCPKG_ROOT% if installed or use our local snapshot of it. +REM ########################################################################################## +if defined VCPKG_ROOT ( set "VCPKG_CMAKE=%VCPKG_ROOT%\scripts\buildsystems\vcpkg.cmake" ) else ( set "VCPKG_CMAKE=%CD%\vcpkg\scripts\buildsystems\vcpkg.cmake" ) -REM ******************************************************************** -REM Setup compiler environment -REM ******************************************************************** +REM ########################################################################################## +REM Setup Microsoft Visual C++ compiler environment (if found, if not - fallback to alternate) +REM ########################################################################################## call "%~dp0\vcvars.cmd" -REM ******************************************************************** -REM Use cmake -REM ******************************************************************** -set "PATH=%PATH%;C:\Program Files\CMake\bin\" +REM Prefer Visual Studio C++ compiler if found +for /f "tokens=*" %%F in ('where cl.exe') do ( + set CONFIG=!CONFIG! -DCMAKE_C_COMPILER:FILEPATH="%%F" -DCMAKE_CXX_COMPILER:FILEPATH="%%F" + echo !CONFIG! +) -REM ******************************************************************** -REM Build with nostd implementation -REM ******************************************************************** +REM ########################################################################################## +REM The following two configurations are built below: +REM - nostd - build with OpenTelemetry C++ Template library +REM - stl - build with Standard Template Library +REM ########################################################################################## +REM Build with nostd implementation. Supported VS_TOOLS_VERSION: +REM - vs2015 (C++11) +REM - vs2017 (C++14) +REM - vs2019 (C++20) +REM ########################################################################################## set CONFIG=-DWITH_STL:BOOL=OFF %* set "OUTDIR=%ROOT%\out\%VS_TOOLS_VERSION%\nostd" call :build_config -REM ******************************************************************** -REM Build with STL implementation only for vs2017+ -REM ******************************************************************** +REM ########################################################################################## +REM Build with STL implementation (only for vs2017+). Supported VS_TOOLS_VERSION: +REM - vs2017 (C++14) +REM - vs2019 (C++20) - optimal config with all OpenTelemetry API classes using STL only. +REM ########################################################################################## if "%VS_TOOLS_VERSION%" neq "vs2015" ( set CONFIG=-DWITH_STL:BOOL=ON %* set "OUTDIR=%ROOT%\out\%VS_TOOLS_VERSION%\stl" @@ -59,22 +101,54 @@ if "%VS_TOOLS_VERSION%" neq "vs2015" ( ) popd -REM ******************************************************************** - - -REM ******************************************************************** -REM Function that allows to build given build configuration -REM ******************************************************************** +exit +REM ########################################################################################## +REM Function that allows to build given build configuration with MSBuild or Ninja +REM ########################################################################################## :build_config REM TODO: consider rmdir for clean builds if not exist "%OUTDIR%" mkdir "%OUTDIR%" cd "%OUTDIR%" -if ("%VS_TOOLS_VERSION%"=="vs2019") ( + +if "!VS_TOOLS_VERSION!" == "vs2019" ( + REM Prefer ninja if avilable + if "!CMAKE_GEN!" == "Ninja" ( + call :build_config_ninja + exit /b + ) REM Only latest vs2019 generator supports and requires -A parameter - cmake %ROOT% -G "%CMAKE_GEN%" -A %CMAKE_ARCH% -DCMAKE_TOOLCHAIN_FILE="%VCPKG_CMAKE%" %CONFIG% + cmake -G "!CMAKE_GEN!" -A !ARCH! -DCMAKE_TOOLCHAIN_FILE="!VCPKG_CMAKE!" !CONFIG! "!ROOT!" ) else ( - cmake %ROOT% -G "%CMAKE_GEN%" -DCMAKE_TOOLCHAIN_FILE="%VCPKG_CMAKE%" %CONFIG% + REM Old vs2017 generator does not support -A parameter + cmake -G "!CMAKE_GEN!" -DCMAKE_TOOLCHAIN_FILE="!VCPKG_CMAKE!" !CONFIG! "!ROOT!" ) set "SOLUTION=%OUTDIR%\opentelemetry-cpp.sln" msbuild "%SOLUTION%" /p:Configuration=Release /p:VcpkgEnabled=true -exit /b 0 +exit /b + +REM ########################################################################################## +REM Build using CMake+ninja: vs2019 is known to work well. vs2017 was not tested. +REM ########################################################################################## +REM +REM Optional parameters may be passed to `build.cmd ARG1 ARG2 .. ARGN`. +REM +REM These arguments get appended to CONFIG and passed to CMake. +REM +REM To build for Debug: +REM -DCMAKE_BUILD_TYPE:STRING="Debug" +REM +REM To specify alternate installation path: +REM -DCMAKE_INSTALL_PREFIX:PATH=C:\path\to\install +REM +REM To specify alternate toolchain version: +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" +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" +REM +REM To specify alternate version of Ninja.exe: +REM -DCMAKE_MAKE_PROGRAM="C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\ENTERPRISE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" +REM +REM ########################################################################################## +:build_config_ninja +cmake -G "Ninja" -DCMAKE_MAKE_PROGRAM="!NINJA!" -DCMAKE_TOOLCHAIN_FILE="!VCPKG_CMAKE!" !CONFIG! "!ROOT!" +%NINJA% +exit /b diff --git a/tools/setup-buildtools.cmd b/tools/setup-buildtools.cmd index 39270b9c7c..7a215fc3cf 100644 --- a/tools/setup-buildtools.cmd +++ b/tools/setup-buildtools.cmd @@ -1,5 +1,7 @@ @echo off -set "PATH=%ProgramFiles%\CMake\bin;%~dp0;%~dp0vcpkg;%PATH%" +setlocal enableextensions +setlocal enabledelayedexpansion +set "PATH=%ProgramFiles%\CMake\bin;%~dp0;%~dp0vcpkg;%ProgramData%\chocolatey\bin;%PATH%" if "%VCPKG_ROOT%" NEQ "" set "PATH=%VCPKG_ROOT%;%PATH%" pushd %~dp0 @@ -27,18 +29,25 @@ if %ERRORLEVEL% == 0 ( vswhere -property installationPath ) +REM This script allows to pass architecture in ARCH env var +if not defined ARCH ( + set ARCH=x64 +) + REM Try to autodetect Visual Studio -call "%~dp0vcvars.cmd" x64 +call "%~dp0vcvars.cmd" if "%TOOLS_VS_NOTFOUND%" == "1" ( - REM Cannot detect MSBuild path - REM TODO: no command line tools.. - REM TODO: use MSBuild from vswhere? + echo WARNING: cannot autodetect Visual Studio installation! ) where /Q vcpkg.exe if %ERRORLEVEL% == 1 ( REM Build our own vcpkg from source - pushd .\vcpkg + REM Prefer building in VCPKG_ROOT + if not defined VCPKG_ROOT ( + set "VCPKG_ROOT=%~dp0\vcpkg" + ) + pushd "!VCPKG_ROOT!" call bootstrap-vcpkg.bat popd ) else ( @@ -46,14 +55,14 @@ if %ERRORLEVEL% == 1 ( ) REM Install dependencies -vcpkg install gtest:x64-windows -vcpkg install --head --overlay-ports=%~dp0ports benchmark:x64-windows -vcpkg install ms-gsl:x64-windows -vcpkg install nlohmann-json:x64-windows -vcpkg install abseil:x64-windows -vcpkg install protobuf:x64-windows -vcpkg install gRPC:x64-windows -vcpkg install prometheus-cpp:x64-windows -vcpkg install curl:x64-windows +vcpkg install gtest:%ARCH%-windows +vcpkg install --head --overlay-ports=%~dp0ports benchmark:%ARCH%-windows +vcpkg install ms-gsl:%ARCH%-windows +vcpkg install nlohmann-json:%ARCH%-windows +vcpkg install abseil:%ARCH%-windows +vcpkg install protobuf:%ARCH%-windows +vcpkg install gRPC:%ARCH%-windows +vcpkg install prometheus-cpp:%ARCH%-windows +vcpkg install curl:%ARCH%-windows popd exit /b 0 diff --git a/tools/vcvars.cmd b/tools/vcvars.cmd index fe728367a6..69308778a7 100644 --- a/tools/vcvars.cmd +++ b/tools/vcvars.cmd @@ -1,85 +1,117 @@ @echo off -REM -REM Make sure to enable the 'Visual C++ ATL' components for all platforms during the setup. -REM -REM This build script auto-detects and configures Visual Studio in the following order: -REM 1. Visual Studio 2017 Enterprise -REM 2. Visual Studio 2017 BuildTools -REM 3. Visual Studio 2019 Enterprise -REM 4. Visual Studio 2019 Community -REM 5. Visual Studio 2019 BuildTools -REM +REM +-------------------------------------------------------------------+ +REM | Autodetect and set up the build environment for Visual Studio. | +REM | Visual Studio version may be specified as 1st argument. | +REM +-------------------------------------------------------------------+ +REM | Description | Argument value | +REM +-----------------------------------------+-------------------------+ +REM | Autodetect Visual Studio 2019 | vs2019 | +REM | Visual Studio 2019 Enterprise | vs2019_enterprise | +REM | Visual Studio 2019 Professional | vs2019_professional | +REM | Visual Studio 2019 Community | vs2019_community | +REM | Visual Studio 2019 Build Tools (no IDE) | vs2019_buildtools | +REM | | | +REM | Autodetect Visual Studio 2017 | vs2017 | +REM | Visual Studio 2017 Enterprise | vs2017_enterprise | +REM | Visual Studio 2017 Professional | vs2017_professional | +REM | Visual Studio 2017 Community | vs2017_community | +REM | Visual Studio 2017 Build Tools (no IDE) | vs2017_buildtools | +REM | | | +REM | Visual Studio 2015 Build Tools (no IDE) | vs2015 | +REM +-----------------------------------------+-------------------------+ +set "VSCMD_START_DIR=%CD%" + +if not defined ARCH ( + set ARCH=x64 +) -REM 1st parameter - Visual Studio version if "%1" neq "" ( goto %1 ) -if "%VS_TOOLS_VERSION%" neq "" ( +if defined VS_TOOLS_VERSION ( goto %VS_TOOLS_VERSION% ) -REM vs2017 Enterprise -:vs2017 -:vs2017_enterprise -set TOOLS_VS2017_ENTERPRISE="%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat" -if exist %TOOLS_VS2017_ENTERPRISE% ( - echo Building with vs2017 Enterprise... - call %TOOLS_VS2017_ENTERPRISE% - goto tools_configured -) - -REM vs2017 BuildTools -:vs2017_buildtools -set TOOLS_VS2017="%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\VsDevCmd.bat" -if exist %TOOLS_VS2017% ( - echo Building with vs2017 BuildTools... - call %TOOLS_VS2017% - goto tools_configured -) - -REM vs2019 Enterprise :vs2019 :vs2019_enterprise -set TOOLS_VS2019_ENTERPRISE="%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\VsDevCmd.bat" +set TOOLS_VS2019_ENTERPRISE="%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" if exist %TOOLS_VS2019_ENTERPRISE% ( echo Building with vs2019 Enterprise... - call %TOOLS_VS2019_ENTERPRISE% + call %TOOLS_VS2019_ENTERPRISE% %ARCH% + goto tools_configured +) + +:vs2019_professional +set TOOLS_VS2019_PRO="%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvarsall.bat" +if exist %TOOLS_VS2019_PRO% ( + echo Building with vs2019 Professional... + call %TOOLS_VS2019_PRO% %ARCH% goto tools_configured ) -REM vs2019 Community :vs2019_community -set TOOLS_VS2019_COMMUNITY="%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat" +set TOOLS_VS2019_COMMUNITY="%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" if exist %TOOLS_VS2019_COMMUNITY% ( echo Building with vs2019 Community... - call %TOOLS_VS2019_COMMUNITY% + call %TOOLS_VS2019_COMMUNITY% %ARCH% goto tools_configured ) -REM vs2019 BuildTools :vs2019_buildtools -set TOOLS_VS2019="%ProgramFiles(x86)%\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\VsDevCmd.bat" +set TOOLS_VS2019="%ProgramFiles(x86)%\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" if exist %TOOLS_VS2019% ( echo Building with vs2019 BuildTools... - call %TOOLS_VS2019% + call %TOOLS_VS2019% %ARCH% + goto tools_configured +) + +:vs2017 +:vs2017_enterprise +set TOOLS_VS2017_ENTERPRISE="%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" +if exist %TOOLS_VS2017_ENTERPRISE% ( + echo Building with vs2017 Enterprise... + call %TOOLS_VS2017_ENTERPRISE% %ARCH% + goto tools_configured +) + +:vs2017_professional +set TOOLS_VS2017_PRO="%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvarsall.bat" +if exist %TOOLS_VS2017_PRO% ( + echo Building with vs2017 Professional... + call %TOOLS_VS2017_PRO% %ARCH% + goto tools_configured +) + +:vs2017_community +set TOOLS_VS2017_COMMUNITY="%ProgramFiles(x86)%\Microsoft Visual Studio\2017\VC\Auxiliary\Build\vcvarsall.bat" +if exist %TOOLS_VS2017_COMMUNITY% ( + echo Building with vs2017 Community... + call %TOOLS_VS2017_COMMUNITY% %ARCH% + goto tools_configured +) + +:vs2017_buildtools +set TOOLS_VS2017="%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" +if exist %TOOLS_VS2017% ( + echo Building with vs2017 BuildTools... + call %TOOLS_VS2017% %ARCH% goto tools_configured ) -REM vs2015 :vs2015 -set TOOLS_VS2015="%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\bin\vcvars32.bat" +set TOOLS_VS2015="%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\bin\vcvarsall.bat" if exist %TOOLS_VS2015% ( echo Building with vs2015 BuildTools... - call %TOOLS_VS2015% + call %TOOLS_VS2015% %ARCH% set "VCPKG_VISUAL_STUDIO_PATH=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0" set VCPKG_PLATFORM_TOOLSET=v140 goto tools_configured ) -echo WARNING:********************************************* echo WARNING: cannot auto-detect Visual Studio version !!! -echo WARNING:********************************************* +REM Caller may decide what to do if Visual Studio environment +REM is not set up by checking TOOLS_VS_NOTFOUND set TOOLS_VS_NOTFOUND=1 exit /b 0 From 5521c101d0f7c00534f6ab6610b852e5ce327403 Mon Sep 17 00:00:00 2001 From: Max Golovanov Date: Fri, 14 May 2021 13:14:57 -0700 Subject: [PATCH 3/9] Formatting cleanup --- CMakeLists.txt | 2 +- docs/building-with-vs2019.md | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 53167eeb7f..e584a754d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,7 +69,7 @@ if(WITH_STL) # Turn off optimizations for DEBUG set(CMAKE_CXX_FLAGS_SPEED "/Od") else() - STRING(REGEX MATCH "\/O" result ${CMAKE_CXX_FLAGS}) + string(REGEX MATCH "\/O" result ${CMAKE_CXX_FLAGS}) if(NOT ${result} MATCHES "\/O") set(CMAKE_CXX_FLAGS_SPEED "/O2") endif() diff --git a/docs/building-with-vs2019.md b/docs/building-with-vs2019.md index a752f63743..4bf3129a0b 100644 --- a/docs/building-with-vs2019.md +++ b/docs/building-with-vs2019.md @@ -1,4 +1,4 @@ -# Building OpenTelemetry C++ SDK with Visual Studio 2019, CMake and Ninja. +# Building OpenTelemetry C++ SDK with Visual Studio 2019, CMake and Ninja ## Preface @@ -6,6 +6,7 @@ These instructions are focused on developers and integrators, providing a hassle and FAST option of building OpenTelemetry C++ SDK with Visual Studio on Windows. The process is optimized for both scenarios: + - SDK developer experience on developer machine. - final product CI/CD pipeline. @@ -13,6 +14,7 @@ The process is optimized for both scenarios: Visual Studio 2019 is a Full-featured integrated development environment (IDE) for Android, iOS, Windows, web, and cloud. There are three editions: + - FREE [Community Edition](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community&rel=16) - [Professional](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Professional&rel=16) - [Enterprise](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Enterprise&rel=16) @@ -67,12 +69,14 @@ Please install the following software: - Install [Git tools for Windows](https://git-scm.com/downloads). Setup script below uses Chocolatey to install the following components: + - `vswhere` - utility to auto-discover Visual Studio installation. - `cmake` - `git` - `vcpkg` to download, compile and install 3rd party C++ libraries from source. List of C++ dependencies compiled and installed via `vcpkg`: + - [Google Test](https://github.com/google/googletest) - [Google Benchmark](https://github.com/google/benchmark) - [Microsoft GSL](https://github.com/microsoft/GSL) @@ -119,6 +123,7 @@ tools\build.cmd The build of all SDK components is done using CMake + ninja in less than couple minutes. Above script shows you how to build both configurations: + - `nostd` - OpenTelemetry implementation of standard containers. - `stdlib` - Standard Template Library containers. @@ -157,24 +162,26 @@ built in one run: `nostd-debug` and `stdlib-debug`. set CMAKE_GEN=Visual Studio 16 2019 ptime build.cmd ... +Execution time: 543.701 s ``` -*Execution time: 543.701 s* - -# Ninja build timing +### Ninja build timing ```console REM Unset CMAKE_GEN= - default is ninja with autodetection of ninja.exe tool path set CMAKE_GEN= ptime build.cmd ... +Execution time: 105.158 s ``` -*Execution time: 105.158 s* +## Conclusion -It is recommended to built the SDK with *Ninja* since it allows to build the SDK at -least x5 times faster than MSBuild for a full clean build. Incremental builds with -*Ninja* are also considerably faster, taking about 10 seconds total for 2 build -configurations. Absolute time may differ depending on machine being benchmarked. -Relative ratio on most machines would demonstrate that building with *Ninja* build -greatly optimizes your development cycle and is much more energy-friendly. +It is strongly recommended to built the SDK with *Ninja* since it allows to build +the SDK at least x5 times faster than MSBuild for a full clean build. Incremental +builds with *Ninja* are also considerably faster. Each incremental build is taking +about 10 seconds total for 2 build configurations. Absolute time may differ depending +on machine being benchmarked. Relative ratio on most machines demonstrate that +building with *Ninja* build greatly optimizes your development cycle. Not only it +saves your development time, optimizes your CI/CD cycle, it is also much more +environmentally friendly. From 095e48967e6de65d19a495ed80b1314022146cbe Mon Sep 17 00:00:00 2001 From: Max Golovanov Date: Fri, 14 May 2021 13:27:03 -0700 Subject: [PATCH 4/9] Update building-with-vs2019.md Clean-up a few typos --- docs/building-with-vs2019.md | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/docs/building-with-vs2019.md b/docs/building-with-vs2019.md index 4bf3129a0b..5224e11baa 100644 --- a/docs/building-with-vs2019.md +++ b/docs/building-with-vs2019.md @@ -115,7 +115,7 @@ The necessary build tools are installed. This step requires elevation to install additional tooling, e.g. CMake to `Program Files`. The necessary dependencies are being built using [vcpkg package manager](https://vcpkg.io/en/index.html). This one-time step is time-consuming - about up to 5-10 minutes. It has to be done once -once during the initial installation and configuration of build tools and dependencies. +during the initial installation and configuration of build tools and dependencies. ```console tools\build.cmd @@ -145,16 +145,18 @@ Visual Studio extension to run all SDK and Exporter tests in IDE. Visual Studio provides an excellent debugging and troubleshooting experience, with incremental builds using Ninja typically taking just one click to build -and less than a few seconds for the build to be complete. +and less than a few seconds for the build to complete. ## Build time comparison between `MSBuild` and `Ninja` After the initial set of 3rd party dependencies have been built via `tools\setup-buildtools.cmd`, we can benchmark the OpenTelemetry C++ SDK build times with [MSBuild](https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild?view=vs-2019) -vs with [Ninja](https://ninja-build.org/). [ptime utility](https://community.chocolatey.org/packages/ptime) -may be used to measure the total execution time for two build configurations -built in one run: `nostd-debug` and `stdlib-debug`. +vs with [Ninja](https://ninja-build.org/). + +[ptime utility](https://community.chocolatey.org/packages/ptime) may be used +to measure the total execution time for two build configurations built in one +run: `nostd-debug` and `stdlib-debug`. ### MSBuild build timing @@ -177,11 +179,11 @@ Execution time: 105.158 s ## Conclusion -It is strongly recommended to built the SDK with *Ninja* since it allows to build -the SDK at least x5 times faster than MSBuild for a full clean build. Incremental -builds with *Ninja* are also considerably faster. Each incremental build is taking -about 10 seconds total for 2 build configurations. Absolute time may differ depending -on machine being benchmarked. Relative ratio on most machines demonstrate that -building with *Ninja* build greatly optimizes your development cycle. Not only it -saves your development time, optimizes your CI/CD cycle, it is also much more +It is strongly recommended to build the SDK with *Ninja* since it is at least x5 +times faster than MSBuild for a full clean build. Incremental builds with *Ninja* +are also considerably faster. Each incremental build is taking about 10 seconds +total for 2 build configurations. Absolute time may differ depending on machine +being benchmarked. Relative ratio on most machines demonstrate that building +with *Ninja* build greatly optimizes your development cycle. Not only it saves +your development time, optimizes your CI/CD cycle, it is also much more environmentally friendly. From 26297ea954ea31bd65977e5b9a68e9942e93c204 Mon Sep 17 00:00:00 2001 From: Max Golovanov Date: Fri, 14 May 2021 13:28:46 -0700 Subject: [PATCH 5/9] Update building-with-vs2019.md add "Edition" word --- docs/building-with-vs2019.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/building-with-vs2019.md b/docs/building-with-vs2019.md index 5224e11baa..eb65b6e391 100644 --- a/docs/building-with-vs2019.md +++ b/docs/building-with-vs2019.md @@ -16,8 +16,8 @@ Visual Studio 2019 is a Full-featured integrated development environment (IDE) f Android, iOS, Windows, web, and cloud. There are three editions: - FREE [Community Edition](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community&rel=16) -- [Professional](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Professional&rel=16) -- [Enterprise](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Enterprise&rel=16) +- [Professional Edition](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Professional&rel=16) +- [Enterprise Edition](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Enterprise&rel=16) There is also no-IDE 'headless' set of command line tools available as `Visual Studio 2019 Build Tools` package. You may install it on Windows From c4e3942b059528b38ed6af32062254ec728f63f1 Mon Sep 17 00:00:00 2001 From: Max Golovanov Date: Fri, 14 May 2021 13:30:46 -0700 Subject: [PATCH 6/9] Update building-with-vs2019.md Fix wording --- docs/building-with-vs2019.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/building-with-vs2019.md b/docs/building-with-vs2019.md index eb65b6e391..36dc4c5f4a 100644 --- a/docs/building-with-vs2019.md +++ b/docs/building-with-vs2019.md @@ -133,7 +133,7 @@ instructions that detail how to [run build build tools in a docker container](ht ## Building in Visual Studio 2019 IDE - Run as Administrator: `tools\setup-buildtools.cmd` to install the necessary -build tooling. This builds installs all build tools and builds all 3rd party +build tooling. This script installs all build tools and builds all 3rd party dependencies from source using [vcpkg package manager](https://vcpkg.io/en/index.html). - Launch Visual Studio 2019 IDE. - Use `Open a local folder` option to open the folder where you cloned the source code. From 0730e51e3a76390775f762583e89054777ae1d33 Mon Sep 17 00:00:00 2001 From: Max Golovanov Date: Fri, 14 May 2021 13:32:56 -0700 Subject: [PATCH 7/9] Update building-with-vs2019.md Reword the last sentence --- docs/building-with-vs2019.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/building-with-vs2019.md b/docs/building-with-vs2019.md index 36dc4c5f4a..c2783e60ca 100644 --- a/docs/building-with-vs2019.md +++ b/docs/building-with-vs2019.md @@ -183,7 +183,7 @@ It is strongly recommended to build the SDK with *Ninja* since it is at least x5 times faster than MSBuild for a full clean build. Incremental builds with *Ninja* are also considerably faster. Each incremental build is taking about 10 seconds total for 2 build configurations. Absolute time may differ depending on machine -being benchmarked. Relative ratio on most machines demonstrate that building -with *Ninja* build greatly optimizes your development cycle. Not only it saves -your development time, optimizes your CI/CD cycle, it is also much more +being benchmarked. Relative ratio on most machines demonstrates that building +with *Ninja* build system greatly optimizes your development cycle. Not only it +saves your development time, optimizes your CI/CD cycle, but it is also much more environmentally friendly. From a0ddc0632c37dbdab975544a5fe827909c51b2eb Mon Sep 17 00:00:00 2001 From: Max Golovanov Date: Fri, 14 May 2021 13:41:18 -0700 Subject: [PATCH 8/9] Fix typo --- tools/build.cmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/build.cmd b/tools/build.cmd index 5583dfbb35..14da784661 100644 --- a/tools/build.cmd +++ b/tools/build.cmd @@ -111,7 +111,7 @@ if not exist "%OUTDIR%" mkdir "%OUTDIR%" cd "%OUTDIR%" if "!VS_TOOLS_VERSION!" == "vs2019" ( - REM Prefer ninja if avilable + REM Prefer ninja if available if "!CMAKE_GEN!" == "Ninja" ( call :build_config_ninja exit /b From af46050900198282a3c2f624ec13e3e8d12eb816 Mon Sep 17 00:00:00 2001 From: Max Golovanov Date: Fri, 14 May 2021 14:38:19 -0700 Subject: [PATCH 9/9] Update CMakeLists.txt Change optimization flag name to MSVC_CXX_OPT_FLAG --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e584a754d5..67ee554fe5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,15 +67,15 @@ if(WITH_STL) if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") if(CMAKE_BUILD_TYPE MATCHES Debug) # Turn off optimizations for DEBUG - set(CMAKE_CXX_FLAGS_SPEED "/Od") + set(MSVC_CXX_OPT_FLAG "/Od") else() string(REGEX MATCH "\/O" result ${CMAKE_CXX_FLAGS}) if(NOT ${result} MATCHES "\/O") - set(CMAKE_CXX_FLAGS_SPEED "/O2") + set(MSVC_CXX_OPT_FLAG "/O2") endif() endif() set(CMAKE_CXX_FLAGS - "${CMAKE_CXX_FLAGS} /Zc:__cplusplus ${CMAKE_CXX_FLAGS_SPEED}") + "${CMAKE_CXX_FLAGS} /Zc:__cplusplus ${MSVC_CXX_OPT_FLAG}") endif() endif()