From 16cb0502e6a117cc2758e69042518a33326107dc Mon Sep 17 00:00:00 2001 From: Connor Baker Date: Sat, 20 Jan 2024 20:37:09 +0000 Subject: [PATCH] cmake: make pre-built library discoverable via CMake configuration files Co-Authored-By: Someone Serge --- .gitignore | 1 + CMakeLists.txt | 58 ++++++++++++++++++++++++++++++++--------- drjit-core-config.cmake | 1 + 3 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 drjit-core-config.cmake diff --git a/.gitignore b/.gitignore index aabfccc92..f3a36f602 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /.clangd /compile_commands.json *.cmake +!drjit-core-config.cmake CMakeCache.txt CMakeFiles Makefile diff --git a/CMakeLists.txt b/CMakeLists.txt index 02250dbbc..3a0acbcc5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,11 +70,42 @@ if (NOT APPLE) endif() # ---------------------------------------------------------- -# Build the nanothread library +# Find or build the dependencies # ---------------------------------------------------------- -add_subdirectory(ext/nanothread) -mark_as_advanced(NANOTHREAD_ENABLE_TESTS) +find_package(nanothread) +if (NOT nanothread_FOUND) + 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 ext/lz4/xxhash.c) + target_include_directories(lz4 PUBLIC 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) +endif() + +find_package(tsl-robin-map) +if (NOT tsl-robin-map_FOUND) + add_subdirectory(ext/robin_map) +endif() + +find_package(LLVM) +if (LLVM_FOUND) + target_link_libraries(drjit-core PRIVATE LLVM) +endif() # ---------------------------------------------------------- # Build Dr.Jit-Core @@ -135,21 +166,13 @@ add_library( src/init.cpp src/api.cpp - # LZ4 compression library & XXHash hash function - ext/lz4/lz4.h ext/lz4/lz4.c - ext/lz4/xxhash.h ext/lz4/xxh3.h ext/lz4/xxhash.c - # Precompiled kernels in compressed PTX format resources/kernels.h resources/kernels.c ) target_compile_features(drjit-core PRIVATE cxx_std_17) -target_include_directories(drjit-core PRIVATE - ext/nanothread/include - ext/robin_map/include - ext/lz4 -) +target_link_libraries(drjit-core PRIVATE nanothread tsl::robin_map lz4 xxHash::xxhash) if (MSVC) # Conditional expression is constant (a few in robin_hash.h) @@ -170,7 +193,7 @@ target_compile_definitions(drjit-core PRIVATE -DLZ4LIB_VISIBILITY=) target_include_directories(drjit-core PUBLIC $ - $) + $) target_compile_definitions(drjit-core PRIVATE -DDRJIT_BUILD=1) target_link_libraries(drjit-core PRIVATE nanothread) @@ -229,3 +252,12 @@ set_target_properties(drjit-core PROPERTIES INTERPROCEDURAL_OPTIMIZATION_DEBUG if (DRJIT_ENABLE_TESTS) add_subdirectory(tests) endif() + +configure_file(drjit-core-config.cmake "${CMAKE_CURRENT_BINARY_DIR}/drjit-core/drjit-core-config.cmake") +install(TARGETS drjit-core EXPORT drjit-core-targets INCLUDES DESTINATION include) +install(DIRECTORY include/drjit-core DESTINATION include) +install(FILES drjit-core-config.cmake DESTINATION lib/cmake/drjit-core) +install( + EXPORT drjit-core-targets + FILE drjit-core-targets.cmake + DESTINATION lib/cmake/drjit-core) diff --git a/drjit-core-config.cmake b/drjit-core-config.cmake new file mode 100644 index 000000000..0b800fd66 --- /dev/null +++ b/drjit-core-config.cmake @@ -0,0 +1 @@ +include("${CMAKE_CURRENT_LIST_DIR}/drjit-core-targets.cmake")