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

Add ability to specify library directories for target rpaths #295

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
502f7b3
Initial version of rpath setting with new lib dirs.
vyasr Oct 25, 2022
f4882a9
Add new function to default includes.
vyasr Oct 25, 2022
ddd52ce
Move file to match function name.
vyasr Oct 25, 2022
95a24f5
Remove init check.
vyasr Oct 25, 2022
f91c4df
Properly propagate value up to parent scope.
vyasr Oct 25, 2022
ea5d8c4
Various bug fixes.
vyasr Oct 25, 2022
157eb7a
make sure paths are relative to origin and fix separator.
vyasr Oct 25, 2022
2fa5569
Remove message.
vyasr Oct 25, 2022
71836fc
Add docs file.
vyasr Oct 25, 2022
1433cdc
Fix typos in error messages.
vyasr Oct 25, 2022
24f403e
Add a test.
vyasr Oct 25, 2022
a44285b
Fix test and add comments.
vyasr Oct 25, 2022
cd76481
Update api.rst.
vyasr Oct 25, 2022
ba8ee68
Update format file.
vyasr Oct 25, 2022
ee033b9
Apply formatting.
vyasr Oct 25, 2022
a10879c
Add new approach add_rpath_entries with tests.
vyasr Oct 26, 2022
ba6ecd1
Update docs and format files.
vyasr Oct 26, 2022
81c4de6
Remove set_lib_dirs.
vyasr Oct 26, 2022
ba59734
Fix style.
vyasr Oct 26, 2022
5495b0b
Update docs a bit.
vyasr Oct 26, 2022
fe7d474
Remove set_lib_dirs from api.rst.
vyasr Oct 26, 2022
420fbb1
Fix path to file in docs.
vyasr Oct 26, 2022
2f3d976
Append to global property rather than overwriting.
vyasr Oct 26, 2022
b1ad4a8
Fix bugs that arise when the target is not created in the same direct…
vyasr Oct 26, 2022
5e9cca8
Fix style.
vyasr Oct 26, 2022
fd3f66f
Use set_property(TARGET) to enable appending.
vyasr Oct 27, 2022
f455f09
Fix path handling.
vyasr Nov 1, 2022
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
10 changes: 10 additions & 0 deletions cmake-format-rapids-cmake.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@
"LINKED_LIBRARIES": "*",
"INSTALL_DIR": "1"
}
},
"rapids_cython_add_rpath_entries": {
"pargs": {
"nargs": "0"
},
"kwargs": {
"PATHS": "+",
"TARGET": "1",
"ROOT_DIRECTORY": "1"
}
}

}
Expand Down
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ The `rapids_cython` functions allow projects to easily build cython modules usin

/command/rapids_cython_init
/command/rapids_cython_create_modules
/command/rapids_cython_add_rpath_entries


Find
Expand Down
1 change: 1 addition & 0 deletions docs/command/rapids_cython_add_rpath_entries.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. cmake-module:: ../../rapids-cmake/cython/add_rpath_entries.cmake
89 changes: 89 additions & 0 deletions rapids-cmake/cython/add_rpath_entries.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# =============================================================================
# Copyright (c) 2022, 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_guard(GLOBAL)

#[=======================================================================[.rst:
rapids_cython_add_rpath_entries
-------------------------------

.. versionadded:: v22.12.00

Set the RPATH entries for all targets associated with a provided associated target.

.. code-block:: cmake

rapids_cython_add_rpath_entries(
TARGET <associated_target>
PATHS <path1> <path2> ...
[ROOT_DIRECTORY <root-dir>]
)

This function will affect all targets created up to the point of this call. It
will have no effect on targets created afterwards.

``TARGET``
The associated target for which we are setting RPATH entries. Any target
created using :code:command:`rapids_cython_create_modules` with the argument
`ASSOCIATED_TARGET associated_target` will have its RPATH entries updated.

``PATHS``
The paths to add to the RPATH. Paths may either be absolute or relative to
the ROOT_DIRECTORY. The paths are always converted to be relative to the
current directory i.e relative to $ORIGIN in the RPATH.

``ROOT_DIRECTORY``
The ROOT_DIRECTORY for the provided paths. Defaults to ${PROJECT_SOURCE_DIR}.
Has no effect on absolute paths. If the ROOT_DIRECTORY is a relative path, it
is assumed to be relative to the directory from which
`rapids_cython_add_rpath_entries` is called.

#]=======================================================================]
function(rapids_cython_add_rpath_entries)
list(APPEND CMAKE_MESSAGE_CONTEXT "rapids.cython.add_rpath_entries")

set(options)
set(one_value ROOT_DIRECTORY TARGET)
set(multi_value PATHS)
cmake_parse_arguments(_RAPIDS_CYTHON "${options}" "${one_value}" "${multi_value}" ${ARGN})

# By default paths are relative to the current project root.
if(NOT _RAPIDS_CYTHON_ROOT_DIRECTORY)
set(_RAPIDS_CYTHON_ROOT_DIRECTORY "${PROJECT_SOURCE_DIR}")
endif()

# Transform all paths to paths relative to the current directory.
set(cleaned_paths)
cmake_path(ABSOLUTE_PATH _RAPIDS_CYTHON_ROOT_DIRECTORY)
foreach(path IN LISTS _RAPIDS_CYTHON_PATHS)
if(NOT IS_ABSOLUTE path)
cmake_path(ABSOLUTE_PATH path BASE_DIRECTORY "${_RAPIDS_CYTHON_ROOT_DIRECTORY}")
endif()
list(APPEND cleaned_paths "${path}")
endforeach()

get_property(targets GLOBAL PROPERTY "rapids_cython_associations_${_RAPIDS_CYTHON_TARGET}")
foreach(target IN LISTS targets)
# Compute the path relative to the current target.
set(target_paths)
get_target_property(target_source_dir ${target} SOURCE_DIR)
foreach(target_path IN LISTS cleaned_paths)
cmake_path(RELATIVE_PATH target_path BASE_DIRECTORY "${target_source_dir}")
list(APPEND target_paths "$ORIGIN/${target_path}")
endforeach()
list(JOIN target_paths ";" target_paths)

set_property(TARGET ${target} APPEND PROPERTY INSTALL_RPATH "${target_paths}")
endforeach()
endfunction()
16 changes: 15 additions & 1 deletion rapids-cmake/cython/create_modules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ $ORIGIN.
useful when multiple Cython modules in different subpackages of the the same
project have the same name. The default prefix is the empty string.

``ASSOCIATED_TARGETS``
A list of targets that are associated with the Cython targets created in this
function. The target<-->associated target mapping is stored and may be
leveraged by the following functions:
- :code:command:`rapids_cython_add_rpath_entries` accepts a path for an
associated target and updates the RPATH of each target with which that
associated target is associated.

Result Variables
^^^^^^^^^^^^^^^^
:cmake:variable:`RAPIDS_CYTHON_CREATED_TARGETS` will be set to a list of
Expand All @@ -71,7 +79,7 @@ function(rapids_cython_create_modules)

set(_rapids_cython_options CXX)
set(_rapids_cython_one_value INSTALL_DIR MODULE_PREFIX)
set(_rapids_cython_multi_value SOURCE_FILES LINKED_LIBRARIES)
set(_rapids_cython_multi_value SOURCE_FILES LINKED_LIBRARIES ASSOCIATED_TARGETS)
cmake_parse_arguments(_RAPIDS_CYTHON "${_rapids_cython_options}" "${_rapids_cython_one_value}"
"${_rapids_cython_multi_value}" ${ARGN})

Expand Down Expand Up @@ -120,6 +128,12 @@ function(rapids_cython_create_modules)
# Default the INSTALL_RPATH for all modules to $ORIGIN.
set_target_properties(${cython_module} PROPERTIES INSTALL_RPATH "\$ORIGIN")

# Store any provided associated targets in a global list
foreach(associated_target IN LISTS _RAPIDS_CYTHON_ASSOCIATED_TARGETS)
set_property(GLOBAL PROPERTY "rapids_cython_associations_${associated_target}"
"${cython_module}" APPEND)
endforeach()

list(APPEND CREATED_TARGETS "${cython_module}")
endforeach()

Expand Down
1 change: 1 addition & 0 deletions rapids-cmake/rapids-cython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ include_guard(GLOBAL)

include(${CMAKE_CURRENT_LIST_DIR}/cython/init.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/cython/create_modules.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/cython/add_rpath_entries.cmake)
2 changes: 2 additions & 0 deletions testing/cython/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ add_cmake_config_test(create_modules_errors.cmake SHOULD_FAIL "You must call rap
add_cmake_config_test(create_modules)
add_cmake_config_test(create_modules_with_library)
add_cmake_config_test(create_modules_with_prefix)

add_cmake_config_test(add_rpath_entries)
61 changes: 61 additions & 0 deletions testing/cython/add_rpath_entries/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#=============================================================================
# Copyright (c) 2022, 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.
#=============================================================================

cmake_minimum_required(VERSION 3.20)

include(${rapids-cmake-dir}/cython/create_modules.cmake)
include(${rapids-cmake-dir}/cython/init.cmake)
include(${rapids-cmake-dir}/cython/add_rpath_entries.cmake)

project(rapids_cython-set_lib_dirs LANGUAGES C CXX)

# Silence warning about running without scikit-build.
set(SKBUILD ON)

# Ensure that scikit-build's CMake files are discoverable. The glob is to
# capture the current git commit hash.
file(GLOB skbuild_resource_dir LIST_DIRECTORIES ON "${CPM_SOURCE_CACHE}/skbuild/*/skbuild/resources/cmake")
LIST(APPEND CMAKE_MODULE_PATH "${skbuild_resource_dir}")

rapids_cython_init()

rapids_cython_create_modules(
SOURCE_FILES test.pyx
ASSOCIATED_TARGETS associated_target
)

# Test a relative path.
rapids_cython_add_rpath_entries(TARGET associated_target PATHS ../libraries)
get_target_property(rpath test INSTALL_RPATH)
if (NOT rpath STREQUAL "$ORIGIN;$ORIGIN/../libraries")
message(FATAL_ERROR "rapids_cython_set_lib_dirs failed to set the RPATH correctly.")
endif()

# Test a relative path with a root directory.
rapids_cython_add_rpath_entries(TARGET associated_target PATHS ../libraries ROOT_DIRECTORY ../)
get_target_property(rpath test INSTALL_RPATH)
if (NOT rpath STREQUAL "$ORIGIN;$ORIGIN/../libraries;$ORIGIN/../../libraries")
message(FATAL_ERROR "rapids_cython_set_lib_dirs failed to set the RPATH correctly.")
endif()

# Test an absolute path
rapids_cython_add_rpath_entries(TARGET associated_target PATHS /path/to/libraries)
get_target_property(rpath test INSTALL_RPATH)
set(the_path /path/to/libraries)
cmake_path(RELATIVE_PATH the_path)
if (NOT rpath STREQUAL "$ORIGIN;$ORIGIN/../libraries;$ORIGIN/../../libraries;$ORIGIN/${the_path}")
message(FATAL_ERROR "rapids_cython_set_lib_dirs failed to set the RPATH correctly.")
endif()
Empty file.
2 changes: 1 addition & 1 deletion testing/cython/create_modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ rapids_cython_create_modules(
)

if(NOT TARGET test)
message(FATAL_ERROR "rapids_cython_init didn't create the target `test`")
message(FATAL_ERROR "rapids_cython_create_modules didn't create the target `test`")
endif()
2 changes: 1 addition & 1 deletion testing/cython/create_modules_with_prefix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ rapids_cython_create_modules(
)

if(NOT TARGET test_test)
message(FATAL_ERROR "rapids_cython_init didn't create the prefixed library target `test_test`")
message(FATAL_ERROR "rapids_cython_create_modules didn't create the prefixed library target `test_test`")
endif()