Skip to content

Commit f26eec1

Browse files
authored
Added version option to ddsrouter tool (#239)
* Refs #14889: Added version option to ddsrouter tool Signed-off-by: Anton Rey <aanton.rv@gmail.com> * Refs #14889: Revision changes Signed-off-by: Anton Rey <aanton.rv@gmail.com> * Refs #14889: Rebase change Signed-off-by: Anton Rey <aanton.rv@gmail.com>
1 parent 7b45dab commit f26eec1

11 files changed

Lines changed: 88 additions & 7 deletions

File tree

ddsrouter_cmake/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ cmake_policy(VERSION 3.5...3.13)
2424
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/project/in_source_build_prevention.cmake)
2525
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/project/load_project_settings.cmake)
2626
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/project/read_version.cmake)
27+
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/project/read_commit_hash.cmake)
2728
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/project/installation_paths.cmake)
2829
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/project/configure_project.cmake)
2930
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/project/cmake_options.cmake)

ddsrouter_cmake/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Those variables which default is `x` must be set, and those with `-` are not req
7474
| MODULE_VERSION_MINOR | x (if not set, are taken from VERSION file) | Minor version |
7575
| MODULE_VERSION_PATCH | x (if not set, are taken from VERSION file) | Patch version |
7676
| MODULE_VERSION | ${MODULE_VERSION_MAJOR}.${MODULE_VERSION_MINOR}.${MODULE_VERSION_PATCH} | Module version |
77-
| MODULE_VERSION_STRING | ${MODULE_VERSION} | Module version |
77+
| MODULE_VERSION_STRING | v${MODULE_VERSION} | Module version |
7878
|------------------------------|-------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------|
7979
| MODULE_LICENSE_FILE_PATH | ../LICENSE | Path to the license file |
8080
| MODULE_RESOURCES_PATH | (if not given, no resources will be installed) | Path of the resources to install |

ddsrouter_cmake/cmake/project/configure_project.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ macro(configure_project)
6363
read_version(${MODULE_VERSION_FILE_PATH})
6464
endif()
6565

66+
# Read git commit hash if not already set
67+
if( NOT DEFINED MODULE_COMMIT_HASH)
68+
69+
read_commit_hash()
70+
endif()
71+
6672
# Set CMake options
6773
configure_cmake_options()
6874

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
###############################################################################
16+
# Commit hash Reader macro
17+
###############################################################################
18+
# Load commit hash
19+
20+
#
21+
# RETURN VARIABLES:
22+
# - MODULE_COMMIT_HASH : commit-hash
23+
24+
# No arguments
25+
26+
27+
macro(read_commit_hash)
28+
29+
execute_process(COMMAND
30+
git rev-parse HEAD
31+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
32+
OUTPUT_VARIABLE MODULE_COMMIT_HASH
33+
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
34+
35+
message(STATUS "Read commit hash ${MODULE_COMMIT_HASH}")
36+
37+
endmacro()

ddsrouter_cmake/cmake/project/read_version.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ macro(read_version VERSION_FILE_NAME)
5151
endif()
5252

5353
if(NOT DEFINED MODULE_VERSION_STRING)
54-
set (MODULE_VERSION_STRING ${MODULE_VERSION})
54+
set (MODULE_VERSION_STRING "v${MODULE_VERSION}")
55+
message(STATUS "Set module version string ${MODULE_VERSION_STRING}")
5556
endif()
5657

5758
# Finish macro

ddsrouter_cmake/templates/cpp/library/config.h.in

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
#ifndef @MODULE_MACRO@_VERSION_MICRO
2525
#define @MODULE_MACRO@_VERSION_MICRO @PROJECT_VERSION_PATCH@
2626
#endif // ifndef @MODULE_MACRO@_VERSION_MICRO
27-
#ifndef @MODULE_MACRO@_VERSION_STR
28-
#define @MODULE_MACRO@_VERSION_STR "@PROJECT_VERSION@"
27+
#ifndef @MODULE_MACRO@_VERSION_STRING
28+
#define @MODULE_MACRO@_VERSION_STRING "@MODULE_VERSION_STRING@"
2929
#endif // ifndef @MODULE_MACRO@_VERSION_STR
30+
#ifndef @MODULE_MACRO@_COMMIT_HASH
31+
#define @MODULE_MACRO@_COMMIT_HASH "@MODULE_COMMIT_HASH@"
32+
#endif // ifndef @MODULE_MACRO@_COMMIT_HASH
3033

3134
#endif // ifndef _@MODULE_MACRO@_LIBRARY_CONFIG_H_

tools/ddsrouter_tool/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ ddsrouter --help
2121
# -r --reload-time Time period in seconds to reload configuration file. This is needed when FileWatcher functionality is not available (e.g. config file is a symbolic link).
2222
# Value 0 does not reload file. [Default: 0].
2323
# -d --debug Activate debug Logs (be aware that some logs may require specific CMAKE compilation options).
24+
# -v --version Print version, branch and commit hash
2425
```
2526

2627
---

tools/ddsrouter_tool/src/cpp/main.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ int main(
4141
int argc,
4242
char** argv)
4343
{
44-
logUser(DDSROUTER_EXECUTION, "Starting DDS Router Tool execution.");
45-
4644
// Configuration File path
4745
std::string file_path = "";
4846

@@ -60,6 +58,10 @@ int main(
6058
{
6159
return static_cast<int>(ui::ProcessReturnCode::success);
6260
}
61+
else if (arg_parse_result == ui::ProcessReturnCode::version_argument)
62+
{
63+
return static_cast<int>(ui::ProcessReturnCode::success);
64+
}
6365
else if (arg_parse_result != ui::ProcessReturnCode::success)
6466
{
6567
return static_cast<int>(arg_parse_result);
@@ -85,6 +87,8 @@ int main(
8587
return static_cast<int>(ui::ProcessReturnCode::required_argument_failed);
8688
}
8789

90+
logUser(DDSROUTER_EXECUTION, "Starting DDS Router Tool execution.");
91+
8892
// Activate Debug
8993
if (activate_debug)
9094
{
@@ -98,7 +102,6 @@ int main(
98102
// Change it when Log Module is independent and with more extensive API.
99103
// Log::SetCategoryFilter(std::regex("(DDSROUTER)"));
100104
}
101-
102105
// Encapsulating execution in block to erase all memory correctly before closing process
103106
try
104107
{

tools/ddsrouter_tool/src/cpp/user_interface/ProcessReturnCode.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum class ProcessReturnCode : int
2828
{
2929
success = 0,
3030
help_argument = 1,
31+
version_argument = 2,
3132
incorrect_argument = 10,
3233
required_argument_failed = 11,
3334
execution_failed = 20,

tools/ddsrouter_tool/src/cpp/user_interface/arguments_configuration.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string>
2222
#include <vector>
2323

24+
#include <ddsrouter_core/library/config.h>
2425
#include <ddsrouter_utils/Log.hpp>
2526
#include <ddsrouter_utils/utils.hpp>
2627

@@ -86,10 +87,25 @@ const option::Descriptor usage[] = {
8687
" -d \t--debug\t \t" \
8788
"Activate debug Logs (be aware that some logs may require specific CMAKE compilation options)." \
8889
},
90+
{
91+
optionIndex::VERSION,
92+
0,
93+
"v",
94+
"version",
95+
Arg::None,
96+
" -v \t--version\t \t" \
97+
"Print version, branch and commit hash." \
98+
},
8999

90100
{ 0, 0, 0, 0, 0, 0 }
91101
};
92102

103+
void print_version()
104+
{
105+
std::cout << "DDSRouter " << DDSROUTER_CORE_VERSION_STRING << "\ncommit hash: " << DDSROUTER_CORE_COMMIT_HASH <<
106+
std::endl;
107+
}
108+
93109
ProcessReturnCode parse_arguments(
94110
int argc,
95111
char** argv,
@@ -149,6 +165,12 @@ ProcessReturnCode parse_arguments(
149165
return ProcessReturnCode::help_argument;
150166
}
151167

168+
if (options[optionIndex::VERSION])
169+
{
170+
print_version();
171+
return ProcessReturnCode::version_argument;
172+
}
173+
152174
for (int i = 0; i < parse.optionsCount(); ++i)
153175
{
154176
option::Option& opt = buffer[i];

0 commit comments

Comments
 (0)