Debian Codegraph @

tools/common-code/toolfpcversion.pas

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