| 1 |
{ |
| 2 |
Copyright 2019-2022 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 |
{ Information about compiler and IDE. } |
| 17 |
unit ToolCompilerInfo; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses SysUtils, |
| 22 |
CastleUtils; |
| 23 |
|
| 24 |
var |
| 25 |
{ If set, use this path to search for FPC and related utils. } |
| 26 |
FpcCustomPath: String; |
| 27 |
{ If set, use this path to search for Lazarus and related utils. } |
| 28 |
LazarusCustomPath: String; |
| 29 |
|
| 30 |
{ Executable path of a tool ExeName, that is part of FPC. |
| 31 |
For example 'fpc', or 'fppkg'. |
| 32 |
Returns '' if not found, just like vanilla FindExe. } |
| 33 |
function FindExeFpc(const ExeName: String): String; |
| 34 |
|
| 35 |
{ Executable path of a tool ExeName, that is part of Lazarus. |
| 36 |
For example 'lazarus', or 'lazbuild'. |
| 37 |
Returns '' if not found, just like vanilla FindExe. } |
| 38 |
function FindExeLazarus(const ExeName: String): String; |
| 39 |
|
| 40 |
{ Extend $PATH environment variable as necessary to add FpcCustomPath |
| 41 |
and LazarusCustomPath. } |
| 42 |
function PathExtendForFpcLazarus(const PathList: String): String; |
| 43 |
|
| 44 |
{ Write environment information to the standard output. |
| 45 |
See "castle-engine --help" for valid arguments after "castle-engine output-environment", |
| 46 |
these are values allowed for OutputKey. } |
| 47 |
procedure DoOutputEnvironment(const OutputKey: String); |
| 48 |
|
| 49 |
type |
| 50 |
EExecutableNotFound = class(EShortErrorMessage); |
| 51 |
|
| 52 |
{ Find the executable of FPC compiler. |
| 53 |
|
| 54 |
It uses FindExeFpc, searching for 'fpc.sh' (script set by fpcupdeluxe |
| 55 |
that should be used to run FPC) |
| 56 |
or just 'fpc' (normal way to run FPC). |
| 57 |
It can also find and return a "bundled" FPC version with CGE. |
| 58 |
|
| 59 |
The FpcStandardUnitsPath, if returned non-empty, indicates that you should |
| 60 |
pass "-Fu<FpcStandardUnitsPath>" to FPC call. |
| 61 |
This is right now used to make "bundled" FPC version work. |
| 62 |
|
| 63 |
@raises EExecutableNotFound When the exe is not found and ExceptionWhenMissing. } |
| 64 |
function FindExeFpcCompiler(const ExceptionWhenMissing: Boolean; out FpcStandardUnitsPath: String): String; |
| 65 |
function FindExeFpcCompiler(const ExceptionWhenMissing: Boolean = true): String; |
| 66 |
|
| 67 |
{ Find the main executable of Lazarus IDE. |
| 68 |
This uses FindExeLazarus for common executable names like 'lazarus' or 'lazarus-ide'. |
| 69 |
|
| 70 |
@raises EExecutableNotFound When the exe is not found and ExceptionWhenMissing. } |
| 71 |
function FindExeLazarusIDE(const ExceptionWhenMissing: Boolean = true): String; |
| 72 |
|
| 73 |
{ Find lazbuild. |
| 74 |
@raises EExecutableNotFound When the exe is not found and ExceptionWhenMissing. } |
| 75 |
function FindExeLazbuild(const ExceptionWhenMissing: Boolean = true): String; |
| 76 |
|
| 77 |
{ Find the path of Delphi installation. |
| 78 |
Ends with PathDelim. |
| 79 |
|
| 80 |
Overloaded version with AppExe argument returns the IDE exe (that should be used |
| 81 |
to open pas files), which is practically always inside "bin/bds.exe" under Delphi path. |
| 82 |
|
| 83 |
When missing: ExceptionWhenMissing -> raise EExecutableNotFound, |
| 84 |
otherwise return ''. } |
| 85 |
function FindDelphiPath(const ExceptionWhenMissing: Boolean): String; |
| 86 |
function FindDelphiPath(const ExceptionWhenMissing: Boolean; out AppExe: String): String; |
| 87 |
|
| 88 |
implementation |
| 89 |
|
| 90 |
uses CastleFilesUtils, CastleStringUtils, CastleLog, |
| 91 |
{$ifdef MSWINDOWS} Registry, {$endif} |
| 92 |
ToolArchitectures, ToolCommonUtils; |
| 93 |
|
| 94 |
function FindExeFpc(const ExeName: String): String; |
| 95 |
begin |
| 96 |
if FpcCustomPath <> '' then |
| 97 |
begin |
| 98 |
Result := InclPathDelim(FpcCustomPath) + ExeName + ExeExtension; |
| 99 |
if RegularFileExists(Result) then |
| 100 |
Exit; |
| 101 |
Result := InclPathDelim(FpcCustomPath) + |
| 102 |
'bin' + PathDelim + |
| 103 |
CPUToString(DefaultCPU) + '-' + OSToString(DefaultOS) + PathDelim + |
| 104 |
ExeName + ExeExtension; |
| 105 |
if RegularFileExists(Result) then |
| 106 |
Exit; |
| 107 |
end; |
| 108 |
Result := FindExe(ExeName); |
| 109 |
end; |
| 110 |
|
| 111 |
function FindExeLazarus(const ExeName: String): String; |
| 112 |
begin |
| 113 |
if LazarusCustomPath <> '' then |
| 114 |
begin |
| 115 |
Result := InclPathDelim(LazarusCustomPath) + ExeName + ExeExtension; |
| 116 |
if RegularFileExists(Result) then |
| 117 |
Exit; |
| 118 |
end; |
| 119 |
Result := FindExe(ExeName); |
| 120 |
end; |
| 121 |
|
| 122 |
function PathExtendForFpcLazarus(const PathList: String): String; |
| 123 |
|
| 124 |
function PathAppend(const P, NewPart: String): String; |
| 125 |
begin |
| 126 |
if P = '' then |
| 127 |
Result := NewPart |
| 128 |
else |
| 129 |
{ Add NewPart at the beginning, this way FPC explicitly specified |
| 130 |
in CGE editor "Preferences" should override FPC on PATH. } |
| 131 |
Result := NewPart + PathSeparator + P; |
| 132 |
end; |
| 133 |
|
| 134 |
var |
| 135 |
FpcBin: String; |
| 136 |
begin |
| 137 |
Result := PathList; |
| 138 |
|
| 139 |
if FpcCustomPath <> '' then |
| 140 |
begin |
| 141 |
FpcBin := InclPathDelim(FpcCustomPath) + |
| 142 |
'bin' + PathDelim + |
| 143 |
CPUToString(DefaultCPU) + '-' + OSToString(DefaultOS); |
| 144 |
if DirectoryExists(FpcBin) then |
| 145 |
Result := PathAppend(Result, FpcBin) |
| 146 |
else |
| 147 |
Result := PathAppend(Result, FpcCustomPath); |
| 148 |
end; |
| 149 |
|
| 150 |
if LazarusCustomPath <> '' then |
| 151 |
Result := PathAppend(Result, LazarusCustomPath); |
| 152 |
end; |
| 153 |
|
| 154 |
procedure DoOutputEnvironment(const OutputKey: String); |
| 155 |
var |
| 156 |
StandardUnitsPath: String; |
| 157 |
begin |
| 158 |
case OutputKey of |
| 159 |
'fpc-exe': |
| 160 |
Writeln(FindExeFpcCompiler(false)); |
| 161 |
'fpc-standard-units-path': |
| 162 |
begin |
| 163 |
FindExeFpcCompiler(false, StandardUnitsPath); |
| 164 |
Writeln(StandardUnitsPath); |
| 165 |
end |
| 166 |
else raise Exception.CreateFmt('Unsupported output key: "%s"', [OutputKey]); |
| 167 |
end; |
| 168 |
end; |
| 169 |
|
| 170 |
function FindExeFpcCompiler(const ExceptionWhenMissing: Boolean; out FpcStandardUnitsPath: String): String; |
| 171 |
var |
| 172 |
BundledFpcPath, BundledFpcExe: String; |
| 173 |
begin |
| 174 |
FpcStandardUnitsPath := ''; |
| 175 |
|
| 176 |
Result := FindExeFpc('fpc.sh'); |
| 177 |
if Result = '' then |
| 178 |
Result := FindExeFpc('fpc'); |
| 179 |
if (Result = '') and (CastleEnginePath <> '') then |
| 180 |
begin |
| 181 |
BundledFpcPath := CastleEnginePath + 'tools' + PathDelim + |
| 182 |
'contrib' + PathDelim + |
| 183 |
'fpc' + PathDelim; |
| 184 |
BundledFpcExe := BundledFpcPath + 'bin' + PathDelim + 'fpc' + ExeExtension; |
| 185 |
if FileExists(BundledFpcExe) then |
| 186 |
begin |
| 187 |
FpcStandardUnitsPath := BundledFpcPath + 'units' + PathDelim + '$fpctarget' + PathDelim + '*'; |
| 188 |
Result := BundledFpcExe; |
| 189 |
end; |
| 190 |
end; |
| 191 |
|
| 192 |
if (Result = '') and ExceptionWhenMissing then |
| 193 |
raise EExecutableNotFound.Create('Cannot find "fpc" program. Make sure it is installed, and available on environment variable $PATH. If you use the CGE editor, you can also set FPC location in "Preferences".'); |
| 194 |
end; |
| 195 |
|
| 196 |
function FindExeFpcCompiler(const ExceptionWhenMissing: Boolean): String; |
| 197 |
var |
| 198 |
FpcStandardUnitsPath: String; |
| 199 |
begin |
| 200 |
Result := FindExeFpcCompiler(ExceptionWhenMissing, FpcStandardUnitsPath); |
| 201 |
end; |
| 202 |
|
| 203 |
function FindExeLazarusIDE(const ExceptionWhenMissing: Boolean): String; |
| 204 |
begin |
| 205 |
Result := FindExeLazarus('lazarus'); |
| 206 |
if Result = '' then |
| 207 |
begin |
| 208 |
{ Alternative possible Lazarus executable name on non-Windows, |
| 209 |
if installed from deb files or by "make install". } |
| 210 |
Result := FindExeLazarus('lazarus-ide'); |
| 211 |
end; |
| 212 |
|
| 213 |
if (Result = '') and ExceptionWhenMissing then |
| 214 |
// Note: FormProject using this message also for ErrorBox, so make sure it looks sensible. |
| 215 |
raise EExecutableNotFound.Create('Cannot find "lazarus" or "lazarus-ide" program. Make sure it is installed, and available on environment variable $PATH. If you use the CGE editor, you can also set Lazarus location in "Preferences".'); |
| 216 |
end; |
| 217 |
|
| 218 |
function FindExeLazbuild(const ExceptionWhenMissing: Boolean): String; |
| 219 |
begin |
| 220 |
Result := FindExeLazarus('lazbuild'); |
| 221 |
if (Result = '') and ExceptionWhenMissing then |
| 222 |
// Note: FormProject using this message also for ErrorBox, so make sure it looks sensible. |
| 223 |
raise EExecutableNotFound.Create('Cannot find "lazbuild" program. Make sure it is installed, and available on environment variable $PATH. If you use the CGE editor, you can also set Lazarus location in "Preferences".'); |
| 224 |
end; |
| 225 |
|
| 226 |
function FindDelphiPath(const ExceptionWhenMissing: Boolean; out AppExe: String): String; |
| 227 |
{$ifdef MSWINDOWS} |
| 228 |
|
| 229 |
{ Our algorithm to find Delphi location in the registry (using some guesswork and regedit searching, confirmed by |
| 230 |
https://docwiki.embarcadero.com/RADStudio/Sydney/en/System_Registry_Keys_for_IDE_Visual_Settings |
| 231 |
https://stackoverflow.com/questions/6870282/how-are-delphi-environment-variables-such-as-bds-evaluated ) : |
| 232 |
|
| 233 |
- Read subdirs from current user: HKEY_CURRENT_USER\SOFTWARE\Embarcadero\BDS\ |
| 234 |
- Read subdirs from machine: HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Embarcadero\BDS\ |
| 235 |
- Each subdirectory is the version number. Pick the latest. |
| 236 |
Pick user-specific latest, if both HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE will have the same versions. |
| 237 |
To compare them, treat as floats (as they look like "22.0"). |
| 238 |
- Inside the chosen subdir, use: |
| 239 |
- key "App" which is like "c:\program files (x86)\embarcadero\studio\22.0\bin\bds.exe". |
| 240 |
- key "RootDir", like "c:\program files (x86)\embarcadero\studio\22.0\" |
| 241 |
(without "bin" or exe inside). |
| 242 |
|
| 243 |
Things inside Delphi path: |
| 244 |
|
| 245 |
bin/bds.exe - Delphi IDE |
| 246 |
bin/dcc[xxx].exe - Delphi compiler, name determines target OS/arch. |
| 247 |
See CompileDelphi for a list. |
| 248 |
|
| 249 |
See also |
| 250 |
https://docwiki.embarcadero.com/RADStudio/Sydney/en/Delphi_Compiler |
| 251 |
https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Delphi_Toolchains |
| 252 |
https://docwiki.embarcadero.com/RADStudio/Alexandria/en/IDE_Command_Line_Switches_and_Options |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
var |
| 257 |
R: TRegistry; |
| 258 |
LargestKeyAsFloat: Double; |
| 259 |
LargestKeyDelphiRootDir: String; |
| 260 |
LargestKeyDelphiApp: String; |
| 261 |
|
| 262 |
procedure ScanRegistry(const RootKey: HKEY; const BasePath: UnicodeString); |
| 263 |
var |
| 264 |
KeyName: UnicodeString; |
| 265 |
KeyNames: TUnicodeStringArray; |
| 266 |
DelphiRootDir, DelphiApp: String; |
| 267 |
KeyFloat: Double; |
| 268 |
begin |
| 269 |
R.RootKey := RootKey; |
| 270 |
|
| 271 |
R.OpenKeyReadOnly(BasePath); |
| 272 |
KeyNames := R.GetKeyNames; |
| 273 |
R.CloseKey; |
| 274 |
|
| 275 |
for KeyName in KeyNames do |
| 276 |
if TryStrToFloatDot(UTF8Encode(KeyName), KeyFloat) and |
| 277 |
(KeyFloat >= LargestKeyAsFloat) then |
| 278 |
begin |
| 279 |
R.OpenKeyReadOnly(BasePath + KeyName); |
| 280 |
DelphiRootDir := R.ReadString('RootDir'); |
| 281 |
R.CloseKey; |
| 282 |
|
| 283 |
R.OpenKeyReadOnly(BasePath + KeyName); |
| 284 |
DelphiApp := R.ReadString('App'); |
| 285 |
R.CloseKey; |
| 286 |
|
| 287 |
if (DelphiRootDir <> '') and (DelphiApp <> '') then |
| 288 |
begin |
| 289 |
LargestKeyAsFloat := KeyFloat; |
| 290 |
LargestKeyDelphiRootDir := DelphiRootDir; |
| 291 |
LargestKeyDelphiApp := DelphiApp; |
| 292 |
//WritelnLog('Delphi %f found in %s', [LargestKeyAsFloat, LargestKeyDelphiRootDir]); |
| 293 |
end; |
| 294 |
end; |
| 295 |
end; |
| 296 |
|
| 297 |
begin |
| 298 |
R := TRegistry.Create(KEY_READ); |
| 299 |
try |
| 300 |
LargestKeyAsFloat := 0; |
| 301 |
ScanRegistry(HKEY_LOCAL_MACHINE, 'SOFTWARE\WOW6432Node\Embarcadero\BDS\'); |
| 302 |
ScanRegistry(HKEY_CURRENT_USER, 'SOFTWARE\Embarcadero\BDS\'); |
| 303 |
|
| 304 |
Result := LargestKeyDelphiRootDir; |
| 305 |
if Result <> '' then |
| 306 |
begin |
| 307 |
Result := InclPathDelim(Result); |
| 308 |
AppExe := LargestKeyDelphiApp; |
| 309 |
end; |
| 310 |
finally FreeAndNil(R)end; |
| 311 |
{$else} |
| 312 |
begin |
| 313 |
// just in case Delphi IDE will be available on non-Windows some day |
| 314 |
Result := FindExe('dcc32'); |
| 315 |
if Result <> '' then |
| 316 |
Result := ExtractFilePath(Result); |
| 317 |
{$endif} |
| 318 |
|
| 319 |
if (Result = '') and ExceptionWhenMissing then |
| 320 |
raise EExecutableNotFound.Create('Cannot find Delphi installation in the registry. Make sure Delphi is installed correctly.'); |
| 321 |
end; |
| 322 |
|
| 323 |
function FindDelphiPath(const ExceptionWhenMissing: Boolean): String; |
| 324 |
var |
| 325 |
IgnoreAppExe: String; |
| 326 |
begin |
| 327 |
Result := FindDelphiPath(ExceptionWhenMissing, IgnoreAppExe); |
| 328 |
end; |
| 329 |
|
| 330 |
end. |