1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
{
Copyright 2019-2021 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Detecting FPC and Lazarus version and some capabilities. }
unit ToolFpcVersion;
interface
type
TToolVersion = class
protected
{ Get Major,Minor,Release,ReleaseRemark from S.
Raise exception if it doesn't start with necessary numbers. }
procedure ParseVersion(const S: String);
public
Major, Minor, Release: Integer;
ReleaseRemark: String;
function AtLeast(const AMajor, AMinor, ARelease: Integer): boolean;
function ToString: String; override;
end;
TFpcVersion = class(TToolVersion)
private
CachedFpcPath: String;
public
IsCodeTyphon: Boolean;
{ Get FPC version by running "fpc -iV". }
constructor Create;
end;
TLazarusVersion = class(TToolVersion)
private
CachedLazarusPath: String;
public
{ Get Lazarus version by running "lazbuild --version". }
constructor Create;
end;
{ FPC version singleton, automatically created and destroyed in this unit.
Using this raises exception EExecutableNotFound if FPC cannot be found,
or any other Exception in case FPC cannot be executed
or version cannot be parsed.
Value of this may change when FpcCustomPath changes. }
function FpcVersion: TFpcVersion;
{ Lazarus version singleton, automatically created and destroyed in this unit.
Using this raises exception EExecutableNotFound if lazbuild cannot be found,
or any other Exception in case lazbuild cannot be executed
or version cannot be parsed.
Value of this may change when LazarusCustomPath changes. }
function LazarusVersion: TLazarusVersion;
implementation
uses SysUtils, Classes,
CastleFilesUtils, CastleLog, CastleStringUtils,
ToolCommonUtils, ToolCompilerInfo;
{ TToolVersion --------------------------------------------------------------- }
function TToolVersion.AtLeast(const AMajor, AMinor, ARelease: Integer): boolean;
begin
Result :=
(AMajor < Major) or
( (AMajor = Major) and (AMinor < Minor) ) or
( (AMajor = Major) and (AMinor = Minor) and (ARelease <= Release) );
end;
function TToolVersion.ToString: String;
begin
Result := Format('%d.%d.%d', [Major, Minor, Release]) + ReleaseRemark;
end;
procedure TToolVersion.ParseVersion(const S: String);
const
{ Major / Minor / Release are separated by dots.
ReleaseRemark is whatever follows after Release, it may be 'rc2'.
So using NextToken to just scan for digits is most reliable and simple.
It means that resulting Token is either '', or a valid integer that should
be handled by StrToInt OK (well, if it fits in Integer range). }
NonDigits = AllChars - ['0'..'9'];
var
Token: String;
SeekPos: Integer;
begin
SeekPos := 1;
Token := NextToken(S, SeekPos, NonDigits);
if Token = '' then
raise Exception.CreateFmt('Failed to query version: no major number in "%s"', [S]);
Major := StrToInt(Token);
Token := NextToken(S, SeekPos, NonDigits);
if Token = '' then
raise Exception.CreateFmt('Failed to query version: no minor number in "%s"', [S]);
Minor := StrToInt(Token);
Token := NextToken(S, SeekPos, NonDigits);
if Token = '' then
begin
// Do not warn about this, Lazarus has now version numbers without release, like "3.2"
// WritelnWarning('Failed to query version: no release number in "%s", assuming 0', [S]);
Release := 0;
end else
Release := StrToInt(Token);
{ Use "SeekPos - 1", as after NextToken the SeekPos points to
the character right after the ending character that was in NonDigits. }
ReleaseRemark := SEnding(S, SeekPos - 1);
end;
{ TFpcVersion ---------------------------------------------------------------- }
constructor TFpcVersion.Create;
var
ToolOutput, ToolExe: string;
ToolExitStatus: Integer;
begin
inherited;
ToolExe := FindExeFpcCompiler;
MyRunCommandIndir(GetCurrentDir, ToolExe, ['-iV'], ToolOutput, ToolExitStatus,
// use rcNoConsole to not blink with a console when CGE editor starts
nil, nil, [rcNoConsole]);
if ToolExitStatus <> 0 then
raise Exception.Create('Failed to query FPC version');
IsCodeTyphon := Pos('codetyphon', LowerCase(ToolExe)) > 0;
{ parse ToolOutput into Major,Minor,Release }
ParseVersion(Trim(ToolOutput));
WritelnVerbose('FPC version: ' + ToString);
end;
var
FpcVersionCached: TFpcVersion;
function FpcVersion: TFpcVersion;
begin
{ Invalidate cache if FpcCustomPath changed.
FpcCustomPath is used by FindExeFpcCompiler which is used by TFpcVersion.Create. }
if (FpcVersionCached <> nil) and
(FpcVersionCached.CachedFpcPath <> FpcCustomPath) then
FreeAndNil(FpcVersionCached);
if FpcVersionCached = nil then
begin
FpcVersionCached := TFpcVersion.Create;
FpcVersionCached.CachedFpcPath := FpcCustomPath;
end;
Result := FpcVersionCached;
end;
{ TLazarusVersion ---------------------------------------------------------------- }
constructor TLazarusVersion.Create;
var
ToolOutput, ToolExe: string;
ToolExitStatus: Integer;
ToolOutputList: TStringList;
begin
inherited;
ToolExe := FindExeLazbuild;
MyRunCommandIndir(GetCurrentDir, ToolExe, ['--version'], ToolOutput, ToolExitStatus,
// use rcNoConsole to not blink with a console when CGE editor starts
nil, nil, [rcNoConsole]);
if ToolExitStatus <> 0 then
raise Exception.Create('Failed to query Lazarus (lazbuild) version');
{ parse ToolOutput into Major,Minor,Release }
ToolOutputList := TStringList.Create;
try
ToolOutputList.Text := ToolOutput;
if ToolOutputList.Count >= 2 then
begin
{ Typical case. Lazbuild returns
using config file ..../lazarus.cfg
2.3.0
with "using config file" possibly localized.
So we first try 2nd line as a version number. }
try
ParseVersion(ToolOutputList[1]);
except
ParseVersion(ToolOutputList[0]);
end;
end else
if ToolOutputList.Count = 1 then
begin
ParseVersion(ToolOutputList[0]);
end else
raise Exception.Create('Failed to query Lazarus (lazbuild) version, got empty output');
finally FreeAndNil(ToolOutputList) end;
WritelnVerbose('Lazarus version: ' + ToString);
end;
var
LazarusVersionCached: TLazarusVersion;
function LazarusVersion: TLazarusVersion;
begin
{ Invalidate cache if LazarusCustomPath changed.
LazarusCustomPath is used by FindExeLazbuild which is used by TLazarusVersion.Create. }
if (LazarusVersionCached <> nil) and
(LazarusVersionCached.CachedLazarusPath <> LazarusCustomPath) then
FreeAndNil(LazarusVersionCached);
if LazarusVersionCached = nil then
begin
LazarusVersionCached := TLazarusVersion.Create;
LazarusVersionCached.CachedLazarusPath := LazarusCustomPath;
end;
Result := LazarusVersionCached;
end;
finalization
FreeAndNil(FpcVersionCached);
FreeAndNil(LazarusVersionCached);
end.
|