Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{cmake} Use git tag in library version for more precision #298

Merged
merged 1 commit into from
Jun 20, 2024
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
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ string( TOUPPER ${PROJECT_NAME} PROJECT_NAME_UPPERCASE )
# Creates a build tag which is used in the REVISION_ID
# e.g. "x86_64-darwin-AppleClang140"
include( Tags )
include( GitInfo )

# Collect all our current git info using:
# https://github.com/cppmf/GitInfo.cmake
GitInfo( ${CMAKE_CURRENT_SOURCE_DIR} )
# GitInfoReport()

# Check if we are building ourself or being included and use this to set some defaults
if ( ${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME} )
Expand Down Expand Up @@ -111,7 +117,7 @@ option( E57_RELEASE_LTO "Compile release library with link-time optimization" ON

#########################################################################################

set( REVISION_ID "${PROJECT_NAME}-${PROJECT_VERSION}-${${PROJECT_NAME}_BUILD_TAG}" )
set( REVISION_ID "${PROJECT_NAME}-${GIT_LATEST_TAG}-${${PROJECT_NAME}_BUILD_TAG}" )
message( STATUS "[${PROJECT_NAME}] Revision ID: ${REVISION_ID}" )

# Target
Expand Down
138 changes: 138 additions & 0 deletions cmake/GitInfo.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# ---------------------------------------------------------------------------- #
#
# Copyright (c) 2020 C++ Modern Framework
#
# https://github.com/cppmf/GitInfo.cmake
#
# ---------------------------------------------------------------------------- #

# Modified June 2024 - Andy Maloney <asmaloney@gmail.com>
# - remove some vars we aren't using
# - fix spelling/grammar

# ---------------------------------------------------------------------------- #
#
# Following variables will be set when calling GitInfo
#
# GIT_DIR: path to the project .git directory
# GIT_IS_DIRTY: whether or not the working tree is dirty
# GIT_HEAD_BRANCH : name of the branch associated to HEAD
# GIT_REVISION_HASH: current HEAD sha hash
# GIT_REVISION: short version of GIT_REVISION_HASH
# GIT_REVISION_NAME: name associated to GIT_REVISION_HASH
# GIT_REMOTE_ORIGIN_URL : origin remote url
# GIT_LATEST_TAG_LONG : most recent tag of the current branch
# GIT_LATEST_TAG : most recent tagname of the current branch
#
# ---------------------------------------------------------------------------- #

# This is the main function to call in project CMakeLists.txt
# source should point to the root project directory
function(GitInfo source)

# Check is source is a valid path
if(NOT EXISTS ${source})
message(FATAL_ERROR "'${source}' is not a valid path")
endif()

# Define the possible location of the .git directory
set(GIT_DIR "${source}/.git")

# Check if .git folder exist
if(EXISTS ${GIT_DIR})

#
set(GIT_DIR "${GIT_DIR}" CACHE PATH "Project .git directory")

# Check if git is installed
if(NOT GIT_FOUND)
find_package(Git QUIET)
endif()
if(NOT GIT_FOUND)
message(AUTHOR_WARNING "Git not found, cannot get git informations")
return()
endif()

# whether or not the working tree is dirty
execute_process(COMMAND ${GIT_EXECUTABLE} status --porcelain
WORKING_DIRECTORY ${source}
RESULT_VARIABLE exit_code
OUTPUT_VARIABLE GIT_IS_DIRTY OUTPUT_STRIP_TRAILING_WHITESPACE)
# the working tree is dirty when the error code is different from 0
# or if the output is not empty
if(NOT exit_code EQUAL 0 OR NOT ${GIT_IS_DIRTY} STREQUAL "")
unset(GIT_IS_DIRTY)
set(GIT_IS_DIRTY ON CACHE BOOL "Indicate if current branch is dirty")
else()
unset(GIT_IS_DIRTY)
set(GIT_IS_DIRTY OFF CACHE BOOL "Indicate if current branch is dirty")
endif()

# name of the branch associated to HEAD
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_HEAD_BRANCH OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_HEAD_BRANCH "${GIT_HEAD_BRANCH}" CACHE INTERNAL "name of the branch associated to HEAD")

# git revision full hash
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%H" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_REVISION_HASH OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_REVISION_HASH "${GIT_REVISION_HASH}" CACHE INTERNAL "git revision full hash")

# short version of git revision
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%h" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_REVISION OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_REVISION "${GIT_REVISION}" CACHE INTERNAL "short version of git revision")

# short version of git revision name
execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%s" HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_REVISION_NAME OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_REVISION_NAME "${GIT_REVISION_NAME}" CACHE INTERNAL "short version of git revision name")

# origin remote url
execute_process(COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_REMOTE_ORIGIN_URL OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_REMOTE_ORIGIN_URL "${GIT_REMOTE_ORIGIN_URL}" CACHE INTERNAL "git origin remote url")

# most recent tag of the current branch
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_LATEST_TAG_LONG OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_LATEST_TAG_LONG "${GIT_LATEST_TAG_LONG}" CACHE INTERNAL "git most recent tag of the current branch")

# most recent tagname of the current branch
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags HEAD
WORKING_DIRECTORY ${source}
OUTPUT_VARIABLE GIT_LATEST_TAG OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_LATEST_TAG "${GIT_LATEST_TAG}" CACHE INTERNAL "git most recent tagname of the current branch")

endif()

endfunction()


# Report git information
function(GitInfoReport)
message(STATUS "")
message(STATUS "----------------------------------------------------")
message(STATUS " GitInfo.cmake")
message(STATUS "")
message(STATUS "GIT_DIR : ${GIT_DIR}")
message(STATUS "")
message(STATUS "GIT_IS_DIRTY : ${GIT_IS_DIRTY}")
message(STATUS "GIT_HEAD_BRANCH : ${GIT_HEAD_BRANCH}")
message(STATUS "GIT_REVISION : ${GIT_REVISION}")
message(STATUS "GIT_REVISION_HASH : ${GIT_REVISION_HASH}")
message(STATUS "GIT_REVISION_NAME : ${GIT_REVISION_NAME}")
message(STATUS "")
message(STATUS "GIT_REMOTE_ORIGIN_URL : ${GIT_REMOTE_ORIGIN_URL}")
message(STATUS "GIT_LATEST_TAG_LONG : ${GIT_LATEST_TAG_LONG}")
message(STATUS "GIT_LATEST_TAG : ${GIT_LATEST_TAG}")
message(STATUS "")
message(STATUS "----------------------------------------------------")
message(STATUS "")
endfunction()
Loading