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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
|
{
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code 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. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
* *
***************************************************************************
}
unit FppkgHelper;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, pkgFppkg, fpmkunit, fprepos,
// LazUtils
LazLoggerBase, LazFileCache, FileUtil, LazFileUtils,
// IdeConfig
IdeConfStrConsts;
type
TFppkgPackageVariantArray = array of TStringArray;
TFppkgPropConfigured = (fpcUnknown, fpcYes, fpcNo);
{ TFppkgHelper }
TFppkgHelper = class
private
FFPpkg: TpkgFPpkg;
FIsProperlyConfigured: TFppkgPropConfigured;
FConfStatusMessage: string;
FOverrideConfigurationFilename: string;
function HasFPCPackagesOnly(const PackageName: string): Boolean;
procedure InitializeFppkg;
procedure SetOverrideConfigurationFilename(AValue: string);
public
constructor Create;
destructor Destroy; override;
class function Instance: TFppkgHelper;
function HasPackage(const PackageName: string): Boolean;
procedure ListPackages(AList: TStringList);
function GetPackageUnitPath(PackageName: string): string;
function IsProperlyConfigured(out Message: string): Boolean;
function GetCompilerFilename: string;
function GetConfigurationFileName: string;
function GetCompilerConfigurationFileName: string;
// Temporary solution, because fpc 3.2.0 does not has support for package-variants
// in TFPPackage
function GetPackageVariantArray(const PackageName: string): TFppkgPackageVariantArray;
procedure ReInitialize;
property OverrideConfigurationFilename: string read FOverrideConfigurationFilename write SetOverrideConfigurationFilename;
end;
implementation
var
GFppkgHelper: TFppkgHelper = nil;
{ TFppkgHelper }
procedure TFppkgHelper.InitializeFppkg;
var
FPpkg: TpkgFPpkg;
aFilename: String;
begin
FPpkg := TpkgFPpkg.Create(nil);
try
try
if not Assigned(Defaults) then
Defaults := TBasicDefaults.Create;
FPpkg.InitializeGlobalOptions(FOverrideConfigurationFilename);
FPpkg.InitializeCompilerOptions;
aFilename:=ExpandFileName(FPpkg.CompilerOptions.Compiler);
if not FileExistsCached(aFilename) then
exit; // no fppkg -> silently ignore here
FPpkg.CompilerOptions.Compiler:=aFilename;
FPpkg.CompilerOptions.CheckCompilerValues;
FPpkg.FpmakeCompilerOptions.CheckCompilerValues;
FPpkg.LoadLocalAvailableMirrors;
FPpkg.ScanPackages;
FFPpkg := FPpkg;
FPpkg := nil;
except
on E: Exception do
debugln(['InitializeFppkg failed: '+E.Message]);
end;
finally
FPpkg.Free;
end;
end;
constructor TFppkgHelper.Create;
begin
inherited Create;
InitializeFppkg;
end;
destructor TFppkgHelper.Destroy;
begin
FreeAndNil(FFPpkg);
inherited Destroy;
end;
class function TFppkgHelper.Instance: TFppkgHelper;
begin
if not Assigned(GFppkgHelper) then
GFppkgHelper := TFppkgHelper.Create;
Result := GFppkgHelper;
end;
function TFppkgHelper.HasPackage(const PackageName: string): Boolean;
var
Msg: string;
begin
if IsProperlyConfigured(Msg) then
begin
Result :=
Assigned(FFPpkg.FindPackage(PackageName,pkgpkInstalled)) or
Assigned(FFPpkg.FindPackage(PackageName,pkgpkAvailable)) or
Assigned(FFPpkg.FindPackage(PackageName,pkgpkBoth));
if not Result then
begin
// rescan and try again
FFppkg.LoadLocalAvailableMirrors;
FFppkg.ScanPackages;
Result :=
Assigned(FFPpkg.FindPackage(PackageName,pkgpkInstalled)) or
Assigned(FFPpkg.FindPackage(PackageName,pkgpkAvailable)) or
Assigned(FFPpkg.FindPackage(PackageName,pkgpkBoth));
end;
end
else
Result := HasFPCPackagesOnly(PackageName);
end;
procedure TFppkgHelper.ListPackages(AList: TStringList);
var
I, J: Integer;
Repository: TFPRepository;
begin
AList.Clear;
if not Assigned(FFPpkg) then
Exit;
for I := 0 to FFPpkg.RepositoryList.Count -1 do
begin
Repository := FFPpkg.RepositoryList.Items[I] as TFPRepository;
for J := 0 to Repository.PackageCount -1 do
begin
AList.AddObject(Repository.Packages[J].Name, Repository.Packages[J]);
end;
end;
end;
function TFppkgHelper.GetPackageUnitPath(PackageName: string): string;
// Don't use "const" for PackageName parameter.
var
FppkgPackage: TFPPackage;
{$IF not (FPC_FULLVERSION>30300)}
PackageVariantsArray: TFppkgPackageVariantArray;
{$ENDIF}
i: Integer;
begin
if not Assigned(FFPpkg) then
begin
Result := '';
Exit;
end;
FppkgPackage := FFPpkg.FindPackage(PackageName, pkgpkInstalled);
if Assigned(FppkgPackage) then
begin
Result := FppkgPackage.PackagesStructure.GetUnitDirectory(FppkgPackage);
{$IF FPC_FULLVERSION>30300}
for i := 0 to FppkgPackage.PackageVariants.Count -1 do
begin
Result := ConcatPaths([Result, FppkgPackage.PackageVariants.Items[i].Options[0]]);
end;
{$ELSE}
PackageVariantsArray := GetPackageVariantArray(PackageName);
for i := 0 to High(PackageVariantsArray) do
begin
Result := ConcatPaths([Result, PackageVariantsArray[i][1]]);
end;
{$ENDIF FPC_FULLVERSION>30300}
end
else
begin
// The package has not been installed, so there is no unit-path yet.
// ToDo: if this leads to problems, we could 'guess' the repository it will
// be installed into, and use the corresponding packagestructure.
Result := '';
end;
end;
function TFppkgHelper.GetPackageVariantArray(const PackageName: string): TFppkgPackageVariantArray;
var
FppkgPackage: TFPPackage;
UnitConfigFile: TStringList;
PackageVariantStr, PackageVariant, UnitConfigFilename: String;
PackageVariantOptions: TStringArray;
i: Integer;
begin
Result := [];
if not Assigned(FFPpkg) then
begin
Result := [];
Exit;
end;
FppkgPackage := FFPpkg.FindPackage(PackageName, pkgpkInstalled);
if Assigned(FppkgPackage) then
begin
UnitConfigFilename := FppkgPackage.PackagesStructure.GetConfigFileForPackage(FppkgPackage);
if FileExists(UnitConfigFilename) then
begin
UnitConfigFile := TStringList.Create;
try
UnitConfigFile.LoadFromFile(UnitConfigFilename);
i := 1;
repeat
PackageVariantStr := UnitConfigFile.Values['PackageVariant_'+IntToStr(i)];
if PackageVariantStr<>'' then
begin
PackageVariant := Copy(PackageVariantStr, 1, pos(':', PackageVariantStr) -1);
if RightStr(PackageVariant, 1) = '*' then
PackageVariant := Copy(PackageVariant, 1, Length(PackageVariant) -1);
PackageVariantOptions := Copy(PackageVariantStr, pos(':', PackageVariantStr) +1).Split(',');
Insert(PackageVariant, PackageVariantOptions, -1);
Insert(PackageVariantOptions, Result, 100);
end;
inc(i);
until PackageVariantStr='';
finally
UnitConfigFile.Free;
end;
end
end
end;
function TFppkgHelper.IsProperlyConfigured(out Message: string): Boolean;
var
CompilerFilename: string;
begin
Message := '';
if Assigned(FFPpkg) and (FIsProperlyConfigured=fpcUnknown) then
begin
FIsProperlyConfigured := fpcYes;
FConfStatusMessage := '';
if not HasPackage('rtl') then
begin
FIsProperlyConfigured := fpcNo;
FConfStatusMessage := lisFppkgRtlNotFound;
end
else
begin
CompilerFilename := FFPpkg.CompilerOptions.Compiler;
if Pos(PathDelim, CompilerFilename) > 0 then
begin
if not FileExistsCached(CompilerFilename) then
begin
FIsProperlyConfigured := fpcNo;
FConfStatusMessage := Format(lisFppkgCompilerNotExists, [CompilerFilename]);
end
else if not FileIsExecutableCached(CompilerFilename) then
begin
FIsProperlyConfigured := fpcNo;
FConfStatusMessage := Format(lisFppkgCompilerNotExecutable, [CompilerFilename]);
end;
end
else
begin
CompilerFilename := ExeSearch(CompilerFilename);
if CompilerFilename = '' then
begin
FIsProperlyConfigured := fpcNo;
FConfStatusMessage := Format(lisFppkgCompilerNotFound, [FFPpkg.CompilerOptions.Compiler]);
end
else if not FileIsExecutableCached(CompilerFilename) then
begin
FIsProperlyConfigured := fpcNo;
FConfStatusMessage := Format(lisFppkgCompilerNotExecutable, [CompilerFilename]);
end;
end
end;
end;
result := FIsProperlyConfigured=fpcYes;
Message := FConfStatusMessage;
end;
function TFppkgHelper.HasFPCPackagesOnly(const PackageName: string): Boolean;
const
FpcPackages: array[0..120] of String = (
// All packages of fpc-trunk from 20181231
'rtl',
'rtl-generics',
'fcl-res',
'fpindexer',
'lua',
'regexpr',
'fcl-db',
'cdrom',
'paszlib',
'libgc',
'libtar',
'fcl-report',
'libcups',
'sqlite',
'libsee',
'newt',
'sdl',
'gnome1',
'ldap',
'openssl',
'libpng',
'graph',
'bzip2',
'fcl-extra',
'dbus',
'symbolic',
'rtl-objpas',
'mad',
'httpd24',
'fcl-process',
'fcl-sound',
'gdbint',
'rtl-unicode',
'gtk1',
'fcl-net',
'utils-lexyacc',
'mysql',
'ptc',
'libvlc',
'fcl-image',
'webidl',
'fcl-base',
'oggvorbis',
'a52',
'fcl-pdf',
'opencl',
'pthreads',
'libgd',
'tcl',
'xforms',
'iconvenc',
'dts',
'gmp',
'httpd22',
'jni',
'syslog',
'pasjpeg',
'users',
'postgres',
'rtl-extra',
'pxlib',
'fv',
'ncurses',
'zlib',
'fastcgi',
'aspell',
'rtl-console',
'googleapi',
'fpgtk',
'bfd',
'libusb',
'unzip',
'libenet',
'x11',
'libcurl',
'utils-pas2js',
'chm',
'numlib',
'fcl-registry',
'libxml2',
'fcl-web',
'imlib',
'fpmkunit',
'libmicrohttpd',
'pcap',
'utmp',
'odbc',
'fcl-xml',
'fcl-fpcunit',
'ibase',
'fcl-passrc',
'cairo',
'ide',
'fppkg',
'gtk2',
'fcl-async',
'pastojs',
'hermes',
'ggi',
'openal',
'opengl',
'zorba',
'hash',
'fcl-json',
'gdbm',
'oracle',
'fftw',
'uuid',
'libfontconfig',
'modplug',
'rsvg',
'fcl-sdo',
'fcl-js',
'proj4',
'dblib',
'svgalib',
'opengles',
'libffi',
'odata',
'fcl-stl',
'imagemagick'
);
var
i: Integer;
begin
for i := 0 to High(FpcPackages) do
if SameText(PackageName, FpcPackages[i]) then
Exit(True);
Result := False;
end;
function TFppkgHelper.GetCompilerFilename: string;
begin
Result := '';
if Assigned(FFPpkg) then
begin
Result := FFPpkg.CompilerOptions.Compiler;
end;
end;
procedure TFppkgHelper.ReInitialize;
begin
FIsProperlyConfigured := fpcUnknown;
FreeAndNil(FFPpkg);
InitializeFppkg;
end;
function TFppkgHelper.GetCompilerConfigurationFileName: string;
var
FPpkg: TpkgFPpkg;
begin
Result := '';
if Assigned(FFPpkg) then
Result:=ConcatPaths([FFPpkg.Options.GlobalSection.CompilerConfigDir, FFPpkg.Options.CommandLineSection.CompilerConfig])
else
begin
FPpkg := TpkgFPpkg.Create(nil);
try
try
FPpkg.InitializeGlobalOptions(FOverrideConfigurationFilename);
Result:=ConcatPaths([FPpkg.Options.GlobalSection.CompilerConfigDir, FPpkg.Options.CommandLineSection.CompilerConfig])
except
on E: Exception do
debugln(['Fppkg initialize global options failed: '+E.Message]);
end;
finally
FPpkg.Free;
end;
end
end;
function TFppkgHelper.GetConfigurationFileName: string;
begin
Result := '';
{$IF FPC_FULLVERSION>30200}
if Assigned(FFPpkg) then
Result:=FFPpkg.ConfigurationFilename;
{$ENDIF}
end;
procedure TFppkgHelper.SetOverrideConfigurationFilename(AValue: string);
begin
if FOverrideConfigurationFilename = AValue then Exit;
FOverrideConfigurationFilename := AValue;
ReInitialize;
end;
finalization
GFppkgHelper.Free;
GFppkgHelper:=nil;
end.
|