Skip to content

Commit

Permalink
cmake: add flags to use system-provided libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorBaker authored and Connor Baker committed Jan 31, 2024
1 parent 12a5301 commit e77049e
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ project(drjit-core
# Optional features available to users
# ----------------------------------------------------------

option(DRJIT_USE_SYSTEM_NANOTHREAD "Use the system nanothread library?" OFF)
option(DRJIT_USE_SYSTEM_LZ4 "Use the system lz4 library?" OFF)
option(DRJIT_USE_SYSTEM_XXHASH "Use the system xxHash library?" OFF)
option(DRJIT_USE_SYSTEM_ROBIN_MAP "Use the system robin-map library?" OFF)

option(DRJIT_DYNAMIC_LLVM "Resolve LLVM dynamically at run time?" ON)
option(DRJIT_ENABLE_TESTS "Build Dr.Jit-Core test suite?" OFF)

Expand Down Expand Up @@ -43,6 +48,11 @@ include(ext/nanothread/ext/cmake-defaults/CMakeLists.txt)
# Print a few messages explaining what will be compiled
# ----------------------------------------------------------

message(STATUS "Dr.Jit-Core: using system nanothread: ${DRJIT_USE_SYSTEM_NANOTHREAD}")
message(STATUS "Dr.Jit-Core: using system lz4: ${DRJIT_USE_SYSTEM_LZ4}")
message(STATUS "Dr.Jit-Core: using system xxHash: ${DRJIT_USE_SYSTEM_XXHASH}")
message(STATUS "Dr.Jit-Core: using system robin-map: ${DRJIT_USE_SYSTEM_ROBIN_MAP}")

if (DRJIT_DYNAMIC_LLVM)
message(STATUS "Dr.Jit-Core: LLVM will be loaded dynamically at runtime.")
else()
Expand Down Expand Up @@ -73,32 +83,40 @@ endif()
# Find or build the dependencies
# ----------------------------------------------------------

find_package(nanothread)
if (NOT nanothread_FOUND)
if (DRJIT_USE_SYSTEM_NANOTHREAD)
find_package(nanothread REQUIRED)
else()
add_subdirectory(ext/nanothread)
mark_as_advanced(NANOTHREAD_ENABLE_TESTS)
endif()

find_package(PkgConfig)
if (PkgConfig_FOUND)
pkg_check_modules(lz4 liblz4 IMPORTED_TARGET)
add_library(lz4 ALIAS PkgConfig::lz4)
endif()
if(NOT lz4_FOUND)
add_library(lz4 ext/lz4/lz4.c)
target_include_directories(lz4 PUBLIC ext/lz4)
if (DRJIT_USE_SYSTEM_LZ4)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LZ4 REQUIRED liblz4 IMPORTED_TARGET)
add_library(lz4 ALIAS PkgConfig::LZ4)
else()
add_library(lz4 SHARED ext/lz4/lz4.c)
# We only need to link against the lz4 library, not include its headers.
# NOTE: It is important that we use a generator here to avoid including a relative path
# in the INTERFACE_INCLUDE_DIRECTORIES of the lz4 target. This would cause the path to
# be invalid when the library is used in another project.
target_include_directories(lz4 PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ext/lz4>)
endif()

find_package(xxHash)
if (NOT xxHash_FOUND)
add_library(xxhash ext/lz4/xxhash.c)
target_include_directories(xxhash PUBLIC ext/lz4)

add_library(xxHash::xxhash ALIAS xxhash)
if (DRJIT_USE_SYSTEM_XXHASH)
find_package(xxHash REQUIRED)
add_library(xxhash ALIAS xxHash::xxhash)
else()
add_library(xxhash SHARED ext/lz4/xxhash.c)
# We directly include the headers from the xxHash library, so we add the include directory
# as a public include directory.
# See note above for LZ4 about why we must use a generator expression for the path.
target_include_directories(xxhash PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ext/lz4>)
endif()

find_package(tsl-robin-map)
if (NOT tsl-robin-map_FOUND)
if (DRJIT_USE_SYSTEM_ROBIN_MAP)
find_package(tsl-robin-map REQUIRED)
else()
add_subdirectory(ext/robin_map)
endif()

Expand Down Expand Up @@ -168,7 +186,7 @@ add_library(
target_compile_features(drjit-core PRIVATE cxx_std_17)

# Link against the dependencies
target_link_libraries(drjit-core PRIVATE nanothread tsl::robin_map lz4 xxHash::xxhash)
target_link_libraries(drjit-core PRIVATE nanothread tsl::robin_map lz4 xxhash)

if (MSVC)
# Conditional expression is constant (a few in robin_hash.h)
Expand Down Expand Up @@ -251,9 +269,21 @@ endif()

configure_file(drjit-core-config.cmake "${CMAKE_CURRENT_BINARY_DIR}/drjit-core/drjit-core-config.cmake")
install(
TARGETS drjit-core nanothread
TARGETS drjit-core
EXPORT drjit-core-targets
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

# Install the dependencies if they are not system libraries or packaged with CMake.
foreach(_dep nanothread lz4 xxhash)
string(TOUPPER ${_dep} _dep_upper)
if (NOT DRJIT_USE_SYSTEM_${_dep_upper})
install(
TARGETS ${_dep}
EXPORT drjit-core-targets
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif()
endforeach()

install(DIRECTORY include/drjit-core DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(FILES drjit-core-config.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/drjit-core)
install(
Expand Down

0 comments on commit e77049e

Please sign in to comment.