From 811304f42890bcf3cc348f90b45c0c2a17f3fd05 Mon Sep 17 00:00:00 2001 From: Fred Hornsey Date: Mon, 27 Nov 2023 13:10:35 -0600 Subject: [PATCH] Write setenv in CMake --- CMakeLists.txt | 4 ++ cmake/opendds_testing.cmake | 95 +++++++++++++++++++++++++++++++++---- 2 files changed, 89 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6856dec55e8..97df61fb0f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,6 +88,10 @@ if(OPENDDS_SECURITY) endif() add_subdirectory(tools/inspect) +# Generate setenv +include("${_OPENDDS_CMAKE_DIR}/opendds_testing.cmake") +_opendds_write_setenv() + # Installation set(cmake_dest "${CMAKE_INSTALL_DATAROOTDIR}/cmake/OpenDDS") install(EXPORT opendds_targets diff --git a/cmake/opendds_testing.cmake b/cmake/opendds_testing.cmake index c874318b537..ae99fe358d6 100644 --- a/cmake/opendds_testing.cmake +++ b/cmake/opendds_testing.cmake @@ -20,14 +20,66 @@ function(opendds_add_test) list(APPEND arg_COMMAND ${arg_ARGS}) add_test(NAME "${test_name}" ${arg_UNPARSED_ARGUMENTS} COMMAND ${arg_COMMAND}) - set(env "ACE_ROOT=${ACE_ROOT}" "TAO_ROOT=${TAO_ROOT}") + _opendds_env(OUT env EXTRA_LIB_DIRS ${arg_EXTRA_LIB_DIRS}) + if(env) + set_property(TEST "${test_name}" PROPERTY ENVIRONMENT "${env}") + endif() + if(arg_NAME) + _opendds_write_setenv(SUFFIX "${arg_NAME}" EXTRA_LIB_DIRS ${arg_EXTRA_LIB_DIRS}) + else() + _opendds_write_setenv(EXTRA_LIB_DIRS ${arg_EXTRA_LIB_DIRS}) + endif() +endfunction() + +function(_opendds_env_append setenv) + list(LENGTH ARGN len) + while(len GREATER 1) + list(GET ARGN 0 name) + list(GET ARGN 1 value) + if(setenv) + if(WIN32) + set(env "${env}set \"${name}=${value}\"\n") + else() + set(env "${env}export ${name}=\"${value}\"\n") + endif() + else() + list(APPEND env "${name}=${value}") + endif() + list(REMOVE_AT ARGN 0) + list(REMOVE_AT ARGN 0) + list(LENGTH ARGN len) + endwhile() + if(len GREATER 0) + message(FATAL_ERROR "Arguments must be in name value pairs") + endif() + set(env "${env}" PARENT_SCOPE) +endfunction() + +function(_opendds_env) + set(no_value_options SETENV) + set(single_value_options OUT DIR) + set(multi_value_options EXTRA_LIB_DIRS) + cmake_parse_arguments(arg + "${no_value_options}" "${single_value_options}" "${multi_value_options}" ${ARGN}) - if(MSVC) + set(env "") + _opendds_env_append("${arg_SETENV}" ACE_ROOT "${ACE_ROOT}" TAO_ROOT "${TAO_ROOT}") + + if(WIN32) set(env_var_name PATH) else() set(env_var_name LD_LIBRARY_PATH) endif() - _opendds_path_list(lib_dir_list "$ENV{${env_var_name}}" "${TAO_LIB_DIR}") + if(arg_SETENV) + if(WIN32) + set(first "%PATH%") + else() + set(first "$LD_LIBRARY_PATH") + endif() + else() + set(first "$ENV{${env_var_name}}") + endif() + _opendds_path_list(lib_dir_list "${first}" "${TAO_LIB_DIR}") if(DEFINED _OPENDDS_GOOGLE_TEST_DIR) _opendds_path_list(lib_dir_list APPEND "${_OPENDDS_GOOGLE_TEST_DIR}") if(CMAKE_CONFIGURATION_TYPES) @@ -45,14 +97,14 @@ function(opendds_add_test) string(REPLACE "/" "\\" lib_dir_list "${lib_dir_list}") string(REPLACE ";" "\;" lib_dir_list "${lib_dir_list}") endif() - list(APPEND env "${env_var_name}=${lib_dir_list}") + _opendds_env_append("${arg_SETENV}" ${env_var_name} "${lib_dir_list}") if(DEFINED OPENDDS_BUILD_DIR) - list(APPEND env "OPENDDS_BUILD_DIR=${OPENDDS_BUILD_DIR}") + _opendds_env_append("${arg_SETENV}" OPENDDS_BUILD_DIR "${OPENDDS_BUILD_DIR}") endif() if(DEFINED OPENDDS_CONFIG_DIR) - list(APPEND env "OPENDDS_CONFIG_DIR=${OPENDDS_CONFIG_DIR}") + _opendds_env_append("${arg_SETENV}" OPENDDS_CONFIG_DIR "${OPENDDS_CONFIG_DIR}") endif() if(DEFINED OPENDDS_SOURCE_DIR) @@ -60,11 +112,34 @@ function(opendds_add_test) set(ACE_SOURCE_DIR "${ACE_ROOT}") endif() _opendds_path_list(perl5lib "${OPENDDS_SOURCE_DIR}/bin" "${ACE_SOURCE_DIR}/bin") - list(APPEND env "PERL5LIB=${perl5lib}") - list(APPEND env "OPENDDS_SOURCE_DIR=${OPENDDS_SOURCE_DIR}") + _opendds_env_append("${arg_SETENV}" + OPENDDS_SOURCE_DIR "${OPENDDS_SOURCE_DIR}" + PERL5LIB "${perl5lib}" + ) endif() - if(env) - set_property(TEST "${test_name}" PROPERTY ENVIRONMENT "${env}") + set("${arg_OUT}" ${env} PARENT_SCOPE) +endfunction() + +function(_opendds_write_setenv) + set(no_value_options) + set(single_value_options DIR SUFFIX) + set(multi_value_options EXTRA_LIB_DIRS) + cmake_parse_arguments(arg + "${no_value_options}" "${single_value_options}" "${multi_value_options}" ${ARGN}) + if(NOT DEFINED arg_DIR) + set(arg_DIR "${CMAKE_CURRENT_BINARY_DIR}") + endif() + if(DEFINED arg_SUFFIX) + set(arg_SUFFIX "${arg_SUFFIX}_") + endif() + + _opendds_env(SETENV OUT setenv_contents EXTRA_LIB_DIRS "${arg_EXTRA_LIB_DIRS}") + set(path "${arg_DIR}/${arg_SUFFIX}setenv") + if(WIN32) + set(path "${path}.bat") + else() + set(path "${path}.sh") endif() + file(WRITE "${path}" "${setenv_contents}") endfunction()