Skip to content

Commit

Permalink
build: Determine PROJECT_VERSION from git tag
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonKagstrom committed Jul 14, 2024
1 parent 29ae165 commit 4cb984c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
41 changes: 40 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
cmake_minimum_required (VERSION 3.12)
project (kcov)

set (PROJECT_VERSION_MAJOR 39)

# See https://eatmyrandom.blogspot.com/2010/06/automate-version-numbering-using-git.html
if (EXISTS "${CMAKE_SOURCE_DIR}/.git")
find_program (GIT_EXECUTABLE NAMES git)
if (GIT_EXECUTABLE)
message (STATUS "Found Git: ${GIT_EXECUTABLE}")
else (GIT_EXECUTABLE)
message (FATAL_ERROR "Could NOT find Git")
endif (GIT_EXECUTABLE)

execute_process (COMMAND "${GIT_EXECUTABLE}"
"--git-dir=${CMAKE_SOURCE_DIR}/.git"
describe
--abbrev=4
--tags
HEAD
OUTPUT_VARIABLE POD_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)

message (STATUS "kcov version: ${POD_VERSION}")
else (EXISTS "${CMAKE_SOURCE_DIR}/.git")
execute_process (COMMAND head
-n 1
"${CMAKE_SOURCE_DIR}/ChangeLog"
COMMAND cut -d \( -f 2
COMMAND cut -d \) -f 1
OUTPUT_VARIABLE POD_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)

message (STATUS "kcov version: ${POD_VERSION} (from changelog)")
endif (EXISTS "${CMAKE_SOURCE_DIR}/.git")

# Example POD_VERSION format: v42-148-g29ae1
string(REGEX REPLACE "v([0-9]+).*" "\\1" PROJECT_VERSION_MAJOR ${POD_VERSION})

# Set the PROJECT_VERSION_MAJOR with the extracted version
set (PROJECT_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set (PROJECT_VERSION_MINOR 0)
set (PROJECT_VERSION_PATCH 0)
set (PROJECT_VERSION
"${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")


if (POLICY CMP0042)
# MACOSX_RPATH is enabled by default.
cmake_policy (SET CMP0042 NEW)
Expand Down
33 changes: 0 additions & 33 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,6 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set (KCOV kcov)


# See https://eatmyrandom.blogspot.com/2010/06/automate-version-numbering-using-git.html
if (EXISTS "${CMAKE_SOURCE_DIR}/.git")
find_program (GIT_EXECUTABLE NAMES git)
if (GIT_EXECUTABLE)
message (STATUS "Found Git: ${GIT_EXECUTABLE}")
else (GIT_EXECUTABLE)
message (FATAL_ERROR "Could NOT find Git")
endif (GIT_EXECUTABLE)

execute_process (COMMAND "${GIT_EXECUTABLE}"
"--git-dir=${CMAKE_SOURCE_DIR}/.git"
describe
--abbrev=4
--tags
HEAD
OUTPUT_VARIABLE POD_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)

message (STATUS "kcov version: ${POD_VERSION}")
else (EXISTS "${CMAKE_SOURCE_DIR}/.git")
execute_process (COMMAND head
-n 1
"${CMAKE_SOURCE_DIR}/ChangeLog"
COMMAND cut -d \( -f 2
COMMAND cut -d \) -f 1
OUTPUT_VARIABLE POD_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)

message (STATUS "kcov version: ${POD_VERSION} (from changelog)")
endif (EXISTS "${CMAKE_SOURCE_DIR}/.git")

add_custom_command(
OUTPUT version.c
COMMAND "${CMAKE_COMMAND}"
Expand Down

0 comments on commit 4cb984c

Please sign in to comment.