Skip to content

Commit a1b763b

Browse files
committed
Refactor: separate cmake tools/options + setup docs generation
1 parent c3cfcc2 commit a1b763b

File tree

12 files changed

+102
-73
lines changed

12 files changed

+102
-73
lines changed

.github/workflows/docs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
push:
88
branches: [master]
99
paths:
10-
- 'Doxyfile'
10+
- 'docs/Doxyfile.in'
1111
- 'include/**/*.hpp'
1212
- 'src/**/*.cpp'
1313
- '.github/workflows/docs.yml'
@@ -25,7 +25,7 @@ jobs:
2525
- name: Set up CMake + Doxygen dependencies
2626
run: |
2727
sudo apt update
28-
sudo apt install -y cmake doxygen graphviz ninja-build
28+
sudo apt install -y doxygen graphviz cmake ninja-build g++ gcc
2929
3030
- name: Configure project with CMake Preset
3131
run: cmake --preset gcc-RelWithDebInfo

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ cmake_install.cmake
66
Makefile
77
compile_commands.json
88
install/
9+
!cmake/install
910

1011
# === Ninja build ===
1112
*.ninja
@@ -51,5 +52,5 @@ test_output/
5152

5253
# === Docs ===
5354
docs/*
55+
!docs/Doxyfile.in
5456
!docs/mainpage.md
55-

CMakeLists.txt

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ if(CMAKE_CONFIGURATION_TYPES)
1818
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING "" FORCE)
1919
endif()
2020

21-
# Load cmake modules: sanitizers, warnings, lto
21+
# Load cmake modules
2222
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
2323

24-
include(LTO)
25-
include(Warnings)
26-
include(Sanitizers)
24+
# Warnings, sanitizers, Link Time Optimiziatiion (LTO)
25+
include(options/LTO)
26+
include(options/Warnings)
27+
include(options/Sanitizers)
2728

2829
# === Libraries ===
29-
add_subdirectory(src)
30+
add_subdirectory(src) # modern_cpp_template::math
3031

3132
# === Application ===
3233
add_executable(${PROJECT_NAME}
@@ -41,72 +42,14 @@ enable_lto(${PROJECT_NAME})
4142
enable_sanitizers(${PROJECT_NAME})
4243
enable_strict_warnings(${PROJECT_NAME})
4344

44-
# === Unit Testing ===
45+
# === Tests ===
4546
if(ENABLE_TESTING)
4647
enable_testing()
4748
add_subdirectory(tests)
4849
endif()
4950

50-
# === Installtion ===
51-
include(GNUInstallDirs)
52-
include(CMakePackageConfigHelpers)
51+
# === Docs ===
52+
include(tools/Doxygen)
5353

54-
install(TARGETS math
55-
EXPORT ${PROJECT_NAME}Targets
56-
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
57-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
58-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
59-
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
60-
)
61-
62-
install(
63-
DIRECTORY ${CMAKE_SOURCE_DIR}/include/
64-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
65-
)
66-
67-
# Export the targets to a file
68-
install(
69-
EXPORT ${PROJECT_NAME}Targets
70-
NAMESPACE ${PROJECT_NAME}::
71-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
72-
)
73-
74-
# Configure a version file
75-
write_basic_package_version_file(
76-
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
77-
VERSION ${PROJECT_VERSION}
78-
COMPATIBILITY SameMajorVersion
79-
)
80-
81-
# Configure the main config file
82-
configure_package_config_file(
83-
"${CMAKE_SOURCE_DIR}/cmake/Config.cmake.in"
84-
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
85-
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
86-
)
87-
88-
# Install the config + version
89-
install(FILES
90-
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
91-
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
92-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
93-
)
94-
95-
# === Documentation ===
96-
find_package(Doxygen QUIET)
97-
98-
if(DOXYGEN_FOUND)
99-
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile)
100-
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile.generated)
101-
102-
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
103-
104-
add_custom_target(docs
105-
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
106-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
107-
COMMENT "Generating API documentation with Doxygen"
108-
VERBATIM
109-
)
110-
else()
111-
message(STATUS "Doxygen not found. 'docs' target will not be available.")
112-
endif()
54+
# === Installation ===
55+
include(install/InstallConfig)

cmake/install/InstallConfig.cmake

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Enables standard install paths: lib/, include/, bin/, etc.
2+
include(GNUInstallDirs)
3+
4+
# Provides helpers for writing version/config files
5+
include(CMakePackageConfigHelpers)
6+
7+
# Install compiled library (static or shared)
8+
install(TARGETS math
9+
EXPORT ${PROJECT_NAME}Targets # export target for find_package
10+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # static libs
11+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} # shared libs
12+
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
13+
)
14+
15+
# Install public headers (recursive copy of include/)
16+
install(
17+
DIRECTORY ${CMAKE_SOURCE_DIR}/include/
18+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
19+
)
20+
21+
# Install target exports as <project>Targets.cmake
22+
install(
23+
EXPORT ${PROJECT_NAME}Targets
24+
NAMESPACE ${PROJECT_NAME}:: # enables find_package to use <project>::<library>
25+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
26+
)
27+
28+
# Generate a <project>ConfigVersion.cmake file
29+
write_basic_package_version_file(
30+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
31+
VERSION ${PROJECT_VERSION}
32+
COMPATIBILITY SameMajorVersion # enforces semantic versioning: X.*
33+
)
34+
35+
# Generate <project>Config.cmake from a template
36+
configure_package_config_file(
37+
"${CMAKE_SOURCE_DIR}/cmake/install/Config.cmake.in"
38+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
39+
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
40+
)
41+
42+
# Install config files
43+
install(FILES
44+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
45+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
46+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
47+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
# Apply LTO to a specific target
12
function(enable_lto target)
23
if(CMAKE_INTERPROCEDURAL_OPTIMIZATION)
34
set_property(TARGET ${target} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
45
message(STATUS "🔧\tEnabled LTO for target ${target}")
6+
else()
7+
message(STATUS "ℹ️\tCMAKE_INTERPROCEDURAL_OPTIMIZATION not enabled globally. Skipping LTO for target ${target}")
58
endif()
69
endfunction()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Define sanitizer flags only in Sanitize build type
12
if(CMAKE_BUILD_TYPE STREQUAL "Sanitize")
23
set(SANITIZER_FLAGS
34
-fsanitize=address
@@ -8,10 +9,13 @@ if(CMAKE_BUILD_TYPE STREQUAL "Sanitize")
89
message(STATUS "✅\tSanitize build enabled with flags: ${SANITIZER_FLAGS}")
910
endif()
1011

12+
# Apply sanitizer flags to a specific target
1113
function(enable_sanitizers target)
1214
if(CMAKE_BUILD_TYPE STREQUAL "Sanitize")
1315
target_compile_options(${target} PRIVATE ${SANITIZER_FLAGS})
1416
target_link_options(${target} PRIVATE ${SANITIZER_FLAGS})
1517
message(STATUS "✅\tEnabled sanitizers for target ${target}")
18+
else()
19+
message(STATUS "ℹ️\tSkipping sanitizers for target: ${target} (build type: ${CMAKE_BUILD_TYPE})")
1620
endif()
1721
endfunction()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Applies standard warning flags (-Wall, -Wextra, etc.)
12
function(enable_warnings target)
23
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
34
target_compile_options(${target} PRIVATE
@@ -8,9 +9,12 @@ function(enable_warnings target)
89
-Wsign-conversion
910
)
1011
message(STATUS "⚠️\tEnabled warnings for target ${target}")
12+
else()
13+
message(STATUS "Skipping warnings: unsupported compiler ${CMAKE_CXX_COMPILER_ID}")
1114
endif()
1215
endfunction()
1316

17+
# Applies warnings and treats them as errors (-Werror)
1418
function(enable_strict_warnings target)
1519
enable_warnings(${target})
1620
target_compile_options(${target} PRIVATE -Werror)

cmake/tools/ClangFormat.cmake

Whitespace-only changes.

cmake/tools/ClangTidy.cmake

Whitespace-only changes.

0 commit comments

Comments
 (0)