Skip to content

Commit

Permalink
Add support for external dependencies with find_package
Browse files Browse the repository at this point in the history
  • Loading branch information
eliaskosunen committed Nov 26, 2023
1 parent 8afba35 commit 2767261
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 46 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ target_include_directories(scn
target_link_libraries(scn
PRIVATE
simdutf::simdutf
fast_float
FastFloat::fast_float
)
set_library_flags(scn)

Expand All @@ -146,7 +146,7 @@ add_library(scn_internal INTERFACE)
target_link_libraries(scn_internal
INTERFACE
scn::scn
simdutf::simdutf fast_float
simdutf::simdutf FastFloat::fast_float
)
target_include_directories(scn_internal
INTERFACE
Expand Down
1 change: 1 addition & 0 deletions cmake/buildflags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ function(set_interface_flags target)

disable_msvc_secure_flags(${target} INTERFACE)
set_bigobj_flags(${target} INTERFACE)
target_compile_features(${target} INTERFACE cxx_std_17)
endfunction()

function(set_library_flags target)
Expand Down
58 changes: 34 additions & 24 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ if (SCN_TESTS)
# gtest CMake does some flag overriding we don't want, and it's also quite heavy
# Do it manually

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

FetchContent_GetProperties(googletest)
if(NOT googletest)
FetchContent_Populate(googletest)
Expand All @@ -25,15 +27,15 @@ if (SCN_TESTS)
add_library(scn_gtest
"${googletest_SOURCE_DIR}/googletest/src/gtest-all.cc"
"${googletest_SOURCE_DIR}/googlemock/src/gmock-all.cc"
)
)
target_include_directories(scn_gtest SYSTEM
PUBLIC
"${googletest_SOURCE_DIR}/googletest/include"
"${googletest_SOURCE_DIR}/googlemock/include"
PRIVATE
"${googletest_SOURCE_DIR}/googletest"
"${googletest_SOURCE_DIR}/googlemock"
)
)
target_link_libraries(scn_gtest PRIVATE Threads::Threads)
target_compile_features(scn_gtest PUBLIC cxx_std_14)
target_compile_options(scn_gtest PRIVATE $<$<CXX_COMPILER_ID:GNU>: -Wno-psabi>)
Expand All @@ -55,13 +57,6 @@ endif()

# simdutf

FetchContent_Declare(
simdutf
GIT_REPOSITORY https://github.com/simdutf/simdutf.git
GIT_TAG v4.0.3
GIT_SHALLOW TRUE
)

# simdutf CMake includes tests if BUILD_TESTING is globally ON
# we don't want to include tests of dependencies, so we need to do some manual work

Expand All @@ -71,11 +66,22 @@ set(BUILD_TESTING_BEFORE_SIMDUTF ${BUILD_TESTING})
set(SIMDUTF_BENCHMARKS OFF)
set(BUILD_TESTING OFF)

FetchContent_GetProperties(simdutf)
if(NOT simdutf_POPULATED)
FetchContent_Populate(simdutf)
if (SCN_USE_EXTERNAL_SIMDUTF)
find_package(simdutf CONFIG REQUIRED 4.0.5)
else ()
FetchContent_Declare(
simdutf
GIT_REPOSITORY https://github.com/simdutf/simdutf.git
GIT_TAG v4.0.5
GIT_SHALLOW TRUE
)

FetchContent_GetProperties(simdutf)
if(NOT simdutf_POPULATED)
FetchContent_Populate(simdutf)

add_subdirectory(${simdutf_SOURCE_DIR} ${simdutf_BINARY_DIR} EXCLUDE_FROM_ALL)
add_subdirectory(${simdutf_SOURCE_DIR} ${simdutf_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
endif()

set(SIMDUTF_BENCHMARKS ${SIMDUTF_BENCHMARKS_BEFORE_SIMDUTF})
Expand All @@ -84,20 +90,24 @@ set(BUILD_TESTING ${BUILD_TESTING_BEFORE_SIMDUTF})
# fast_float

cmake_policy(SET CMP0077 NEW)
FetchContent_Declare(
fast_float
GIT_REPOSITORY https://github.com/fastfloat/fast_float.git
GIT_TAG v5.2.0
GIT_SHALLOW TRUE
)

set(FASTFLOAT_INSTALL OFF CACHE INTERNAL "")

FetchContent_GetProperties(fast_float)
if(NOT fast_float_POPULATED)
FetchContent_Populate(fast_float)
if (SCN_USE_EXTERNAL_FAST_FLOAT)
find_package(FastFloat CONFIG REQUIRED 5.3.0)
else()
FetchContent_Declare(
fast_float
GIT_REPOSITORY https://github.com/fastfloat/fast_float.git
GIT_TAG v5.3.0
GIT_SHALLOW TRUE
)

FetchContent_GetProperties(fast_float)
if(NOT fast_float_POPULATED)
FetchContent_Populate(fast_float)

add_subdirectory(${fast_float_SOURCE_DIR} ${fast_float_BINARY_DIR} EXCLUDE_FROM_ALL)
add_subdirectory(${fast_float_SOURCE_DIR} ${fast_float_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
endif()

# make available
Expand Down
28 changes: 9 additions & 19 deletions cmake/install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ endif()
include(GNUInstallDirs)
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/scn)

install(TARGETS
scn
fast_float simdutf
set(targets "scn")
if(NOT SCN_USE_EXTERNAL_SIMDUTF)
list(APPEND targets simdutf)
endif()
if(NOT SCN_USE_EXTERNAL_FAST_FLOAT)
list(APPEND targets fast_float)
endif()

install(TARGETS ${targets}
EXPORT scn-targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand Down Expand Up @@ -57,19 +63,3 @@ export(EXPORT scn-targets
)

export(PACKAGE scn)
















4 changes: 3 additions & 1 deletion cmake/options.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

option(SCN_CI "Enable CI preset" OFF)
if (SCN_IS_TOP_PROJECT)
set(SCN_ENABLE_EXTRAS ON)
Expand All @@ -23,6 +22,9 @@ option(SCN_COVERAGE "Enable coverage reporting" OFF)
option(SCN_PEDANTIC "Enable pedantic compilation flags" ${SCN_ENABLE_EXTRAS})
option(SCN_WERROR "Halt compilation in case of a warning" ${SCN_CI})

option(SCN_USE_EXTERNAL_SIMDUTF "Use find_package for simdutf, instead of FetchContent" OFF)
option(SCN_USE_EXTERNAL_FAST_FLOAT "Use find_package for fast_float, instead of FetchContent" OFF)

option(SCN_USE_32BIT "Compile as 32-bit (gcc or clang only)" OFF)
option(SCN_USE_EXCEPTIONS "Compile with exception support (disabling will cause test failures)" ON)
option(SCN_USE_RTTI "Compile with RTTI (run-time type information) support" ON)
Expand Down
5 changes: 5 additions & 0 deletions include/scn/detail/stdin_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ namespace scn {
}
return a.m_current_index == b.m_current_index;
}
friend bool operator!=(const stdin_iterator& a,
const stdin_iterator& b)
{
return !(a == b);
}

private:
friend class stdin_manager;
Expand Down

0 comments on commit 2767261

Please sign in to comment.