Skip to content

Commit 9130e91

Browse files
committed
Add version info
1 parent 50ca6ce commit 9130e91

File tree

6 files changed

+287
-2
lines changed

6 files changed

+287
-2
lines changed

CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ endif()
1515
set(CMAKE_CXX_STANDARD 17)
1616
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1717

18-
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
18+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
19+
20+
#Git version
21+
include(GetGitRevisionDescription)
22+
get_git_head_revision(GIT_REFSPEC GIT_SHA1)
23+
git_describe(GIT_SHA1_SHORT "--always")
24+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/version.cpp.in" "${CMAKE_CURRENT_BINARY_DIR}/src/version.cpp" @ONLY)
25+
26+
#FindLIBUSB
1927
if (${CMAKE_CXX_COMPILER_ID} STREQUAL GNU OR ${CMAKE_CXX_COMPILER_ID} STREQUAL AppleClang)
2028
find_package(LibUSB REQUIRED)
2129
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
@@ -48,6 +56,7 @@ set(ALL_SRC
4856
src/tyt_fw.cpp
4957
src/cs_fw.cpp
5058
src/rdt.cpp
59+
"${CMAKE_CURRENT_BINARY_DIR}/src/version.cpp"
5160
)
5261

5362
add_library(radiotool ${ALL_SRC})

cmake/GetGitRevisionDescription.cmake

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# - Returns a version string from Git
2+
#
3+
# These functions force a re-configure on each git commit so that you can
4+
# trust the values of the variables in your build system.
5+
#
6+
# get_git_head_revision(<refspecvar> <hashvar> [<additional arguments to git describe> ...])
7+
#
8+
# Returns the refspec and sha hash of the current head revision
9+
#
10+
# git_describe(<var> [<additional arguments to git describe> ...])
11+
#
12+
# Returns the results of git describe on the source tree, and adjusting
13+
# the output so that it tests false if an error occurs.
14+
#
15+
# git_get_exact_tag(<var> [<additional arguments to git describe> ...])
16+
#
17+
# Returns the results of git describe --exact-match on the source tree,
18+
# and adjusting the output so that it tests false if there was no exact
19+
# matching tag.
20+
#
21+
# git_local_changes(<var>)
22+
#
23+
# Returns either "CLEAN" or "DIRTY" with respect to uncommitted changes.
24+
# Uses the return code of "git diff-index --quiet HEAD --".
25+
# Does not regard untracked files.
26+
#
27+
# Requires CMake 2.6 or newer (uses the 'function' command)
28+
#
29+
# Original Author:
30+
# 2009-2010 Ryan Pavlik <[email protected]> <[email protected]>
31+
# http://academic.cleardefinition.com
32+
# Iowa State University HCI Graduate Program/VRAC
33+
#
34+
# Copyright Iowa State University 2009-2010.
35+
# Distributed under the Boost Software License, Version 1.0.
36+
# (See accompanying file LICENSE_1_0.txt or copy at
37+
# http://www.boost.org/LICENSE_1_0.txt)
38+
39+
if(__get_git_revision_description)
40+
return()
41+
endif()
42+
set(__get_git_revision_description YES)
43+
44+
# We must run the following at "include" time, not at function call time,
45+
# to find the path to this module rather than the path to a calling list file
46+
get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
47+
48+
function(get_git_head_revision _refspecvar _hashvar)
49+
set(GIT_PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
50+
set(GIT_DIR "${GIT_PARENT_DIR}/.git")
51+
while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
52+
set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
53+
get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
54+
if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
55+
# We have reached the root directory, we are not in git
56+
set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
57+
set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
58+
return()
59+
endif()
60+
set(GIT_DIR "${GIT_PARENT_DIR}/.git")
61+
endwhile()
62+
# check if this is a submodule
63+
if(NOT IS_DIRECTORY ${GIT_DIR})
64+
file(READ ${GIT_DIR} submodule)
65+
string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
66+
get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
67+
get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
68+
endif()
69+
if(NOT IS_DIRECTORY "${GIT_DIR}")
70+
file(READ ${GIT_DIR} worktree)
71+
string(REGEX REPLACE "gitdir: (.*)worktrees(.*)\n$" "\\1" GIT_DIR ${worktree})
72+
endif()
73+
set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
74+
if(NOT EXISTS "${GIT_DATA}")
75+
file(MAKE_DIRECTORY "${GIT_DATA}")
76+
endif()
77+
78+
if(NOT EXISTS "${GIT_DIR}/HEAD")
79+
return()
80+
endif()
81+
set(HEAD_FILE "${GIT_DATA}/HEAD")
82+
configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
83+
84+
configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
85+
"${GIT_DATA}/grabRef.cmake"
86+
@ONLY)
87+
include("${GIT_DATA}/grabRef.cmake")
88+
89+
set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
90+
set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
91+
endfunction()
92+
93+
function(git_describe _var)
94+
if(NOT GIT_FOUND)
95+
find_package(Git QUIET)
96+
endif()
97+
get_git_head_revision(refspec hash)
98+
if(NOT GIT_FOUND)
99+
set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
100+
return()
101+
endif()
102+
if(NOT hash)
103+
set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
104+
return()
105+
endif()
106+
107+
# TODO sanitize
108+
#if((${ARGN}" MATCHES "&&") OR
109+
# (ARGN MATCHES "||") OR
110+
# (ARGN MATCHES "\\;"))
111+
# message("Please report the following error to the project!")
112+
# message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
113+
#endif()
114+
115+
#message(STATUS "Arguments to execute_process: ${ARGN}")
116+
117+
execute_process(COMMAND
118+
"${GIT_EXECUTABLE}"
119+
describe
120+
${hash}
121+
${ARGN}
122+
WORKING_DIRECTORY
123+
"${CMAKE_CURRENT_SOURCE_DIR}"
124+
RESULT_VARIABLE
125+
res
126+
OUTPUT_VARIABLE
127+
out
128+
ERROR_QUIET
129+
OUTPUT_STRIP_TRAILING_WHITESPACE)
130+
if(NOT res EQUAL 0)
131+
set(out "${out}-${res}-NOTFOUND")
132+
endif()
133+
134+
set(${_var} "${out}" PARENT_SCOPE)
135+
endfunction()
136+
137+
function(git_get_exact_tag _var)
138+
git_describe(out --exact-match ${ARGN})
139+
set(${_var} "${out}" PARENT_SCOPE)
140+
endfunction()
141+
142+
function(git_local_changes _var)
143+
if(NOT GIT_FOUND)
144+
find_package(Git QUIET)
145+
endif()
146+
get_git_head_revision(refspec hash)
147+
if(NOT GIT_FOUND)
148+
set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
149+
return()
150+
endif()
151+
if(NOT hash)
152+
set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
153+
return()
154+
endif()
155+
156+
execute_process(COMMAND
157+
"${GIT_EXECUTABLE}"
158+
diff-index --quiet HEAD --
159+
WORKING_DIRECTORY
160+
"${CMAKE_CURRENT_SOURCE_DIR}"
161+
RESULT_VARIABLE
162+
res
163+
OUTPUT_VARIABLE
164+
out
165+
ERROR_QUIET
166+
OUTPUT_STRIP_TRAILING_WHITESPACE)
167+
if(res EQUAL 0)
168+
set(${_var} "CLEAN" PARENT_SCOPE)
169+
else()
170+
set(${_var} "DIRTY" PARENT_SCOPE)
171+
endif()
172+
endfunction()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# Internal file for GetGitRevisionDescription.cmake
3+
#
4+
# Requires CMake 2.6 or newer (uses the 'function' command)
5+
#
6+
# Original Author:
7+
# 2009-2010 Ryan Pavlik <[email protected]> <[email protected]>
8+
# http://academic.cleardefinition.com
9+
# Iowa State University HCI Graduate Program/VRAC
10+
#
11+
# Copyright Iowa State University 2009-2010.
12+
# Distributed under the Boost Software License, Version 1.0.
13+
# (See accompanying file LICENSE_1_0.txt or copy at
14+
# http://www.boost.org/LICENSE_1_0.txt)
15+
16+
set(HEAD_HASH)
17+
18+
file(READ "@HEAD_FILE@" HEAD_CONTENTS LIMIT 1024)
19+
20+
string(STRIP "${HEAD_CONTENTS}" HEAD_CONTENTS)
21+
if(HEAD_CONTENTS MATCHES "ref")
22+
# named branch
23+
string(REPLACE "ref: " "" HEAD_REF "${HEAD_CONTENTS}")
24+
if(EXISTS "@GIT_DIR@/${HEAD_REF}")
25+
configure_file("@GIT_DIR@/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
26+
else()
27+
configure_file("@GIT_DIR@/packed-refs" "@GIT_DATA@/packed-refs" COPYONLY)
28+
file(READ "@GIT_DATA@/packed-refs" PACKED_REFS)
29+
if(${PACKED_REFS} MATCHES "([0-9a-z]*) ${HEAD_REF}")
30+
set(HEAD_HASH "${CMAKE_MATCH_1}")
31+
endif()
32+
endif()
33+
else()
34+
# detached HEAD
35+
configure_file("@GIT_DIR@/HEAD" "@GIT_DATA@/head-ref" COPYONLY)
36+
endif()
37+
38+
if(NOT HEAD_HASH)
39+
file(READ "@GIT_DATA@/head-ref" HEAD_HASH LIMIT 1024)
40+
string(STRIP "${HEAD_HASH}" HEAD_HASH)
41+
endif()

include/radio_tool/version.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* This file is part of radio_tool.
3+
* Copyright (c) 2020 Kieran Harkin <[email protected]>
4+
*
5+
* radio_tool is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* radio_tool is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with radio_tool. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
#pragma once
19+
20+
extern const char g_GIT_SHA1[];
21+
22+
extern const char g_GIT_SHA1_SHORT[];
23+
24+
extern const char g_PROJECT_VERSION[];
25+
26+
extern const char g_PROJECT_NAME[];

src/radio_tool.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <radio_tool/dfu/dfu_exception.hpp>
2323
#include <radio_tool/util.hpp>
24+
#include <radio_tool/version.hpp>
2425

2526
#ifdef XOR_TOOL
2627
#include <xor_tool.hpp>
@@ -53,7 +54,11 @@ int main(int argc, char **argv)
5354
{
5455
try
5556
{
56-
cxxopts::Options options(argv[0]);
57+
std::stringstream ssVersion;
58+
ssVersion << g_PROJECT_NAME << " v" << g_PROJECT_VERSION << "-" << g_GIT_SHA1_SHORT;
59+
auto version = ssVersion.str();
60+
61+
cxxopts::Options options(argv[0], version);
5762

5863
options.add_options("General")
5964
("h,help", "Show this message", cxxopts::value<std::string>(), "<command>")

src/version.cpp.in

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* This file is part of radio_tool.
3+
* Copyright (c) 2020 Kieran Harkin <[email protected]>
4+
*
5+
* radio_tool is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* radio_tool is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with radio_tool. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include <radio_tool/version.hpp>
20+
21+
#define GIT_SHA1 "@GIT_SHA1@"
22+
#define GIT_SHA1_SHORT "@GIT_SHA1_SHORT@"
23+
#define PROJECT_VERSION "@PROJECT_VERSION@"
24+
#define PROJECT_NAME "@PROJECT_NAME@"
25+
26+
const char g_GIT_SHA1[] = GIT_SHA1;
27+
28+
const char g_GIT_SHA1_SHORT[] = GIT_SHA1_SHORT;
29+
30+
const char g_PROJECT_VERSION[] = PROJECT_VERSION;
31+
32+
const char g_PROJECT_NAME[] = PROJECT_NAME;

0 commit comments

Comments
 (0)