Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/update grp cdevice 2 #1087

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/build_cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ jobs:
run: >
cmake
--build build
--clean-first
--config ${{ matrix.config.build_type }}
-j ${{ steps.cpu-cores.outputs.count }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_nilrt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 USE_NILRT_LEGACY_TOOLCHAIN=on
RESULT_VARIABLE result
)
if (NOT result EQUAL 0)
Expand Down
208 changes: 137 additions & 71 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,100 @@ 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()


#---
# 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)
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)
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)

set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
set(_REFLECTION grpc++_reflection)
set(_GRPC grpc)
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
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)
else()
# The archetypical linux build case (including OpenEmbedded)
find_library(_GRPC_GRPCPP grpc++)
find_library(_PROTOBUF_LIBPROTOBUF protobuf)
find_library(_REFLECTION grpc++_reflection)

find_package(gRPC REQUIRED)
find_package(GTest REQUIRED)
find_package(nlohmann_json REQUIRED)
find_package(utf8cpp REQUIRED)

set(_GRPC gRPC::grpc)
set(_UTF8CPP utf8cpp::utf8cpp)
endif()
endif()

# 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/grpc ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL)
set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
set(_REFLECTION grpc++_reflection)
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
set(_GRPC_GRPCPP grpc++)
set(_PROTOBUF_LIBPROTOBUF libprotobuf)
find_package(Python3 REQUIRED)
set(PYTHON_EXE ${Python3_EXECUTABLE})
endif()

#----------------------------------------------------------------------
CreateVirtualEnvironment(virtual_environment
REQUIREMENTS_TXT
${CMAKE_SOURCE_DIR}/python_build_requirements.txt
ENV_NAME
venv
OUT_PYTHON_EXE
PYTHON_EXE
)
enable_testing()

#----------------------------------------------------------------------
# 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)
else()
add_subdirectory(third_party/utfcpp ${CMAKE_CURRENT_BINARY_DIR}/utfcpp EXCLUDE_FROM_ALL)
# 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()
include_directories(
"./third_party/utfcpp/source"
)

#----------------------------------------------------------------------
# Use C++17 (needed for shared_mutex support on Linux)
Expand Down Expand Up @@ -141,6 +179,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"
Expand Down Expand Up @@ -191,7 +233,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}"
Expand All @@ -211,7 +256,6 @@ add_custom_command(
DEPENDS
${all_codegen_dependencies}
${codegen_scripts}
virtual_environment
)

set(nidriver_service_srcs
Expand Down Expand Up @@ -240,6 +284,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)
Expand All @@ -250,7 +297,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}"
Expand Down Expand Up @@ -409,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$<$<CONFIG:Debug>: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")
Expand Down Expand Up @@ -472,14 +525,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)

Expand All @@ -500,20 +560,6 @@ add_custom_command(
COMMAND ${PYTHON_EXE} ${codegen_dir}/generate_server_capabilities.py ${metadata_dir}/
-o $<TARGET_FILE_DIR:ni_grpc_device_server>/)


#----------------------------------------------------------------------
# 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"
Expand Down Expand Up @@ -563,17 +609,23 @@ 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$<$<CONFIG:Debug>:Debug>")
endif()
endif()

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
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
Expand Down Expand Up @@ -655,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$<$<CONFIG:Debug>: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)
Expand All @@ -673,12 +731,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
Expand All @@ -695,8 +754,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)
Expand Down Expand Up @@ -801,12 +862,17 @@ 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$<$<CONFIG:Debug>:Debug>")
endif()
endif()

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
Expand All @@ -819,7 +885,7 @@ add_custom_command(
${CMAKE_SOURCE_DIR}/source/tests/assets/data/
$<TARGET_FILE_DIR:SystemTestsRunner>/)

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()
Expand Down
1 change: 0 additions & 1 deletion cmake/nilrt-x86_64.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading