From 295a69174bc2664c525ddfaa648bdc0031040c88 Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Wed, 21 Aug 2024 12:25:38 +0530 Subject: [PATCH 01/16] CMakeLists: Make grpc-device buildable on NILRT 11 NILRT 11 will ship with grpc >1.60 and protobuf >v25.2. grpc-device doesn't compile with these toolchain versions, throwing errors about undefined symbols. Downgrading grpc back to 1.51 (with python3-grpcio and protobuf recipes) successfully works around these issues. But for security and general currency reasons, we cannot afford to ship NILRT 11 with these downgrades. Current CMakeLists.txt is limited to some of the bitbake functionalities which makes it difficult to build with new changes. Changes in this commit will make sure NILRT 11 compiles grpc-device with the latest/upgraded grpc version without affecting the existing build process. Changes: 1. refactor toolchain link logic - Deprecate the CMAKE_CROSSCOMPILING variable, in favor of USE_SUBMODULE_LIBS cmake option. Refactor the linking logic to be a consolidation of all the linking actions from across the file, and to better support builds in generic linux environments. 2. fixup utf8cpp library link - The utf8cpp cmake library namespace is incorrectly identified as 'utf8cpp', instead of the proper 'utf8cpp:utf8cpp'. As a result, cmake does not link the utf8.h header and compilation fails. 3. parameterize python3 venv - Create a USE_PYTHON_VIRTUALENV cmake option. When asserted, it will add the bespoke venv to the toolchain. Otherwise, the cmake config will use the system python environment. 4. link the device server to grpc_gpr - ni_grpc_device_server target depends on symbols from grpc gpr.so, namely gpr_log. Add grpc_gpr to link libraries for ni_grpc_device_server. 5. add abseil_sync dep to server target - ni_grpc_device_server uses symbology from libabsl_synchronization library. Add a library dependency to reflect that relationship. 6. add utf8cpp dep to IntegrationTestsRunner - The IntegrationTestsRunner depends on utf8.h header indirectly, via its access to the device server source. 7. fill out target lib deps - Shove ni_grpc_device_server library dependencies into a variable, so that it can be easily passed along to the test targets. 8. suppress protobuf installation in SM - Set protobuf_INSTALL=OFF, which suppresses the protobuf installation codepaths - that we don't want to use anyway and which cause the failure. 9. add necessary gRPC dep to ni_grpc_device_server - ni_grpc_device_server must be linked against libgrpc, as well as the grpccpp libs. 10.fixup venv codegen deps - Give the codegen targets a dependency on the python virtualenv via the all_codegen_dependencies variable. Signed-off-by: Rajendra Desai --- CMakeLists.txt | 174 +++++++++++++++++++++++++++++-------------------- 1 file changed, 102 insertions(+), 72 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d33d7e0e7..4ce602bc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,62 +5,88 @@ project(ni_grpc_device_server VERSION 2.7.0) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") -include(CreateVirtualEnvironment) + # Workaround for: https://bugs.chromium.org/p/boringssl/issues/detail?id=423 if (CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64") set(CMAKE_SYSTEM_PROCESSOR "amd64") endif() + +#--- +# Conifguration options +#--- +option(USE_NILRT_LEGACY_TOOLCHAIN "Enable to use tools and libraries from a NILRT compile toolchain." OFF) +option(USE_PYTHON_VIRTUALENV "Enable to use the automatically-generated python venv, local to this project source." ON) +option(USE_SUBMODULE_LIBS "Enable to link against the submodule libraries, rather than their native-OS equivalents." ON) + #---------------------------------------------------------------------- -# Use the grpc targets directly from this build, only when not cross-compiling. +# Setup build dependencies, according to the toolchain options. #---------------------------------------------------------------------- -if(CMAKE_CROSSCOMPILING) + +if(USE_SUBMODULE_LIBS) + # The archetypical WIN32 build case + # protobuf_INSTALL must be turned OFF whenever building it as a cmake subdir. + set(protobuf_INSTALL OFF) + + add_subdirectory(third_party/grpc ${CMAKE_CURRENT_BINARY_DIR}/grpc) + add_subdirectory(third_party/gtest ${CMAKE_CURRENT_BINARY_DIR}/gtest) + add_subdirectory(third_party/json ${CMAKE_CURRENT_BINARY_DIR}/json) + add_subdirectory(third_party/utfcpp ${CMAKE_CURRENT_BINARY_DIR}/utfcpp) + + set(_PROTOBUF_PROTOC $) + set(_REFLECTION grpc++_reflection) + set(_GRPC grpc) + set(_GRPC_CPP_PLUGIN_EXECUTABLE $) + set(_GRPC_GRPCPP grpc++) + set(_PROTOBUF_LIBPROTOBUF libprotobuf) + set(_UTF8CPP utf8cpp) +else() find_program(_PROTOBUF_PROTOC protoc) find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin) + find_library(_GRPC_GPR gpr) + find_library(_ABSEIL_SYNC absl_synchronization REQUIRED) - if(NOT _GRPC_DEVICE_NILRT_LEGACY_TOOLCHAIN) - find_package(gRPC REQUIRED) - find_library(_REFLECTION grpc++_reflection) - find_library(_GRPC_GRPCPP grpc++) - find_library(_PROTOBUF_LIBPROTOBUF protobuf) - else() + if(USE_NILRT_LEGACY_TOOLCHAIN) + # The archetypical NILRT SDK toolchain build case add_subdirectory(third_party/grpc ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL) set(_REFLECTION grpc++_reflection) set(_GRPC_GRPCPP grpc++) set(_PROTOBUF_LIBPROTOBUF libprotobuf) - endif() + else() + # The archetypical linux build case (including OpenEmbedded) + find_library(_GRPC_GRPCPP grpc++) + find_library(_PROTOBUF_LIBPROTOBUF protobuf) + find_library(_REFLECTION grpc++_reflection) -else() - add_subdirectory(third_party/grpc ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL) - set(_PROTOBUF_PROTOC $) - set(_REFLECTION grpc++_reflection) - set(_GRPC_CPP_PLUGIN_EXECUTABLE $) - set(_GRPC_GRPCPP grpc++) - set(_PROTOBUF_LIBPROTOBUF libprotobuf) -endif() + find_package(gRPC REQUIRED) + find_package(GTest REQUIRED) + find_package(nlohmann_json REQUIRED) + find_package(utf8cpp REQUIRED) -#---------------------------------------------------------------------- -CreateVirtualEnvironment(virtual_environment - REQUIREMENTS_TXT - ${CMAKE_SOURCE_DIR}/python_build_requirements.txt - ENV_NAME - venv - OUT_PYTHON_EXE - PYTHON_EXE -) + set(_GRPC gRPC::grpc) + set(_UTF8CPP utf8cpp::utf8cpp) + endif() +endif() -#---------------------------------------------------------------------- -# Use the utfcpp targets directly from this build, only when not cross-compiling. -#---------------------------------------------------------------------- -if(CMAKE_CROSSCOMPILING AND NOT _GRPC_DEVICE_NILRT_LEGACY_TOOLCHAIN) - find_package(utf8cpp REQUIRED) +# Python3 Virtual Environment +if(USE_PYTHON_VIRTUALENV) + include(CreateVirtualEnvironment) + CreateVirtualEnvironment(virtual_environment + REQUIREMENTS_TXT + ${CMAKE_SOURCE_DIR}/python_build_requirements.txt + ENV_NAME + venv + OUT_PYTHON_EXE + PYTHON_EXE + ) else() - add_subdirectory(third_party/utfcpp ${CMAKE_CURRENT_BINARY_DIR}/utfcpp EXCLUDE_FROM_ALL) + find_package(Python3 REQUIRED) + set(PYTHON_EXE ${Python3_EXECUTABLE}) endif() -include_directories( - "./third_party/utfcpp/source" -) + +enable_testing() +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) #---------------------------------------------------------------------- # Use C++17 (needed for shared_mutex support on Linux) @@ -141,6 +167,10 @@ endif() set(all_codegen_dependencies "") +if(USE_PYTHON_VIRTUALENV) + list(APPEND all_codegen_dependencies virtual_environment) +endif() + foreach(api ${nidrivers}) set(codegen_dependencies "${metadata_dir}/${api}/attributes.py" @@ -191,7 +221,10 @@ foreach(api ${nidrivers}) ${nidriver_client_srcs} "${service_output_dir}/${api}/${api}_client.cpp") endif() - set(proto_dependencies ${codegen_dependencies} ${codegen_scripts} virtual_environment) + set(proto_dependencies ${codegen_dependencies} ${codegen_scripts}) + if (USE_PYTHON_VIRTUALENV) + list(APPEND proto_dependencies virtual_environment) + endif() add_custom_command(OUTPUT ${output_files} ${gen_command} COMMENT "Generating proto file and service for ${api}" @@ -211,7 +244,6 @@ add_custom_command( DEPENDS ${all_codegen_dependencies} ${codegen_scripts} - virtual_environment ) set(nidriver_service_srcs @@ -240,6 +272,9 @@ function(GenerateGrpcSources) cmake_parse_arguments(GENERATE_ARGS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) set(output_files "${GENERATE_ARGS_OUTPUT}") set(proto_file "${GENERATE_ARGS_PROTO}") + if(USE_SUBMODULE_LIBS) + set(protobuf_includes_arg -I ${CMAKE_SOURCE_DIR}/third_party/grpc/third_party/protobuf/src/) + endif() get_filename_component(proto_path "${proto_file}" PATH) # Asssumption: all outputs are in the same directory: use the zero-th. list(GET output_files 0 proto_srcs) @@ -250,7 +285,7 @@ function(GenerateGrpcSources) ARGS --grpc_out "${proto_out_path}" --cpp_out "${proto_out_path}" -I "${proto_path}" - -I ${CMAKE_SOURCE_DIR}/third_party/grpc/third_party/protobuf/src/ + ${protobuf_includes_arg} -I ${CMAKE_SOURCE_DIR}/imports/protobuf --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}" "${proto_file}" @@ -472,14 +507,21 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows) PRIVATE "source/server/windows/console_ctrl_handler.cpp") endif() +set(server_lib_deps + ${_ABSEIL_SYNC} + ${_GRPC_GPR} + ${_GRPC_GRPCPP} + ${_GRPC} + ${_PROTOBUF_LIBPROTOBUF} + ${_REFLECTION} + ${_UTF8CPP} + ${CMAKE_DL_LIBS} + nlohmann_json::nlohmann_json +) + target_link_libraries(ni_grpc_device_server - ${_REFLECTION} - ${_GRPC_GRPCPP} - ${_PROTOBUF_LIBPROTOBUF} - ${CMAKE_DL_LIBS} - nlohmann_json::nlohmann_json - utf8cpp - ) + ${server_lib_deps} +) set_target_properties(ni_grpc_device_server PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE) @@ -500,20 +542,6 @@ add_custom_command( COMMAND ${PYTHON_EXE} ${codegen_dir}/generate_server_capabilities.py ${metadata_dir}/ -o $/) - -#---------------------------------------------------------------------- -# Add JSON parser and configure google tests -#---------------------------------------------------------------------- -if(CMAKE_CROSSCOMPILING AND NOT _GRPC_DEVICE_NILRT_LEGACY_TOOLCHAIN) - find_package(nlohmann_json REQUIRED) - find_package(GTest REQUIRED) -else() - add_subdirectory(third_party/json ${CMAKE_CURRENT_BINARY_DIR}/json EXCLUDE_FROM_ALL) - enable_testing() - set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) - add_subdirectory(third_party/gtest ${CMAKE_CURRENT_BINARY_DIR}/gtest EXCLUDE_FROM_ALL) -endif() - # Link test executable against gtest add_executable(IntegrationTestsRunner "imports/include/nierr_Status.cpp" @@ -568,12 +596,12 @@ add_executable(IntegrationTestsRunner find_package(Threads REQUIRED) target_link_libraries(IntegrationTestsRunner - gtest + ${server_lib_deps} gmock - ${_GRPC_GRPCPP} - ${CMAKE_DL_LIBS} + grpc + gtest Threads::Threads - nlohmann_json::nlohmann_json) +) # Ignore the use of deprecated functions in test code target_compile_definitions(IntegrationTestsRunner @@ -673,12 +701,13 @@ target_include_directories(UnitTestsRunner PRIVATE "source/server") target_link_libraries(UnitTestsRunner - gtest - gmock - ${_GRPC_GRPCPP} ${CMAKE_DL_LIBS} + ${server_lib_deps} + gmock + grpc + gtest Threads::Threads - nlohmann_json::nlohmann_json) +) #---------------------------------------------------------------------- # Copy test asset certificates to binary output certs sub-directory @@ -695,8 +724,10 @@ foreach(api ${nidrivers_to_build}) "${service_output_dir}/${api}/${api}_compilation_test.cpp") endforeach() add_library(CompilationTests STATIC ${compilation_test_sources}) + target_link_libraries(CompilationTests ${_GRPC_GRPCPP} + ${_UTF8CPP} ) add_custom_target(generated_nidriver_service_library_hdrs DEPENDS ${nidriver_service_library_hdrs}) add_dependencies(CompilationTests generated_nidriver_service_library_hdrs) @@ -802,11 +833,10 @@ endif() add_executable(SystemTestsRunner ${system_test_runner_sources}) target_link_libraries(SystemTestsRunner - gtest - gmock - ${_GRPC_GRPCPP} ${CMAKE_DL_LIBS} - nlohmann_json::nlohmann_json + ${server_lib_deps} + gmock + gtest ) # Ignore the use of deprecated functions in test code From 12e4eaa29ecdbef434e77a3e985521fb19ccfc18 Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Thu, 22 Aug 2024 20:45:40 +0530 Subject: [PATCH 02/16] Updated variable name of NILRT_LEGACY_TOOLCHAIN in build artifacts and fixed a small typo Signed-off-by: Rajendra Desai --- .github/workflows/build_nilrt.yml | 2 +- CMakeLists.txt | 2 +- cmake/nilrt-x86_64.cmake | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build_nilrt.yml b/.github/workflows/build_nilrt.yml index 9a0e5e4f5..a38aa6307 100644 --- a/.github/workflows/build_nilrt.yml +++ b/.github/workflows/build_nilrt.yml @@ -96,7 +96,7 @@ jobs: -S . -B build -D CMAKE_BUILD_TYPE=$ENV{BUILD_TYPE} - -D CMAKE_TOOLCHAIN_FILE=$ENV{GITHUB_WORKSPACE}/cmake/nilrt-x86_64.cmake + -D grpc_device_USE_NILRT_LEGACY_TOOLCHAIN=on RESULT_VARIABLE result ) if (NOT result EQUAL 0) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ce602bc7..26caf7fc8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ endif() #--- -# Conifguration options +# Configuration options #--- option(USE_NILRT_LEGACY_TOOLCHAIN "Enable to use tools and libraries from a NILRT compile toolchain." OFF) option(USE_PYTHON_VIRTUALENV "Enable to use the automatically-generated python venv, local to this project source." ON) diff --git a/cmake/nilrt-x86_64.cmake b/cmake/nilrt-x86_64.cmake index c007f6966..7f00969f5 100644 --- a/cmake/nilrt-x86_64.cmake +++ b/cmake/nilrt-x86_64.cmake @@ -3,7 +3,6 @@ #---------------------------------------------------------------------- set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSTEM_PROCESSOR x86_64) -set(_GRPC_DEVICE_NILRT_LEGACY_TOOLCHAIN TRUE) #---------------------------------------------------------------------- # Path variables for toolchains From 3d1249ce1ce00bfdf0bb79eee807aec3bd58167b Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Thu, 22 Aug 2024 22:36:09 +0530 Subject: [PATCH 03/16] build_nilrt.yml: Fixed a typo --- .github/workflows/build_nilrt.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_nilrt.yml b/.github/workflows/build_nilrt.yml index a38aa6307..0542ed17c 100644 --- a/.github/workflows/build_nilrt.yml +++ b/.github/workflows/build_nilrt.yml @@ -96,7 +96,7 @@ jobs: -S . -B build -D CMAKE_BUILD_TYPE=$ENV{BUILD_TYPE} - -D grpc_device_USE_NILRT_LEGACY_TOOLCHAIN=on + -D USE_NILRT_LEGACY_TOOLCHAIN=on RESULT_VARIABLE result ) if (NOT result EQUAL 0) From 01fbd1a81d75d1fc590d5b9883794de820718e1b Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Fri, 23 Aug 2024 16:48:51 +0530 Subject: [PATCH 04/16] Enforce consistent runtime library settings in case of MSVC Signed-off-by: Rajendra Desai --- CMakeLists.txt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 26caf7fc8..75d3f2ead 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,6 +88,12 @@ endif() enable_testing() set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +# Enforce consistent runtime library settings in case of MSVC +if(CMAKE_LINKER MATCHES "MSVC") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") +endif() + #---------------------------------------------------------------------- # Use C++17 (needed for shared_mutex support on Linux) #---------------------------------------------------------------------- @@ -849,7 +855,7 @@ add_custom_command( ${CMAKE_SOURCE_DIR}/source/tests/assets/data/ $/) -if(_GRPC_DEVICE_NILRT_LEGACY_TOOLCHAIN) +if(USE_NILRT_LEGACY_TOOLCHAIN) target_link_libraries(SystemTestsRunner stdc++fs) target_compile_definitions(SystemTestsRunner PRIVATE FS_EXPERIMENTAL) endif() From babadb2f14999136b9aa1f064402b568bd61bf4f Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Fri, 23 Aug 2024 17:53:52 +0530 Subject: [PATCH 05/16] Update linker condition --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 75d3f2ead..2395a085a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,7 +89,7 @@ enable_testing() set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # Enforce consistent runtime library settings in case of MSVC -if(CMAKE_LINKER MATCHES "MSVC") +if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") endif() From a5f652bb0e4cce9ccce67d61daad55f43b0268e2 Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Fri, 23 Aug 2024 20:28:14 +0530 Subject: [PATCH 06/16] Add a condition to check for POLICY CMP0091 (enabling the use of MSVC_RUNTIME_LIBRARY property) which is supported on cmake version >3.15 in case of MSVC compiler Signed-off-by: Rajendra Desai --- CMakeLists.txt | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2395a085a..0c4a1be64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,9 +89,15 @@ enable_testing() set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # Enforce consistent runtime library settings in case of MSVC -if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + if (POLICY CMP0091) + cmake_policy(SET CMP0091 NEW) + message("Setting CMP0091 policy") + else() + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") + message("Using default policy since cmake version is <3.15") + endif() endif() #---------------------------------------------------------------------- @@ -450,6 +456,12 @@ add_executable(ni_grpc_device_server ${calibrationoperations_restricted_proto_srcs} ${calibrationoperations_restricted_grpc_srcs} ${nidriver_service_srcs}) + +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + if (POLICY CMP0091) + set_property(TARGET ni_grpc_device_server PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + endif() +endif() # Enable warnings only on source that we own, not generated code or dependencies file(GLOB_RECURSE SOURCE_TO_ENABLE_WARNINGS "source/*.cpp") @@ -597,6 +609,12 @@ add_executable(IntegrationTestsRunner "${custom_dir}/nifake_service.custom.cpp" ) +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + if (POLICY CMP0091) + set_property(TARGET IntegrationTestsRunner PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + endif() +endif() + set(CMAKE_THREAD_PREFER_PTHREAD TRUE) set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED) @@ -689,6 +707,12 @@ add_executable(UnitTestsRunner "${custom_dir}/nixnet_service.custom.cpp" ) +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + if (POLICY CMP0091) + set_property(TARGET UnitTestsRunner PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + endif() +endif() + # ni_fake_service_tests.cpp and several DAQ cpp files exceed the MSVC limit for the number of sections # in an obj file defined by PE-COFF. This line disables the limit. if(MSVC) @@ -838,6 +862,12 @@ endif() add_executable(SystemTestsRunner ${system_test_runner_sources}) +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + if (POLICY CMP0091) + set_property(TARGET SystemTestsRunner PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + endif() +endif() + target_link_libraries(SystemTestsRunner ${CMAKE_DL_LIBS} ${server_lib_deps} From 2ed6d06b70945fc9462e1c0b203a83d944ea1fb7 Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Thu, 29 Aug 2024 20:07:03 +0530 Subject: [PATCH 07/16] CMakeLists: Code cleanup --- CMakeLists.txt | 48 ++++++++++++------------------------------------ 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c4a1be64..8d347619b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,18 @@ if (CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64") set(CMAKE_SYSTEM_PROCESSOR "amd64") endif() +# Enforce consistent runtime library settings in case of MSVC +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + if (POLICY CMP0091) + cmake_policy(SET CMP0091 NEW) + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + message("Setting policy CMP0091 and runtime library") + else() + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") + message("Using dynamic runtime library") + endif() +endif() #--- # Configuration options @@ -88,18 +100,6 @@ endif() enable_testing() set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) -# Enforce consistent runtime library settings in case of MSVC -if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - if (POLICY CMP0091) - cmake_policy(SET CMP0091 NEW) - message("Setting CMP0091 policy") - else() - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") - message("Using default policy since cmake version is <3.15") - endif() -endif() - #---------------------------------------------------------------------- # Use C++17 (needed for shared_mutex support on Linux) #---------------------------------------------------------------------- @@ -456,12 +456,6 @@ add_executable(ni_grpc_device_server ${calibrationoperations_restricted_proto_srcs} ${calibrationoperations_restricted_grpc_srcs} ${nidriver_service_srcs}) - -if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - if (POLICY CMP0091) - set_property(TARGET ni_grpc_device_server PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") - endif() -endif() # Enable warnings only on source that we own, not generated code or dependencies file(GLOB_RECURSE SOURCE_TO_ENABLE_WARNINGS "source/*.cpp") @@ -609,12 +603,6 @@ add_executable(IntegrationTestsRunner "${custom_dir}/nifake_service.custom.cpp" ) -if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - if (POLICY CMP0091) - set_property(TARGET IntegrationTestsRunner PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") - endif() -endif() - set(CMAKE_THREAD_PREFER_PTHREAD TRUE) set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED) @@ -707,12 +695,6 @@ add_executable(UnitTestsRunner "${custom_dir}/nixnet_service.custom.cpp" ) -if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - if (POLICY CMP0091) - set_property(TARGET UnitTestsRunner PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") - endif() -endif() - # ni_fake_service_tests.cpp and several DAQ cpp files exceed the MSVC limit for the number of sections # in an obj file defined by PE-COFF. This line disables the limit. if(MSVC) @@ -862,12 +844,6 @@ endif() add_executable(SystemTestsRunner ${system_test_runner_sources}) -if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - if (POLICY CMP0091) - set_property(TARGET SystemTestsRunner PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") - endif() -endif() - target_link_libraries(SystemTestsRunner ${CMAKE_DL_LIBS} ${server_lib_deps} From 26a3c63d05e64b326c18cbe778f0d14e886433ab Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Thu, 29 Aug 2024 21:20:00 +0530 Subject: [PATCH 08/16] CMakeLists: use static runtime library --- CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d347619b..2c1041b09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,13 +15,13 @@ endif() # Enforce consistent runtime library settings in case of MSVC if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") if (POLICY CMP0091) + message("Policy CMP0091 available, setting compiler flags automatically") cmake_policy(SET CMP0091 NEW) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") - message("Setting policy CMP0091 and runtime library") else() - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") - message("Using dynamic runtime library") + message("Policy CMP0091 not available, setting compiler flags manually") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") endif() endif() From 36890c9fff9f6ccdc1ee322a100a07770c4ef1d9 Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Thu, 29 Aug 2024 21:28:37 +0530 Subject: [PATCH 09/16] CMakeLists: change gtest_force_shared_crt to default --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c1041b09..74d183f0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,7 +98,6 @@ else() endif() enable_testing() -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) #---------------------------------------------------------------------- # Use C++17 (needed for shared_mutex support on Linux) From 2ea12e9fbde1c969c8a57eddc375346a7f6900b5 Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Thu, 29 Aug 2024 23:54:15 +0530 Subject: [PATCH 10/16] CMakeLists: add compile options if MSVC --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 74d183f0f..a6a2ddcc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ endif() # Enforce consistent runtime library settings in case of MSVC if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + add_compile_options("/MT$<$:d>") if (POLICY CMP0091) message("Policy CMP0091 available, setting compiler flags automatically") cmake_policy(SET CMP0091 NEW) From 03be79703073ac72aacadc9979ccce953950110a Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Fri, 30 Aug 2024 17:50:49 +0530 Subject: [PATCH 11/16] Workaround: Do a clean build --- .github/workflows/build_cmake.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build_cmake.yml b/.github/workflows/build_cmake.yml index cb5351c02..6fd9e046c 100644 --- a/.github/workflows/build_cmake.yml +++ b/.github/workflows/build_cmake.yml @@ -74,6 +74,7 @@ jobs: run: > cmake --build build + --clean-first --config ${{ matrix.config.build_type }} -j ${{ steps.cpu-cores.outputs.count }} From db9baca1313d1cde945d956c0ed84d2cc23aa1b6 Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Fri, 30 Aug 2024 18:00:33 +0530 Subject: [PATCH 12/16] Revert "CMakeLists: add compile options if MSVC" This reverts commit 2ea12e9fbde1c969c8a57eddc375346a7f6900b5. --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a6a2ddcc7..74d183f0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,6 @@ endif() # Enforce consistent runtime library settings in case of MSVC if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - add_compile_options("/MT$<$:d>") if (POLICY CMP0091) message("Policy CMP0091 available, setting compiler flags automatically") cmake_policy(SET CMP0091 NEW) From 15707967cf26c67ed56f5dcefe15540ac3c90dad Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Mon, 2 Sep 2024 11:57:09 +0530 Subject: [PATCH 13/16] Revert "CMakeLists: change gtest_force_shared_crt to default" This reverts commit 36890c9fff9f6ccdc1ee322a100a07770c4ef1d9. --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 74d183f0f..2c1041b09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,6 +98,7 @@ else() endif() enable_testing() +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) #---------------------------------------------------------------------- # Use C++17 (needed for shared_mutex support on Linux) From 52ebfc8c1d84d57f9368f63a938ed3eb202059cc Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Mon, 2 Sep 2024 11:57:56 +0530 Subject: [PATCH 14/16] Revert "CMakeLists: use static runtime library" This reverts commit 26a3c63d05e64b326c18cbe778f0d14e886433ab. --- CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c1041b09..8d347619b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,13 +15,13 @@ endif() # Enforce consistent runtime library settings in case of MSVC if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") if (POLICY CMP0091) - message("Policy CMP0091 available, setting compiler flags automatically") cmake_policy(SET CMP0091 NEW) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + message("Setting policy CMP0091 and runtime library") else() - message("Policy CMP0091 not available, setting compiler flags manually") - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") + message("Using dynamic runtime library") endif() endif() From 7eff17d381b6605a4ad052980969488debc76de3 Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Mon, 2 Sep 2024 11:58:05 +0530 Subject: [PATCH 15/16] Revert "CMakeLists: Code cleanup" This reverts commit 2ed6d06b70945fc9462e1c0b203a83d944ea1fb7. --- CMakeLists.txt | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d347619b..0c4a1be64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,18 +12,6 @@ if (CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64") set(CMAKE_SYSTEM_PROCESSOR "amd64") endif() -# Enforce consistent runtime library settings in case of MSVC -if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - if (POLICY CMP0091) - cmake_policy(SET CMP0091 NEW) - set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") - message("Setting policy CMP0091 and runtime library") - else() - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") - message("Using dynamic runtime library") - endif() -endif() #--- # Configuration options @@ -100,6 +88,18 @@ endif() enable_testing() set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +# Enforce consistent runtime library settings in case of MSVC +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + if (POLICY CMP0091) + cmake_policy(SET CMP0091 NEW) + message("Setting CMP0091 policy") + else() + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") + message("Using default policy since cmake version is <3.15") + endif() +endif() + #---------------------------------------------------------------------- # Use C++17 (needed for shared_mutex support on Linux) #---------------------------------------------------------------------- @@ -456,6 +456,12 @@ add_executable(ni_grpc_device_server ${calibrationoperations_restricted_proto_srcs} ${calibrationoperations_restricted_grpc_srcs} ${nidriver_service_srcs}) + +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + if (POLICY CMP0091) + set_property(TARGET ni_grpc_device_server PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + endif() +endif() # Enable warnings only on source that we own, not generated code or dependencies file(GLOB_RECURSE SOURCE_TO_ENABLE_WARNINGS "source/*.cpp") @@ -603,6 +609,12 @@ add_executable(IntegrationTestsRunner "${custom_dir}/nifake_service.custom.cpp" ) +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + if (POLICY CMP0091) + set_property(TARGET IntegrationTestsRunner PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + endif() +endif() + set(CMAKE_THREAD_PREFER_PTHREAD TRUE) set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED) @@ -695,6 +707,12 @@ add_executable(UnitTestsRunner "${custom_dir}/nixnet_service.custom.cpp" ) +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + if (POLICY CMP0091) + set_property(TARGET UnitTestsRunner PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + endif() +endif() + # ni_fake_service_tests.cpp and several DAQ cpp files exceed the MSVC limit for the number of sections # in an obj file defined by PE-COFF. This line disables the limit. if(MSVC) @@ -844,6 +862,12 @@ endif() add_executable(SystemTestsRunner ${system_test_runner_sources}) +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + if (POLICY CMP0091) + set_property(TARGET SystemTestsRunner PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + endif() +endif() + target_link_libraries(SystemTestsRunner ${CMAKE_DL_LIBS} ${server_lib_deps} From 5b456c6c97d92a8f083d5b79212747276687cb76 Mon Sep 17 00:00:00 2001 From: Rajendra Desai Date: Mon, 2 Sep 2024 12:05:06 +0530 Subject: [PATCH 16/16] try a workaround --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c4a1be64..ccdfbdafa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,8 @@ if(USE_SUBMODULE_LIBS) set(protobuf_INSTALL OFF) add_subdirectory(third_party/grpc ${CMAKE_CURRENT_BINARY_DIR}/grpc) - add_subdirectory(third_party/gtest ${CMAKE_CURRENT_BINARY_DIR}/gtest) + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + add_subdirectory(third_party/gtest ${CMAKE_CURRENT_BINARY_DIR}/gtest EXCLUDE_FROM_ALL) add_subdirectory(third_party/json ${CMAKE_CURRENT_BINARY_DIR}/json) add_subdirectory(third_party/utfcpp ${CMAKE_CURRENT_BINARY_DIR}/utfcpp) @@ -86,7 +87,6 @@ else() endif() enable_testing() -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # Enforce consistent runtime library settings in case of MSVC if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")