-
Notifications
You must be signed in to change notification settings - Fork 70
/
CMakeLists.txt.versioning
123 lines (102 loc) · 5.49 KB
/
CMakeLists.txt.versioning
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# This CMake module is used to generate semantic version information for the AWS IoT Device Client
# and inject it into our source code, making the version information available to the compiled binary
# so that it can be written to the logs for debugging purposes. To increment the major/minor versions
# of the device client, this module expects to find a git tag in the form of "v1.0", where the first number
# is the major version and the second number is the minor version. This module will search starting at HEAD
# until it finds the latest versioned tag - git tags that do not start with "v" will be ignored.
#
# Additionally, the PATCH version of the version number is automatically incremented based on the number of commits
# that we see between the current revision and the latest Git tag. For more information on Semantic Versioning,
# check out https://semver.org/ and for more information on Git tags, check out https://git-scm.com/book/en/v2/Git-Basics-Tagging
cmake_minimum_required(VERSION 3.10)
# Marking Device Client directory safe
execute_process(COMMAND git config --global --add safe.directory ${CMAKE_CURRENT_SOURCE_DIR})
# Check to make sure we have Git info for this package
execute_process(COMMAND git log --pretty=format:'%h' -n 1
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_INFO)
function (load_version_from_file)
# Git is not available (this is the case if the source is packaged as an archive), get version from file
file(STRINGS ${CMAKE_SOURCE_DIR}/.version ${PROJECT_NAME}_VERSION_LIST)
string(REPLACE "*" ";" ${PROJECT_NAME}_VERSION_LIST ${${PROJECT_NAME}_VERSION_LIST})
# Set partial versions
list(GET ${PROJECT_NAME}_VERSION_LIST 0 ${PROJECT_NAME}_VERSION_STRING_FULL)
list(GET ${PROJECT_NAME}_VERSION_LIST 1 ${PROJECT_NAME}_VERSION_STRING)
list(GET ${PROJECT_NAME}_VERSION_LIST 2 ${PROJECT_NAME}_VERSION_MAJOR)
list(GET ${PROJECT_NAME}_VERSION_LIST 3 ${PROJECT_NAME}_VERSION_MINOR)
list(GET ${PROJECT_NAME}_VERSION_LIST 4 ${PROJECT_NAME}_VERSION_PATCH)
list(GET ${PROJECT_NAME}_VERSION_LIST 5 ${PROJECT_NAME}_VERSION_AHEAD)
list(GET ${PROJECT_NAME}_VERSION_LIST 6 ${PROJECT_NAME}_VERSION_GIT_SHA)
unset(${PROJECT_NAME}_VERSION_LIST)
message("-- Failed to infer patch version from git, loaded AWS IoT Device Client version from file: ${${PROJECT_NAME}_VERSION_STRING_FULL}")
endfunction()
if (GIT_VERSION AND NOT ${GIT_INFO} STREQUAL "")
message("-- Using Git to calculate AWS IoT Device Client version information...")
# Get first tag with major and minor of the latest tag.
execute_process(
COMMAND bash -c "
read LATEST_MAJOR_VERSION LATEST_MINOR_VERSION < <(git describe --abbrev=0 --tags | awk -F. '
{
gsub(/^v/, \"\", \$0)
split(\$0, parts, \".\")
LATEST_MAJOR_VERSION = parts[1]
LATEST_MINOR_VERSION = parts[2]
print LATEST_MAJOR_VERSION, LATEST_MINOR_VERSION
}')
git tag --sort=creatordate | grep -E \"^v?\${LATEST_MAJOR_VERSION}\.\${LATEST_MINOR_VERSION}(\\.\[0-9]+)?\" | head -n 1
"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE ${PROJECT_NAME}_VERSION_STRING
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE exit)
if (NOT exit EQUAL 0)
load_version_from_file()
return()
endif()
# Determine how many commits since last tag
execute_process(COMMAND ${GIT_EXECUTABLE} rev-list ${${PROJECT_NAME}_VERSION_STRING}..HEAD --count
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE ${PROJECT_NAME}_VERSION_AHEAD
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE exit)
if (NOT exit EQUAL 0)
load_version_from_file()
return()
endif()
# Get current commit SHA from git
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE ${PROJECT_NAME}_VERSION_GIT_SHA
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE exit)
if (NOT exit EQUAL 0)
load_version_from_file()
return()
endif()
# Collect the partial versions into a list
string(REGEX MATCHALL "[0-9]+" ${PROJECT_NAME}_PARTIAL_VERSION_LIST
${${PROJECT_NAME}_VERSION_STRING})
# Set the version numbers
list(GET ${PROJECT_NAME}_PARTIAL_VERSION_LIST
0 ${PROJECT_NAME}_VERSION_MAJOR)
list(GET ${PROJECT_NAME}_PARTIAL_VERSION_LIST
1 ${PROJECT_NAME}_VERSION_MINOR)
set(${PROJECT_NAME}_VERSION_PATCH ${${PROJECT_NAME}_VERSION_AHEAD})
# Unset the list
unset(${PROJECT_NAME}_PARTIAL_VERSION_LIST)
# Set full project version string
set(${PROJECT_NAME}_VERSION_STRING_FULL
v${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}.${${PROJECT_NAME}_VERSION_PATCH}-${${PROJECT_NAME}_VERSION_GIT_SHA})
message("-- Generated AWS IoT Device Client version: ${${PROJECT_NAME}_VERSION_STRING_FULL}")
# Save version to file (which will be used when Git is not available
# or VERSION_UPDATE_FROM_GIT is disabled)
file(WRITE ${CMAKE_SOURCE_DIR}/.version ${${PROJECT_NAME}_VERSION_STRING_FULL}
"*" ${${PROJECT_NAME}_VERSION_STRING}
"*" ${${PROJECT_NAME}_VERSION_MAJOR}
"*" ${${PROJECT_NAME}_VERSION_MINOR}
"*" ${${PROJECT_NAME}_VERSION_PATCH}
"*" ${${PROJECT_NAME}_VERSION_AHEAD}
"*" ${${PROJECT_NAME}_VERSION_GIT_SHA})
# exit from cmake processing
return()
endif()