Skip to content

Commit

Permalink
write_language now properly walks up the add_subdirectory call st…
Browse files Browse the repository at this point in the history
…ack (#671)

Instead of assuming the filesystem directory layout matches `add_subdirectory` calls, we now walk the CMake
`PARENT_DIRECTORY` property.

This allows us to handle more custom layouts of CMake projects

Authors:
  - Robert Maynard (https://github.com/robertmaynard)

Approvers:
  - Vyas Ramasubramani (https://github.com/vyasr)

URL: #671
  • Loading branch information
robertmaynard authored Aug 16, 2024
1 parent 1f26d58 commit 20bb29b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 31 deletions.
32 changes: 10 additions & 22 deletions rapids-cmake/export/write_language.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, 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 @@ -115,16 +115,14 @@ include("${CMAKE_BINARY_DIR}/cmake/PropagateCMake@[email protected]")
# - Each directory but the root needs to include `PropagateCMake@[email protected]`
# - Since the root directory doesn't have a parent it only needs to include
# `CMake@lang@Information`
set(rapids_directories "${CMAKE_CURRENT_SOURCE_DIR}")
get_directory_property(parent_dir DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" PARENT_DIRECTORY)
while(parent_dir)
list(APPEND rapids_directories "${parent_dir}")
get_directory_property(parent_dir DIRECTORY "${parent_dir}" PARENT_DIRECTORY)
endwhile()

set(rapids_directory "${CMAKE_CURRENT_SOURCE_DIR}")
if(DEFINED CMAKE_CURRENT_FUNCTION)
string(APPEND rapids_directory "/fake_dir")
endif()

set(rapids_root_directory "${CMAKE_SOURCE_DIR}")
cmake_path(GET rapids_directory PARENT_PATH rapids_directory)
while(NOT rapids_directory STREQUAL rapids_root_directory)

foreach(rapids_directory IN LISTS rapids_directories)
# Make sure we haven't already installed a language hook for this directory
# Once we found a directory with an existing hook we can safely stop
# as that means hooks exist from that point up in the graph
Expand All @@ -136,20 +134,10 @@ while(NOT rapids_directory STREQUAL rapids_root_directory)
else()
break()
endif()

cmake_path(GET rapids_directory PARENT_PATH rapids_directory)
endwhile()

# Make sure we haven't already installed a language hook for this directory
cmake_language(DEFER DIRECTORY "${CMAKE_SOURCE_DIR}" GET_CALL_IDS rapids_existing_calls)
if(NOT rapids_@lang@_hook IN_LIST rapids_existing_calls)
cmake_language(DEFER DIRECTORY "${CMAKE_SOURCE_DIR}"
ID rapids_@lang@_hook
CALL include "CMake@lang@Information")
endif()
endforeach()

unset(rapids_existing_calls)
unset(rapids_directory)
unset(rapids_directories)
unset(rapids_root_directory)
]=])

Expand Down
11 changes: 2 additions & 9 deletions testing/export/write_language-nested-dirs/A/B/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, 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 All @@ -16,13 +16,6 @@

include(${rapids-cmake-dir}/export/write_language.cmake)

function(enable_nested_language file_path)
#replicates CPM including the file
include("${file_path}")
endfunction()

set(path "${CMAKE_CURRENT_BINARY_DIR}/enable_cuda_language.cmake")
rapids_export_write_language(INSTALL CUDA "${path}")
enable_nested_language("${path}")
add_subdirectory(../C C)

add_library(B STATIC static.cu)
28 changes: 28 additions & 0 deletions testing/export/write_language-nested-dirs/A/C/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#=============================================================================
# Copyright (c) 2021-2024, 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}/export/write_language.cmake)

function(enable_nested_language file_path)
#replicates CPM including the file
include("${file_path}")
endfunction()

set(path "${CMAKE_CURRENT_BINARY_DIR}/enable_cuda_language.cmake")
rapids_export_write_language(INSTALL CUDA "${path}")
enable_nested_language("${path}")

add_library(C STATIC static.cu)
24 changes: 24 additions & 0 deletions testing/export/write_language-nested-dirs/A/C/static.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2021-2024, 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.
*/

static __global__ void example_cuda_kernel(int& r, int x, int y) { r = x * y + (x * 4 - (y / 2)); }

int static_launch_kernelC(int x, int y)
{
int r;
example_cuda_kernel<<<1, 1>>>(r, x, y);
return r;
}

0 comments on commit 20bb29b

Please sign in to comment.