Skip to content

Commit b9d706f

Browse files
ig15UbuntuamazonKamath
authored
Added cmake versioning feature (aws-samples#165)
* Added cmake versioning feature * Modified a comment --------- Co-authored-by: Ubuntu <[email protected]> Co-authored-by: Nikhil Kamath <[email protected]>
1 parent 5c9cd01 commit b9d706f

File tree

6 files changed

+149
-3
lines changed

6 files changed

+149
-3
lines changed

CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@ project(${AWS_TUNNEL_LOCAL_PROXY_TARGET_NAME} CXX)
66

77
option(BUILD_TESTS "Build tests" OFF)
88
option(LINK_STATIC_OPENSSL "Use static openssl libs" ON)
9+
option(GIT_VERSION "Updates the version number using the Git commit history" ON)
910
if(BUILD_TESTS)
1011
set(AWS_TUNNEL_LOCAL_PROXY_TEST_NAME localproxytest)
1112
project(${AWS_TUNNEL_LOCAL_PROXY_TEST_NAME} CXX)
1213
endif(BUILD_TESTS)
1314

15+
#########################################
16+
# Generate Version Information from Git #
17+
#########################################
18+
find_package(Git)
19+
include(CMakeLists.txt.versioning)
20+
# Now we inject the version information into a header that is accessible from the local proxy executable
21+
configure_file("src/Version.h.in" "${PROJECT_BINARY_DIR}/Version.h")
22+
1423
######################################
1524
# Section : Disable in-source builds #
1625
######################################

CMakeLists.txt.versioning

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# This CMake module is used to generate semantic version information for the AWS IoT Secure Tunneling Local Proxy
2+
# and inject it into our source code, making the version information available to the compiled binary
3+
# so that it can be written to the logs for debugging purposes. To increment the major/minor versions
4+
# of the Secure Tunneling Local Proxy, this module expects to find a git tag in the form of "v1.0", where the first number
5+
# is the major version and the second number is the minor version. This module will search starting at HEAD
6+
# until it finds the latest versioned tag - git tags that do not start with "v" will be ignored.
7+
#
8+
# Additionally, the PATCH version of the version number is automatically incremented based on the number of commits
9+
# that we see between the current revision and the latest Git tag. For more information on Semantic Versioning,
10+
# check out https://semver.org/ and for more information on Git tags, check out https://git-scm.com/book/en/v2/Git-Basics-Tagging
11+
12+
cmake_minimum_required(VERSION 3.10)
13+
14+
# Marking Secure Tunneling Local Proxy directory safe
15+
execute_process(COMMAND git config --global --add safe.directory ${CMAKE_CURRENT_SOURCE_DIR})
16+
17+
# Check to make sure we have Git info for this package
18+
execute_process(COMMAND git log --pretty=format:'%h' -n 1
19+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
20+
OUTPUT_VARIABLE GIT_INFO)
21+
22+
function (load_version_from_file)
23+
# Git is not available (this is the case if the source is packaged as an archive), get version from file
24+
file(STRINGS ${CMAKE_SOURCE_DIR}/.version ${PROJECT_NAME}_VERSION_LIST)
25+
string(REPLACE "*" ";" ${PROJECT_NAME}_VERSION_LIST ${${PROJECT_NAME}_VERSION_LIST})
26+
# Set partial versions
27+
list(GET ${PROJECT_NAME}_VERSION_LIST 0 ${PROJECT_NAME}_VERSION_STRING_FULL)
28+
list(GET ${PROJECT_NAME}_VERSION_LIST 1 ${PROJECT_NAME}_VERSION_STRING)
29+
list(GET ${PROJECT_NAME}_VERSION_LIST 2 ${PROJECT_NAME}_VERSION_MAJOR)
30+
list(GET ${PROJECT_NAME}_VERSION_LIST 3 ${PROJECT_NAME}_VERSION_MINOR)
31+
list(GET ${PROJECT_NAME}_VERSION_LIST 4 ${PROJECT_NAME}_VERSION_PATCH)
32+
list(GET ${PROJECT_NAME}_VERSION_LIST 5 ${PROJECT_NAME}_VERSION_AHEAD)
33+
list(GET ${PROJECT_NAME}_VERSION_LIST 6 ${PROJECT_NAME}_VERSION_GIT_SHA)
34+
unset(${PROJECT_NAME}_VERSION_LIST)
35+
36+
message("-- Failed to infer patch version from git, loaded AWS IoT Secure Tunneling Local Proxy version from file: ${${PROJECT_NAME}_VERSION_STRING_FULL}")
37+
endfunction()
38+
39+
if (GIT_VERSION AND NOT ${GIT_INFO} STREQUAL "")
40+
message("-- Using Git to calculate AWS IoT Secure Tunneling Local Proxy version information...")
41+
42+
# Get last tag from git - this only matches tags starting with v, so we ignore non-versioning tags
43+
execute_process(COMMAND ${GIT_EXECUTABLE} describe --abbrev=0 --tags --match "v[0-9]*"
44+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
45+
OUTPUT_VARIABLE ${PROJECT_NAME}_VERSION_STRING
46+
OUTPUT_STRIP_TRAILING_WHITESPACE
47+
RESULT_VARIABLE exit)
48+
49+
if (NOT exit EQUAL 0)
50+
load_version_from_file()
51+
return()
52+
endif()
53+
54+
# Determine how many commits since last tag
55+
execute_process(COMMAND ${GIT_EXECUTABLE} rev-list ${${PROJECT_NAME}_VERSION_STRING}..HEAD --count
56+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
57+
OUTPUT_VARIABLE ${PROJECT_NAME}_VERSION_AHEAD
58+
OUTPUT_STRIP_TRAILING_WHITESPACE
59+
RESULT_VARIABLE exit)
60+
61+
if (NOT exit EQUAL 0)
62+
load_version_from_file()
63+
return()
64+
endif()
65+
66+
# Get current commit SHA from git
67+
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
68+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
69+
OUTPUT_VARIABLE ${PROJECT_NAME}_VERSION_GIT_SHA
70+
OUTPUT_STRIP_TRAILING_WHITESPACE
71+
RESULT_VARIABLE exit)
72+
73+
if (NOT exit EQUAL 0)
74+
load_version_from_file()
75+
return()
76+
endif()
77+
78+
# Collect the partial versions into a list
79+
string(REGEX MATCHALL "[0-9]+" ${PROJECT_NAME}_PARTIAL_VERSION_LIST
80+
${${PROJECT_NAME}_VERSION_STRING})
81+
82+
# Set the version numbers
83+
list(GET ${PROJECT_NAME}_PARTIAL_VERSION_LIST
84+
0 ${PROJECT_NAME}_VERSION_MAJOR)
85+
list(GET ${PROJECT_NAME}_PARTIAL_VERSION_LIST
86+
1 ${PROJECT_NAME}_VERSION_MINOR)
87+
set(${PROJECT_NAME}_VERSION_PATCH ${${PROJECT_NAME}_VERSION_AHEAD})
88+
89+
# Unset the list
90+
unset(${PROJECT_NAME}_PARTIAL_VERSION_LIST)
91+
92+
# Set full project version string
93+
set(${PROJECT_NAME}_VERSION_STRING_FULL
94+
v${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}.${${PROJECT_NAME}_VERSION_PATCH}-${${PROJECT_NAME}_VERSION_GIT_SHA})
95+
96+
97+
message("-- Generated AWS IoT Secure Tunneling Local Proxy version: ${${PROJECT_NAME}_VERSION_STRING_FULL}")
98+
# Save version to file (which will be used when Git is not available
99+
# or VERSION_UPDATE_FROM_GIT is disabled)
100+
file(WRITE ${CMAKE_SOURCE_DIR}/.version ${${PROJECT_NAME}_VERSION_STRING_FULL}
101+
"*" ${${PROJECT_NAME}_VERSION_STRING}
102+
"*" ${${PROJECT_NAME}_VERSION_MAJOR}
103+
"*" ${${PROJECT_NAME}_VERSION_MINOR}
104+
"*" ${${PROJECT_NAME}_VERSION_PATCH}
105+
"*" ${${PROJECT_NAME}_VERSION_AHEAD}
106+
"*" ${${PROJECT_NAME}_VERSION_GIT_SHA})
107+
108+
# exit from cmake processing
109+
return()
110+
endif()

src/Version.h.in

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
/**
5+
* CMake will inject version information into this file at compile time, and will
6+
* make it accessible to our source code as "Version.h"
7+
*/
8+
#ifndef AWS_IOT_SECURE_TUNNELING_LOCAL_PROXY_VERSION_H
9+
#define AWS_IOT_SECURE_TUNNELING_LOCAL_PROXY_VERSION_H
10+
11+
#define LOCAL_PROXY_VERSION_FULL "${${PROJECT_NAME}_VERSION_STRING_FULL}"
12+
#define LOCAL_PROXY_VERSION "${${PROJECT_NAME}_VERSION_STRING}"
13+
14+
#endif // AWS_IOT_SECURE_TUNNELING_LOCAL_PROXY_VERSION_H

src/config/ConfigFile.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <boost/format.hpp>
2929

3030
#include "ConfigFile.h"
31+
#include "Version.h"
3132

3233
using std::uint16_t;
3334
using std::endl;
@@ -57,6 +58,12 @@ namespace aws { namespace iot { namespace securedtunneling { namespace config_fi
5758
* @param file_dir : directory file path
5859
* @return true: valid configuration. false: invalid configuration
5960
*/
61+
62+
std::string PrintVersion()
63+
{
64+
return LOCAL_PROXY_VERSION_FULL;
65+
}
66+
6067
bool is_valid_directory(string const & file_dir) {
6168
bool is_dir = false;
6269
try {

src/config/ConfigFile.h

+1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ namespace aws { namespace iot { namespace securedtunneling { namespace config_fi
2020
unordered_set<string> const & service_ids,
2121
unordered_map<string, string> & serviceId_to_endpoint_mapping);
2222
void update_port_mapping(const string & cli_input, unordered_map<string, string> & serviceId_to_endpoint_mapping);
23+
std::string PrintVersion();
2324
}}}}

src/main.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "TcpAdapterProxy.h"
3030
#include "config/ConfigFile.h"
3131
#include "LocalproxyConfig.h"
32+
#include "./config/ConfigFile.h"
3233

3334
using std::uint16_t;
3435
using std::endl;
@@ -50,6 +51,7 @@ using aws::iot::securedtunneling::tcp_adapter_proxy;
5051
using aws::iot::securedtunneling::proxy_mode;
5152
using aws::iot::securedtunneling::get_region_endpoint;
5253
using aws::iot::securedtunneling::settings::apply_region_overrides;
54+
using aws::iot::securedtunneling::config_file::PrintVersion;
5355

5456
char const * const ACCESS_TOKEN_ENV_VARIABLE = "AWSIOT_TUNNEL_ACCESS_TOKEN";
5557
char const * const CLIENT_TOKEN_ENV_VARIABLE = "AWSIOT_TUNNEL_CLIENT_TOKEN";
@@ -158,7 +160,7 @@ bool process_cli(int argc, char ** argv, LocalproxyConfig &cfg, ptree &settings,
158160
options_description cliargs_desc("Allowed options");
159161
cliargs_desc.add_options()
160162
("help,h", "Show help message")
161-
("version", "Show version")
163+
("version", "Show current version of Local Proxy")
162164
("access-token,t", value<string>()->required(), "Client access token")
163165
("client-token,i", value<string>(), "Optional Client Token")
164166
("proxy-endpoint,e", value<string>(), "Endpoint of proxy server with port (if not default 443). Example: data.tunneling.iot.us-east-1.amazonaws.com:443")
@@ -180,8 +182,7 @@ bool process_cli(int argc, char ** argv, LocalproxyConfig &cfg, ptree &settings,
180182

181183
if (vm.count("version"))
182184
{
183-
std::cerr << "3.1.2" << "\n"; // hardcoding this as a temporary measure
184-
return false;
185+
PrintVersion();
185186
}
186187
if (vm.count("help"))
187188
{
@@ -431,6 +432,10 @@ int main(int argc, char ** argv)
431432
{
432433
try
433434
{
435+
436+
std::string version = PrintVersion();
437+
std::cout << "Running AWS IoT Secure Tunneling Local Proxy version: " << version << std::endl;
438+
434439
LocalproxyConfig cfg;
435440
ptree settings;
436441
std::uint16_t logging_level;

0 commit comments

Comments
 (0)