forked from smessmer/gitversion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmake.cmake
59 lines (53 loc) · 2.77 KB
/
cmake.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
find_package (Python COMPONENTS Interpreter REQUIRED )
set(DIR_OF_GITVERSION_TOOL "${CMAKE_CURRENT_LIST_DIR}" CACHE INTERNAL "DIR_OF_GITVERSION_TOOL")
function (_CREATE_GIT_VERSION_FILE)
FILE(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/messmer_gitversion")
FILE(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/messmer_gitversion/gitversion")
if(UNIX)
IF(DEFINED ENV{PYTHONPATH})
SET(ENV{PYTHONPATH} "${DIR_OF_GITVERSION_TOOL}/src:ENV{PYTHONPATH}")
ELSE()
SET(ENV{PYTHONPATH} "${DIR_OF_GITVERSION_TOOL}/src")
ENDIF()
EXECUTE_PROCESS(COMMAND /usr/bin/env ${Python_EXECUTABLE} -m gitversionbuilder --lang cpp --dir "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/gitversion/gitversion/version.h"
WORKING_DIRECTORY "${DIR_OF_GITVERSION_TOOL}/src"
RESULT_VARIABLE result)
elseif(WIN32)
EXECUTE_PROCESS(COMMAND "${Python_EXECUTABLE}" -m gitversionbuilder --lang cpp --dir "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/gitversion/gitversion/version.h"
WORKING_DIRECTORY "${DIR_OF_GITVERSION_TOOL}/src"
RESULT_VARIABLE result)
endif()
IF(NOT ${result} EQUAL 0)
MESSAGE(FATAL_ERROR "Error running messmer/git-version tool. Return code is: ${result}")
ENDIF()
endfunction (_CREATE_GIT_VERSION_FILE)
function(_SET_GITVERSION_CMAKE_VARIABLE OUTPUT_VARIABLE)
# Load version string and write it to a cmake variable so it can be accessed from cmake.
FILE(READ "${CMAKE_CURRENT_BINARY_DIR}/messmer_gitversion/gitversion/version.h" VERSION_H_FILE_CONTENT)
STRING(REGEX REPLACE ".*VERSION_STRING = \"([^\"]*)\".*" "\\1" VERSION_STRING "${VERSION_H_FILE_CONTENT}")
MESSAGE(STATUS "Version from git: ${VERSION_STRING}")
SET(${OUTPUT_VARIABLE} "${VERSION_STRING}" CACHE INTERNAL "${OUTPUT_VARIABLE}")
endfunction(_SET_GITVERSION_CMAKE_VARIABLE)
######################################################
# Add git version information
# Uses:
# TARGET_GIT_VERSION_INIT(buildtarget)
# Then, you can write in your source file:
# #include <gitversion/version.h>
# cout << gitversion::VERSION.toString() << endl;
######################################################
function(TARGET_GIT_VERSION_INIT TARGET)
_CREATE_GIT_VERSION_FILE()
TARGET_INCLUDE_DIRECTORIES(${TARGET} PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/messmer_gitversion")
_SET_GITVERSION_CMAKE_VARIABLE(GITVERSION_VERSION_STRING)
endfunction(TARGET_GIT_VERSION_INIT)
######################################################
# Load git version information into a cmake variable
# Uses:
# GET_GIT_VERSION(OUTPUT_VARIABLE)
# MESSAGE(STATUS "The version is ${OUTPUT_VARIABLE}")
######################################################
function(GET_GIT_VERSION OUTPUT_VARIABLE)
_CREATE_GIT_VERSION_FILE()
_SET_GITVERSION_CMAKE_VARIABLE(${OUTPUT_VARIABLE})
endfunction(GET_GIT_VERSION OUTPUT_VARIABLE)