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
71 changes: 30 additions & 41 deletions CMakeLists.txt
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Skip the package test when install rules are disabled

When CPP_REQUEST_BUILD_TESTS=ON and CPP_REQUEST_ENABLE_INSTALL=OFF—including a subproject that enables tests while retaining the new default of disabled installs—tests/CMakeLists.txt still registers PackageConsumer.InstallAndUse. That test runs cmake --install, then its consumer fails at find_package(cpp_request) because this conditional generated neither the package configuration nor export rules. Guard the package-consumer test with CPP_REQUEST_ENABLE_INSTALL, or prevent this option combination.

Useful? React with 👍 / 👎.

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()
23 changes: 0 additions & 23 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
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
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(${target}
PRIVATE
cpp_request::internal
benchmark::benchmark_main
Threads::Threads
)
target_compile_features(${target} PRIVATE cxx_std_17)

if(WIN32)
target_link_libraries(${target} PRIVATE ws2_32)
endif()
endfunction()

cpp_request_add_benchmark(
cpp_request_bench_request_serializer
request_serializer_benchmark.cpp
Expand Down
27 changes: 27 additions & 0 deletions cmake/README.md
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`.
34 changes: 26 additions & 8 deletions cmake/modules/benchmarking.cmake
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()
8 changes: 8 additions & 0 deletions cmake/modules/examples.cmake
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()
51 changes: 51 additions & 0 deletions cmake/modules/install.cmake
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()
30 changes: 30 additions & 0 deletions cmake/modules/optimizations.cmake
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()
56 changes: 0 additions & 56 deletions cmake/modules/optimizing.cmake

This file was deleted.

44 changes: 44 additions & 0 deletions cmake/modules/options.cmake
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()
5 changes: 0 additions & 5 deletions cmake/modules/output.cmake

This file was deleted.

Loading
Loading