Skip to content

Commit

Permalink
Write setenv in CMake
Browse files Browse the repository at this point in the history
  • Loading branch information
iguessthislldo committed Nov 27, 2023
1 parent e22eac1 commit 811304f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
95 changes: 85 additions & 10 deletions cmake/opendds_testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -45,26 +97,49 @@ 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)
if(NOT DEFINED ACE_SOURCE_DIR)
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()

0 comments on commit 811304f

Please sign in to comment.