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

Conditionally modify envvar vs. global CMAKE_PREFIX_PATH in rapids_cmake_support_conda_env #439

56 changes: 46 additions & 10 deletions rapids-cmake/cmake/support_conda_env.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Result Targets
`target_name` target will be created only if called from a conda environment.

#]=======================================================================]
# cmake-lint: disable=R0912,R0915
# cmake-lint: disable=R0912,R0915,W0106
function(rapids_cmake_support_conda_env target)
list(APPEND CMAKE_MESSAGE_CONTEXT "rapids.cmake.support_conda_env")

Expand All @@ -76,12 +76,50 @@ function(rapids_cmake_support_conda_env target)

if(in_conda_build OR in_conda_prefix)

# comment needed here due to cmake-lint bug
macro(modify_cmake_prefix_path_global)
cmake_parse_arguments(_RAPIDS "" "" "PATHS" ${ARGN})

if(DEFINED ENV{CMAKE_PREFIX_PATH})
# If both CMAKE_PREFIX_PATH cmake and environment variables are populated, ensure the
# environment variable's paths are preserved in the cmake variable
cmake_path(CONVERT "$ENV{CMAKE_PREFIX_PATH}" TO_CMAKE_PATH_LIST _paths NORMALIZE)
list(PREPEND _RAPIDS_PATHS ${_paths})
endif()

list(APPEND CMAKE_PREFIX_PATH ${_RAPIDS_PATHS})
list(REMOVE_DUPLICATES CMAKE_PREFIX_PATH)
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} PARENT_SCOPE)
message(VERBOSE "CMAKE_PREFIX_PATH set to: ${CMAKE_PREFIX_PATH}")
endmacro()

# comment needed here due to cmake-lint bug
macro(modify_cmake_prefix_path_envvar)
cmake_parse_arguments(_RAPIDS "" "" "PATHS" ${ARGN})
cmake_path(CONVERT "$ENV{CMAKE_PREFIX_PATH}" TO_CMAKE_PATH_LIST _paths NORMALIZE)
list(APPEND _paths ${_RAPIDS_PATHS})
list(REMOVE_DUPLICATES _paths)
cmake_path(CONVERT "${_paths}" TO_NATIVE_PATH_LIST _paths NORMALIZE)
# cmake-lint: disable=W0106
set(ENV{CMAKE_PREFIX_PATH} ${_paths})
# cmake-lint: disable=W0106
message(VERBOSE "ENV{CMAKE_PREFIX_PATH} set to: $ENV{CMAKE_PREFIX_PATH}")
endmacro()

# comment needed here due to cmake-lint bug
macro(modify_cmake_prefix_path)
if(DEFINED CMAKE_PREFIX_PATH)
modify_cmake_prefix_path_global(${ARGN})
else()
modify_cmake_prefix_path_envvar(${ARGN})
endif()
endmacro()

if(ARGV1 STREQUAL "MODIFY_PREFIX_PATH")
set(modify_prefix_path TRUE)
endif()

add_library(${target} INTERFACE)
set(prefix_paths)

if(in_conda_build)
# For conda-build we add the host conda environment prefix to the cmake search paths so that
Expand Down Expand Up @@ -116,12 +154,12 @@ function(rapids_cmake_support_conda_env target)
endif()

if(modify_prefix_path)
list(PREPEND CMAKE_PREFIX_PATH "$ENV{PREFIX}" "$ENV{BUILD_PREFIX}")
message(VERBOSE "Conda build detected")
set(prefix_paths "$ENV{PREFIX}" "$ENV{BUILD_PREFIX}")
if(DEFINED targetsDir)
list(PREPEND CMAKE_PREFIX_PATH "$ENV{PREFIX}/${targetsDir}")
list(PREPEND prefix_paths "$ENV{PREFIX}/${targetsDir}")
endif()
set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}" PARENT_SCOPE)
message(VERBOSE "Conda build detected, CMAKE_PREFIX_PATH set to: ${CMAKE_PREFIX_PATH}")
modify_cmake_prefix_path(PATHS ${prefix_paths})
endif()

elseif(in_conda_prefix)
Expand All @@ -134,10 +172,8 @@ function(rapids_cmake_support_conda_env target)
endif()

if(modify_prefix_path)
list(PREPEND CMAKE_PREFIX_PATH "$ENV{CONDA_PREFIX}")
set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}" PARENT_SCOPE)
message(VERBOSE
"Conda environment detected, CMAKE_PREFIX_PATH set to: ${CMAKE_PREFIX_PATH}")
message(VERBOSE "Conda environment detected")
modify_cmake_prefix_path(PATHS "$ENV{CONDA_PREFIX}")
endif()
endif()
endif()
Expand Down
4 changes: 4 additions & 0 deletions testing/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ add_cmake_config_test( build_type-multiple.cmake )
add_cmake_config_test( build_type-user-specified.cmake)

add_cmake_config_test( conda_env-build.cmake )
add_cmake_config_test( conda_env-build-envvar.cmake )
add_cmake_config_test( conda_env-cross-build-arm.cmake )
add_cmake_config_test( conda_env-cross-build-arm-envvar.cmake )
add_cmake_config_test( conda_env-cross-build-x86.cmake )
add_cmake_config_test( conda_env-cross-build-x86-envvar.cmake )
add_cmake_config_test( conda_env-invalid.cmake )
add_cmake_config_test( conda_env-prefix.cmake )
add_cmake_config_test( conda_env-prefix-envvar.cmake )

add_cmake_config_test( install_lib_dir-after-gnuinstalldir-include.cmake )
add_cmake_config_test( install_lib_dir-build.cmake )
Expand Down
96 changes: 96 additions & 0 deletions testing/cmake/conda_env-build-envvar.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#=============================================================================
# Copyright (c) 2023, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================
include(${rapids-cmake-dir}/cmake/support_conda_env.cmake)


set(ENV{CONDA_BUILD} "1")
set(ENV{BUILD_PREFIX} "/usr/local/build_prefix")
set(ENV{PREFIX} "/opt/local/prefix")
set(ENV{CONDA_PREFIX} "/opt/conda/prefix")

rapids_cmake_support_conda_env(conda_env)
if(NOT TARGET conda_env)
message(FATAL_ERROR "Expected target conda_env to exist")
endif()

get_target_property(include_dirs conda_env INTERFACE_INCLUDE_DIRECTORIES)
if( NOT "$ENV{BUILD_PREFIX}/include" IN_LIST include_dirs)
message(FATAL_ERROR "Expected env{BUILD_PREFIX} to be in the include dirs of `conda_env`")
endif()
if( NOT "$ENV{PREFIX}/include" IN_LIST include_dirs)
message(FATAL_ERROR "Expected env{PREFIX} to be in the include dirs of `conda_env`")
endif()
if("$ENV{CONDA_PREFIX}/include" IN_LIST include_dirs)
message(FATAL_ERROR "Not expected for env{CONDA_PREFIX} to be in the include dirs of `conda_env`")
endif()

get_target_property(link_dirs conda_env INTERFACE_LINK_DIRECTORIES)
if( NOT "$ENV{BUILD_PREFIX}/lib" IN_LIST link_dirs)
message(FATAL_ERROR "Expected env{BUILD_PREFIX} to be in the link dirs of `conda_env`")
endif()
if( NOT "$ENV{PREFIX}/lib" IN_LIST link_dirs)
message(FATAL_ERROR "Expected env{PREFIX} to be in the link dirs of `conda_env`")
endif()
if("$ENV{CONDA_PREFIX}/lib" IN_LIST link_dirs)
message(FATAL_ERROR "Not expected for env{CONDA_PREFIX} to be in the link dirs of `conda_env`")
endif()

get_target_property(link_options conda_env INTERFACE_LINK_OPTIONS)
message(STATUS "link_options: ${link_options}")
if( NOT "$<HOST_LINK:SHELL:LINKER:-rpath-link=$ENV{BUILD_PREFIX}/lib>" IN_LIST link_options)
message(FATAL_ERROR "Expected rpath-link=env{BUILD_PREFIX} to be in the link options of `conda_env`")
endif()
if( NOT "$<HOST_LINK:SHELL:LINKER:-rpath-link=$ENV{PREFIX}/lib>" IN_LIST link_options)
message(FATAL_ERROR "Expected rpath-link=env{PREFIX} to be in the link options of `conda_env`")
endif()
if("$<HOST_LINK:SHELL:LINKER:-rpath-link=$ENV{CONDA_PREFIX}/lib>" IN_LIST link_options)
message(FATAL_ERROR "Not expected for rpath-link=env{CONDA_PREFIX} to be in the link options of `conda_env`")
endif()

# No effect as the target already exists
set(before_call_value "$ENV{CMAKE_PREFIX_PATH}" )
rapids_cmake_support_conda_env(conda_env MODIFY_PREFIX_PATH)
if(NOT ("${before_call_value}" STREQUAL "$ENV{CMAKE_PREFIX_PATH}") )
message(FATAL_ERROR "Expected rapids_cmake_support_conda_env not to change ENV{CMAKE_PREFIX_PATH}")
endif()

# New target being used, so this should modify ENV{CMAKE_PREFIX_PATH}
set(ENV{CMAKE_PREFIX_PATH} "placeholder" )
rapids_cmake_support_conda_env(conda_env_modify MODIFY_PREFIX_PATH)
if(NOT TARGET conda_env_modify)
message(FATAL_ERROR "Expected target conda_env_modify to exist")
endif()

cmake_path(CONVERT "$ENV{CMAKE_PREFIX_PATH}" TO_CMAKE_PATH_LIST env_cmake_prefix_path NORMALIZE)

list(LENGTH env_cmake_prefix_path len)
if( len GREATER 3)
message(FATAL_ERROR "ENV{CMAKE_PREFIX_PATH} length is wrong after MODIFY_PREFIX_PATH")
endif()

list(GET env_cmake_prefix_path 0 first_value)
list(GET env_cmake_prefix_path 1 second_value)
list(GET env_cmake_prefix_path 2 third_value)
set(correct_list "placeholder" "$ENV{PREFIX}" "$ENV{BUILD_PREFIX}")
set(actual_list "${first_value}" "${second_value}" "${third_value}")

foreach(correct actual IN ZIP_LISTS correct_list actual_list)
if(NOT correct STREQUAL actual)
message(STATUS "correct: ${correct}")
message(STATUS "actual: ${actual}")
message(FATAL_ERROR "MODIFY_PREFIX_PATH failed")
endif()
endforeach()
11 changes: 7 additions & 4 deletions testing/cmake/conda_env-build.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021, NVIDIA CORPORATION.
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -69,21 +69,24 @@ endif()

# New target being used, so this should modify CMAKE_PREFIX_PATH
set(CMAKE_PREFIX_PATH "placeholder" )
set(ENV{CMAKE_PREFIX_PATH} "env_1:env_2" )
rapids_cmake_support_conda_env(conda_env_modify MODIFY_PREFIX_PATH)
if(NOT TARGET conda_env_modify)
message(FATAL_ERROR "Expected target conda_env_modify to exist")
endif()

list(LENGTH CMAKE_PREFIX_PATH len)
if( len GREATER 3)
if( len GREATER 5)
message(FATAL_ERROR "CMAKE_PREFIX_PATH length is wrong after MODIFY_PREFIX_PATH")
endif()

list(GET CMAKE_PREFIX_PATH 0 first_value)
list(GET CMAKE_PREFIX_PATH 1 second_value)
list(GET CMAKE_PREFIX_PATH 2 third_value)
set(correct_list "$ENV{PREFIX}" "$ENV{BUILD_PREFIX}" "placeholder")
set(actual_list "${first_value}" "${second_value}" "${third_value}")
list(GET CMAKE_PREFIX_PATH 3 fourth_value)
list(GET CMAKE_PREFIX_PATH 4 fifth_value)
set(correct_list "placeholder" "env_1" "env_2" "$ENV{PREFIX}" "$ENV{BUILD_PREFIX}")
set(actual_list "${first_value}" "${second_value}" "${third_value}" "${fourth_value}" "${fifth_value}")

foreach(correct actual IN ZIP_LISTS correct_list actual_list)
if(NOT correct STREQUAL actual)
Expand Down
100 changes: 100 additions & 0 deletions testing/cmake/conda_env-cross-build-arm-envvar.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#=============================================================================
# Copyright (c) 2023, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================
include(${rapids-cmake-dir}/cmake/support_conda_env.cmake)


set(ENV{CONDA_BUILD} "1")
set(ENV{BUILD_PREFIX} "/usr/local/build_prefix")
set(ENV{cross_target_platform} "linux-aarch64")
set(ENV{PREFIX} "/opt/local/prefix")
set(ENV{CONDA_PREFIX} "/opt/conda/prefix")

rapids_cmake_support_conda_env(conda_env)
if(NOT TARGET conda_env)
message(FATAL_ERROR "Expected target conda_env to exist")
endif()

get_target_property(include_dirs conda_env INTERFACE_INCLUDE_DIRECTORIES)
if( NOT "$ENV{BUILD_PREFIX}/include" IN_LIST include_dirs)
message(FATAL_ERROR "Expected env{BUILD_PREFIX} to be in the include dirs of `conda_env`")
endif()
if( NOT "$ENV{PREFIX}/include" IN_LIST include_dirs)
message(FATAL_ERROR "Expected env{PREFIX} to be in the include dirs of `conda_env`")
endif()
if("$ENV{CONDA_PREFIX}/include" IN_LIST include_dirs)
message(FATAL_ERROR "Not expected for env{CONDA_PREFIX} to be in the include dirs of `conda_env`")
endif()

get_target_property(link_dirs conda_env INTERFACE_LINK_DIRECTORIES)
if( NOT "$ENV{BUILD_PREFIX}/lib" IN_LIST link_dirs)
message(FATAL_ERROR "Expected env{BUILD_PREFIX} to be in the link dirs of `conda_env`")
endif()
if( NOT "$ENV{PREFIX}/lib" IN_LIST link_dirs)
message(FATAL_ERROR "Expected env{PREFIX} to be in the link dirs of `conda_env`")
endif()
if("$ENV{CONDA_PREFIX}/lib" IN_LIST link_dirs)
message(FATAL_ERROR "Not expected for env{CONDA_PREFIX} to be in the link dirs of `conda_env`")
endif()

get_target_property(link_options conda_env INTERFACE_LINK_OPTIONS)
message(STATUS "link_options: ${link_options}")
if( NOT "$<HOST_LINK:SHELL:LINKER:-rpath-link=$ENV{BUILD_PREFIX}/lib>" IN_LIST link_options)
message(FATAL_ERROR "Expected rpath-link=env{BUILD_PREFIX} to be in the link options of `conda_env`")
endif()
if( NOT "$<HOST_LINK:SHELL:LINKER:-rpath-link=$ENV{PREFIX}/lib>" IN_LIST link_options)
message(FATAL_ERROR "Expected rpath-link=env{PREFIX} to be in the link options of `conda_env`")
endif()
if( NOT "$<HOST_LINK:SHELL:LINKER:-rpath-link=$ENV{PREFIX}/targets/sbsa-linux/lib>" IN_LIST link_options)
message(FATAL_ERROR "Expected rpath-link=env{PREFIX}/targets/sbsa-linux/ to be in the link options of `conda_env`")
endif()
if("$<HOST_LINK:SHELL:LINKER:-rpath-link=$ENV{CONDA_PREFIX}/lib>" IN_LIST link_options)
message(FATAL_ERROR "Not expected for rpath-link=env{CONDA_PREFIX} to be in the link options of `conda_env`")
endif()

# No effect as the target already exists
set(before_call_value "$ENV{CMAKE_PREFIX_PATH}" )
rapids_cmake_support_conda_env(conda_env MODIFY_PREFIX_PATH)
if(NOT ("${before_call_value}" STREQUAL "$ENV{CMAKE_PREFIX_PATH}") )
message(FATAL_ERROR "Expected rapids_cmake_support_conda_env not to change ENV{CMAKE_PREFIX_PATH}")
endif()

# New target being used, so this should modify ENV{CMAKE_PREFIX_PATH}
set(ENV{CMAKE_PREFIX_PATH} "placeholder" )
rapids_cmake_support_conda_env(conda_env_modify MODIFY_PREFIX_PATH)
if(NOT TARGET conda_env_modify)
message(FATAL_ERROR "Expected target conda_env_modify to exist")
endif()

cmake_path(CONVERT "$ENV{CMAKE_PREFIX_PATH}" TO_CMAKE_PATH_LIST env_cmake_prefix_path NORMALIZE)

list(LENGTH env_cmake_prefix_path len)
if( len GREATER 4)
message(FATAL_ERROR "ENV{CMAKE_PREFIX_PATH} length is wrong after MODIFY_PREFIX_PATH")
endif()

list(GET env_cmake_prefix_path 0 first_value)
list(GET env_cmake_prefix_path 1 second_value)
list(GET env_cmake_prefix_path 2 third_value)
list(GET env_cmake_prefix_path 3 fourth_value)
set(correct_list "placeholder" "$ENV{PREFIX}/targets/sbsa-linux" "$ENV{PREFIX}" "$ENV{BUILD_PREFIX}")
set(actual_list "${first_value}" "${second_value}" "${third_value}" "${fourth_value}")
foreach(correct actual IN ZIP_LISTS correct_list actual_list)
if(NOT correct STREQUAL actual)
message(STATUS "correct: ${correct}")
message(STATUS "actual: ${actual}")
message(FATAL_ERROR "MODIFY_PREFIX_PATH failed")
endif()
endforeach()
9 changes: 6 additions & 3 deletions testing/cmake/conda_env-cross-build-arm.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,25 @@ endif()

# New target being used, so this should modify CMAKE_PREFIX_PATH
set(CMAKE_PREFIX_PATH "placeholder" )
set(ENV{CMAKE_PREFIX_PATH} "env_1:env_2" )
rapids_cmake_support_conda_env(conda_env_modify MODIFY_PREFIX_PATH)
if(NOT TARGET conda_env_modify)
message(FATAL_ERROR "Expected target conda_env_modify to exist")
endif()

list(LENGTH CMAKE_PREFIX_PATH len)
if( len GREATER 4)
if( len GREATER 6)
message(FATAL_ERROR "CMAKE_PREFIX_PATH length is wrong after MODIFY_PREFIX_PATH")
endif()

list(GET CMAKE_PREFIX_PATH 0 first_value)
list(GET CMAKE_PREFIX_PATH 1 second_value)
list(GET CMAKE_PREFIX_PATH 2 third_value)
list(GET CMAKE_PREFIX_PATH 3 fourth_value)
set(correct_list "$ENV{PREFIX}/targets/sbsa-linux" "$ENV{PREFIX}" "$ENV{BUILD_PREFIX}" "placeholder")
set(actual_list "${first_value}" "${second_value}" "${third_value}" "${fourth_value}")
list(GET CMAKE_PREFIX_PATH 4 fifth_value)
list(GET CMAKE_PREFIX_PATH 5 sixth_value)
set(correct_list "placeholder" "env_1" "env_2" "$ENV{PREFIX}/targets/sbsa-linux" "$ENV{PREFIX}" "$ENV{BUILD_PREFIX}")
set(actual_list "${first_value}" "${second_value}" "${third_value}" "${fourth_value}" "${fifth_value}" "${sixth_value}")
foreach(correct actual IN ZIP_LISTS correct_list actual_list)
if(NOT correct STREQUAL actual)
message(STATUS "correct: ${correct}")
Expand Down
Loading