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

rapids_find_generate_module Support user code blocks #415

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
55 changes: 54 additions & 1 deletion rapids-cmake/find/generate_module.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Generate a Find*.cmake module for the requested package
[INCLUDE_SUFFIXES <suffixes...>]
[VERSION <version>]
[NO_CONFIG]
[INITIAL_CODE_BLOCK <code_block_variable>]
[FINAL_CODE_BLOCK <code_block_variable>]
[BUILD_EXPORT_SET <name>]
[INSTALL_EXPORT_SET <name>]
)
Expand Down Expand Up @@ -87,6 +89,20 @@ when installed.
When provided will stop the generated Find Module from
first searching for the projects shipped Find Config.

``INITIAL_CODE_BLOCK``
Optional value of the variable that holds a string of code that will
be executed as the first step of this config file.

Note: This requires the code block variable instead of the contents
so that we can properly insert CMake code

``FINAL_CODE_BLOCK``
Optional value of the variable that holds a string of code that will
be executed as the last step of this config file.

Note: This requires the code block variable instead of the contents
so that we can properly insert CMake code

``BUILD_EXPORT_SET``
Record that this custom FindPackage module needs to be part
of our build directory export set. This means that it will be
Expand All @@ -103,12 +119,34 @@ Result Variables
:cmake:variable:`CMAKE_MODULE_PATH` will be modified to include the
folder where `Find<PackageName>.cmake` is located.


Example on how to properly use :cmake:command:`rapids_find_generate_module`:

.. code-block:: cmake

...

rapids_find_generate_module(
RDKAFKA
HEADER_NAMES rdkafkacpp.h
LIBRARY_NAMES rdkafka++
BUILD_EXPORT_SET consumer-exports
INSTALL_EXPORT_SET consumer-exports
vyasr marked this conversation as resolved.
Show resolved Hide resolved
)
rapids_find_package(
RDKAFKA REQUIRED
BUILD_EXPORT_SET consumer-exports
INSTALL_EXPORT_SET consumer-exports
)


#]=======================================================================]
# cmake-lint: disable=R0915
function(rapids_find_generate_module name)
list(APPEND CMAKE_MESSAGE_CONTEXT "rapids.find.generate_module")

set(options NO_CONFIG)
set(one_value VERSION BUILD_EXPORT_SET INSTALL_EXPORT_SET)
set(one_value VERSION BUILD_EXPORT_SET INSTALL_EXPORT_SET INITIAL_CODE_BLOCK FINAL_CODE_BLOCK)
set(multi_value HEADER_NAMES LIBRARY_NAMES INCLUDE_SUFFIXES)
cmake_parse_arguments(_RAPIDS "${options}" "${one_value}" "${multi_value}" ${ARGN})

Expand Down Expand Up @@ -147,6 +185,21 @@ function(rapids_find_generate_module name)
endif()
endif()

if(DEFINED _RAPIDS_INITIAL_CODE_BLOCK)
if(NOT DEFINED ${_RAPIDS_INITIAL_CODE_BLOCK})
message(FATAL_ERROR "INITIAL_CODE_BLOCK variable `${_RAPIDS_INITIAL_CODE_BLOCK}` doesn't exist"
)
endif()
set(_RAPIDS_FIND_INITIAL_CODE_BLOCK "${${_RAPIDS_INITIAL_CODE_BLOCK}}")
endif()

if(DEFINED _RAPIDS_FINAL_CODE_BLOCK)
if(NOT DEFINED ${_RAPIDS_FINAL_CODE_BLOCK})
message(FATAL_ERROR "FINAL_CODE_BLOCK variable `${_RAPIDS_FINAL_CODE_BLOCK}` doesn't exist")
endif()
set(_RAPIDS_FIND_FINAL_CODE_BLOCK "${${_RAPIDS_FINAL_CODE_BLOCK}}")
endif()

# Need to generate the module
configure_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/template/find_module.cmake.in"
"${CMAKE_BINARY_DIR}/cmake/find_modules/Find${name}.cmake" @ONLY)
Expand Down
5 changes: 5 additions & 0 deletions rapids-cmake/find/template/find_module.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ This module will set the following variables in your project:
#]=======================================================================]

# Prefer using a Config module if it exists for this project

@_RAPIDS_FIND_INITIAL_CODE_BLOCK@

set(@_RAPIDS_PKG_NAME@_NO_CONFIG @_RAPIDS_NO_CONFIG@)
if(NOT @_RAPIDS_PKG_NAME@_NO_CONFIG)
find_package(@_RAPIDS_PKG_NAME@ CONFIG QUIET)
Expand Down Expand Up @@ -113,5 +116,7 @@ if(@_RAPIDS_PKG_NAME@_FOUND)
endif()
endif()

@_RAPIDS_FIND_FINAL_CODE_BLOCK@

unset(@_RAPIDS_PKG_NAME@_NO_CONFIG)
unset(@_RAPIDS_PKG_NAME@_IS_HEADER_ONLY)
6 changes: 5 additions & 1 deletion testing/find/CMakeLists.txt
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 @@ -40,3 +40,7 @@ add_cmake_config_test( find_package-version-required-explicit-failed.cmake SHOUL
add_cmake_config_test( find_generate_module-build )
add_cmake_config_test( find_generate_module-install )
add_cmake_config_test( find_generate_module-header-only )

add_cmake_config_test( find_generate_module-code-blocks )
add_cmake_config_test( find_generate_module-back-initial-code-block-var.cmake SHOULD_FAIL "INITIAL_CODE_BLOCK variable `var_doesn't_exist` doesn't exist")
add_cmake_config_test( find_generate_module-back-final-code-block-var.cmake SHOULD_FAIL "FINAL_CODE_BLOCK variable `var_doesn't_exist` doesn't exist")
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#=============================================================================
# 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}/find/generate_module.cmake)

rapids_find_generate_module( RapidsTest
HEADER_NAMES rapids-cmake-test-header_only.hpp
FINAL_CODE_BLOCK var_doesn't_exist
INSTALL_EXPORT_SET test_set
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#=============================================================================
# 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}/find/generate_module.cmake)

rapids_find_generate_module( RapidsTest
HEADER_NAMES rapids-cmake-test-header_only.hpp
INITIAL_CODE_BLOCK var_doesn't_exist
INSTALL_EXPORT_SET test_set
)
40 changes: 40 additions & 0 deletions testing/find/find_generate_module-code-blocks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#=============================================================================
# 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}/find/generate_module.cmake)

cmake_minimum_required(VERSION 3.23.1)
project(rapids-test-project LANGUAGES CXX)

set(CMAKE_PREFIX_PATH "${CMAKE_CURRENT_SOURCE_DIR}")

# Generate the find module
set(initial_code_block [[set(RapidsTest_INITIAL_CODE_BLOCK TRUE)]])
set(final_code_block [[set(RapidsTest_FINAL_CODE_BLOCK TRUE)]])
rapids_find_generate_module( RapidsTest
HEADER_NAMES rapids-cmake-test-header_only.hpp
INITIAL_CODE_BLOCK initial_code_block
FINAL_CODE_BLOCK final_code_block
INSTALL_EXPORT_SET test_set
)

find_package(RapidsTest REQUIRED)

if(NOT RapidsTest_INITIAL_CODE_BLOCK)
message(FATAL_ERROR "RapidsTest_FOUND should be set to TRUE")
endif()
if(NOT RapidsTest_FINAL_CODE_BLOCK)
message(FATAL_ERROR "RAPIDSTEST_FOUND should be set to TRUE")
endif()