From cd2a8a3812725c3524e5d5fd1ab49b9cb08e0890 Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Fri, 9 Oct 2020 17:08:57 -0700 Subject: [PATCH] cmake updates for building transport adapters (#706) Adding CMake module to enable/disable transport adapters TRANSPORT ADAPTER BUILD Default: If no option is explicitly added, curl will be used for POSIX and WIN HTTP for WIndows Windows: Both CURL and WIN_HTTP can be build to be used. POSIX: Only CURL is acceptable. If WIN_HTTP is set, generate step will fail for user Defines `BUILD_WIN_HTTP_TRANSPORT_ADAPTER` and `BUILD_CURL_HTTP_TRANSPORT_ADAPTER` for source code Fixes #350 --- CMakeLists.txt | 11 +++++-- CMakeSettings.json | 10 +++++-- CONTRIBUTING.md | 2 +- cmake-modules/DefineTransportAdapter.cmake | 29 +++++++++++++++++++ .../templates/jobs/archetype-sdk-client.yml | 10 ++++--- sdk/core/azure-core/CMakeLists.txt | 11 +++++-- .../inc/azure/core/http/curl/curl.hpp | 3 ++ .../core/http/winhttp/win_http_client.hpp | 3 ++ sdk/core/azure-core/test/e2e/CMakeLists.txt | 2 +- 9 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 cmake-modules/DefineTransportAdapter.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 7878bccc9..441ace5bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,11 +6,14 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules") # Compile Options option(WARNINGS_AS_ERRORS "Treat compiler warnings as errors" ON) -option(BUILD_CURL_TRANSPORT "Build internal http transport implementation with CURL for HTTP Pipeline" OFF) +option(BUILD_TRANSPORT_CURL "Build an HTTP transport implementation with CURL" OFF) +option(BUILD_TRANSPORT_WINHTTP "Build an HTTP transport implementation with WIN HTTP" OFF) option(BUILD_TESTING "Build test cases" OFF) option(BUILD_DOCUMENTATION "Create HTML based API documentation (requires Doxygen)" OFF) option(RUN_LONG_UNIT_TESTS "Tests that takes more than 5 minutes to complete. No effect if BUILD_TESTING is OFF" OFF) +include(DefineTransportAdapter) + if(BUILD_TESTING) # define a symbol that enables some test hooks in code add_compile_definitions(TESTING_BUILD) @@ -58,8 +61,10 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules/az_version.cmake) # sub-projects # add_subdirectory(sdk/core/performance-stress) add_subdirectory(sdk/core/azure-core) -if(BUILD_CURL_TRANSPORT) - add_subdirectory(sdk/core/azure-core/test/e2e) # will work only if BUILD_CURL_TRANSPORT=ON + +if(BUILD_TESTING) + add_subdirectory(sdk/core/azure-core/test/e2e) endif() + add_subdirectory(sdk/storage) add_subdirectory(sdk/template/azure-template) diff --git a/CMakeSettings.json b/CMakeSettings.json index b0e226343..38de8a370 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -4,10 +4,12 @@ "name": "x64-DebugWithTests", "generator": "Ninja", "configurationType": "Debug", - "inheritEnvironments": [ "msvc_x64_x64" ], + "inheritEnvironments": [ + "msvc_x64_x64" + ], "buildRoot": "${projectDir}\\out\\build\\${name}", "installRoot": "${projectDir}\\out\\install\\${name}", - "cmakeCommandArgs": "-DINSTALL_GTEST=OFF -DBUILD_TESTING=ON -DBUILD_CURL_TRANSPORT=ON -DBUILD_STORAGE_SAMPLES=ON", + "cmakeCommandArgs": "-DINSTALL_GTEST=OFF -DBUILD_TESTING=ON -DBUILD_TRANSPORT_CURL=ON -DBUILD_STORAGE_SAMPLES=ON", "buildCommandArgs": "-v", "ctestCommandArgs": "", "variables": [ @@ -27,7 +29,9 @@ "cmakeCommandArgs": "", "buildCommandArgs": "", "ctestCommandArgs": "", - "inheritEnvironments": [ "msvc_x86" ], + "inheritEnvironments": [ + "msvc_x86" + ], "variables": [ { "name": "VCPKG_TARGET_TRIPLET", diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 60cbc2100..f57e23504 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -122,7 +122,7 @@ cmake --build . ``` If you want to run tests also, generate build files using below command and then build. ```sh -cmake -DBUILD_TESTING=ON -DBUILD_CURL_TRANSPORT=ON .. +cmake -DBUILD_TESTING=ON .. cmake --build . ``` #### Testing the project diff --git a/cmake-modules/DefineTransportAdapter.cmake b/cmake-modules/DefineTransportAdapter.cmake new file mode 100644 index 000000000..b33f65c20 --- /dev/null +++ b/cmake-modules/DefineTransportAdapter.cmake @@ -0,0 +1,29 @@ +## Copyright (c) Microsoft Corporation. All rights reserved. +## SPDX-License-Identifier: MIT + +############# TRANSPORT ADAPTER BUILD ##################### +# Default: If no option is explicitly added, curl will be used for POSIX and WIN HTTP for Windows # +# Windows: Both CURL and WIN_HTTP can be built to be used. # +# POSIX: Only CURL is acceptable. If WIN_HTTP is set, generate step will fail for user # + +# Defines `BUILD_TRANSPORT_WINHTTP_ADAPTER` and `BUILD_CURL_HTTP_TRANSPORT_ADAPTER` for source code + +if (WIN32 OR MINGW OR MSYS OR CYGWIN) + if(BUILD_TRANSPORT_CURL) + add_compile_definitions(BUILD_CURL_HTTP_TRANSPORT_ADAPTER) + endif() + # Make sure to build WinHTTP either if was user-requested or no transport was selected at all + if(BUILD_TRANSPORT_WINHTTP OR NOT BUILD_TRANSPORT_CURL) + add_compile_definitions(BUILD_TRANSPORT_WINHTTP_ADAPTER) + SET(BUILD_TRANSPORT_WINHTTP ON) + endif() +elseif(UNIX) + if(BUILD_TRANSPORT_WINHTTP) + message(FATAL_ERROR "Win HTTP transport adapter is not supported for Unix platforms") + endif() + # regardless if CURL transport is set or not, it is always added for UNIX as default + add_compile_definitions(BUILD_CURL_HTTP_TRANSPORT_ADAPTER) + SET(BUILD_TRANSPORT_CURL ON) +else() + message(FATAL_ERROR "Unsupported platform") +endif () diff --git a/eng/pipelines/templates/jobs/archetype-sdk-client.yml b/eng/pipelines/templates/jobs/archetype-sdk-client.yml index 0019a43e4..ab429b4d0 100644 --- a/eng/pipelines/templates/jobs/archetype-sdk-client.yml +++ b/eng/pipelines/templates/jobs/archetype-sdk-client.yml @@ -31,12 +31,14 @@ jobs: VCPKG_DEFAULT_TRIPLET: 'x86-windows-static' CMAKE_GENERATOR: 'Visual Studio 16 2019' CMAKE_GENERATOR_PLATFORM: Win32 + CmakeArgs: ' -DBUILD_TRANSPORT_CURL=ON' #ToBeRemoved once we have WinHttp Transport and Storage makes HTTP stack user-config Win_x64: OSVmImage: 'windows-2019' VcpkgInstall: 'curl[winssl] libxml2' VCPKG_DEFAULT_TRIPLET: 'x64-windows-static' CMAKE_GENERATOR: 'Visual Studio 16 2019' CMAKE_GENERATOR_PLATFORM: x64 + CmakeArgs: ' -DBUILD_TRANSPORT_CURL=ON' #ToBeRemoved once we have WinHttp Transport and Storage makes HTTP stack user-config MacOS_x64: OSVmImage: 'macOS-10.14' VcpkgInstall: 'curl[ssl] libxml2 openssl' @@ -54,19 +56,19 @@ jobs: VCPKG_DEFAULT_TRIPLET: 'x86-windows-static' CMAKE_GENERATOR: 'Visual Studio 16 2019' CMAKE_GENERATOR_PLATFORM: Win32 - CmakeArgs: ' -DBUILD_TESTING=ON -DRUN_LONG_UNIT_TESTS=ON' + CmakeArgs: ' -DBUILD_TESTING=ON -DRUN_LONG_UNIT_TESTS=ON -DBUILD_TRANSPORT_CURL=ON' Win_x64_with_unit_test: OSVmImage: 'windows-2019' VcpkgInstall: 'curl[winssl] libxml2' VCPKG_DEFAULT_TRIPLET: 'x64-windows-static' CMAKE_GENERATOR: 'Visual Studio 16 2019' CMAKE_GENERATOR_PLATFORM: x64 - CmakeArgs: ' -DBUILD_TESTING=ON -DRUN_LONG_UNIT_TESTS=ON' + CmakeArgs: ' -DBUILD_TESTING=ON -DRUN_LONG_UNIT_TESTS=ON -DBUILD_TRANSPORT_CURL=ON' MacOS_x64_with_unit_test: OSVmImage: 'macOS-10.14' VcpkgInstall: 'curl[ssl] libxml2 openssl' VCPKG_DEFAULT_TRIPLET: 'x64-osx' - CmakeArgs: ' -DBUILD_TESTING=ON -DRUN_LONG_UNIT_TESTS=ON' + CmakeArgs: ' -DBUILD_TESTING=ON -DRUN_LONG_UNIT_TESTS=ON -DBUILD_TRANSPORT_CURL=ON' pool: vmImage: $(OSVmImage) variables: @@ -130,7 +132,7 @@ jobs: GenerateArgs: >- -DINSTALL_GTEST=OFF -DBUILD_TESTING=OFF - -DBUILD_CURL_TRANSPORT=OFF + -DBUILD_TRANSPORT_CURL=OFF -DBUILD_DOCUMENTATION=YES - pwsh: npm install -g moxygen diff --git a/sdk/core/azure-core/CMakeLists.txt b/sdk/core/azure-core/CMakeLists.txt index cd4803d1b..38a395616 100644 --- a/sdk/core/azure-core/CMakeLists.txt +++ b/sdk/core/azure-core/CMakeLists.txt @@ -14,13 +14,20 @@ if(NOT CURL_FOUND) find_package(CURL ${CURL_MIN_REQUIRED_VERSION} REQUIRED) endif() +if(BUILD_TRANSPORT_CURL) + SET(CURL_TRANSPORT_ADAPTER_SRC src/http/curl/curl.cpp) +endif() +if(BUILD_TRANSPORT_WINHTTP) + SET(WIN_TRANSPORT_ADAPTER_SRC src/http/winhttp/win_http_transport.cpp) +endif() + add_library ( ${TARGET_NAME} src/context.cpp src/credentials/credentials.cpp src/credentials/policy/policies.cpp src/http/body_stream.cpp - src/http/curl/curl.cpp + ${CURL_TRANSPORT_ADAPTER_SRC} src/http/http.cpp src/http/logging_policy.cpp src/http/policy.cpp @@ -30,7 +37,7 @@ add_library ( src/http/transport_policy.cpp src/http/telemetry_policy.cpp src/http/url.cpp - src/http/winhttp/win_http_transport.cpp + ${BUILD_WIN_TRANSPORT} src/logging/logging.cpp src/strings.cpp src/version.cpp) diff --git a/sdk/core/azure-core/inc/azure/core/http/curl/curl.hpp b/sdk/core/azure-core/inc/azure/core/http/curl/curl.hpp index 1384f0d46..0dd60eca7 100644 --- a/sdk/core/azure-core/inc/azure/core/http/curl/curl.hpp +++ b/sdk/core/azure-core/inc/azure/core/http/curl/curl.hpp @@ -6,6 +6,7 @@ */ #pragma once +#ifdef BUILD_CURL_HTTP_TRANSPORT_ADAPTER #include "azure/core/http/http.hpp" #include "azure/core/http/policy.hpp" @@ -606,3 +607,5 @@ namespace Azure { namespace Core { namespace Http { }; }}} // namespace Azure::Core::Http + +#endif diff --git a/sdk/core/azure-core/inc/azure/core/http/winhttp/win_http_client.hpp b/sdk/core/azure-core/inc/azure/core/http/winhttp/win_http_client.hpp index 487edc521..d878aa113 100644 --- a/sdk/core/azure-core/inc/azure/core/http/winhttp/win_http_client.hpp +++ b/sdk/core/azure-core/inc/azure/core/http/winhttp/win_http_client.hpp @@ -6,6 +6,7 @@ */ #pragma once +#ifdef BUILD_TRANSPORT_WINHTTP_ADAPTER #include "azure/core/http/http.hpp" #include "azure/core/http/policy.hpp" @@ -29,3 +30,5 @@ namespace Azure { namespace Core { namespace Http { }; }}} // namespace Azure::Core::Http + +#endif diff --git a/sdk/core/azure-core/test/e2e/CMakeLists.txt b/sdk/core/azure-core/test/e2e/CMakeLists.txt index faeee1ba7..a6ad5d7eb 100644 --- a/sdk/core/azure-core/test/e2e/CMakeLists.txt +++ b/sdk/core/azure-core/test/e2e/CMakeLists.txt @@ -1,7 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # SPDX-License-Identifier: MIT -if (BUILD_CURL_TRANSPORT) +if (BUILD_TRANSPORT_CURL) cmake_minimum_required (VERSION 3.13) set(TARGET_NAME "azure_core_with_curl")