|
| 1 | +cmake_minimum_required(VERSION 3.14...3.19) |
| 2 | + |
| 3 | +project( |
| 4 | + hello |
| 5 | + VERSION "0.1.0" |
| 6 | + LANGUAGES CXX) |
| 7 | + |
| 8 | +include(GNUInstallDirs) |
| 9 | + |
| 10 | +# define the C++ library "hello" |
| 11 | +add_library(hello SHARED "${PROJECT_SOURCE_DIR}/src/hello.cpp") |
| 12 | + |
| 13 | +target_include_directories( |
| 14 | + hello PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> |
| 15 | + $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) |
| 16 | + |
| 17 | +install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/" |
| 18 | + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) |
| 19 | + |
| 20 | +# Standard installation subdirs for the C++ library are used. The files will end |
| 21 | +# up in the specified subdirectories under the Python package root. For example, |
| 22 | +# "<python package prefix>/hello/lib/" if the destination is "lib/". |
| 23 | +# |
| 24 | +# Installing the objects in the package provides encapsulation and will become |
| 25 | +# important later for binary redistribution reasons. |
| 26 | +install( |
| 27 | + TARGETS hello |
| 28 | + EXPORT helloTargets |
| 29 | + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) |
| 30 | + |
| 31 | +# The CMake package config and target files are installed under the Python |
| 32 | +# package root. This is necessary to ensure that all the relative paths in the |
| 33 | +# helloTargets.cmake resolve correctly. It also provides encapsulation. |
| 34 | +# |
| 35 | +# The actual path used must be selected so that consuming projects can locate it |
| 36 | +# via `find_package`. To support finding CMake packages in the Python package |
| 37 | +# prefix, using `find_package`s default search path of |
| 38 | +# `<prefix>/<name>/share/<name>*/cmake/` is reasonable. Adding the Python |
| 39 | +# package installation prefix to CMAKE_PREFIX_PATH in combination with this path |
| 40 | +# will allow `find_package` to find this package and any other package installed |
| 41 | +# via a Python package if the CMake and Python packages are named the same. |
| 42 | +set(HELLO_CMAKE_PACKAGE_INSTALL_SUBDIR "share/hello/cmake") |
| 43 | + |
| 44 | +install( |
| 45 | + EXPORT helloTargets |
| 46 | + NAMESPACE hello:: |
| 47 | + DESTINATION ${HELLO_CMAKE_PACKAGE_INSTALL_SUBDIR}) |
| 48 | + |
| 49 | +include(CMakePackageConfigHelpers) |
| 50 | + |
| 51 | +write_basic_package_version_file( |
| 52 | + helloConfigVersion.cmake |
| 53 | + VERSION ${PROJECT_VERSION} |
| 54 | + COMPATIBILITY SameMinorVersion) |
| 55 | + |
| 56 | +configure_package_config_file( |
| 57 | + "${PROJECT_SOURCE_DIR}/cmake/helloConfig.cmake.in" helloConfig.cmake |
| 58 | + INSTALL_DESTINATION ${HELLO_CMAKE_PACKAGE_INSTALL_SUBDIR}) |
| 59 | + |
| 60 | +install(FILES "${PROJECT_BINARY_DIR}/helloConfig.cmake" |
| 61 | + "${PROJECT_BINARY_DIR}/helloConfigVersion.cmake" |
| 62 | + DESTINATION ${HELLO_CMAKE_PACKAGE_INSTALL_SUBDIR}) |
| 63 | + |
| 64 | +# We are using the SKBUILD variable, which is defined when scikit-build is |
| 65 | +# running the CMake build, to control building the Python wrapper. This allows |
| 66 | +# the C++ project to be installed, standalone, when using the standard CMake |
| 67 | +# build flow. |
| 68 | +if(DEFINED SKBUILD) |
| 69 | + |
| 70 | + # prevent an unused variable warning |
| 71 | + set(ignoreMe "${SKBUILD}") |
| 72 | + |
| 73 | + # call pybind11-config to obtain the root of the cmake package |
| 74 | + execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pybind11 --cmakedir |
| 75 | + OUTPUT_VARIABLE pybind11_ROOT_RAW) |
| 76 | + string(STRIP ${pybind11_ROOT_RAW} pybind11_ROOT) |
| 77 | + find_package(pybind11) |
| 78 | + |
| 79 | + pybind11_add_module(_hello MODULE |
| 80 | + "${PROJECT_SOURCE_DIR}/src/hello/hello_py.cpp") |
| 81 | + |
| 82 | + target_link_libraries(_hello PRIVATE hello) |
| 83 | + |
| 84 | + # Installing the extension module to the root of the package |
| 85 | + install(TARGETS _hello DESTINATION .) |
| 86 | + |
| 87 | + configure_file("${PROJECT_SOURCE_DIR}/src/hello/__main__.py.in" |
| 88 | + "${PROJECT_BINARY_DIR}/src/hello/__main__.py") |
| 89 | + |
| 90 | + install(FILES "${PROJECT_BINARY_DIR}/src/hello/__main__.py" DESTINATION .) |
| 91 | + |
| 92 | + # The extension module must load the hello library as a dependency when the |
| 93 | + # extension module is loaded. The easiest way to locate the hello library is |
| 94 | + # via RPATH. Absolute RPATHs are possible, but they make the resulting |
| 95 | + # binaries not redistributable to other Python installations (conda is broke, |
| 96 | + # wheel reuse is broke, and more!). |
| 97 | + # |
| 98 | + # Placing the hello library in the package and using relative RPATHs that |
| 99 | + # doesn't point outside of the package means that the built package is |
| 100 | + # relocatable. This allows for safe binary redistribution. |
| 101 | + if(APPLE) |
| 102 | + set_target_properties( |
| 103 | + _hello PROPERTIES INSTALL_RPATH "@loader_path/${CMAKE_INSTALL_LIBDIR}") |
| 104 | + else() |
| 105 | + set_target_properties(_hello PROPERTIES INSTALL_RPATH |
| 106 | + "$ORIGIN/${CMAKE_INSTALL_LIBDIR}") |
| 107 | + endif() |
| 108 | + |
| 109 | +endif() |
0 commit comments