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

cmake: Add install rules and option to use submodule dependencies #356

Merged
merged 3 commits into from
Nov 9, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 48 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ else()
set(NB_MASTER_PROJECT OFF)
endif()

option(NB_CREATE_INSTALL_RULES "Create installation rules" ${NB_MASTER_PROJECT})
option(NB_USE_SUBMODULE_DEPS "Use the nanobind dependencies shipped as a git submodule of this repository" ON)

option(NB_TEST "Compile nanobind tests?" ${NB_MASTER_PROJECT})
option(NB_TEST_STABLE_ABI "Test the stable ABI interface?" OFF)
option(NB_TEST_SHARED_BUILD "Build a shared nanobind library for the test suite?" OFF)
Expand All @@ -30,12 +33,56 @@ endif()
# Check whether all dependencies are present
# ---------------------------------------------------------------------------

if (NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/ext/robin_map/include")
if (NB_USE_SUBMODULE_DEPS AND NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/ext/robin_map/include")
message(FATAL_ERROR "The nanobind dependencies are missing! "
"You probably did not clone the project with --recursive. It is possible to recover "
"by invoking\n$ git submodule update --init --recursive")
endif()

# ---------------------------------------------------------------------------
# Installation rules
# ---------------------------------------------------------------------------

if(NB_CREATE_INSTALL_RULES AND NOT CMAKE_SKIP_INSTALL_RULES)
include(GNUInstallDirs)
set(NB_INSTALL_DATADIR "${CMAKE_INSTALL_DATADIR}/nanobind"
CACHE PATH "Installation path for read-only architecture-independent nanobind data files")

# Normally these would be configurable by the user, but we can't allow that
# because the lookup paths are hard-coded in 'cmake/nanobind-config.cmake'
set(CMAKE_INSTALL_INCLUDEDIR "${NB_INSTALL_DATADIR}/include")
set(NB_INSTALL_SRCDIR "${NB_INSTALL_DATADIR}/src")
set(NB_INSTALL_EXTDIR "${NB_INSTALL_DATADIR}/ext")
set(NB_INSTALL_CMAKEDIR "${NB_INSTALL_DATADIR}/cmake")

install(
DIRECTORY include/
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

install(
DIRECTORY src/
DESTINATION "${NB_INSTALL_SRCDIR}"
PATTERN "*.py" EXCLUDE
)

if(NB_USE_SUBMODULE_DEPS)
install(
DIRECTORY ext/robin_map/include/
DESTINATION "${NB_INSTALL_EXTDIR}/robin_map/include"
)
install(
FILES ext/robin_map/CMakeLists.txt
DESTINATION "${NB_INSTALL_EXTDIR}/robin_map"
)
endif()

install(
DIRECTORY cmake/
DESTINATION "${NB_INSTALL_CMAKEDIR}"
)
endif()

# ---------------------------------------------------------------------------
# Compile with a few more compiler warnings turned on
# ---------------------------------------------------------------------------
Expand Down
16 changes: 14 additions & 2 deletions cmake/nanobind-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,20 @@ function (nanobind_build_library TARGET_NAME)
target_compile_definitions(${TARGET_NAME} PRIVATE
$<${NB_OPT_SIZE}:NB_COMPACT_ASSERTIONS>)

target_include_directories(${TARGET_NAME} PRIVATE
${NB_DIR}/ext/robin_map/include)
# If nanobind was installed without submodule dependencies, then the
# dependencies directory won't exist and we need to find them.
# However, if the directory _does_ exist, then the user is free to choose
# whether nanobind uses them (based on `NB_USE_SUBMODULE_DEPS`), with a
# preference to choose them if `NB_USE_SUBMODULE_DEPS` is not defined
if (NOT IS_DIRECTORY ${NB_DIR}/ext/robin_map/include OR
(DEFINED NB_USE_SUBMODULE_DEPS AND NOT NB_USE_SUBMODULE_DEPS))
include(CMakeFindDependencyMacro)
find_dependency(tsl-robin-map)
wjakob marked this conversation as resolved.
Show resolved Hide resolved
target_link_libraries(${TARGET_NAME} PRIVATE tsl::robin_map)
else()
target_include_directories(${TARGET_NAME} PRIVATE
${NB_DIR}/ext/robin_map/include)
endif()

target_include_directories(${TARGET_NAME} PUBLIC
${Python_INCLUDE_DIRS}
Expand Down