azure-sdk-for-cpp/CMakeLists.txt

108 строки
4.0 KiB
CMake
Исходник Обычный вид История

2020-02-20 23:10:01 +03:00
# Copyright (c) Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: MIT
cmake_minimum_required (VERSION 3.13)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules")
2020-02-20 23:10:01 +03:00
2020-12-11 22:53:37 +03:00
# Variable to indicate whether the entire SDK is being built, as opposed to an individual library.
set(AZ_ALL_LIBRARIES ON)
# Compile Options
include(FolderList)
SetGlobalOptions()
if(FETCH_SOURCE_DEPS)
message(FATAL_ERROR, "FETCH_SOURCE_DEPS flag not for global use, instead use when building individual components")
return()
endif()
2020-02-20 23:10:01 +03:00
include(AzureTransportAdapters)
include(AzureVcpkg)
2022-01-20 21:09:06 +03:00
include(AzureBuildTargetForCI)
az_vcpkg_integrate()
2020-02-20 23:10:01 +03:00
# Project definition
project(azure-sdk LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
2020-02-20 23:10:01 +03:00
set(CMAKE_CXX_STANDARD_REQUIRED True)
if(MSVC_USE_STATIC_CRT AND MSVC)
# 1. More about static/shared CRT:
# https://docs.microsoft.com/cpp/build/reference/md-mt-ld-use-run-time-library?view=msvc-160
#
# 2. MSVC_USE_STATIC_CRT build flag approach is used/inspired by libcurl
# (https://github.com/curl/curl/blob/master/CMakeLists.txt) and some other projects.
#
# 3. GTest would emit the following warning:
# warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library
# AddGoogleTest.cmake uses gtest_force_shared_crt
# (see https://github.com/google/googletest/blob/master/googletest/README.md),
# which respects linker settings that we set below, and our settings below are all in sync.
#
# 4. Sometimes, the following approach is recommended instead:
# +-----------------------------------------------------------------------------------+
# | # Use the static runtime libraries when building statically |
# | # for consistency with the vcpkg arch-windows-static triplets: |
# | cmake_policy(SET CMP0091 NEW) |
# | # see https://cmake.org/cmake/help/v3.15/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html |
# | if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY) |
# | set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") |
# | endif() |
# +-----------------------------------------------------------------------------------+
# However, it only works when cmake installed is 3.15+;
# we have to require a minimum of 3.13.
#
# 5. We "replace with empty string" (i.e. remove) first, then add, so that '/MT'
# will be present (and present once) even if '/MD' was not.
string(REGEX REPLACE "/MD" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MT")
string(REGEX REPLACE "/MD" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
string(REGEX REPLACE "/MD" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
string(REGEX REPLACE "/MD" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT")
string(REGEX REPLACE "/MDd" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
endif()
if(BUILD_TESTING)
include(AddGoogleTest)
enable_testing ()
endif()
# compiler warning flags globally
include(AzureGlobalCompileOptions)
# Add create_map_file function
include(CreateMapFile)
# Documentation automation function
include(AzureDoxygen)
# Functions for library versions
include(AzureVersion)
Encapsulate getenv(), and make it work on UWP (#3275) It all started with UWP. The [docs](https://docs.microsoft.com/en-us/cpp/cppcx/crt-functions-not-supported-in-universal-windows-platform-apps?view=msvc-170) say: "`Environment variables are not available to UWP apps.`". And it truly won't work, I tried: linker error, the function is simply not present. So, for a year or so, we were `ifdef`ing everything enivoronment-related: console logger, environment credential, managed identity credential. And then just recently we wanted to enable our CI for UWP, including tests and samples. And it required to do more ifdefs (in vcpkg, we don't build samples or tests, so that problem did not exist). It just became more messy. Especially in samples - you can see how we would disable warning with `#pragma warning(disable : 4996)` or defining `_CRT_SECURE_NO_WARNINGS` already, but now came UWP, so we would have to add comment that `getenv()` is not available and make the sample compilation to either fail with clear message, or throw an exception. Plus we would have to detect that we are being compiled on UWP, which also adds visual clutter for reader. You can see how such an irrelevant (for a sample) thing as `getenv` was consuming more and more lines of sample code and reader's attention. But then! I read docs on more APIs for UWP. And I noticed that on .NET you can read environment variables. So I went and checked Win32 API docs for [GetEnvironmentVariable()](https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getenvironmentvariable) - it says: "`Minimum supported client: ... UWP apps`". **GetEnvironmentVariable() works on UWP!** And so does `SetEnvironmentVariable()` (our tests use it, which means we can make all of them work and execute for UWP). That's good news, but now it would probably be more code: it usually takes more lines to invoke WinAPI, it is no more an one-liner to call `getenv()/setenv()`. So, I encapsulated that into `Azure::Core::_internal::Environment::GetVariable()` and `SetVariable()`. You can see how much less ifdefs is in our code now. Not to mention it works on UWP! Per team request, that API is SDK-internal. Samples use their own mini-helper project, `get-env-helper` that makes is so that `getenv()` works naturally on Linux and macOS, compiles without warnings and works on Win32, and compiles and works on UWP (using `GetEnvironmentStringsA()`) If it was for me, I would just make `Azure::Core::Environment::GetVariable()` public and simplify even further, I think it would be beneficial for sample readers (you can see that extra `get-env-helper` stuff adds just a little more visual clutter, compared to nothing). But I can see reasons against that, why team did not want to do it.
2022-01-29 11:22:33 +03:00
if(BUILD_SAMPLES)
add_subdirectory(samples/helpers/get-env)
endif()
# sub-projects
add_subdirectory(sdk/core)
add_subdirectory(sdk/attestation)
add_subdirectory(sdk/identity)
add_subdirectory(sdk/keyvault)
add_subdirectory(sdk/storage)
add_subdirectory(sdk/template)
Encapsulate getenv(), and make it work on UWP (#3275) It all started with UWP. The [docs](https://docs.microsoft.com/en-us/cpp/cppcx/crt-functions-not-supported-in-universal-windows-platform-apps?view=msvc-170) say: "`Environment variables are not available to UWP apps.`". And it truly won't work, I tried: linker error, the function is simply not present. So, for a year or so, we were `ifdef`ing everything enivoronment-related: console logger, environment credential, managed identity credential. And then just recently we wanted to enable our CI for UWP, including tests and samples. And it required to do more ifdefs (in vcpkg, we don't build samples or tests, so that problem did not exist). It just became more messy. Especially in samples - you can see how we would disable warning with `#pragma warning(disable : 4996)` or defining `_CRT_SECURE_NO_WARNINGS` already, but now came UWP, so we would have to add comment that `getenv()` is not available and make the sample compilation to either fail with clear message, or throw an exception. Plus we would have to detect that we are being compiled on UWP, which also adds visual clutter for reader. You can see how such an irrelevant (for a sample) thing as `getenv` was consuming more and more lines of sample code and reader's attention. But then! I read docs on more APIs for UWP. And I noticed that on .NET you can read environment variables. So I went and checked Win32 API docs for [GetEnvironmentVariable()](https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getenvironmentvariable) - it says: "`Minimum supported client: ... UWP apps`". **GetEnvironmentVariable() works on UWP!** And so does `SetEnvironmentVariable()` (our tests use it, which means we can make all of them work and execute for UWP). That's good news, but now it would probably be more code: it usually takes more lines to invoke WinAPI, it is no more an one-liner to call `getenv()/setenv()`. So, I encapsulated that into `Azure::Core::_internal::Environment::GetVariable()` and `SetVariable()`. You can see how much less ifdefs is in our code now. Not to mention it works on UWP! Per team request, that API is SDK-internal. Samples use their own mini-helper project, `get-env-helper` that makes is so that `getenv()` works naturally on Linux and macOS, compiles without warnings and works on Win32, and compiles and works on UWP (using `GetEnvironmentStringsA()`) If it was for me, I would just make `Azure::Core::Environment::GetVariable()` public and simplify even further, I think it would be beneficial for sample readers (you can see that extra `get-env-helper` stuff adds just a little more visual clutter, compared to nothing). But I can see reasons against that, why team did not want to do it.
2022-01-29 11:22:33 +03:00
if(BUILD_SAMPLES)
add_subdirectory(samples/integration/vcpkg-keyvault)
add_subdirectory(samples/integration/vcpkg-all-smoke)
Encapsulate getenv(), and make it work on UWP (#3275) It all started with UWP. The [docs](https://docs.microsoft.com/en-us/cpp/cppcx/crt-functions-not-supported-in-universal-windows-platform-apps?view=msvc-170) say: "`Environment variables are not available to UWP apps.`". And it truly won't work, I tried: linker error, the function is simply not present. So, for a year or so, we were `ifdef`ing everything enivoronment-related: console logger, environment credential, managed identity credential. And then just recently we wanted to enable our CI for UWP, including tests and samples. And it required to do more ifdefs (in vcpkg, we don't build samples or tests, so that problem did not exist). It just became more messy. Especially in samples - you can see how we would disable warning with `#pragma warning(disable : 4996)` or defining `_CRT_SECURE_NO_WARNINGS` already, but now came UWP, so we would have to add comment that `getenv()` is not available and make the sample compilation to either fail with clear message, or throw an exception. Plus we would have to detect that we are being compiled on UWP, which also adds visual clutter for reader. You can see how such an irrelevant (for a sample) thing as `getenv` was consuming more and more lines of sample code and reader's attention. But then! I read docs on more APIs for UWP. And I noticed that on .NET you can read environment variables. So I went and checked Win32 API docs for [GetEnvironmentVariable()](https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getenvironmentvariable) - it says: "`Minimum supported client: ... UWP apps`". **GetEnvironmentVariable() works on UWP!** And so does `SetEnvironmentVariable()` (our tests use it, which means we can make all of them work and execute for UWP). That's good news, but now it would probably be more code: it usually takes more lines to invoke WinAPI, it is no more an one-liner to call `getenv()/setenv()`. So, I encapsulated that into `Azure::Core::_internal::Environment::GetVariable()` and `SetVariable()`. You can see how much less ifdefs is in our code now. Not to mention it works on UWP! Per team request, that API is SDK-internal. Samples use their own mini-helper project, `get-env-helper` that makes is so that `getenv()` works naturally on Linux and macOS, compiles without warnings and works on Win32, and compiles and works on UWP (using `GetEnvironmentStringsA()`) If it was for me, I would just make `Azure::Core::Environment::GetVariable()` public and simplify even further, I think it would be beneficial for sample readers (you can see that extra `get-env-helper` stuff adds just a little more visual clutter, compared to nothing). But I can see reasons against that, why team did not want to do it.
2022-01-29 11:22:33 +03:00
endif()