Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ cpp_library_setup(
[DOCS_INPUTS input_list] # Additional documentation files/directories
[DOCS_OPTIONS setting_list] # Additive single-line Doxygen assignments
[REQUIRES_CPP_VERSION 17|20|23] # C++ version (default: 20)
[ENABLE_THREADS ON|OFF] # Transitive thread requirements (default: ON)
[INSTALL_OPTION option_name] # Independent installation control (default: uppercase NAMESPACE_INSTALL)
[LICENSE_FILES file_list] # Additional project-relative or absolute license/notice files
)
Expand All @@ -515,6 +516,15 @@ cpp_library_setup(
and example targets default to `CXX_EXTENSIONS OFF` unless
`CMAKE_CXX_EXTENSIONS` is set by the caller. This does not impose a dialect on
consumers or enable pedantic diagnostics.
- `ENABLE_THREADS` defaults to `ON`. Setup requires CMake's `Threads` package and
links `Threads::Threads` publicly for compiled libraries or through the interface
for header-only libraries. Production code, generated tests, in-source test
runners, examples, and downstream consumers inherit the platform's thread
requirements, including compiler/linker flags when needed. Installed packages
declare `find_dependency(Threads)`.
This applies even when `BUILD_TESTING=OFF` or the library is a subproject.
Pass `ENABLE_THREADS OFF` to skip automatic thread discovery and linkage;
this does not disable C++ thread APIs or remove thread requirements you add manually.

### Documentation configuration

Expand Down
6 changes: 1 addition & 5 deletions cmake/cpp-library-install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
# _cpp_library_setup_install() to avoid requiring project() to be called
# when this module is loaded.

# System packages that don't require version constraints in find_dependency()
# These are commonly available system libraries where version requirements are typically not specified.
# To extend this list in your project, use cpp_library_map_dependency() to explicitly map additional packages.
set(_CPP_LIBRARY_SYSTEM_PACKAGES "Threads" "OpenMP" "ZLIB" "CURL" "OpenSSL")

# Registers a project-local dependency mapping for find_dependency() generation.
# - Precondition: TARGET is a namespaced target (e.g., "Qt6::Core", "stlab::enum-ops") or non-namespaced (e.g., "opencv_core")
# - Postcondition: FIND_DEPENDENCY_CALL overrides TARGET's requirement only for libraries
Expand Down Expand Up @@ -133,6 +128,7 @@ endfunction()
# - Postcondition: OUTPUT_VAR contains find_dependency() call syntax or error is raised
# - Optional fourth argument receives the unverified dependency, or an empty string
function(_cpp_library_resolve_dependency LIB NAMESPACE OUTPUT_VAR)
set(_CPP_LIBRARY_SYSTEM_PACKAGES "Threads" "OpenMP" "ZLIB" "CURL" "OpenSSL")
if(ARGC GREATER 3)
set(${ARGV3} "" PARENT_SCOPE)
endif()
Expand Down
16 changes: 15 additions & 1 deletion cpp-library.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ function(cpp_library_setup)
DESCRIPTION # Description string
NAMESPACE # Namespace (e.g., "stlab")
REQUIRES_CPP_VERSION # C++ version (default: 20)
ENABLE_THREADS # Portable thread requirements (default: ON)
INSTALL_OPTION # Custom installation option name
)
set(multiValueArgs
Expand All @@ -201,11 +202,14 @@ function(cpp_library_setup)
if("INSTALL_OPTION" IN_LIST ARG_KEYWORDS_MISSING_VALUES)
message(FATAL_ERROR "cpp_library_setup: INSTALL_OPTION requires a value")
endif()
foreach(keyword DOCS_INPUTS DOCS_OPTIONS LICENSE_FILES)
foreach(keyword DOCS_INPUTS DOCS_OPTIONS LICENSE_FILES ENABLE_THREADS)
if(keyword IN_LIST ARG_KEYWORDS_MISSING_VALUES)
message(FATAL_ERROR "cpp_library_setup: ${keyword} requires a value")
endif()
endforeach()
if(NOT DEFINED ARG_ENABLE_THREADS)
set(ARG_ENABLE_THREADS ON)
endif()
set(install_args)
if(DEFINED ARG_INSTALL_OPTION)
list(APPEND install_args INSTALL_OPTION "${ARG_INSTALL_OPTION}")
Expand Down Expand Up @@ -321,6 +325,16 @@ function(cpp_library_setup)
${install_args}
)

if(ARG_ENABLE_THREADS)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
if(ARG_SOURCES)
target_link_libraries("${ARG_NAME}" PUBLIC Threads::Threads)
else()
target_link_libraries("${ARG_NAME}" INTERFACE Threads::Threads)
endif()
endif()

cpp_library_add_clangd_headers(TARGET "${ARG_NAME}" HEADERS ${GENERATED_HEADERS})

# Only setup development infrastructure when building as top-level project
Expand Down
8 changes: 4 additions & 4 deletions tests/install/test_nested_install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ function(run_case name parent_installed leaf_installed shared)
"-DTOOLKIT_SOURCE=${toolkit_source}" "-DBUILD_SHARED_LIBS=${shared}" -DLICENSE_TEST=ON ${ARGN})
run_command("${CMAKE_COMMAND}" --build "${binary}")
run_command("${CMAKE_COMMAND}" --install "${binary}" --prefix "${prefix}")
check_package("${prefix}" parent "${parent_installed}" "")
check_package("${prefix}" leaf "${leaf_installed}" "")
check_package("${prefix}" parent "${parent_installed}" "find_dependency(Threads)")
check_package("${prefix}" leaf "${leaf_installed}" "find_dependency(Threads)")
set(datadir share)
if(name STREQUAL "custom-datadir")
set(datadir "custom data")
Expand Down Expand Up @@ -164,9 +164,9 @@ function(run_invalid_case invalid clean target)
run_command("${CMAKE_COMMAND}" "-DCMAKE_INSTALL_PREFIX=${prefix}"
-DCMAKE_INSTALL_LOCAL_ONLY=TRUE -P "${${clean}_binary}/cmake_install.cmake")
if(clean STREQUAL "parent")
check_package("${prefix}" parent TRUE "")
check_package("${prefix}" parent TRUE "find_dependency(Threads)")
else()
check_package("${prefix}" leaf TRUE "")
check_package("${prefix}" leaf TRUE "find_dependency(Threads)")
endif()
execute_process(COMMAND "${CMAKE_COMMAND}" "-DCMAKE_INSTALL_PREFIX=${prefix}"
-DCMAKE_INSTALL_LOCAL_ONLY=TRUE -P "${${invalid}_binary}/cmake_install.cmake"
Expand Down
84 changes: 84 additions & 0 deletions tests/setup/fixtures/target_type/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,97 @@ endif()
if(DEFINED FIXTURE_CPP_VERSION)
set(version_args REQUIRES_CPP_VERSION "${FIXTURE_CPP_VERSION}")
endif()
if(CHECK_THREADS)
include(CTest)
include("${TOOLKIT_SOURCE}/tests/setup/fixtures/mock_doctest.cmake")
set(test_args TESTS thread_test.cpp EXAMPLES thread_example.cpp)
if(DEFINED FIXTURE_ENABLE_THREADS)
set(thread_args ENABLE_THREADS "${FIXTURE_ENABLE_THREADS}")
elseif(MISSING_THREADS_VALUE)
set(thread_args ENABLE_THREADS)
endif()
if(EXPECT_THREADS)
if(FORCE_PTHREAD_FLAG AND UNIX AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set(CMAKE_HAVE_LIBC_PTHREAD FALSE)
endif()
file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/include/fixture/thread.hpp" [=[
#pragma once
#include <latch>
#include <thread>
inline int thread_value() {
int result = 0;
std::latch ready(1);
std::jthread worker([&] { result = 42; ready.count_down(); });
ready.wait();
worker.join();
return result;
}
]=])
file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/src/sample.cpp" [=[
#include <fixture/thread.hpp>
#include <cpp-library/testing.hpp>
namespace fixture { int sample() { return thread_value(); } }
#if CPP_LIBRARY_TESTING()
const int embedded_thread_result = thread_value();
#endif
]=])
set(thread_main [=[
#include <fixture/thread.hpp>
#include <fixture/sample.hpp>
int main() {
if (thread_value() != 42) return 1;
#ifndef HEADER_ONLY
if (fixture::sample() != 42) return 2;
#endif
return 0;
}
]=])
file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/tests/thread_test.cpp" "${thread_main}")
file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/examples/thread_example.cpp" "${thread_main}")
if(OMIT_SOURCES)
add_compile_definitions(HEADER_ONLY)
endif()
else()
set(CMAKE_DISABLE_FIND_PACKAGE_Threads TRUE)
file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/tests/thread_test.cpp" "int main() { return 0; }\n")
file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/examples/thread_example.cpp" "int main() { return 0; }\n")
endif()
endif()
cpp_library_setup(
DESCRIPTION "Consumer-selected target type fixture"
NAMESPACE fixture
HEADERS sample.hpp
${version_args}
${thread_args}
${test_args}
${source_args})
if(CHECK_THREADS)
if(BUILD_SHARED_LIBS AND NOT OMIT_SOURCES)
set_target_properties(sample PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()
get_target_property(links sample INTERFACE_LINK_LIBRARIES)
_cpp_library_generate_dependencies(dependencies sample fixture)
if(EXPECT_THREADS)
if(NOT "Threads::Threads" IN_LIST links OR NOT dependencies MATCHES "find_dependency\\(Threads\\)")
message(FATAL_ERROR "Missing transitive thread dependency: ${links}; ${dependencies}")
endif()
if(FORCE_PTHREAD_FLAG AND UNIX AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
get_target_property(options Threads::Threads INTERFACE_COMPILE_OPTIONS)
if(NOT options MATCHES "-pthread")
message(FATAL_ERROR "Explicit pthread flag case did not supply compile flags: ${options}")
endif()
endif()
elseif(TARGET Threads::Threads OR "Threads::Threads" IN_LIST links OR dependencies MATCHES "Threads")
message(FATAL_ERROR "Disabled threading still configured thread support")
endif()
if(NOT PROJECT_IS_TOP_LEVEL OR NOT BUILD_TESTING)
foreach(target thread_test thread_example sample_in_source_tests)
if(TARGET "${target}")
message(FATAL_ERROR "Unexpected development target ${target}")
endif()
endforeach()
endif()
endif()
if(CHECK_INSTALL_DIRS)
if(NOT CMAKE_INSTALL_DATADIR STREQUAL EXPECTED_DATADIR)
message(FATAL_ERROR "Expected DATADIR ${EXPECTED_DATADIR}, got '${CMAKE_INSTALL_DATADIR}'")
Expand Down
31 changes: 30 additions & 1 deletion tests/setup/test_target_type.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ function(run_case name shared expected_type)
file(MAKE_DIRECTORY "${case_source}")
file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/target_type/"
DESTINATION "${case_source}")
if(name MATCHES "^threads_nested")
file(WRITE "${test_root}/${name}/CMakeLists.txt"
"cmake_minimum_required(VERSION 3.24)\ninclude(\"\${TOOLKIT_SOURCE}/cpp-library.cmake\")\ncpp_library_enable_dependency_tracking()\nproject(parent LANGUAGES CXX)\nenable_testing()\nadd_subdirectory(source)\n")
set(case_source "${test_root}/${name}")
endif()

execute_process(
COMMAND "${CMAKE_COMMAND}" -S "${case_source}" -B "${case_binary}" -G Ninja
Expand All @@ -26,7 +31,15 @@ function(run_case name shared expected_type)
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
${ARGN}
RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err)
if(result EQUAL 0 AND name MATCHES "^dialect_")
if(name STREQUAL "threads_missing_value")
if(NOT result EQUAL 0 AND "${out}${err}" MATCHES "ENABLE_THREADS requires a value")
set(result 0)
set(err "")
else()
set(result 1)
set(err "Expected a missing ENABLE_THREADS value diagnostic:\n${out}\n${err}")
endif()
elseif(result EQUAL 0 AND name MATCHES "^(dialect_|threads_)")
execute_process(COMMAND "${CMAKE_COMMAND}" --build "${case_binary}"
RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err)
if(result EQUAL 0)
Expand Down Expand Up @@ -86,6 +99,22 @@ run_case(dialect_on OFF STATIC_LIBRARY -DCHECK_DIALECT=ON
-DCMAKE_CXX_EXTENSIONS=ON -DEXPECTED_EXTENSIONS=ON -DEXPECTED_CALLER_EXTENSIONS=ON)
run_case(dialect_off OFF STATIC_LIBRARY -DCHECK_DIALECT=ON
-DCMAKE_CXX_EXTENSIONS=OFF -DEXPECTED_EXTENSIONS=OFF -DEXPECTED_CALLER_EXTENSIONS=OFF)
run_case(threads_default OFF STATIC_LIBRARY -DCHECK_THREADS=ON -DEXPECT_THREADS=ON)
run_case(threads_shared ON SHARED_LIBRARY -DCHECK_THREADS=ON -DEXPECT_THREADS=ON
-DFIXTURE_ENABLE_THREADS=ON -DFORCE_PTHREAD_FLAG=ON)
run_case(threads_interface OFF INTERFACE_LIBRARY -DOMIT_SOURCES=ON
-DCHECK_THREADS=ON -DEXPECT_THREADS=ON)
run_case(threads_disabled OFF STATIC_LIBRARY -DCHECK_THREADS=ON
-DEXPECT_THREADS=OFF -DFIXTURE_ENABLE_THREADS=OFF)
run_case(threads_interface_disabled OFF INTERFACE_LIBRARY -DOMIT_SOURCES=ON
-DCHECK_THREADS=ON -DEXPECT_THREADS=OFF -DFIXTURE_ENABLE_THREADS=OFF)
run_case(threads_without_tests OFF STATIC_LIBRARY -DCHECK_THREADS=ON
-DEXPECT_THREADS=ON -DBUILD_TESTING=OFF)
run_case(threads_nested OFF STATIC_LIBRARY -DCHECK_THREADS=ON -DEXPECT_THREADS=ON)
run_case(threads_nested_disabled OFF STATIC_LIBRARY -DCHECK_THREADS=ON
-DEXPECT_THREADS=OFF -DFIXTURE_ENABLE_THREADS=OFF)
run_case(threads_missing_value OFF STATIC_LIBRARY -DCHECK_THREADS=ON
-DEXPECT_THREADS=OFF -DMISSING_THREADS_VALUE=ON)

file(REMOVE_RECURSE "${test_root}")
if(failed_cases)
Expand Down
Loading