Skip to content

Commit c76e2ad

Browse files
fusionfoxyThomas
andauthored
Sc3788 (#17)
* add version number to sdk * log version on start * linting --------- Co-authored-by: Thomas <tfk@debian>
1 parent 537215c commit c76e2ad

File tree

7 files changed

+125
-0
lines changed

7 files changed

+125
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/build
22
/sdk/build
3+
/sdk/src/signaling_device/src/version.cpp
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Calculate the version from the current git repository.
2+
3+
# This script is copied from/maintained at https://github.com/nabto/nabto-embedded-sdk
4+
# Script version 2020-12-08
5+
6+
function(nabto_version version_var error_var)
7+
8+
find_program(GIT "git")
9+
if (NOT GIT)
10+
set(${error_var} "git executable not found" PARENT_SCOPE)
11+
return()
12+
endif()
13+
14+
execute_process(
15+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
16+
COMMAND git rev-parse --is-inside-work-tree
17+
RESULT_VARIABLE IS_GIT_REPOSITORY)
18+
19+
if (NOT IS_GIT_REPOSITORY EQUAL 0)
20+
set(${error_var} "directory is not a git repository" PARENT_SCOPE)
21+
return()
22+
endif()
23+
# A git repo, generate version and put it into version_var
24+
25+
execute_process(
26+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
27+
COMMAND git diff --quiet --exit-code
28+
RESULT_VARIABLE GIT_DIRTY)
29+
execute_process(
30+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
31+
COMMAND git describe --exact-match --tags
32+
OUTPUT_VARIABLE GIT_TAG ERROR_QUIET)
33+
execute_process(
34+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
35+
COMMAND git rev-parse --abbrev-ref HEAD
36+
OUTPUT_VARIABLE GIT_BRANCH)
37+
execute_process(
38+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
39+
COMMAND git rev-list --count HEAD
40+
OUTPUT_VARIABLE GIT_COUNT ERROR_QUIET)
41+
execute_process(
42+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
43+
COMMAND git rev-parse --short HEAD
44+
OUTPUT_VARIABLE GIT_HASH ERROR_QUIET)
45+
46+
if (GIT_DIRTY EQUAL 0)
47+
set(GIT_DIRTY "")
48+
else()
49+
set(GIT_DIRTY ".dirty")
50+
endif()
51+
52+
53+
string(STRIP "${GIT_DIRTY}" GIT_DIRTY)
54+
string(STRIP "${GIT_TAG}" GIT_TAG)
55+
string(STRIP "${GIT_BRANCH}" GIT_BRANCH)
56+
string(STRIP "${GIT_COUNT}" GIT_COUNT)
57+
string(STRIP "${GIT_HASH}" GIT_HASH)
58+
59+
message("version.cmake variables: GIT_DIRTY ${GIT_DIRTY}, GIT_TAG ${GIT_TAG}, GIT_BRANCH ${GIT_BRANCH}, GIT_COUNT ${GIT_COUNT}, GIT_HASH: ${GIT_HASH}")
60+
61+
if (GIT_TAG AND NOT GIT_DIRTY)
62+
# string v4.5.6 -> 4.5.6
63+
string(SUBSTRING ${GIT_TAG} 1 -1 VERSION)
64+
else()
65+
# A feature branch
66+
set(VERSION "0.0.0-branch.${GIT_BRANCH}.commits.${GIT_COUNT}+${GIT_HASH}${GIT_DIRTY}")
67+
endif()
68+
69+
message("Generated the version: ${VERSION} based on the git repository information")
70+
set(${version_var} ${VERSION} PARENT_SCOPE)
71+
endfunction()

sdk/cmake_scripts/version.cmake

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
include("${CMAKE_CURRENT_SOURCE_DIR}/../../cmake_scripts/nabto_version.cmake")
2+
cmake_policy(SET CMP0007 NEW)
3+
4+
if ("${NABTO_WEBRTC_VERSION}" STREQUAL "")
5+
6+
nabto_version(version_out version_error)
7+
8+
if (NOT version_out)
9+
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/api/version.cpp)
10+
11+
message(FATAL_ERROR "No file ${CMAKE_CURRENT_SOURCE_DIR}/api/version.cpp exists and it cannot be auto generated. ${version_error}")
12+
endif()
13+
endif()
14+
else()
15+
set(version_out ${NABTO_WEBRTC_VERSION})
16+
endif()
17+
18+
if (version_out)
19+
set(VERSION "#include <nabto/webrtc/device.hpp>\n
20+
#include <string>\n
21+
#include <array>\n
22+
constexpr std::array<char, 100> version_str {\"${version_out}\"};
23+
std::string nabto::webrtc::SignalingDevice::version() { return {std::begin(version_str),std::end(version_str)}; }\n")
24+
25+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/version.cpp)
26+
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/src/version.cpp VERSION_)
27+
else()
28+
set(VERSION_ "")
29+
endif()
30+
31+
if (NOT "${VERSION}" STREQUAL "${VERSION_}")
32+
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/src/version.cpp "${VERSION}")
33+
endif()
34+
endif()

sdk/src/signaling_device/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11

2+
3+
add_custom_target(GENERATE_VERSION ALL
4+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
5+
BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/src/version.cpp
6+
COMMAND ${CMAKE_COMMAND} -DNABTO_WEBRTC_VERSION=${NABTO_WEBRTC_VERSION} -P
7+
${CMAKE_CURRENT_SOURCE_DIR}/../../cmake_scripts/version.cmake
8+
)
9+
210
set(src
311
src/signaling_device_impl.cpp
412
src/signaling_channel_impl.cpp
513
src/signaling_device_factory.cpp
614
src/websocket_connection.cpp
715
src/signaling_error.cpp
816
src/signaling.cpp
17+
src/version.cpp
918
)
1019

1120
add_library(nabto_webrtc_signaling_device ${src})

sdk/src/signaling_device/include/nabto/webrtc/device.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,13 @@ class SignalingDevice {
617617
* @param id The ID returned when adding the listener.
618618
*/
619619
virtual void removeReconnectListener(ReconnectListenerId id) = 0;
620+
621+
/**
622+
* Get a string representation of the Nabto WebRTC Device version.
623+
*
624+
* @returns String representation of the library version
625+
*/
626+
static std::string version();
620627
};
621628

622629
/**

sdk/src/signaling_device/src/signaling_device_impl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ SignalingDeviceImpl::SignalingDeviceImpl(const SignalingDeviceConfig& conf)
6363

6464
void SignalingDeviceImpl::start() {
6565
mutex_.lock();
66+
NABTO_SIGNALING_LOGI << "Signaling Device started in version: " << version();
6667
if (state_ == SignalingDeviceState::NEW) {
6768
mutex_.unlock();
6869
doConnect();

sdk/test/create_destroy_signaling_device.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
TEST(create_destroy, create_destroy) {
66
nabto::webrtc::SignalingDeviceConfig conf;
77
auto ptr = nabto::webrtc::SignalingDeviceFactory::create(conf);
8+
auto version = ptr->version();
9+
ASSERT_TRUE(version.size() > 0);
810
}

0 commit comments

Comments
 (0)