Skip to content

Commit

Permalink
[#529] Check correct C++ runtime library version during compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Sep 5, 2023
1 parent 4451948 commit 330e0d1
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 5 deletions.
2 changes: 2 additions & 0 deletions compiler/extensions/cpp/freemarker/Bitmask.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<@include_guard_begin package.path, name/>

<@runtime_version_check generatorVersion/>

#include <zserio/BitStreamReader.h>
#include <zserio/BitStreamWriter.h>
#include <zserio/DeltaContext.h>
Expand Down
2 changes: 2 additions & 0 deletions compiler/extensions/cpp/freemarker/Choice.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

<@include_guard_begin package.path, name/>

<@runtime_version_check generatorVersion/>

<#if withWriterCode && fieldList?has_content>
#include <zserio/Traits.h>
</#if>
Expand Down
2 changes: 2 additions & 0 deletions compiler/extensions/cpp/freemarker/Constant.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<@include_guard_begin package.path, name/>

<@runtime_version_check generatorVersion/>

<@system_includes headerSystemIncludes/>
<@user_includes headerUserIncludes/>
<@namespace_begin package.path/>
Expand Down
2 changes: 2 additions & 0 deletions compiler/extensions/cpp/freemarker/Enumeration.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<@include_guard_begin package.path, name/>

<@runtime_version_check generatorVersion/>

#include <array>

#include <zserio/Enums.h>
Expand Down
8 changes: 8 additions & 0 deletions compiler/extensions/cpp/freemarker/FileHeader.inc.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ ${generatorDescription}
#endif // <@include_guard_name packagePath, typeName/>
</#macro>

<#macro runtime_version_check generatorVersion>
#include <zserio/CppRuntimeVersion.h>
#if CPP_EXTENSION_RUNTIME_VERSION_NUMBER != ${generatorVersion.versionNumber}
#error Version mismatch between Zserio runtime library and Zserio compiler!
#error Please update your Zserio runtime library to the version ${generatorVersion.versionString}.
#endif
</#macro>

<#macro namespace_begin packagePath>
<#if packagePath?has_content>

Expand Down
2 changes: 2 additions & 0 deletions compiler/extensions/cpp/freemarker/Pubsub.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

<@include_guard_begin package.path, name/>

<@runtime_version_check generatorVersion/>

#include <memory>
#include <zserio/AllocatorHolder.h>
#include <zserio/IPubsub.h>
Expand Down
2 changes: 2 additions & 0 deletions compiler/extensions/cpp/freemarker/Service.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

<@include_guard_begin package.path, name/>

<@runtime_version_check generatorVersion/>

#include <array>
#include <zserio/Types.h>
<@type_includes types.service/>
Expand Down
2 changes: 2 additions & 0 deletions compiler/extensions/cpp/freemarker/SqlDatabase.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

<@include_guard_begin package.path, name/>

<@runtime_version_check generatorVersion/>

#include <memory>
#include <array>
<@type_includes types.set/>
Expand Down
2 changes: 2 additions & 0 deletions compiler/extensions/cpp/freemarker/SqlTable.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

<@include_guard_begin package.path, name/>

<@runtime_version_check generatorVersion/>

#include <memory>
#include <sqlite3.h>
<@type_includes types.vector/>
Expand Down
2 changes: 2 additions & 0 deletions compiler/extensions/cpp/freemarker/Structure.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

<@include_guard_begin package.path, name/>

<@runtime_version_check generatorVersion/>

<#if withWriterCode && fieldList?has_content>
#include <zserio/Traits.h>
</#if>
Expand Down
2 changes: 2 additions & 0 deletions compiler/extensions/cpp/freemarker/Subtype.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<@include_guard_begin package.path, name/>

<@runtime_version_check generatorVersion/>

<@system_includes headerSystemIncludes/>
<@user_includes headerUserIncludes/>
<@namespace_begin package.path/>
Expand Down
2 changes: 2 additions & 0 deletions compiler/extensions/cpp/freemarker/Union.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

<@include_guard_begin package.path, name/>

<@runtime_version_check generatorVersion/>

<#if withWriterCode && fieldList?has_content>
#include <zserio/Traits.h>
</#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
*/
constexpr const char* CPP_EXTENSION_RUNTIME_VERSION_STRING = "2.11.0";

/**
* C++ extension runtime version in integer value to allow version checking in generated code.
*/
#define CPP_EXTENSION_RUNTIME_VERSION_NUMBER 2011000

#endif // ifndef ZSERIO_CPP_RUNTIME_VERSION_H_INC
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ public class CppExtensionVersion
{
/** C++ extension version string. */
public static final String VERSION_STRING = "2.11.0";

/** C++ extension version in integer value to allow version checking in generated code. */
public static final long VERSION_NUMBER = 2011000;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public abstract class CppTemplateData implements IncludeCollector
public CppTemplateData(TemplateDataContext context)
{
generatorDescription = context.getGeneratorDescription();
generatorVersion = new GeneratorVersionTemplateData(context.getGeneratorVersionString(),
context.getGeneratorVersionNumber());

withWriterCode = context.getWithWriterCode();
withValidationCode = context.getWithValidationCode();
Expand All @@ -38,6 +40,11 @@ public String getGeneratorDescription()
return generatorDescription;
}

public GeneratorVersionTemplateData getGeneratorVersion()
{
return generatorVersion;
}

public boolean getWithWriterCode()
{
return withWriterCode;
Expand Down Expand Up @@ -156,6 +163,28 @@ public Iterable<String> getPath()
private final PackageName packageName;
}

public static class GeneratorVersionTemplateData
{
public GeneratorVersionTemplateData(String versionString, long versionNumber)
{
this.versionString = versionString;
this.versionNumber = Long.toString(versionNumber);
}

public String getVersionString()
{
return versionString;
}

public String getVersionNumber()
{
return versionNumber;
}

private final String versionString;
private final String versionNumber;
}

public static class TypesTemplateData
{
public TypesTemplateData(TypesContext typesContext, CppNativeMapper nativeMapper)
Expand Down Expand Up @@ -403,6 +432,7 @@ public boolean getNeedsAllocatorArgument()
}

private final String generatorDescription;
private final GeneratorVersionTemplateData generatorVersion;

private final boolean withWriterCode;
private final boolean withValidationCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public TemplateDataContext(CppExtensionParameters cppParameters)
CppExtensionVersion.VERSION_STRING + ".\n" +
" * Generator setup: " + cppParameters.getParametersDescription() + ".\n" +
" */";
generatorVersionString = CppExtensionVersion.VERSION_STRING;
generatorVersionNumber = CppExtensionVersion.VERSION_NUMBER;
}

public CppNativeMapper getCppNativeMapper()
Expand Down Expand Up @@ -92,6 +94,16 @@ public String getGeneratorDescription()
return generatorDescription;
}

public String getGeneratorVersionString()
{
return generatorVersionString;
}

public long getGeneratorVersionNumber()
{
return generatorVersionNumber;
}

private final TypesContext typesContext;

private final CppNativeMapper cppNativeMapper;
Expand All @@ -103,4 +115,6 @@ public String getGeneratorDescription()
private final boolean withReflectionCode;
private final boolean withCodeComments;
private final String generatorDescription;
private final String generatorVersionString;
private final long generatorVersionNumber;
}
31 changes: 26 additions & 5 deletions scripts/update_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,32 @@ source "${SCRIPT_DIR}/common_tools.sh"
# Update Zserio version in a single file.
update_version_in_file()
{
exit_if_argc_ne $# 2
exit_if_argc_ne $# 3
local VERSION_FILE="${1}"; shift
local NEW_VERSION_STRING="${1}"; shift
local NEW_VERSION_NUMBER="${1}"; shift

echo -ne "Updating version to ${NEW_VERSION_STRING} in '${VERSION_FILE}'..."
echo -ne "Updating version string to ${NEW_VERSION_STRING} in '${VERSION_FILE}'..."
sed -i -e 's/[0-9]\+\.[0-9]\+\.[0-9]\+\(\-[A-Za-z0-9]\+\)\?/'"${NEW_VERSION_STRING}"'/' "${VERSION_FILE}"
local SED_RESULT=$?
if [ ${SED_RESULT} -ne 0 ] ; then
stderr_echo "Failed with return code ${SED_RESULT}!"
return 1
fi

sed -i -e 's/NUMBER\ =\ [0-9]\+/'NUMBER\ =\ ${NEW_VERSION_NUMBER}'/' "${VERSION_FILE}"
local SED_RESULT=$?
if [ ${SED_RESULT} -ne 0 ] ; then
stderr_echo "Failed with return code ${SED_RESULT}!"
return 1
fi

sed -i -e 's/NUMBER\ [0-9]\+/'NUMBER\ ${NEW_VERSION_NUMBER}'/' "${VERSION_FILE}"
local SED_RESULT=$?
if [ ${SED_RESULT} -ne 0 ] ; then
stderr_echo "Failed with return code ${SED_RESULT}!"
return 1
fi
echo "Done"

return 0
Expand All @@ -25,9 +40,10 @@ update_version_in_file()
# Update Zserio version in local copy of Git repository.
update_version()
{
exit_if_argc_ne $# 2
exit_if_argc_ne $# 3
local ZSERIO_SOURCE_DIR="${1}"; shift
local NEW_VERSION_STRING="${1}"; shift
local NEW_VERSION_NUMBER="${1}"; shift

# find all files with version
local PYTHON_RUNTIME_VERSION_FILE="${ZSERIO_SOURCE_DIR}/extensions/python/runtime/src/zserio/__init__.py"
Expand All @@ -41,7 +57,7 @@ update_version()
for VERSION_FILE in ${VERSION_FILES}
do
if [[ "${VERSION_FILE}" != "${COMPATIBILITY_VERSION_FILE}" ]] ; then
update_version_in_file "${VERSION_FILE}" "${NEW_VERSION_STRING}"
update_version_in_file "${VERSION_FILE}" "${NEW_VERSION_STRING}" ${NEW_VERSION_NUMBER}
if [ $? -ne 0 ] ; then
return 1
fi
Expand Down Expand Up @@ -141,8 +157,13 @@ main()
return 1
fi

# calculate new version number from string
local NEW_VERSION_ARRAY=(${NEW_VERSION_STRING//./ })
local NEW_VERSION_NUMBER=$(printf "%d%03d%03d" ${NEW_VERSION_ARRAY[0]} ${NEW_VERSION_ARRAY[1]} \
${NEW_VERSION_ARRAY[2]})

# update version
update_version "${SCRIPT_DIR}/../compiler" "${NEW_VERSION_STRING}"
update_version "${SCRIPT_DIR}/../compiler" "${NEW_VERSION_STRING}" ${NEW_VERSION_NUMBER}
if [ $? -ne 0 ] ; then
return 1
fi
Expand Down

0 comments on commit 330e0d1

Please sign in to comment.