Skip to content

Commit 8d5dbd7

Browse files
committed
runtime: Add version.hpp
This is generated from src/runtime/version.hpp.in See the file for the documentation.
1 parent e70e036 commit 8d5dbd7

File tree

4 files changed

+149
-2
lines changed

4 files changed

+149
-2
lines changed

runtime/CMakeLists.txt

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
22

33
project(cloe-runtime LANGUAGES CXX)
44

5+
# NOTE: The variable CLOE_VERSION is used throughout this CMakeLists file
6+
# and is supplied from Conan or by hand on the command line.
7+
set(CLOE_VERSION "0.0.0-undefined" CACHE STRING "Cloe version as MAJOR.MINOR.PATCH string")
8+
set(CLOE_VERSION_U32 0 CACHE STRING "Cloe version as (MAJOR<<16)|(MINOR<<8)|PATCH integer")
9+
510
include(GNUInstallDirs)
611
include(cmake/TargetLinting.cmake)
712

@@ -48,12 +53,14 @@ add_library(${alias} ALIAS ${target})
4853
set_target_properties(${target} PROPERTIES
4954
CXX_STANDARD 17
5055
CXX_STANDARD_REQUIRED ON
51-
VERSION ${CLOE_PROJECT_VERSION}
56+
VERSION ${CLOE_VERSION}
5257
)
5358
set_target_linting(${target})
59+
configure_file(src/cloe/version.hpp.in include/cloe/version.hpp @ONLY)
5460
target_include_directories(${target}
5561
PUBLIC
5662
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
63+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>"
5764
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
5865
)
5966
target_link_libraries(${target}
@@ -83,6 +90,7 @@ if(BUILD_TESTING)
8390

8491
add_executable(test-cloe
8592
# find src -type f -name "*_test.cpp"
93+
src/cloe/version_test.cpp
8694
src/cloe/utility/statistics_test.cpp
8795
src/cloe/utility/uid_tracker_test.cpp
8896
)
@@ -115,3 +123,6 @@ install(
115123
DIRECTORY include/
116124
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
117125
)
126+
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
127+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
128+
)

runtime/conanfile.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
from pathlib import Path
6+
from semver import SemVer
67

78
from conan import ConanFile
89
from conan.tools import cmake, files, scm
@@ -54,11 +55,22 @@ def build_requirements(self):
5455

5556
def layout(self):
5657
cmake.cmake_layout(self)
58+
self.cpp.source.includedirs.append(os.path.join(self.folders.build, "include"))
5759

5860
def generate(self):
61+
# The version as a single 32-bit number takes the format:
62+
#
63+
# (EPOCH << 24) | (MAJOR_VERSION << 16) | (MINOR_VERSION << 8) | PATCH_VERSION
64+
#
65+
# Each version consists of at most 8 bits, so 256 potential values, including 0.
66+
# The epoch starts with 0, and is bumped after each version naming scheme.
67+
semver = SemVer(self.version, True)
68+
version_u32 = (0<<24) | (semver.major << 16) | (semver.minor << 8) | semver.patch
69+
5970
tc = cmake.CMakeToolchain(self)
6071
tc.cache_variables["CMAKE_EXPORT_COMPILE_COMMANDS"] = True
61-
tc.cache_variables["CLOE_PROJECT_VERSION"] = self.version
72+
tc.cache_variables["CLOE_VERSION"] = self.version
73+
tc.cache_variables["CLOE_VERSION_U32"] = version_u32
6274
tc.cache_variables["TargetLintingExtended"] = self.options.pedantic
6375
tc.generate()
6476

@@ -90,6 +102,7 @@ def package_info(self):
90102
# mode and in the normal package mode:
91103
if not self.in_local_cache:
92104
self.cpp_info.builddirs.append(os.path.join(self.source_folder, "cmake"))
105+
self.cpp_info.includedirs.append(os.path.join(self.build_folder, "include"))
93106
self.cpp_info.libs = ["cloe-runtime"]
94107
else:
95108
self.cpp_info.builddirs.append(os.path.join("lib", "cmake", "cloe"))

runtime/src/cloe/version.hpp.in

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2023 Robert Bosch GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
/**
19+
* \file version.hpp
20+
*
21+
* This file just provides version information.
22+
*
23+
* We expect the CMakeLists.txt to define a constant CLOE_VERSION_U32,
24+
* which takes the following format:
25+
*
26+
* (EPOCH << 24) | (MAJOR_VERSION << 16) | (MINOR_VERSION << 8) | PATCH_VERSION
27+
*
28+
* Each version consists of at most 8 bits, so 256 potential values, including 0.
29+
* The epoch starts with 0, and is bumped after each version naming scheme.
30+
*
31+
* The following macros are defined from CLOE_VERSION_U32:
32+
*
33+
* - CLOE_VERSION_EPOCH
34+
* - CLOE_VERSION_MAJOR
35+
* - CLOE_VERSION_MINOR
36+
* - CLOE_VERSION_PATCH
37+
*
38+
* It's unlikely that EPOCH will ever be changed, but better safe than sorry.
39+
*
40+
* You can check for some Cloe version like so:
41+
*
42+
* #if CLOE_VERSION_U32 < VERSION_U32(0, 1, 0, 0)
43+
*/
44+
45+
#pragma once
46+
47+
/**
48+
* VERSION_U32 is a helper macro to create 32-bit unsigned
49+
* Epoch-Major-Minor-Patch version integers.
50+
*
51+
* These can be then conveniently used to compare versions with the normal
52+
* integer comparison operators.
53+
*
54+
* For example:
55+
*
56+
* #if CLOE_VERSION_U32 >= VERSION_U32(1, 0, 0, 0)
57+
* #error "cloe epoch > 0 not supported"
58+
* #else if (CLOE_VERSION_U32 >= VERSION_U32(0, 1, 0, 0)) && (CLOE_VERSION_U32 < VERSION_U32(0, 1, 2, 0))
59+
* // Code for cloe versions between 1.0 and 1.1.*
60+
* #endif
61+
*
62+
* Note: This macro is defined with the same name and in the same way as
63+
* fable/version.hpp and is not redefined here if already defined.
64+
* As long as these macros exist it is unlikely that they will change
65+
* in mechanism or form, as this would break all code that depends on
66+
* version comparison.
67+
*/
68+
#ifndef VERSION_U32
69+
#define VERSION_U32(epoch, major, minor, patch) \
70+
(((epoch) << 24) | ((major) << 16) | ((minor) << 8) | (patch))
71+
#endif
72+
73+
#ifndef CLOE_VERSION
74+
#define CLOE_VERSION "@CLOE_VERSION@"
75+
#endif
76+
77+
#ifndef CLOE_VERSION_U32
78+
#define CLOE_VERSION_U32 @CLOE_VERSION_U32@
79+
#endif
80+
81+
#define CLOE_VERSION_EPOCH ((CLOE_VERSION_U32 >> 24) && 0xff)
82+
#define CLOE_VERSION_MAJOR ((CLOE_VERSION_U32 >> 16) && 0xff)
83+
#define CLOE_VERSION_MINOR ((CLOE_VERSION_U32 >> 8) && 0xff)
84+
#define CLOE_VERSION_PATCH ((CLOE_VERSION_U32 >> 0) && 0xff)

runtime/src/cloe/version_test.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2023 Robert Bosch GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
#include <gtest/gtest.h>
20+
21+
#include <fable/version.hpp>
22+
#include <cloe/version.hpp>
23+
24+
TEST(fable_version, check_valid) {
25+
// Ensure we can access the fable version information.
26+
ASSERT_NE(FABLE_VERSION_U32, 0);
27+
ASSERT_NE(FABLE_VERSION, "0.0.0-undefined");
28+
}
29+
30+
TEST(cloe_version, check_valid) {
31+
// Ensure we can access the cloe version, and it isn't undefined
32+
ASSERT_NE(CLOE_VERSION_U32, 0);
33+
ASSERT_NE(CLOE_VERSION, "0.0.0-undefined");
34+
}
35+
36+
TEST(cloe_version, check_identical_to_fable_version) {
37+
ASSERT_EQ(FABLE_VERSION_U32, CLOE_VERSION_U32);
38+
ASSERT_EQ(FABLE_VERSION, CLOE_VERSION);
39+
}

0 commit comments

Comments
 (0)