-
Notifications
You must be signed in to change notification settings - Fork 0
build: modularize CMake configuration #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d07ba0c
build: modularize project configuration
zuudevs dcd7a9a
build: centralize target helpers
zuudevs ceb8373
build: centralize install package rules
zuudevs 3df97bd
build: clean up CMake tooling
zuudevs f9dd1dd
build: finalize modular CMake orchestration
zuudevs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,59 +1,48 @@ | ||
| cmake_minimum_required(VERSION 3.20) | ||
| cmake_minimum_required(VERSION 3.21) | ||
|
|
||
| project( | ||
| cpp_request | ||
| VERSION 0.9.0 | ||
| DESCRIPTION "A lightweight synchronous C++17 HTTP/1.1 client built on native sockets" | ||
| VERSION 0.1.0 | ||
| LANGUAGES CXX C | ||
| LANGUAGES CXX | ||
| ) | ||
|
|
||
| set(CMAKE_CXX_STANDARD 17) | ||
| set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
| set(CMAKE_CXX_EXTENSIONS OFF) | ||
| set(CMAKE_EXPORT_COMPILE_COMMANDS ON) | ||
| list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules") | ||
|
|
||
| option(CPP_REQUEST_BUILD_BENCHMARKS "Build cpp_request benchmarks" OFF) | ||
| include(options) | ||
| cpp_request_setup_options() | ||
|
|
||
| include(CTest) | ||
| include(GNUInstallDirs) | ||
| include(CMakePackageConfigHelpers) | ||
| include(cmake/modules/output.cmake) | ||
| include(cmake/modules/warning.cmake) | ||
| include(cmake/modules/sanitizing.cmake) | ||
| include(cmake/modules/optimizing.cmake) | ||
| include(project-settings) | ||
| cpp_request_setup_project() | ||
|
|
||
| add_subdirectory(src) | ||
| if(BUILD_TESTING) | ||
|
|
||
| if(CPP_REQUEST_BUILD_TESTS) | ||
| include(testing) | ||
| add_subdirectory(tests) | ||
| endif() | ||
|
|
||
| if(CPP_REQUEST_BUILD_BENCHMARKS) | ||
| include(benchmarking) | ||
| add_subdirectory(benchmarks) | ||
| endif() | ||
| add_subdirectory(examples) | ||
|
|
||
| set(CPP_REQUEST_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/cpp_request") | ||
|
|
||
| configure_package_config_file( | ||
| ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cpp_requestConfig.cmake.in | ||
| ${CMAKE_CURRENT_BINARY_DIR}/cpp_requestConfig.cmake | ||
| INSTALL_DESTINATION ${CPP_REQUEST_INSTALL_CMAKEDIR} | ||
| ) | ||
| if(CPP_REQUEST_BUILD_EXAMPLES) | ||
| include(examples) | ||
| add_subdirectory(examples) | ||
| endif() | ||
|
|
||
| write_basic_package_version_file( | ||
| ${CMAKE_CURRENT_BINARY_DIR}/cpp_requestConfigVersion.cmake | ||
| VERSION ${PROJECT_VERSION} | ||
| COMPATIBILITY SameMinorVersion | ||
| ) | ||
| if(CPP_REQUEST_ENABLE_INSTALL) | ||
| include(install) | ||
| cpp_request_configure_install() | ||
| endif() | ||
|
|
||
| install( | ||
| EXPORT cpp_requestTargets | ||
| FILE cpp_requestTargets.cmake | ||
| NAMESPACE cpp_request:: | ||
| DESTINATION ${CPP_REQUEST_INSTALL_CMAKEDIR} | ||
| ) | ||
| if(PROJECT_IS_TOP_LEVEL AND CPP_REQUEST_ENABLE_CLANG_FORMAT) | ||
| include("${PROJECT_SOURCE_DIR}/cmake/tools/clang-format.cmake") | ||
| cpp_request_add_clang_format_targets() | ||
| endif() | ||
|
|
||
| install( | ||
| FILES | ||
| ${CMAKE_CURRENT_BINARY_DIR}/cpp_requestConfig.cmake | ||
| ${CMAKE_CURRENT_BINARY_DIR}/cpp_requestConfigVersion.cmake | ||
| DESTINATION ${CPP_REQUEST_INSTALL_CMAKEDIR} | ||
| ) | ||
| if(PROJECT_IS_TOP_LEVEL AND CPP_REQUEST_ENABLE_DOXYGEN) | ||
| include("${PROJECT_SOURCE_DIR}/cmake/tools/doxygen.cmake") | ||
| cpp_request_add_doxygen_target(cpp_request_docs) | ||
| endif() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # CMake structure | ||
|
|
||
| `cpp_request` keeps the root `CMakeLists.txt` as an orchestrator. Project behavior is split by responsibility: | ||
|
|
||
| - `modules/` — project options, target policy, tests, benchmarks, examples, and install/export rules. | ||
| - `packages/` — development-only third-party discovery with `find_package()` first and `FetchContent` fallback. | ||
| - `tools/` — optional developer tooling such as clang-format, clang-tidy, and Doxygen. | ||
| - `templates/` — inputs for files generated into the build/install tree. | ||
|
|
||
| Normal CMake configure must not modify files in the source tree. In particular, `vcpkg.json` is source-controlled input and is not generated by CMake. | ||
|
|
||
| ## Main options | ||
|
|
||
| - `CPP_REQUEST_BUILD_TESTS` | ||
| - `CPP_REQUEST_BUILD_BENCHMARKS` | ||
| - `CPP_REQUEST_BUILD_EXAMPLES` | ||
| - `CPP_REQUEST_ENABLE_INSTALL` | ||
| - `CPP_REQUEST_ENABLE_WARNINGS` | ||
| - `CPP_REQUEST_WARNINGS_AS_ERRORS` | ||
| - `CPP_REQUEST_ENABLE_SANITIZERS` | ||
| - `CPP_REQUEST_ENABLE_IPO` | ||
| - `CPP_REQUEST_ENABLE_NATIVE_OPTIMIZATION` | ||
| - `CPP_REQUEST_ENABLE_CLANG_TIDY` | ||
| - `CPP_REQUEST_ENABLE_CLANG_FORMAT` | ||
| - `CPP_REQUEST_ENABLE_DOXYGEN` | ||
|
|
||
| Tests, examples, and install rules default on only when `cpp_request` is the top-level project. Benchmarks and developer tools are opt-in. For compatibility with conventional CMake callers, an explicitly supplied `BUILD_TESTING` value is used as the default for `CPP_REQUEST_BUILD_TESTS`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,28 @@ | ||
| include(${CMAKE_SOURCE_DIR}/cmake/packages/google-benchmark.cmake) | ||
|
|
||
| function(add_benchmark_target name) | ||
| add_executable(${name} ${ARGN}) | ||
| target_link_libraries(${name} | ||
| PRIVATE | ||
| include_guard(GLOBAL) | ||
|
|
||
| include("${PROJECT_SOURCE_DIR}/cmake/packages/google-benchmark.cmake") | ||
| find_package(Threads REQUIRED) | ||
|
|
||
| function(cpp_request_add_benchmark target source) | ||
| add_executable(${target} ${source}) | ||
|
|
||
| target_include_directories(${target} | ||
| PRIVATE | ||
| ${PROJECT_SOURCE_DIR}/src | ||
| ${PROJECT_SOURCE_DIR}/benchmarks | ||
| ) | ||
|
|
||
| target_link_libraries(${target} | ||
| PRIVATE | ||
| cpp_request::cpp_request | ||
| benchmark::benchmark_main | ||
| ${PROJECT_NAME}::internal | ||
| Threads::Threads | ||
| ) | ||
| endfunction() | ||
|
|
||
| if(WIN32) | ||
| target_link_libraries(${target} PRIVATE ws2_32) | ||
| endif() | ||
|
|
||
| target_compile_features(${target} PRIVATE cxx_std_17) | ||
| cpp_request_configure_target(${target}) | ||
| endfunction() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| include_guard(GLOBAL) | ||
|
|
||
| function(cpp_request_add_example target source) | ||
| add_executable(${target} ${source}) | ||
| target_link_libraries(${target} PRIVATE cpp_request::cpp_request) | ||
| target_compile_features(${target} PRIVATE cxx_std_17) | ||
| cpp_request_configure_target(${target}) | ||
| endfunction() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| include_guard(GLOBAL) | ||
|
|
||
| include(CMakePackageConfigHelpers) | ||
|
|
||
| function(cpp_request_configure_install) | ||
| set( | ||
| CPP_REQUEST_INSTALL_CMAKEDIR | ||
| "${CMAKE_INSTALL_LIBDIR}/cmake/cpp_request" | ||
| ) | ||
|
|
||
| configure_package_config_file( | ||
| "${PROJECT_SOURCE_DIR}/cmake/templates/cpp_requestConfig.cmake.in" | ||
| "${PROJECT_BINARY_DIR}/cpp_requestConfig.cmake" | ||
| INSTALL_DESTINATION "${CPP_REQUEST_INSTALL_CMAKEDIR}" | ||
| ) | ||
|
|
||
| write_basic_package_version_file( | ||
| "${PROJECT_BINARY_DIR}/cpp_requestConfigVersion.cmake" | ||
| VERSION "${PROJECT_VERSION}" | ||
| COMPATIBILITY SameMinorVersion | ||
| ) | ||
|
|
||
| install( | ||
| TARGETS cpp_request | ||
| EXPORT cpp_requestTargets | ||
| ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" | ||
| LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" | ||
| RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" | ||
| INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" | ||
| ) | ||
|
|
||
| install( | ||
| DIRECTORY "${PROJECT_SOURCE_DIR}/include/cpp_request" | ||
| DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" | ||
| FILES_MATCHING PATTERN "*.hpp" | ||
| ) | ||
|
|
||
| install( | ||
| EXPORT cpp_requestTargets | ||
| FILE cpp_requestTargets.cmake | ||
| NAMESPACE cpp_request:: | ||
| DESTINATION "${CPP_REQUEST_INSTALL_CMAKEDIR}" | ||
| ) | ||
|
|
||
| install( | ||
| FILES | ||
| "${PROJECT_BINARY_DIR}/cpp_requestConfig.cmake" | ||
| "${PROJECT_BINARY_DIR}/cpp_requestConfigVersion.cmake" | ||
| DESTINATION "${CPP_REQUEST_INSTALL_CMAKEDIR}" | ||
| ) | ||
| endfunction() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| include_guard(GLOBAL) | ||
|
|
||
| function(cpp_request_apply_optimizations target) | ||
| if(CPP_REQUEST_ENABLE_NATIVE_OPTIMIZATION) | ||
| if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") | ||
| target_compile_options(${target} | ||
| PRIVATE | ||
| $<$<CONFIG:Release>:-march=native> | ||
| $<$<CONFIG:Release>:-mtune=native> | ||
| ) | ||
| else() | ||
| message(WARNING | ||
| "CPP_REQUEST_ENABLE_NATIVE_OPTIMIZATION currently supports Clang and GCC only" | ||
| ) | ||
| endif() | ||
| endif() | ||
|
|
||
| if(CPP_REQUEST_ENABLE_IPO) | ||
| include(CheckIPOSupported) | ||
| check_ipo_supported(RESULT _cpp_request_ipo_supported OUTPUT _cpp_request_ipo_error) | ||
| if(_cpp_request_ipo_supported) | ||
| set_property( | ||
| TARGET ${target} | ||
| PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE | ||
| ) | ||
| else() | ||
| message(WARNING "IPO is unavailable: ${_cpp_request_ipo_error}") | ||
| endif() | ||
| endif() | ||
| endfunction() |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| include_guard(GLOBAL) | ||
|
|
||
| function(cpp_request_setup_options) | ||
| set(_cpp_request_top_level_default OFF) | ||
| if(PROJECT_IS_TOP_LEVEL) | ||
| set(_cpp_request_top_level_default ON) | ||
| endif() | ||
|
|
||
| set(_cpp_request_test_default ${_cpp_request_top_level_default}) | ||
| if(DEFINED BUILD_TESTING) | ||
| set(_cpp_request_test_default ${BUILD_TESTING}) | ||
| endif() | ||
|
|
||
| option( | ||
| CPP_REQUEST_BUILD_TESTS | ||
| "Build cpp_request tests" | ||
| ${_cpp_request_test_default} | ||
| ) | ||
| option( | ||
| CPP_REQUEST_BUILD_BENCHMARKS | ||
| "Build cpp_request benchmarks" | ||
| OFF | ||
| ) | ||
| option( | ||
| CPP_REQUEST_BUILD_EXAMPLES | ||
| "Build cpp_request examples" | ||
| ${_cpp_request_top_level_default} | ||
| ) | ||
| option( | ||
| CPP_REQUEST_ENABLE_INSTALL | ||
| "Enable cpp_request install and package export rules" | ||
| ${_cpp_request_top_level_default} | ||
| ) | ||
|
|
||
| option(CPP_REQUEST_ENABLE_WARNINGS "Enable compiler warnings" ON) | ||
| option(CPP_REQUEST_WARNINGS_AS_ERRORS "Treat compiler warnings as errors" OFF) | ||
| option(CPP_REQUEST_ENABLE_SANITIZERS "Enable AddressSanitizer and UndefinedBehaviorSanitizer" OFF) | ||
| option(CPP_REQUEST_ENABLE_IPO "Enable interprocedural optimization for Release builds" OFF) | ||
| option(CPP_REQUEST_ENABLE_NATIVE_OPTIMIZATION "Enable host-specific optimization for local Release builds" OFF) | ||
|
|
||
| option(CPP_REQUEST_ENABLE_CLANG_TIDY "Enable clang-tidy for cpp_request targets" OFF) | ||
| option(CPP_REQUEST_ENABLE_CLANG_FORMAT "Add clang-format helper targets" OFF) | ||
| option(CPP_REQUEST_ENABLE_DOXYGEN "Add the Doxygen documentation target" OFF) | ||
| endfunction() |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
CPP_REQUEST_BUILD_TESTS=ONandCPP_REQUEST_ENABLE_INSTALL=OFF—including a subproject that enables tests while retaining the new default of disabled installs—tests/CMakeLists.txtstill registersPackageConsumer.InstallAndUse. That test runscmake --install, then its consumer fails atfind_package(cpp_request)because this conditional generated neither the package configuration nor export rules. Guard the package-consumer test withCPP_REQUEST_ENABLE_INSTALL, or prevent this option combination.Useful? React with 👍 / 👎.