|
| 1 | +# ============================================================================= |
| 2 | +# cmake-format: off |
| 3 | +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# cmake-format: on |
| 6 | +# ============================================================================= |
| 7 | + |
| 8 | +cmake_minimum_required(VERSION 3.30.4 FATAL_ERROR) |
| 9 | + |
| 10 | +# When running under rapids-cmake testing infrastructure, rapids-cmake-dir is already set and we |
| 11 | +# should use the local version instead of downloading RAPIDS.cmake |
| 12 | +if(NOT DEFINED rapids-cmake-dir) |
| 13 | + if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/RAPIDS.cmake) |
| 14 | + file(DOWNLOAD https://raw.githubusercontent.com/rapidsai/rapids-cmake/main/RAPIDS.cmake |
| 15 | + ${CMAKE_CURRENT_BINARY_DIR}/RAPIDS.cmake) |
| 16 | + endif() |
| 17 | + set(rapids-cmake-branch main) |
| 18 | + include(${CMAKE_CURRENT_BINARY_DIR}/RAPIDS.cmake) |
| 19 | +else() |
| 20 | + # Test mode: Set up CMAKE_MODULE_PATH to use local rapids-cmake |
| 21 | + if(NOT "${rapids-cmake-dir}" IN_LIST CMAKE_MODULE_PATH) |
| 22 | + list(APPEND CMAKE_MODULE_PATH "${rapids-cmake-dir}") |
| 23 | + endif() |
| 24 | +endif() |
| 25 | + |
| 26 | +include(rapids-cmake) |
| 27 | +include(rapids-export) |
| 28 | +include(rapids-find) |
| 29 | + |
| 30 | +project(find_generate_module_example VERSION 1.0 LANGUAGES CXX) |
| 31 | + |
| 32 | +rapids_cmake_build_type(Release) |
| 33 | + |
| 34 | +# ################################################################################################## |
| 35 | +# WHY use rapids_find_generate_module? |
| 36 | +# ################################################################################################## |
| 37 | +# |
| 38 | +# Problem: Your library depends on a third-party package (e.g., "SimpleLib") that: 1. Doesn't |
| 39 | +# provide a CMake config file (simplelib-config.cmake) 2. Doesn't have a standard |
| 40 | +# FindSimpleLib.cmake module |
| 41 | +# |
| 42 | +# Solution: Generate a FindSimpleLib.cmake module that: 1. Searches for the package's headers and |
| 43 | +# libraries 2. Creates proper CMake targets 3. Is INSTALLED with your package so downstream users |
| 44 | +# can find SimpleLib too |
| 45 | +# |
| 46 | +# why: - Without this, downstream projects can't use find_package(YourPackage) because they can't |
| 47 | +# find SimpleLib - The generated FindModule is included in your package's export sets - Downstream |
| 48 | +# projects automatically get the FindModule when they find_package(YourPackage) |
| 49 | +# |
| 50 | +# ################################################################################################## |
| 51 | + |
| 52 | +# For this example, we'll use ZLIB which DOES have a FindModule, but we'll pretend it doesn't to |
| 53 | +# demonstrate the pattern. |
| 54 | + |
| 55 | +# STEP 1: Generate the FindModule |
| 56 | +# ================================ |
| 57 | +# This creates FindMyZLIB.cmake and adds it to export sets |
| 58 | +rapids_find_generate_module(MyZLIB |
| 59 | + HEADER_NAMES zlib.h # Required: headers to search for |
| 60 | + LIBRARY_NAMES z # Optional: libraries to search for |
| 61 | + INCLUDE_SUFFIXES include # Optional: extra search paths |
| 62 | + BUILD_EXPORT_SET example_exports # Add to build export |
| 63 | + INSTALL_EXPORT_SET example_exports # Add to install export |
| 64 | +) |
| 65 | + |
| 66 | +# STEP 2: Find the package using the generated module |
| 67 | +# ==================================================== |
| 68 | +# This uses the FindMyZLIB.cmake we just generated |
| 69 | +rapids_find_package(MyZLIB REQUIRED BUILD_EXPORT_SET example_exports |
| 70 | + INSTALL_EXPORT_SET example_exports) |
| 71 | + |
| 72 | +# STEP 3: Create your library that uses the dependency |
| 73 | +# ===================================================== |
| 74 | +add_library(example_lib lib.cpp) |
| 75 | + |
| 76 | +set_target_properties(example_lib |
| 77 | + PROPERTIES CXX_STANDARD 17 |
| 78 | + CXX_STANDARD_REQUIRED ON |
| 79 | + POSITION_INDEPENDENT_CODE ON |
| 80 | + VERSION ${PROJECT_VERSION} |
| 81 | + SOVERSION ${PROJECT_VERSION_MAJOR}) |
| 82 | + |
| 83 | +# Link against the found package |
| 84 | +target_link_libraries(example_lib PUBLIC MyZLIB::MyZLIB) |
| 85 | + |
| 86 | +# STEP 4: Install the library and headers |
| 87 | +# ======================================== |
| 88 | +include(GNUInstallDirs) |
| 89 | + |
| 90 | +install(TARGETS example_lib EXPORT example_exports LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 91 | + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) |
| 92 | + |
| 93 | +# STEP 5: Export the package (FindMyZLIB.cmake is automatically included!) |
| 94 | +# ========================================================================= |
| 95 | +set(doc_string |
| 96 | + [=[ |
| 97 | +Provide targets for the find_generate_module_example library. |
| 98 | +
|
| 99 | +This example demonstrates why rapids_find_generate_module is important: |
| 100 | +
|
| 101 | +The example_lib library depends on MyZLIB. When downstream projects call: |
| 102 | + find_package(find_generate_module_example) |
| 103 | +
|
| 104 | +They automatically get: |
| 105 | +1. The FindMyZLIB.cmake module (installed with this package) |
| 106 | +2. Ability to find MyZLIB using the generated module |
| 107 | +3. Proper transitive dependencies set up |
| 108 | +
|
| 109 | +WITHOUT rapids_find_generate_module: |
| 110 | +- Downstream projects would fail to configure |
| 111 | +- They couldn't find MyZLIB dependency |
| 112 | +- No way to use example_lib |
| 113 | +
|
| 114 | +WITH rapids_find_generate_module: |
| 115 | +- FindMyZLIB.cmake is installed alongside your package config |
| 116 | +- Downstream projects automatically find MyZLIB |
| 117 | +- Everything "just works" |
| 118 | +]=]) |
| 119 | + |
| 120 | +# Create install export (FindMyZLIB.cmake is included in export set) |
| 121 | +rapids_export(INSTALL find_generate_module_example |
| 122 | + EXPORT_SET example_exports |
| 123 | + GLOBAL_TARGETS example_lib |
| 124 | + NAMESPACE example:: |
| 125 | + DOCUMENTATION doc_string) |
| 126 | + |
| 127 | +# Create build export |
| 128 | +rapids_export(BUILD find_generate_module_example EXPORT_SET example_exports |
| 129 | + GLOBAL_TARGETS example_lib NAMESPACE example::) |
0 commit comments