Skip to content

Commit

Permalink
cmake: add versioning module
Browse files Browse the repository at this point in the history
  • Loading branch information
Kola50011 committed Sep 23, 2024
1 parent 7ae7155 commit 1791063
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
20 changes: 17 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
#########
# Setup #
#########

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

include(Versioning)

########
# Main #
########

# After installation this project can be found by 'find_package' command:
#
# find_package(libcreate REQUIRED)
Expand All @@ -6,11 +18,14 @@
#

cmake_minimum_required(VERSION 3.28)
project(libcreate)
project(
libcreate
VERSION ${TAG_VERSION_MAJOR}.${TAG_VERSION_MINOR}.${TAG_VERSION_PATCH}
)

add_compile_options(-Wall -Wextra -Wpedantic)

set(PACKAGE_VERSION 3.0.0)
set(PACKAGE_VERSION ${TAG_VERSION_MAJOR}.${TAG_VERSION_MINOR}.${TAG_VERSION_PATCH})

option(LIBCREATE_BUILD_TESTS "Enable the build of tests." ON)

Expand Down Expand Up @@ -208,5 +223,4 @@ endif()
# Packaging #
#############

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(Packing)
26 changes: 26 additions & 0 deletions cmake/Versioning.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
find_package(Git)

if(GIT_EXECUTABLE)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags
OUTPUT_VARIABLE TAG_VERSION
RESULT_VARIABLE ERROR_CODE
OUTPUT_STRIP_TRAILING_WHITESPACE
)

if(TAG_VERSION STREQUAL "")
set(TAG_VERSION 0.0.0)
message(WARNING "Failed to determine version from Git tags. Using default version \"${TAG_VERSION}\".")
endif()

message(STATUS "Project version: ${TAG_VERSION}")

# Split into major, minor, patch
string(
REGEX MATCH "([0-9]+)\\.([0-9]+)\\.([0-9]+)"
TAG_VERSION_MATCH ${TAG_VERSION}
)
set(TAG_VERSION_MAJOR ${CMAKE_MATCH_1})
set(TAG_VERSION_MINOR ${CMAKE_MATCH_2})
set(TAG_VERSION_PATCH ${CMAKE_MATCH_3})
endif()

0 comments on commit 1791063

Please sign in to comment.