Skip to content

Commit 72c5347

Browse files
authored
Merge pull request #8 from Serial-IO/use-version-module
Refactor CMake configuration and versioning system
2 parents 3ff7894 + c510c22 commit 72c5347

File tree

8 files changed

+1402
-132
lines changed

8 files changed

+1402
-132
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
include/cpp_core/version.h
1+
include/cpp_core/version.hpp
22
build/
33
.cache/
44
compile_commands.json

CMakeLists.txt

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,28 @@ cmake_minimum_required(VERSION 3.30)
33
# Export compile commands to root directory
44
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
55

6-
# ---------------------------------------------------------------------------
7-
# Version information from Git tags
8-
# Version is determined automatically from Git tags and cannot be overridden
9-
# ---------------------------------------------------------------------------
10-
include(${CMAKE_CURRENT_LIST_DIR}/cmake/get_version_from_git.cmake)
11-
12-
# Get version from Git tags
13-
get_version_from_git()
14-
set(CPP_CORE_VERSION_MAJOR ${VERSION_MAJOR} CACHE STRING "Major version number" FORCE)
15-
set(CPP_CORE_VERSION_MINOR ${VERSION_MINOR} CACHE STRING "Minor version number" FORCE)
16-
set(CPP_CORE_VERSION_PATCH ${VERSION_PATCH} CACHE STRING "Patch version number" FORCE)
17-
set(CPP_CORE_VERSION_STRING ${VERSION_STRING} CACHE STRING "Full version string" FORCE)
18-
19-
# Set PROJECT_VERSION for CMake package config (only MAJOR.MINOR.PATCH, no suffix)
20-
set(PROJECT_VERSION "${CPP_CORE_VERSION_MAJOR}.${CPP_CORE_VERSION_MINOR}.${CPP_CORE_VERSION_PATCH}")
6+
include(cmake/CPM.cmake)
7+
8+
CPMAddPackage(
9+
NAME cmake-git-versioning
10+
GITHUB_REPOSITORY Katze719/cmake-git-versioning
11+
GIT_TAG v1.0.0
12+
)
13+
14+
# Include the cmake-git-versioning module
15+
include(${cmake-git-versioning_SOURCE_DIR}/cmake/cmake-git-versioning.cmake)
16+
17+
# Get git version info (sets CMake variables)
18+
get_git_version_info()
19+
generate_git_version(OUTPUT_DIR ${CMAKE_SOURCE_DIR}/include/cpp_core)
2120

2221
project(
2322
cpp-core
24-
VERSION ${PROJECT_VERSION}
23+
VERSION "${GIT_VERSION_MAJOR}.${GIT_VERSION_MINOR}.${GIT_VERSION_PATCH}"
2524
DESCRIPTION "Cross-platform helper library shared by cpp-linux-bindings and cpp-windows-bindings"
2625
LANGUAGES CXX
2726
)
2827

29-
configure_file(
30-
${CMAKE_CURRENT_SOURCE_DIR}/include/cpp_core/version.h.in
31-
${CMAKE_CURRENT_SOURCE_DIR}/include/cpp_core/version.h
32-
@ONLY
33-
)
34-
3528
# Header-only library --------------------------------------------------------
3629
add_library(cpp_core INTERFACE)
3730
add_library(cpp_core::cpp_core ALIAS cpp_core)
@@ -40,8 +33,8 @@ add_library(cpp_core::cpp_core ALIAS cpp_core)
4033
target_include_directories(
4134
cpp_core
4235
INTERFACE
43-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
44-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
36+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
37+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
4538
)
4639

4740
# Set C++ standard
@@ -74,9 +67,9 @@ include(CMakePackageConfigHelpers)
7467

7568
write_basic_package_version_file(
7669
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfigVersion.cmake
77-
VERSION ${PROJECT_VERSION}
70+
VERSION "${GIT_VERSION_MAJOR}.${GIT_VERSION_MINOR}.${GIT_VERSION_PATCH}"
7871
COMPATIBILITY SameMajorVersion
79-
)
72+
)
8073

8174
configure_package_config_file(
8275
${CMAKE_CURRENT_LIST_DIR}/cmake/cpp_core_config.cmake.in
@@ -93,18 +86,18 @@ install(
9386

9487
install(
9588
FILES
96-
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfig.cmake
97-
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfigVersion.cmake
89+
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfig.cmake
90+
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfigVersion.cmake
9891
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpp_core
9992
)
10093

10194
if(CMAKE_EXPORT_COMPILE_COMMANDS AND EXISTS "${CMAKE_BINARY_DIR}/compile_commands.json")
10295
add_custom_target(
10396
copy-compile-commands
10497
ALL
105-
${CMAKE_COMMAND} -E copy_if_different
106-
${CMAKE_BINARY_DIR}/compile_commands.json
107-
${CMAKE_SOURCE_DIR}/compile_commands.json
98+
${CMAKE_COMMAND} -E copy_if_different
99+
${CMAKE_BINARY_DIR}/compile_commands.json
100+
${CMAKE_SOURCE_DIR}/compile_commands.json
108101
DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json
109102
COMMENT "Copying compile_commands.json to project root"
110103
)

0 commit comments

Comments
 (0)