Perhaps you can use some cli xpath utility in 'Project Options/Compiler Options/Compiler Commands/Execute before' to extract /CONFIG/ProjectOptions/VersionInfo/BuildNr/@Value from the *.lpi, write it to a temp file and then include it into your source with {$I temp.inc}.
It is quite easy to write such utility by yourself, see https://wiki.lazarus.freepascal.org/xpath
I decided to give a try to @alpine's suggestion. I wrote the following program:
{$MODE OBJFPC}
uses
laz2_dom, laz2_XMLRead;
const
pref = 'vinfo_';
var
Doc : TXMLDocument;
f : TextFile;
procedure printVer (const Tag : String);
begin
writeln(f, pref, Tag , ' = ',
Doc.GetElementsByTagName(Tag)[0].Attributes.GetNamedItem('Value').TextContent,
';');
end;
procedure printST (const st : TDOMNodeList; const a : array of String);
var
i : Integer;
begin
for i := low(a) to high(a) do
writeln(f, pref, a[i] , ' = ''',
st[0].Attributes.GetNamedItem(a[i]).TextContent, ''';');
end;
begin
AssignFile(f, paramstr(2));
try
Rewrite(f);
ReadXMLFile(Doc, paramstr(1));
printVer('MajorVersionNr');
printVer('MinorVersionNr');
printVer('RevisionNr');
printVer('BuildNr');
printST(Doc.GetElementsByTagName('StringTable'),
['Comments', 'CompanyName', 'FileDescription', 'InternalName', 'LegalCopyright',
'LegalTrademarks', 'OriginalFilename', 'ProductName', 'ProductVersion']);
finally
CloseFile(f);
Doc.Free;
end;
end.
On Linux, you can compile it with:
fpc -Fu/usr/share/lazarus/2.2.6/components/lazutils/lib/x86_64-linux/ vinfo.pas
fpc -Fu/usr/share/lazarus/2.2.6/components/lazutils/lib/i386-linux/ vinfo.pasOn Windows, with:
\lazarus\fpc\3.2.2\bin\x86_64-win64\fpc -Fu\lazarus\components\lazutils\lib\x86_64-win64 vinfo.pas
\lazarus\fpc\3.2.2\bin\i386-win32\fpc -Fu\lazarus\components\lazutils\lib\i386-win32 vinfo.pasAlso, on Windows run,
ren vinfo.exe vinfoafter the compilation.
Then, on Lazarus
Project Options... →
Compiler Commands:
Execute before Build put:
vinfo your_project.lpi vinfo.incand in your project's main unit's public
const declarations insert:
After selecting
Run →
Build in Lazarus, you can compile your project which contains references to the
vinfo_ constants created automatically from the
.lpi file by the
vinfo program.