Build/Features/detail/use-version-write-prjver-run.lua
Jamie Reece Wilson af432ad631 [+] winStack
[*] snapshot dates now may contain an E
2023-10-25 11:50:45 +01:00

290 lines
6.9 KiB
Lua

local function GetSnapshotName(full)
local strYear = os.date("%y")
local strMon = os.date("%m")
local iWeekdayOffset = tonumber(os.date("%w"))
local iMonthOffset = tonumber(os.date("%d"))
if (iWeekdayOffset == 0) then iWeekdayOffset = 6
else iWeekdayOffset = iWeekdayOffset - 1 end
local iWeekIdxOffset = math.floor((iMonthOffset - iWeekdayOffset) / 7)
if (iWeekIdxOffset < 0) then iWeekIdxOffset = 0 end
local bWeekTest = iWeekdayOffset > 2
local cWeekBase = "A"
if (bWeekTest) then cWeekBase = "a" else cWeekBase = "A" end
if (iWeekIdxOffset > 4) then iWeekIdxOffset = 4 end
local cWeekChar = string.char(string.byte(cWeekBase) + iWeekIdxOffset)
if (not full) then strYear = strYear:sub(2, 2) end
return strYear .. strMon .. cWeekChar
end
local function DoAction()
local projectName = auBuild.projectName
local version = auBuild.projectRoot .. "/Version.json"
local manifest = json.decode(io.readfile(version))
local prjex = ""
if (not manifest.noPrjName) then
prjex = projectName
else
prjex = "Au"
end
local versionH = auBuild.projectRoot .. "/Source/" .. prjex .. "Version.h"
local versionHPP = auBuild.projectRoot .. "/Source/" .. prjex .. "Version.hpp"
local versionC = auBuild.projectRoot .. "/Source/" .. prjex .. "Version.c"
local patchVer = 0
if (manifest.patch) then
patchVer = manifest.patch
if (manifest.incrementPatchOnBuild) then
manifest.patch = patchVer + 1;
end
end
local includeGit = manifest.includeGit
local includeARD = manifest.includeARD
local includeARDExtended = manifest.includeARDExtended
local includeARDFull = manifest.includeARDFull
local relString = ""
local commitString = ""
if (includeGit) then
local cmd = nil
if (auBuild.shortGitCommit) then
local result, errorCode = os.outputof("git rev-parse --short HEAD")
commitString = result
else
local result, errorCode = os.outputof("git rev-parse --verify HEAD")
commitString = result
end
end
if (includeARD) then
relString = GetSnapshotName(includeARDFull)
if (includeARDExtended) then
relString = relString .. patchVer
end
end
io.writefile(version, json.encode_pretty(manifest))
if (manifest.patch) then
manifest.patch = patchVer
end
local hheader = manifest.hheader or ""
local chTemplate = [[
// Aurora Build Pipeline: Auto-generated Version File
#include "AuroraTypes.h"
#include "AuroraEnvironment.h" // For "AURORA_SYMBOL_EXPORT", if required
%s
%suint32_ct Get%sVersionHash();
%suint16_ct Get%sPatchVersion();
%suint16_ct Get%sMinorVersion();
%suint16_ct Get%sMajorVersion();
%suint32_ct Get%sGitVersionDetail();%s
]]
local vis = nil
if (manifest.visibility) then
vis = manifest.visibility .. " "
else
vis = ""
end
if (includeGit) then
hheader = hheader .. "\n"
if (shortGitCommit) then
hheader = hheader .. "#define VER_" .. string.upper(projectName) .. "_USES_SMALL_HASH 1\n"
end
hheader = hheader .. "#define VER_" .. string.upper(projectName) .. "_HAS_GIT_VERSION 1\n"
end
local cppTemplate = [[
// Aurora Build Pipeline: Auto-generated Version File
extern "C"
{
#include "%sVersion.h"
}
]]
local cHeaderGitExt = ""
if (includeGit) then
cHeaderGitExt = [[
%sconst char *Get%sCommitString();]]
cHeaderGitExt = string.format(cHeaderGitExt, vis, projectName)
end
if (includeARD) then
local hARDFooter = [[
%sconst char *Get%sAuroraReleaseDate();]]
cHeaderGitExt = cHeaderGitExt .. string.format(hARDFooter, vis, projectName)
end
local formattedChTemplate = string.format(chTemplate,
hheader,
vis, projectName,
vis, projectName,
vis, projectName,
vis, projectName,
vis, projectName,
cHeaderGitExt
)
if (not os.isfile(versionH)) then
io.writefile(versionH, formattedChTemplate)
end
if (not os.isfile(versionHPP)) then
io.writefile(versionHPP, string.format(cppTemplate, projectName))
end
local cheader = manifest.cheader or ""
local cTemplate = [[
// Aurora Build Pipeline: Auto-generated Version File
#define FNV1_MAGIC_VAL32 0x811c9dc5u
#define FNV1_MAGIC_PRIME32 0x01000193u
#include "AuroraCommon.h"
#include "AuroraEnvironment.h" // For "AURORA_SYMBOL_EXPORT", if required
%s
%suint16_ct Get%sPatchVersion()
{
return %s;
}
%suint16_ct Get%sMinorVersion()
{
return %s;
}
%suint16_ct Get%sMajorVersion()
{
return %s;
}
%suint32_ct Get%sVersionHash()
{
uint32_ct result = FNV1_MAGIC_VAL32;
static uint32_ct g_result = 0;
if (g_result)
{
return g_result;
}
%s
uint32_ct i = 0;
for (; i < sizeof(info); i++)
{
result ^= (uint32_ct)(((const char *)&info)[i]) * (uint32_ct)(FNV1_MAGIC_PRIME32);
}
return g_result = result;
}
%suint32_ct Get%sGitVersionDetail()
{
return %su | (%s << 1u);
}
%s]]
local hashTemplateA = [[
struct dummy
{
uint16_ct a;
uint16_ct b;
uint16_ct c;
} info;
info.a = Get%sPatchVersion();
info.b = Get%sMinorVersion();
info.c = Get%sMajorVersion();
]]
local hashTemplateB = [[
const char info[sizeof(%s)] = %s;
]]
local hashTemplate = nil
if (not includeGit) then
hashTemplate = string.format(hashTemplateA, projectName, projectName, projectName)
else
hashTemplate = string.format(hashTemplateB, "\"" .. commitString .. "\"", "\"" .. commitString .. "\"")
end
local strVerHash = ""
local strPatch = manifest.patch or "0"
local strMinor = manifest.minor or "0"
local strMajor = manifest.major or "0"
local gitFooter = ""
if (includeGit) then
gitFooter = [[
%sconst char *Get%sCommitString()
{
return %s;
}]]
gitFooter = string.format(gitFooter, vis, projectName, "\"" .. commitString .. "\"")
end
if (manifest.includeARD) then
local atfooter = [[
%sconst char *Get%sAuroraReleaseDate()
{
return %s;
}]]
if (includeGit) then
atfooter = [[
]] .. atfooter
end
atfooter = string.format(atfooter, vis, projectName, "\"" .. relString .. "\"")
gitFooter = gitFooter .. atfooter
end
local formattedCTemplate = string.format(cTemplate,
cheader,
vis, projectName, strPatch,
vis, projectName, strMinor,
vis, projectName, strMajor,
vis, projectName, hashTemplate,
vis, projectName, manifest.includeGit and "1" or "0", auBuild.shortGitCommit and "1" or "0",
gitFooter
)
io.writefile(versionC, formattedCTemplate)
end
return DoAction