Skip to content

Commit e70e036

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

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

fable/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ project(fable LANGUAGES CXX)
44

55
# NOTE: The variable FABLE_VERSION is used throughout this CMakeLists file
66
# and is supplied from Conan or by hand on the command line.
7+
set(FABLE_VERSION "0.0.0-undefined" CACHE STRING "Fable version as MAJOR.MINOR.PATCH string")
8+
set(FABLE_VERSION_U32 0 CACHE STRING "Fable version as (MAJOR<<16)|(MINOR<<8)|PATCH integer")
79

810
include(GNUInstallDirs)
911

@@ -40,9 +42,11 @@ set_target_properties(${target} PROPERTIES
4042
CXX_STANDARD_REQUIRED ON
4143
VERSION ${FABLE_VERSION}
4244
)
45+
configure_file(src/fable/version.hpp.in include/fable/version.hpp @ONLY)
4346
target_include_directories(${target}
4447
PUBLIC
4548
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
49+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>"
4650
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
4751
)
4852
target_link_libraries(${target}
@@ -68,6 +72,7 @@ if(BUILD_TESTING)
6872

6973
add_executable(test-fable
7074
# find src -type f -name "*_test.cpp"
75+
src/fable/version_test.cpp
7176
src/fable/environment_test.cpp
7277
src/fable/schema/const_test.cpp
7378
src/fable/schema/custom_test.cpp
@@ -106,6 +111,9 @@ install(TARGETS ${target}
106111
install(DIRECTORY include/
107112
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
108113
)
114+
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
115+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
116+
)
109117
install(EXPORT ${namespace}Targets
110118
NAMESPACE ${namespace}::
111119
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${namespace}

fable/conanfile.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# mypy: ignore-errors
22
# pylint: skip-file
33

4+
import os
45
from pathlib import Path
6+
from semver import SemVer
57

68
from conan import ConanFile
79
from conan.tools import cmake, files, scm
@@ -53,11 +55,22 @@ def build_requirements(self):
5355

5456
def layout(self):
5557
cmake.cmake_layout(self)
58+
self.cpp.source.includedirs.append(os.path.join(self.folders.build, "include"))
5659

5760
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+
5870
tc = cmake.CMakeToolchain(self)
5971
tc.cache_variables["CMAKE_EXPORT_COMPILE_COMMANDS"] = True
6072
tc.cache_variables["FABLE_VERSION"] = self.version
73+
tc.cache_variables["FABLE_VERSION_U32"] = version_u32
6174
tc.cache_variables["FABLE_ALLOW_COMMENTS"] = self.options.allow_comments
6275
tc.generate()
6376

@@ -85,5 +98,6 @@ def package_info(self):
8598
self.cpp_info.set_property("pkg_config_name", "fable")
8699
if not self.in_local_cache:
87100
self.cpp_info.libs = ["fable"]
101+
self.cpp_info.includedirs.append(os.path.join(self.build_folder, "include"))
88102
else:
89103
self.cpp_info.libs = files.collect_libs(self)

fable/src/fable/version.hpp.in

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 FABLE_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 FABLE_VERSION_U32:
32+
*
33+
* - FABLE_VERSION_EPOCH
34+
* - FABLE_VERSION_MAJOR
35+
* - FABLE_VERSION_MINOR
36+
* - FABLE_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 Fable version like so:
41+
*
42+
* #if FABLE_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 FABLE_VERSION_U32 >= VERSION_U32(1, 0, 0, 0)
57+
* #error "fable epoch > 0 not supported"
58+
* #else if (FABLE_VERSION_U32 >= VERSION_U32(0, 1, 0, 0)) && (FABLE_VERSION_U32 < VERSION_U32(0, 1, 2, 0))
59+
* // Code for fable versions between 1.0 and 1.1.*
60+
* #endif
61+
*/
62+
#ifndef VERSION_U32
63+
#define VERSION_U32(epoch, major, minor, patch) \
64+
(((epoch) << 24) | ((major) << 16) | ((minor) << 8) | (patch))
65+
#endif
66+
67+
#ifndef FABLE_VERSION
68+
#define FABLE_VERSION "@FABLE_VERSION@"
69+
#endif
70+
71+
#ifndef FABLE_VERSION_U32
72+
#define FABLE_VERSION_U32 @FABLE_VERSION_U32@
73+
#endif
74+
75+
#define FABLE_VERSION_EPOCH ((FABLE_VERSION_U32 >> 24) && 0xff)
76+
#define FABLE_VERSION_MAJOR ((FABLE_VERSION_U32 >> 16) && 0xff)
77+
#define FABLE_VERSION_MINOR ((FABLE_VERSION_U32 >> 8) && 0xff)
78+
#define FABLE_VERSION_PATCH ((FABLE_VERSION_U32 >> 0) && 0xff)

fable/src/fable/version_test.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
23+
TEST(fable_version, check_valid) {
24+
ASSERT_NE(FABLE_VERSION_U32, 0);
25+
ASSERT_NE(FABLE_VERSION, "0.0.0-undefined");
26+
}

0 commit comments

Comments
 (0)