fix(cmake): properly fetch dev version by appending latest Falco tag, delta between master and tag, and hash.

`describe` can no more be used as tags are now made on release branches.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
This commit is contained in:
Federico Di Pierro
2022-11-15 15:59:38 +01:00
committed by poiana
parent ec04b758e6
commit 4696948754
3 changed files with 144 additions and 27 deletions

View File

@@ -16,18 +16,32 @@ include(GetGitRevisionDescription)
# Create the falco version variable according to git index # Create the falco version variable according to git index
if(NOT FALCO_VERSION) if(NOT FALCO_VERSION)
string(STRIP "${FALCO_HASH}" FALCO_HASH)
# Try to obtain the exact git tag # Try to obtain the exact git tag
git_get_exact_tag(FALCO_TAG) git_get_exact_tag(FALCO_TAG)
if(NOT FALCO_TAG) if(NOT FALCO_TAG)
# Obtain the closest tag # Fetch current hash
git_describe(FALCO_VERSION "--always" "--tags" "--abbrev=7") get_git_head_revision(refspec FALCO_HASH)
# Fallback version if(NOT FALCO_HASH OR FALCO_HASH MATCHES "NOTFOUND$")
if(FALCO_VERSION MATCHES "NOTFOUND$") set(FALCO_VERSION "0.0.0")
set(FALCO_VERSION "0.0.0") else()
endif() # Obtain the closest tag
# Format FALCO_VERSION to be semver with prerelease and build part git_get_latest_tag(FALCO_LATEST_TAG)
string(REPLACE "-g" "+" FALCO_VERSION "${FALCO_VERSION}") if(NOT FALCO_LATEST_TAG OR FALCO_LATEST_TAG MATCHES "NOTFOUND$")
set(FALCO_VERSION "0.0.0")
else()
# Compute commit delta since tag
git_get_delta_from_tag(FALCO_DELTA ${FALCO_LATEST_TAG} ${FALCO_HASH})
if(NOT FALCO_DELTA OR FALCO_DELTA MATCHES "NOTFOUND$")
set(FALCO_VERSION "0.0.0")
else()
# Cut hash to 7 bytes
string(SUBSTRING ${FALCO_HASH} 0 7 FALCO_HASH)
# Format FALCO_VERSION to be semver with prerelease and build part
set(FALCO_VERSION
"${FALCO_LATEST_TAG}-${FALCO_DELTA}+${FALCO_HASH}")
endif()
endif()
endif()
else() else()
# A tag has been found: use it as the Falco version # A tag has been found: use it as the Falco version
set(FALCO_VERSION "${FALCO_TAG}") set(FALCO_VERSION "${FALCO_TAG}")

View File

@@ -86,29 +86,36 @@ function(get_git_head_revision _refspecvar _hashvar)
PARENT_SCOPE) PARENT_SCOPE)
endfunction() endfunction()
function(git_describe _var) function(git_get_latest_tag _var)
if(NOT GIT_FOUND) if(NOT GIT_FOUND)
find_package(Git QUIET) find_package(Git QUIET)
endif() endif()
get_git_head_revision(refspec hash)
if(NOT GIT_FOUND) # We use git describe --tags `git rev-list --tags --max-count=1`
set(${_var} execute_process(COMMAND
"GIT-NOTFOUND" "${GIT_EXECUTABLE}"
PARENT_SCOPE) rev-list
return() --tags
endif() --max-count=1
if(NOT hash) WORKING_DIRECTORY
set(${_var} "${CMAKE_CURRENT_SOURCE_DIR}"
"HEAD-HASH-NOTFOUND" COMMAND tail -n1
PARENT_SCOPE) RESULT_VARIABLE
res
OUTPUT_VARIABLE
tag_hash
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0)
set(out "${tag_hash}-${res}-NOTFOUND" PARENT_SCOPE)
return() return()
endif() endif()
execute_process(COMMAND execute_process(COMMAND
"${GIT_EXECUTABLE}" "${GIT_EXECUTABLE}"
describe describe
${hash} --tags
${ARGN} ${tag_hash}
WORKING_DIRECTORY WORKING_DIRECTORY
"${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}"
RESULT_VARIABLE RESULT_VARIABLE
@@ -120,10 +127,108 @@ function(git_describe _var)
if(NOT res EQUAL 0) if(NOT res EQUAL 0)
set(out "${out}-${res}-NOTFOUND") set(out "${out}-${res}-NOTFOUND")
endif() endif()
set(${_var} "${out}" PARENT_SCOPE)
endfunction()
function(git_get_delta_from_tag _var tag hash)
if(NOT GIT_FOUND)
find_package(Git QUIET)
endif()
# Count commits in HEAD
execute_process(COMMAND
"${GIT_EXECUTABLE}"
rev-list
--count
${hash}
WORKING_DIRECTORY
"${CMAKE_CURRENT_SOURCE_DIR}"
RESULT_VARIABLE
res
OUTPUT_VARIABLE
out_counter_head
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0)
set(${_var} "HEADCOUNT-NOTFOUND" PARENT_SCOPE)
return()
endif()
# Count commits in latest tag
execute_process(COMMAND
"${GIT_EXECUTABLE}"
rev-list
--count
${tag}
WORKING_DIRECTORY
"${CMAKE_CURRENT_SOURCE_DIR}"
RESULT_VARIABLE
res
OUTPUT_VARIABLE
out_counter_tag
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0)
set(${_var} "TAGCOUNT-NOTFOUND" PARENT_SCOPE)
return()
endif()
execute_process(COMMAND
expr
${out_counter_head} - ${out_counter_tag}
WORKING_DIRECTORY
"${CMAKE_CURRENT_SOURCE_DIR}"
RESULT_VARIABLE
res
OUTPUT_VARIABLE
out_delta
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0)
set(${_var} "DELTA-NOTFOUND" PARENT_SCOPE)
return()
endif()
set(${_var} "${out_delta}" PARENT_SCOPE)
endfunction()
function(git_describe _var)
if(NOT GIT_FOUND)
find_package(Git QUIET)
endif()
get_git_head_revision(refspec hash)
if(NOT GIT_FOUND)
set(${_var}
"GIT-NOTFOUND"
PARENT_SCOPE)
return()
endif()
if(NOT hash)
set(${_var}
"HEAD-HASH-NOTFOUND"
PARENT_SCOPE)
return()
endif()
execute_process(COMMAND
"${GIT_EXECUTABLE}"
describe
${hash}
${ARGN}
WORKING_DIRECTORY
"${CMAKE_CURRENT_SOURCE_DIR}"
RESULT_VARIABLE
res
OUTPUT_VARIABLE
out
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0)
set(out "${out}-${res}-NOTFOUND")
endif()
set(${_var} set(${_var}
"${out}" "${out}"
PARENT_SCOPE) PARENT_SCOPE)
endfunction() endfunction()
function(git_get_exact_tag _var) function(git_get_exact_tag _var)

View File

@@ -16,8 +16,6 @@ limitations under the License.
#pragma once #pragma once
#define FALCO_BRANCH "@FALCO_REF@"
#define FALCO_HASH "@FALCO_HASH@"
#define FALCO_VERSION "@FALCO_VERSION@" #define FALCO_VERSION "@FALCO_VERSION@"
#define FALCO_VERSION_MAJOR @FALCO_VERSION_MAJOR@ #define FALCO_VERSION_MAJOR @FALCO_VERSION_MAJOR@
#define FALCO_VERSION_MINOR @FALCO_VERSION_MINOR@ #define FALCO_VERSION_MINOR @FALCO_VERSION_MINOR@