Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions CMake/ystdlib-cpp-helpers.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# check_if_header_only_library()
#
# @param SOURCE_LIST The list of source files that a target library uses
# @param IS_HEADER_ONLY Returns whether the target library only contains header files
function(check_if_header_only_library SOURCE_LIST IS_HEADER_ONLY)
set(_LOCAL_SOURCE_LIST "${${SOURCE_LIST}}")
foreach(src_file IN LISTS _LOCAL_SOURCE_LIST)
if(${src_file} MATCHES ".*\\.(h|hpp|inc)")
list(REMOVE_ITEM _LOCAL_SOURCE_LIST "${src_file}")
endif()
endforeach()

if(_LOCAL_SOURCE_LIST STREQUAL "")
set(${IS_HEADER_ONLY} 1 PARENT_SCOPE)
else()
set(${IS_HEADER_ONLY} 0 PARENT_SCOPE)
endif()
endfunction()

# Adds a c++20 interface library in the subdirectory NAME with the target NAME and alias
# NAMESPACE::NAME. Libraries with multiple levels of namespace nesting are currently not supported.
#
Expand All @@ -6,6 +25,9 @@
#
# @param NAME
# @param NAMESPACE
# @param HEADERS
# @param SOURCES
# @param DEPENDS
# @parms TESTS_SOURCES
# @param [LIB_BUILD_INTERFACE="${PROJECT_SOURCE_DIR}/src"] The list of include paths for building
# the library and for external projects that link against it via the add_subdirectory() function.
Expand All @@ -16,6 +38,9 @@ function(cpp_library)
NAMESPACE
)
set(multiValueArgs
HEADERS
SOURCES

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think everything works even if we don't separate/distinguish headers and source files, so I think just SOURCES is fine. If it is important to separate them, then maybe we should concatenate them and pass both to check_if_header_only_library to avoid user error (currently if you put a cpp file in HEADERS I think things will break)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not important to separate now, but will make a difference when we only want to install public headers into install library paths.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a second thought, I've decided to keep them split and renamed the params for more clarity. I've also enforced a check of the public header list.

DEPENDS
TESTS_SOURCES
LIB_BUILD_INTERFACE
)
Expand All @@ -31,14 +56,33 @@ function(cpp_library)
set(arg_cpp_lib_LIB_BUILD_INTERFACE "${PROJECT_SOURCE_DIR}/src")
endif()

# Build interface library
add_library(${arg_cpp_lib_NAME} INTERFACE)
target_include_directories(
${arg_cpp_lib_NAME}
INTERFACE
"$<BUILD_INTERFACE:${arg_cpp_lib_LIB_BUILD_INTERFACE}>"
)
target_compile_features(${arg_cpp_lib_NAME} INTERFACE cxx_std_20)
check_if_header_only_library(arg_cpp_lib_SOURCES _IS_INTERFACE_LIB)

if(_IS_INTERFACE_LIB)
add_library(${arg_cpp_lib_NAME} INTERFACE)
target_include_directories(
${arg_cpp_lib_NAME}
INTERFACE
"$<BUILD_INTERFACE:${arg_cpp_lib_LIB_BUILD_INTERFACE}>"
)
target_compile_features(${arg_cpp_lib_NAME} INTERFACE cxx_std_20)
else()
add_library(${arg_cpp_lib_NAME} "") # Library type determined by BUILD_SHARED_LIBS
target_sources(
${arg_cpp_lib_NAME}
PRIVATE
${arg_cpp_lib_HEADERS}
${arg_cpp_lib_SOURCES}
)
target_include_directories(
${arg_cpp_lib_NAME}
PUBLIC
"$<BUILD_INTERFACE:${arg_cpp_lib_LIB_BUILD_INTERFACE}>"
)
target_compile_features(${arg_cpp_lib_NAME} PUBLIC cxx_std_20)
endif()

target_link_libraries(${arg_cpp_lib_NAME} PUBLIC ${arg_cpp_lib_DEPENDS})
add_library(${arg_cpp_lib_NAMESPACE}::${arg_cpp_lib_NAME} ALIAS ${arg_cpp_lib_NAME})

if(YSTDLIB_CPP_ENABLE_TESTS)
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ if(YSTDLIB_CPP_IS_TOP_LEVEL)
include(CTest)
endif()

option(BUILD_SHARED_LIBS "Build using shared libraries." OFF)

Comment thread
Bill-hbrhbr marked this conversation as resolved.
Outdated
option(YSTDLIB_CPP_BUILD_TESTING "Build the testing tree for ystdlib-cpp." ON)
if(BUILD_TESTING AND YSTDLIB_CPP_BUILD_TESTING)
set(YSTDLIB_CPP_ENABLE_TESTS ON)
Expand Down