Skip to content

Commit 73ee421

Browse files
committed
[#529] Check correct C++ runtime library version during compilation
1 parent e664e39 commit 73ee421

17 files changed

+108
-5
lines changed

compiler/extensions/cpp/freemarker/Bitmask.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
<@include_guard_begin package.path, name/>
66

7+
<@runtime_version_check generatorVersion/>
8+
79
#include <zserio/BitStreamReader.h>
810
#include <zserio/BitStreamWriter.h>
911
#include <zserio/DeltaContext.h>

compiler/extensions/cpp/freemarker/Choice.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
<@include_guard_begin package.path, name/>
1010

11+
<@runtime_version_check generatorVersion/>
12+
1113
<#if withWriterCode && fieldList?has_content>
1214
#include <zserio/Traits.h>
1315
</#if>

compiler/extensions/cpp/freemarker/Constant.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
<@include_guard_begin package.path, name/>
66

7+
<@runtime_version_check generatorVersion/>
8+
79
<@system_includes headerSystemIncludes/>
810
<@user_includes headerUserIncludes/>
911
<@namespace_begin package.path/>

compiler/extensions/cpp/freemarker/Enumeration.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
<@include_guard_begin package.path, name/>
66

7+
<@runtime_version_check generatorVersion/>
8+
79
#include <array>
810

911
#include <zserio/Enums.h>

compiler/extensions/cpp/freemarker/FileHeader.inc.ftl

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ ${generatorDescription}
5050
#endif // <@include_guard_name packagePath, typeName/>
5151
</#macro>
5252

53+
<#macro runtime_version_check generatorVersion>
54+
#include <zserio/CppRuntimeVersion.h>
55+
#if CPP_EXTENSION_RUNTIME_VERSION_NUMBER != ${generatorVersion.versionNumber}
56+
#error Version mismatch between Zserio runtime library and Zserio compiler!
57+
#error Please update your Zserio runtime library to the version ${generatorVersion.versionString}.
58+
#endif
59+
</#macro>
60+
5361
<#macro namespace_begin packagePath>
5462
<#if packagePath?has_content>
5563

compiler/extensions/cpp/freemarker/Pubsub.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
<@include_guard_begin package.path, name/>
77

8+
<@runtime_version_check generatorVersion/>
9+
810
#include <memory>
911
#include <zserio/AllocatorHolder.h>
1012
#include <zserio/IPubsub.h>

compiler/extensions/cpp/freemarker/Service.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
<@include_guard_begin package.path, name/>
77

8+
<@runtime_version_check generatorVersion/>
9+
810
#include <array>
911
#include <zserio/Types.h>
1012
<@type_includes types.service/>

compiler/extensions/cpp/freemarker/SqlDatabase.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
<@include_guard_begin package.path, name/>
1010

11+
<@runtime_version_check generatorVersion/>
12+
1113
#include <memory>
1214
#include <array>
1315
<@type_includes types.set/>

compiler/extensions/cpp/freemarker/SqlTable.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
<@include_guard_begin package.path, name/>
77

8+
<@runtime_version_check generatorVersion/>
9+
810
#include <memory>
911
#include <sqlite3.h>
1012
<@type_includes types.vector/>

compiler/extensions/cpp/freemarker/Structure.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
<@include_guard_begin package.path, name/>
1010

11+
<@runtime_version_check generatorVersion/>
12+
1113
<#if withWriterCode && fieldList?has_content>
1214
#include <zserio/Traits.h>
1315
</#if>

compiler/extensions/cpp/freemarker/Subtype.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
<@include_guard_begin package.path, name/>
66

7+
<@runtime_version_check generatorVersion/>
8+
79
<@system_includes headerSystemIncludes/>
810
<@user_includes headerUserIncludes/>
911
<@namespace_begin package.path/>

compiler/extensions/cpp/freemarker/Union.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
<@include_guard_begin package.path, name/>
1010

11+
<@runtime_version_check generatorVersion/>
12+
1113
<#if withWriterCode && fieldList?has_content>
1214
#include <zserio/Traits.h>
1315
</#if>

compiler/extensions/cpp/runtime/src/zserio/CppRuntimeVersion.h

+5
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@
66
*/
77
constexpr const char* CPP_EXTENSION_RUNTIME_VERSION_STRING = "2.11.0";
88

9+
/**
10+
* C++ extension runtime version in integer value to allow version checking in generated code.
11+
*/
12+
#define CPP_EXTENSION_RUNTIME_VERSION_NUMBER 2011000
13+
914
#endif // ifndef ZSERIO_CPP_RUNTIME_VERSION_H_INC

compiler/extensions/cpp/src/zserio/extension/cpp/CppExtensionVersion.java

+3
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ public class CppExtensionVersion
1010
{
1111
/** C++ extension version string. */
1212
public static final String VERSION_STRING = "2.11.0";
13+
14+
/** C++ extension version in integer value to allow version checking in generated code. */
15+
public static final long VERSION_NUMBER = 2011000;
1316
}

compiler/extensions/cpp/src/zserio/extension/cpp/CppTemplateData.java

+30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public abstract class CppTemplateData implements IncludeCollector
1717
public CppTemplateData(TemplateDataContext context)
1818
{
1919
generatorDescription = context.getGeneratorDescription();
20+
generatorVersion = new GeneratorVersionTemplateData(context.getGeneratorVersionString(),
21+
context.getGeneratorVersionNumber());
2022

2123
withWriterCode = context.getWithWriterCode();
2224
withValidationCode = context.getWithValidationCode();
@@ -38,6 +40,11 @@ public String getGeneratorDescription()
3840
return generatorDescription;
3941
}
4042

43+
public GeneratorVersionTemplateData getGeneratorVersion()
44+
{
45+
return generatorVersion;
46+
}
47+
4148
public boolean getWithWriterCode()
4249
{
4350
return withWriterCode;
@@ -156,6 +163,28 @@ public Iterable<String> getPath()
156163
private final PackageName packageName;
157164
}
158165

166+
public static class GeneratorVersionTemplateData
167+
{
168+
public GeneratorVersionTemplateData(String versionString, long versionNumber)
169+
{
170+
this.versionString = versionString;
171+
this.versionNumber = Long.toString(versionNumber);
172+
}
173+
174+
public String getVersionString()
175+
{
176+
return versionString;
177+
}
178+
179+
public String getVersionNumber()
180+
{
181+
return versionNumber;
182+
}
183+
184+
private final String versionString;
185+
private final String versionNumber;
186+
}
187+
159188
public static class TypesTemplateData
160189
{
161190
public TypesTemplateData(TypesContext typesContext, CppNativeMapper nativeMapper)
@@ -403,6 +432,7 @@ public boolean getNeedsAllocatorArgument()
403432
}
404433

405434
private final String generatorDescription;
435+
private final GeneratorVersionTemplateData generatorVersion;
406436

407437
private final boolean withWriterCode;
408438
private final boolean withValidationCode;

compiler/extensions/cpp/src/zserio/extension/cpp/TemplateDataContext.java

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public TemplateDataContext(CppExtensionParameters cppParameters)
2828
CppExtensionVersion.VERSION_STRING + ".\n" +
2929
" * Generator setup: " + cppParameters.getParametersDescription() + ".\n" +
3030
" */";
31+
generatorVersionString = CppExtensionVersion.VERSION_STRING;
32+
generatorVersionNumber = CppExtensionVersion.VERSION_NUMBER;
3133
}
3234

3335
public CppNativeMapper getCppNativeMapper()
@@ -92,6 +94,16 @@ public String getGeneratorDescription()
9294
return generatorDescription;
9395
}
9496

97+
public String getGeneratorVersionString()
98+
{
99+
return generatorVersionString;
100+
}
101+
102+
public long getGeneratorVersionNumber()
103+
{
104+
return generatorVersionNumber;
105+
}
106+
95107
private final TypesContext typesContext;
96108

97109
private final CppNativeMapper cppNativeMapper;
@@ -103,4 +115,6 @@ public String getGeneratorDescription()
103115
private final boolean withReflectionCode;
104116
private final boolean withCodeComments;
105117
private final String generatorDescription;
118+
private final String generatorVersionString;
119+
private final long generatorVersionNumber;
106120
}

scripts/update_version.sh

+26-5
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,32 @@ source "${SCRIPT_DIR}/common_tools.sh"
66
# Update Zserio version in a single file.
77
update_version_in_file()
88
{
9-
exit_if_argc_ne $# 2
9+
exit_if_argc_ne $# 3
1010
local VERSION_FILE="${1}"; shift
1111
local NEW_VERSION_STRING="${1}"; shift
12+
local NEW_VERSION_NUMBER="${1}"; shift
1213

13-
echo -ne "Updating version to ${NEW_VERSION_STRING} in '${VERSION_FILE}'..."
14+
echo -ne "Updating version string to ${NEW_VERSION_STRING} in '${VERSION_FILE}'..."
1415
sed -i -e 's/[0-9]\+\.[0-9]\+\.[0-9]\+\(\-[A-Za-z0-9]\+\)\?/'"${NEW_VERSION_STRING}"'/' "${VERSION_FILE}"
1516
local SED_RESULT=$?
1617
if [ ${SED_RESULT} -ne 0 ] ; then
1718
stderr_echo "Failed with return code ${SED_RESULT}!"
1819
return 1
1920
fi
21+
22+
sed -i -e 's/NUMBER\ =\ [0-9]\+/'NUMBER\ =\ ${NEW_VERSION_NUMBER}'/' "${VERSION_FILE}"
23+
local SED_RESULT=$?
24+
if [ ${SED_RESULT} -ne 0 ] ; then
25+
stderr_echo "Failed with return code ${SED_RESULT}!"
26+
return 1
27+
fi
28+
29+
sed -i -e 's/NUMBER\ [0-9]\+/'NUMBER\ ${NEW_VERSION_NUMBER}'/' "${VERSION_FILE}"
30+
local SED_RESULT=$?
31+
if [ ${SED_RESULT} -ne 0 ] ; then
32+
stderr_echo "Failed with return code ${SED_RESULT}!"
33+
return 1
34+
fi
2035
echo "Done"
2136

2237
return 0
@@ -25,9 +40,10 @@ update_version_in_file()
2540
# Update Zserio version in local copy of Git repository.
2641
update_version()
2742
{
28-
exit_if_argc_ne $# 2
43+
exit_if_argc_ne $# 3
2944
local ZSERIO_SOURCE_DIR="${1}"; shift
3045
local NEW_VERSION_STRING="${1}"; shift
46+
local NEW_VERSION_NUMBER="${1}"; shift
3147

3248
# find all files with version
3349
local PYTHON_RUNTIME_VERSION_FILE="${ZSERIO_SOURCE_DIR}/extensions/python/runtime/src/zserio/__init__.py"
@@ -41,7 +57,7 @@ update_version()
4157
for VERSION_FILE in ${VERSION_FILES}
4258
do
4359
if [[ "${VERSION_FILE}" != "${COMPATIBILITY_VERSION_FILE}" ]] ; then
44-
update_version_in_file "${VERSION_FILE}" "${NEW_VERSION_STRING}"
60+
update_version_in_file "${VERSION_FILE}" "${NEW_VERSION_STRING}" ${NEW_VERSION_NUMBER}
4561
if [ $? -ne 0 ] ; then
4662
return 1
4763
fi
@@ -141,8 +157,13 @@ main()
141157
return 1
142158
fi
143159

160+
# calculate new version number from string
161+
local NEW_VERSION_ARRAY=(${NEW_VERSION_STRING//./ })
162+
local NEW_VERSION_NUMBER=$(printf "%d%03d%03d" ${NEW_VERSION_ARRAY[0]} ${NEW_VERSION_ARRAY[1]} \
163+
${NEW_VERSION_ARRAY[2]})
164+
144165
# update version
145-
update_version "${SCRIPT_DIR}/../compiler" "${NEW_VERSION_STRING}"
166+
update_version "${SCRIPT_DIR}/../compiler" "${NEW_VERSION_STRING}" ${NEW_VERSION_NUMBER}
146167
if [ $? -ne 0 ] ; then
147168
return 1
148169
fi

0 commit comments

Comments
 (0)