diff --git a/CMakeLists.txt b/CMakeLists.txt index 545b8d08..aae198eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,14 @@ if(NOT(SIMDJSON_JUST_LIBRARY)) endif() install(FILES singleheader/simdjson.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +option(SIMDJSON_ENABLE_THREADS "Link with thread support" ON) +include(CMakePackageConfigHelpers) +configure_package_config_file("${PROJECT_SOURCE_DIR}/cmake/simdjson-config.cmake.in" + "${PROJECT_BINARY_DIR}/simdjson-config.cmake" + INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/simdjson" + NO_SET_AND_CHECK_MACRO + NO_CHECK_REQUIRED_COMPONENTS_MACRO) + # # Compile tools / tests / benchmarks # diff --git a/cmake/simdjson-config.cmake.in b/cmake/simdjson-config.cmake.in new file mode 100644 index 00000000..352c8707 --- /dev/null +++ b/cmake/simdjson-config.cmake.in @@ -0,0 +1,9 @@ +@PACKAGE_INIT@ + +include(CMakeFindDependencyMacro) +if(@SIMDJSON_ENABLE_THREADS@) + set(CMAKE_THREAD_PREFER_PTHREAD TRUE) + find_dependency(Threads) +endif() +# Import targets. +include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake") diff --git a/cmake/simdjson-flags.cmake b/cmake/simdjson-flags.cmake index a9c7ff7b..ddc7975e 100644 --- a/cmake/simdjson-flags.cmake +++ b/cmake/simdjson-flags.cmake @@ -202,8 +202,6 @@ if(SIMDJSON_ENABLE_THREADS) set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED) target_link_libraries(simdjson-flags INTERFACE Threads::Threads) - target_link_libraries(simdjson-flags INTERFACE ${CMAKE_THREAD_LIBS_INIT}) - target_compile_options(simdjson-flags INTERFACE ${CMAKE_THREAD_LIBS_INIT}) target_compile_definitions(simdjson-flags INTERFACE SIMDJSON_THREADS_ENABLED=1) # This will be set in the code automatically. endif() @@ -237,4 +235,6 @@ CHECK_SYMBOL_EXISTS(fork unistd.h HAVE_POSIX_FORK) CHECK_SYMBOL_EXISTS(wait sys/wait.h HAVE_POSIX_WAIT) install(TARGETS simdjson-flags EXPORT simdjson-config) -install(TARGETS simdjson-internal-flags EXPORT simdjson-config) + +# I do not think we want to export our internal flags! +# install(TARGETS simdjson-internal-flags EXPORT simdjson-config) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9e53f07d..bc6948e0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,7 +8,12 @@ add_library(simdjson-include-source INTERFACE) target_link_libraries(simdjson-include-source INTERFACE simdjson-headers) target_include_directories(simdjson-include-source INTERFACE $) -install(TARGETS simdjson-include-source EXPORT simdjson-config) +# If a CMake user installs simdjson and then does... +# find_package(simdjson CONFIG REQUIRED) +# find_package(Threads REQUIRED) +# target_link_libraries(test PRIVATE simdjson-include-source) +# It is not clear what it should do? Does the user get access to the src files? +# install(TARGETS simdjson-include-source EXPORT simdjson-config) # # For callers who intend to compile simdjson.cpp themselves. @@ -19,7 +24,14 @@ install(TARGETS simdjson-include-source EXPORT simdjson-config) add_library(simdjson-source INTERFACE) target_sources(simdjson-source INTERFACE $/simdjson.cpp) target_link_libraries(simdjson-source INTERFACE simdjson-include-source) -install(TARGETS simdjson-source EXPORT simdjson-config) +# Note that simdjson.cpp is *not* installed so installing simdjson-include-source is not great. +# If a CMake user installs simdjson and then does... +# find_package(simdjson CONFIG REQUIRED) +# find_package(Threads REQUIRED) +# target_link_libraries(test PRIVATE simdjson-source) +# It might fail with Cannot find source file: simdjson.cpp +# See issue https://github.com/simdjson/simdjson/issues/1383 +# install(TARGETS simdjson-source EXPORT simdjson-config) # # simdjson is the distributed library compiled with flags. @@ -31,7 +43,15 @@ install(TARGETS simdjson-source EXPORT simdjson-config) if(SIMDJSON_BUILD_STATIC) MESSAGE( STATUS "Building a static library." ) - add_library(simdjson STATIC "") + ### + # In the case of a static library, if you do "target_link_libraries(simdjson PRIVATE simdjson-source simdjson-internal-flags)" + # without also calling "install(TARGETS simdjson-source EXPORT simdjson-config)" you get an error + # to the effect that you are trying to install 'simdjson' which depends on 'simdjson-source' while + # not installing 'simdjson-source', so it fails. So we bypass entirely simdjson-source since we do + # not want to install them. + #### + add_library(simdjson STATIC simdjson.cpp) + target_include_directories(simdjson PRIVATE $) else() MESSAGE( STATUS "Building a dynamic library." ) add_library(simdjson SHARED "") @@ -40,10 +60,12 @@ else() MESSAGE( STATUS "Building a Windows DLL using Visual Studio, exporting all symbols automatically." ) set_target_properties(simdjson PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS 1) endif() + ### + # Somehow, for a dynamic library, the next line is entirely fine (but not for a static one). + ### + target_link_libraries(simdjson PRIVATE simdjson-source simdjson-internal-flags) endif() - target_link_libraries(simdjson PUBLIC simdjson-headers simdjson-flags) # Only expose the headers, not sources -target_link_libraries(simdjson PRIVATE simdjson-source simdjson-internal-flags) ## ## In systems like R, libraries must not use stderr or abort to be acceptable. @@ -119,7 +141,7 @@ install(TARGETS simdjson ) install(EXPORT simdjson-config - FILE simdjson-config.cmake + FILE simdjson-targets.cmake NAMESPACE simdjson:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/simdjson )