build: better way to extract falco commit hash (also extract ref)

Signed-off-by: Leonardo Di Donato <leodidonato@gmail.com>
This commit is contained in:
Leonardo Di Donato 2019-10-04 16:00:42 +00:00 committed by poiana
parent 2a91289ee4
commit bc297bdc8f
3 changed files with 63 additions and 51 deletions

View File

@ -67,20 +67,33 @@ set(CMAKE_CXX_FLAGS_DEBUG "${DRAIOS_DEBUG_FLAGS}")
set(CMAKE_C_FLAGS_RELEASE "-O3 -fno-strict-aliasing -DNDEBUG") set(CMAKE_C_FLAGS_RELEASE "-O3 -fno-strict-aliasing -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -fno-strict-aliasing -DNDEBUG") set(CMAKE_CXX_FLAGS_RELEASE "-O3 -fno-strict-aliasing -DNDEBUG")
add_definitions(-DPLATFORM_NAME="${CMAKE_SYSTEM_NAME}")
add_definitions(-DK8S_DISABLE_THREAD)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
add_definitions(-DHAS_CAPTURE)
endif()
# Retrieve git ref and commit hash
include(GetGitRevisionDescription)
get_git_head_revision(FALCO_REF FALCO_HASH)
# 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)
include(GetGitRevisionDescription) string(STRIP "${FALCO_HASH}" FALCO_HASH)
# 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 nearest tag # Obtain the closest tag
git_describe(FALCO_VERSION "--abbrev=0") # suppress the long format git_describe(FALCO_VERSION "--abbrev=0" "--tags") # suppress the long format
# Obtain the git commit # Fallback version
git_describe(FALCO_VERSION_BUILD "--abbrev=7") if(FALCO_VERSION MATCHES "NOTFOUND$")
string(FIND "${FALCO_VERSION_BUILD}" "g" FALCO_VERSION_BUILD_START) set(FALCO_VERSION "0.0.0")
if(FALCO_VERSION_BUILD_START GREATER 0) endif()
math(EXPR FALCO_VERSION_BUILD_START "${FALCO_VERSION_BUILD_START}+1") # TODO(leodido) > Construct the prerelease part (semver 2) Construct the
string(SUBSTRING "${FALCO_VERSION_BUILD}" # Build metadata part (semver 2)
"${FALCO_VERSION_BUILD_START}" if(NOT FALCO_HASH MATCHES "NOTFOUND$")
string(SUBSTRING "${FALCO_HASH}"
0
7 7
FALCO_VERSION_BUILD) FALCO_VERSION_BUILD)
# Check whether there are uncommitted changes or not # Check whether there are uncommitted changes or not
@ -90,9 +103,12 @@ if(NOT FALCO_VERSION)
set(FALCO_VERSION_BUILD "${FALCO_VERSION_BUILD}.${FALCO_CHANGES}") set(FALCO_VERSION_BUILD "${FALCO_VERSION_BUILD}.${FALCO_CHANGES}")
endif() endif()
endif() endif()
# Add build metadata part (semver 2) # Append the build metadata part (semver 2)
if(FALCO_VERSION_BUILD)
set(FALCO_VERSION "${FALCO_VERSION}+${FALCO_VERSION_BUILD}") set(FALCO_VERSION "${FALCO_VERSION}+${FALCO_VERSION_BUILD}")
endif()
else() else()
# A tag has been found: use it as the Falco version
set(FALCO_VERSION "${FALCO_TAG}") set(FALCO_VERSION "${FALCO_TAG}")
# Remove the starting "v" in case there is one # Remove the starting "v" in case there is one
string(REGEX string(REGEX
@ -101,24 +117,19 @@ if(NOT FALCO_VERSION)
FALCO_VERSION FALCO_VERSION
"${FALCO_TAG}") "${FALCO_TAG}")
endif() endif()
# TODO(leodido) > ensure Falco version is semver before extracting parts
# Populate partial version variables # Populate partial version variables
string( string(REGEX MATCH
REGEX
MATCH
"^(0|[1-9][0-9]*)" "^(0|[1-9][0-9]*)"
FALCO_VERSION_MAJOR FALCO_VERSION_MAJOR
"${FALCO_VERSION}") "${FALCO_VERSION}")
string( string(REGEX
REGEX REPLACE "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\..*"
REPLACE
"^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\..*"
"\\2" "\\2"
FALCO_VERSION_MINOR FALCO_VERSION_MINOR
"${FALCO_VERSION}") "${FALCO_VERSION}")
string( string(REGEX
REGEX REPLACE "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*).*"
REPLACE
"^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*).*"
"\\3" "\\3"
FALCO_VERSION_PATCH FALCO_VERSION_PATCH
"${FALCO_VERSION}") "${FALCO_VERSION}")
@ -133,10 +144,8 @@ if(NOT FALCO_VERSION)
set(FALCO_VERSION_PRERELEASE "") set(FALCO_VERSION_PRERELEASE "")
endif() endif()
if(NOT FALCO_VERSION_BUILD) if(NOT FALCO_VERSION_BUILD)
string( string(REGEX
REGEX REPLACE ".*\\+([0-9a-zA-Z-]+(\\.[0-9a-zA-Z-]+)*)"
REPLACE
".*\\+([0-9a-zA-Z-]+(\\.[0-9a-zA-Z-]+)*)"
"\\1" "\\1"
FALCO_VERSION_BUILD FALCO_VERSION_BUILD
"${FALCO_VERSION}") "${FALCO_VERSION}")

View File

@ -104,18 +104,19 @@ function(git_describe _var)
return() return()
endif() endif()
# TODO sanitize if((${ARGN}" MATCHES "&&") OR (ARGN MATCHES "||") OR (ARGN MATCHES "\\;")) message("Please report the execute_process(COMMAND
# following error to the project!") message(FATAL_ERROR "Looks like someone's doing something nefarious with "${GIT_EXECUTABLE}"
# git_describe! Passed arguments ${ARGN}") endif() describe
${hash}
# message(STATUS "Arguments to execute_process: ${ARGN}") ${ARGN}
WORKING_DIRECTORY
execute_process( "${CMAKE_CURRENT_SOURCE_DIR}"
COMMAND "${GIT_EXECUTABLE}" describe ${hash} ${ARGN} RESULT_VARIABLE
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" res
RESULT_VARIABLE res OUTPUT_VARIABLE
OUTPUT_VARIABLE out out
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0) if(NOT res EQUAL 0)
set(out "${out}-${res}-NOTFOUND") set(out "${out}-${res}-NOTFOUND")
endif() endif()

View File

@ -16,6 +16,8 @@ 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@"