pkgfpmake.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. unit pkgfpmake;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes,SysUtils,DateUtils,
  6. pkghandler, fpmkunit;
  7. implementation
  8. uses
  9. fprepos,
  10. pkgoptions,
  11. pkgglobals,
  12. pkgmessages,
  13. pkgrepos,
  14. pkgFppkg,
  15. fpxmlrep;
  16. type
  17. { TFPMakeCompiler }
  18. TFPMakeCompiler = Class(TPackagehandler)
  19. protected
  20. function DeterminePackage: TFPPackage;
  21. Public
  22. function Execute: Boolean; override;
  23. end;
  24. { TFPMakeRunner }
  25. TFPMakeRunner = Class(TFPMakeCompiler)
  26. Protected
  27. function IncludeInstallationOptions: Boolean; virtual;
  28. Function RunFPMake(const Command:string):Integer;
  29. end;
  30. { TFPMakeRunnerCompile }
  31. TFPMakeRunnerCompile = Class(TFPMakeRunner)
  32. Public
  33. function Execute: Boolean;override;
  34. end;
  35. { TFPMakeRunnerBuild }
  36. TFPMakeRunnerBuild = Class(TFPMakeRunner)
  37. Public
  38. function Execute: Boolean;override;
  39. end;
  40. { TFPMakeRunnerInstall }
  41. TFPMakeRunnerInstall = Class(TFPMakeRunner)
  42. protected
  43. function IncludeInstallationOptions: Boolean; override;
  44. Public
  45. function Execute: Boolean;override;
  46. end;
  47. { TFPMakeRunnerUnInstall }
  48. TFPMakeRunnerUnInstall = Class(TFPMakeRunner)
  49. protected
  50. function IncludeInstallationOptions: Boolean; override;
  51. Public
  52. function Execute: Boolean;override;
  53. end;
  54. { TFPMakeRunnerClean }
  55. TFPMakeRunnerClean = Class(TFPMakeRunner)
  56. Public
  57. function Execute: Boolean;override;
  58. end;
  59. { TFPMakeRunnerManifest }
  60. TFPMakeRunnerManifest = Class(TFPMakeRunner)
  61. Public
  62. function Execute: Boolean;override;
  63. end;
  64. { TFPMakeRunnerArchive }
  65. TFPMakeRunnerArchive = Class(TFPMakeRunner)
  66. Public
  67. function Execute: Boolean;override;
  68. end;
  69. TMyMemoryStream=class(TMemoryStream)
  70. public
  71. constructor Create(p:pointer;mysize:integer);
  72. end;
  73. {
  74. Generated from fpmkunit.pp, using data2inc:
  75. data2inc -b -s fpmkunit.pp fpmkunitsrc.inc fpmkunitsrc
  76. }
  77. {$i fpmkunitsrc.inc}
  78. procedure CreateFPMKUnitSource(const AFileName:string);
  79. var
  80. InStream,
  81. OutStream : TStream;
  82. pend : pchar;
  83. begin
  84. try
  85. // Don't write trailing #0
  86. pend:=pchar(@fpmkunitsrc)+sizeof(fpmkunitsrc)-1;
  87. while pend^=#0 do
  88. dec(pend);
  89. InStream:=TMyMemoryStream.Create(@fpmkunitsrc,pend-pchar(@fpmkunitsrc));
  90. OutStream:=TFileStream.Create(AFileName,fmCreate);
  91. OutStream.CopyFrom(InStream,InStream.Size);
  92. finally
  93. InStream.Destroy;
  94. OutStream.Destroy;
  95. end;
  96. end;
  97. {*****************************************************************************
  98. TMyMemoryStream
  99. *****************************************************************************}
  100. constructor TMyMemoryStream.Create(p:pointer;mysize:integer);
  101. begin
  102. inherited Create;
  103. SetPointer(p,mysize);
  104. end;
  105. { TFPMakeCompiler }
  106. function TFPMakeCompiler.DeterminePackage: TFPPackage;
  107. begin
  108. Result := PackageManager.DetermineSourcePackage(PackageName);
  109. if not Assigned(Result) then
  110. Raise EPackage.CreateFmt(SErrMissingPackage,[PackageName]);
  111. end;
  112. function TFPMakeCompiler.Execute: Boolean;
  113. var
  114. OOptions : string;
  115. function CheckUnitDir(const APackageName:string;Out AUnitDir:string):boolean;
  116. var
  117. P: TFPPackage;
  118. begin
  119. Result:=false;
  120. P := PackageManager.FPMakeRepoFindPackage(APackageName, pkgpkInstalled);
  121. if Assigned(P) then
  122. begin
  123. AUnitDir := P.PackagesStructure.GetUnitDirectory(P);
  124. Result := DirectoryExistsLog(AUnitDir);
  125. end;
  126. end;
  127. procedure AddOption(const s:string);
  128. begin
  129. if OOptions<>'' then
  130. OOptions:=OOptions+' ';
  131. OOptions:=OOptions+maybequoted(s);
  132. end;
  133. procedure AddDependencySearchPaths(ADepDirList: TStrings; APackageName: string);
  134. var
  135. i: Integer;
  136. D: TFPDependency;
  137. UnitDir: string;
  138. Package: TFPPackage;
  139. begin
  140. Package := PackageManager.FindPackage(APackageName, pkgpkInstalled);
  141. if not assigned(Package) then
  142. begin
  143. Error(SErrMissingInstallPackage, [PackageName]);
  144. end;
  145. for i := 0 to Package.Dependencies.Count -1 do
  146. begin
  147. D := Package.Dependencies[i];
  148. if (PackageManager.CompilerOptions.CompilerOS in D.OSes) and
  149. (PackageManager.CompilerOptions.CompilerCPU in D.CPUs) then
  150. begin
  151. if CheckUnitDir(D.PackageName, UnitDir) then
  152. begin
  153. AddDependencySearchPaths(ADepDirList, D.PackageName);
  154. ADepDirList.Add(UnitDir);
  155. end
  156. else
  157. Error(SErrMissingInstallPackage, [D.PackageName]);
  158. end;
  159. end;
  160. end;
  161. Var
  162. i : Integer;
  163. TempBuildDir,
  164. DepDir,
  165. FPMakeBin,
  166. FPMakeSrc : string;
  167. NeedFPMKUnitSource,
  168. HaveFpmake : boolean;
  169. FPMKUnitDepAvailable: Boolean;
  170. FPMKUnitDepPackage: TFPPackage;
  171. P : TFPPackage;
  172. DepPackage: TFPPackage;
  173. DepDirList: TStringList;
  174. S,Reason: string;
  175. begin
  176. Result := True;
  177. P:=DeterminePackage;
  178. NeedFPMKUnitSource:=false;
  179. OOptions:='';
  180. SetCurrentDir(PackageManager.PackageBuildPath(P));
  181. // Generate random name for build path
  182. TempBuildDir:='build_fpmake_'+HexStr(DateTimeToUnix(Now),8)+HexStr(GetProcessId,4);
  183. // Check for fpmake source
  184. FPMakeBin:='fpmake'+ExeExt;
  185. FPMakeSrc:='fpmake.pp';
  186. HaveFpmake:=FileExists(FPMakeSrc);
  187. If Not HaveFPMake then
  188. begin
  189. HaveFPMake:=FileExists('fpmake.pas');
  190. If HaveFPMake then
  191. FPMakeSrc:='fpmake.pas';
  192. end;
  193. // Need to compile fpmake executable?
  194. if not FileExists(FPMakeBin) or
  195. (FileAge(FPMakeBin)<FileAge(FPMakeSrc)) then
  196. begin
  197. if Not HaveFPMake then
  198. Error(SErrMissingFPMake);
  199. AddOption('-n');
  200. AddOption('-dCOMPILED_BY_FPPKG');
  201. DepDirList := TStringList.Create;
  202. try
  203. DepDirList.Sorted := True;
  204. DepDirList.Duplicates := dupIgnore;
  205. for i:=0 to high(FPMKUnitDeps) do
  206. begin
  207. FPMKUnitDepAvailable := FPMKUnitDeps[i].available;
  208. if FPMKUnitDepAvailable then
  209. begin
  210. // Do not try to use packages which are broken. This can happen when
  211. // fixbroken is being called and one of the fpmkunit-dependencies itself
  212. // is broken. In that case, build fpmake without the dependency.
  213. // (Plugins are also fpmkunit-dependencies)
  214. FPMKUnitDepPackage := PackageManager.FindPackage(FPMKUnitDeps[i].package, pkgpkInstalled);
  215. if Assigned(FPMKUnitDepPackage) then
  216. begin
  217. if PackageManager.PackageIsBroken(FPMKUnitDepPackage, Reason, nil) then
  218. FPMKUnitDepAvailable := False;
  219. end;
  220. end;
  221. if FPMKUnitDepAvailable then
  222. begin
  223. if CheckUnitDir(FPMKUnitDeps[i].package,DepDir) then
  224. begin
  225. AddDependencySearchPaths(DepDirList, FPMKUnitDeps[i].package);
  226. DepDirList.Add(DepDir);
  227. end
  228. else
  229. Error(SErrMissingInstallPackage,[FPMKUnitDeps[i].package]);
  230. if FPMKUnitDeps[i].def<>'' then
  231. AddOption('-d'+FPMKUnitDeps[i].def);
  232. if FPMKUnitDeps[i].PluginUnit<>'' then
  233. AddOption('-Fa'+FPMKUnitDeps[i].PluginUnit);
  234. end
  235. else
  236. begin
  237. // If fpmkunit is not installed, we use the internal fpmkunit source
  238. if FPMKUnitDeps[i].package='fpmkunit' then
  239. begin
  240. NeedFPMKUnitSource:=true;
  241. DepDirList.Add(TempBuildDir);
  242. end;
  243. if FPMKUnitDeps[i].undef<>'' then
  244. AddOption('-d'+FPMKUnitDeps[i].undef);
  245. end;
  246. end;
  247. // Add RTL unit dir
  248. if not CheckUnitDir('rtl',DepDir) then
  249. Error(SErrMissingInstallPackage,['rtl']);
  250. DepDirList.Add(DepDir);
  251. DepDirList.Add(TempBuildDir);
  252. for i := 0 to DepDirList.Count -1 do
  253. AddOption('-Fu'+DepDirList[i]);
  254. finally
  255. DepDirList.Free;
  256. end;
  257. // Units in a directory for easy cleaning
  258. DeleteDir(TempBuildDir);
  259. ForceDirectories(TempBuildDir);
  260. try
  261. // Compile options
  262. // -- default is to optimize, smartlink and strip to reduce
  263. // the executable size (there can be 100's of fpmake's on a system)
  264. if llInfo in LogLevels then
  265. AddOption('-vi');
  266. AddOption('-O2');
  267. AddOption('-XXs');
  268. if PackageManager.FPMakeCompilerOptions.HasOptions then
  269. for S in PackageManager.FPMakeCompilerOptions.Options do
  270. AddOption(S);
  271. // AddOption(PackageManager.FPMakeCompilerOptions.Options.DelimitedText) ;
  272. // Create fpmkunit.pp if needed
  273. if NeedFPMKUnitSource then
  274. begin
  275. Log(llWarning,SLogUseInternalFpmkunit);
  276. CreateFPMKUnitSource(TempBuildDir+PathDelim+'fpmkunit.pp');
  277. end;
  278. // Call compiler
  279. If ExecuteProcess(PackageManager.FPMakeCompilerOptions.Compiler,OOptions+' '+FPmakeSrc)<>0 then
  280. begin
  281. if not PackageManager.Options.CommandLineSection.RecoveryMode then
  282. Error(SErrCompileFailureFPMakeTryRecovery)
  283. else
  284. Error(SErrCompileFailureFPMake);
  285. end;
  286. finally
  287. // Cleanup units
  288. DeleteDir(TempBuildDir);
  289. end;
  290. end
  291. else
  292. Log(llCommands,SLogNotCompilingFPMake);
  293. end;
  294. { TFPMakeRunner }
  295. function TFPMakeRunner.IncludeInstallationOptions: Boolean;
  296. begin
  297. Result := False;
  298. end;
  299. Function TFPMakeRunner.RunFPMake(const Command:string) : Integer;
  300. Var
  301. P : TFPPackage;
  302. FPMakeBin,
  303. OOptions : string;
  304. InstallRepo: TFPRepository;
  305. GlobalUnitDir,
  306. LocalUnitDir: string;
  307. BaseInstDir: string;
  308. i: Integer;
  309. procedure AddOption(const s:string);
  310. begin
  311. if OOptions<>'' then
  312. OOptions:=OOptions+' ';
  313. OOptions:=OOptions+maybequoted(s);
  314. Writeln('Options: >>>>',OOptions,'<<<<');
  315. end;
  316. procedure CondAddOption(const Name,Value:string);
  317. begin
  318. if Value<>'' then
  319. AddOption(Name+'='+Value);
  320. end;
  321. procedure ObtainSupportedTargetsFromManifest(p:TFPPackage);
  322. var
  323. X : TFPXMLRepositoryHandler;
  324. ManifestPackages : TFPPackages;
  325. i: integer;
  326. begin
  327. p.OSes:=[];
  328. p.CPUs:=[];
  329. ManifestPackages:=TFPPackages.Create(TFPPackage);
  330. X:=TFPXMLRepositoryHandler.Create;
  331. try
  332. X.LoadFromXml(ManifestPackages,ManifestFileName);
  333. for i := 0 to ManifestPackages.Count-1 do
  334. begin
  335. p.OSes:=p.OSes+ManifestPackages[i].OSes;
  336. p.CPUs:=p.CPUs+ManifestPackages[i].CPUs;
  337. end;
  338. finally
  339. X.Free;
  340. ManifestPackages.Free;
  341. end;
  342. end;
  343. begin
  344. Result := -1;
  345. OOptions:='';
  346. // Does the current package support this CPU-OS?
  347. if PackageName<>'' then
  348. begin
  349. P := DeterminePackage;
  350. if (PackageName=CurrentDirPackageName) and (FileExists(ManifestFileName)) then
  351. ObtainSupportedTargetsFromManifest(p);
  352. end
  353. else
  354. P:=nil;
  355. if assigned(P) then
  356. begin
  357. if (command<>'archive') and (command<>'manifest') and
  358. (not(PackageManager.CompilerOptions.CompilerOS in P.OSes) or
  359. not(PackageManager.CompilerOptions.CompilerCPU in P.CPUs)) then
  360. begin
  361. Error(SErrPackageDoesNotSupportTarget,[P.Name,MakeTargetString(PackageManager.CompilerOptions.CompilerCPU,PackageManager.CompilerOptions.CompilerOS)]);
  362. Exit;
  363. end;
  364. end;
  365. { Maybe compile fpmake executable? }
  366. if not ExecuteAction(PackageName,'compilefpmake') then
  367. Exit;
  368. { Create options }
  369. if llDebug in LogLevels then
  370. AddOption('--debug')
  371. else if llInfo in LogLevels then
  372. AddOption('--verbose');
  373. if (P.FPMakeOptionsString<>'') then
  374. begin
  375. // When the package is being reinstalled because of broken dependencies, use the same fpmake-options
  376. // as were used to compile the package in the first place.
  377. OOptions:=P.FPMakeOptionsString;
  378. end
  379. else
  380. begin
  381. if PackageManager.CompilerOptions.HasOptions then
  382. AddOption('--options='+PackageManager.CompilerOptions.Options.DelimitedText);
  383. if PackageManager.Options.GlobalSection.CustomFPMakeOptions<>'' then
  384. begin
  385. AddOption('--ignoreinvalidoption');
  386. AddOption(PackageManager.Options.GlobalSection.CustomFPMakeOptions);
  387. end;
  388. end;
  389. AddOption('--nofpccfg');
  390. AddOption('--compiler='+PackageManager.CompilerOptions.Compiler);
  391. AddOption('--cpu='+CPUToString(PackageManager.CompilerOptions.CompilerCPU));
  392. AddOption('--os='+OSToString(PackageManager.CompilerOptions.CompilerOS));
  393. // While scanning a source-repository it could be necessary to create manifest
  394. // files. At this moment the InstallRepo could not be initialized yet. And the
  395. // manifest command does not use the --prefix and --baseinstalldir parameters.
  396. if IncludeInstallationOptions then
  397. begin
  398. InstallRepo := PackageManager.GetInstallRepository(P);
  399. if not Assigned(InstallRepo.DefaultPackagesStructure) then
  400. begin
  401. Error(SErrIllConfRepository,[InstallRepo.RepositoryName]);
  402. Exit;
  403. end;
  404. CondAddOption('--baseinstalldir',InstallRepo.DefaultPackagesStructure.GetBaseInstallDir);
  405. CondAddOption('--prefix',InstallRepo.DefaultPackagesStructure.GetPrefix);
  406. end;
  407. for i := PackageManager.RepositoryList.Count-1 downto 0 do
  408. begin
  409. if PackageManager.RepositoryList[i] is TFPRepository then
  410. begin
  411. InstallRepo := TFPRepository(PackageManager.RepositoryList[i]);
  412. if (InstallRepo.RepositoryType = fprtInstalled) and Assigned(InstallRepo.DefaultPackagesStructure) then
  413. begin
  414. BaseInstDir := InstallRepo.DefaultPackagesStructure.GetBaseInstallDir;
  415. CondAddOption('--searchpath', BaseInstDir);
  416. if LocalUnitDir='' then
  417. LocalUnitDir := BaseInstDir
  418. else if GlobalUnitDir='' then
  419. GlobalUnitDir := BaseInstDir;
  420. end;
  421. end;
  422. end;
  423. if GlobalUnitDir<>'' then
  424. CondAddOption('--globalunitdir', GlobalUnitDir);
  425. if LocalUnitDir<>'' then
  426. CondAddOption('--localunitdir', LocalUnitDir);
  427. { Run FPMake }
  428. FPMakeBin:='fpmake'+ExeExt;
  429. SetCurrentDir(PackageManager.PackageBuildPath(P));
  430. Result:=ExecuteProcess(FPMakeBin,Command+' '+OOptions);
  431. if Result<>0 then
  432. Error(SErrExecutionFPMake,[Command]);
  433. end;
  434. function TFPMakeRunnerCompile.Execute: Boolean;
  435. begin
  436. Result := RunFPMake('compile') = 0;
  437. end;
  438. function TFPMakeRunnerBuild.Execute: Boolean;
  439. begin
  440. Result := RunFPMake('build') = 0;
  441. end;
  442. function TFPMakeRunnerInstall.IncludeInstallationOptions: Boolean;
  443. begin
  444. Result := True;
  445. end;
  446. function TFPMakeRunnerInstall.Execute: Boolean;
  447. begin
  448. Result := RunFPMake('install') = 0;
  449. end;
  450. function TFPMakeRunnerUnInstall.IncludeInstallationOptions: Boolean;
  451. begin
  452. Result := True;
  453. end;
  454. function TFPMakeRunnerUnInstall.Execute: Boolean;
  455. begin
  456. Result := RunFPMake('uninstall') = 0;
  457. end;
  458. function TFPMakeRunnerClean.Execute: Boolean;
  459. begin
  460. Result := RunFPMake('clean') = 0;
  461. end;
  462. function TFPMakeRunnerManifest.Execute: Boolean;
  463. begin
  464. Result := RunFPMake('manifest') = 0;
  465. end;
  466. function TFPMakeRunnerArchive.Execute: Boolean;
  467. var
  468. StoredLocalPrefix: string;
  469. StoredGlobalPrefix: string;
  470. begin
  471. // In most (all?) cases we do not want a prefix in the archive.
  472. StoredGlobalPrefix := PackageManager.CompilerOptions.GlobalPrefix;
  473. StoredLocalPrefix := PackageManager.CompilerOptions.LocalPrefix;
  474. PackageManager.CompilerOptions.GlobalPrefix := '';
  475. PackageManager.CompilerOptions.LocalPrefix := '';
  476. try
  477. Result := RunFPMake('archive') = 0;
  478. finally
  479. PackageManager.CompilerOptions.GlobalPrefix := StoredGlobalPrefix;
  480. PackageManager.CompilerOptions.LocalPrefix := StoredLocalPrefix;
  481. end;
  482. end;
  483. initialization
  484. RegisterPkgHandler('compilefpmake',TFPMakeCompiler);
  485. RegisterPkgHandler('fpmakecompile',TFPMakeRunnerCompile);
  486. RegisterPkgHandler('fpmakebuild',TFPMakeRunnerBuild);
  487. RegisterPkgHandler('fpmakeinstall',TFPMakeRunnerInstall);
  488. RegisterPkgHandler('fpmakeuninstall',TFPMakeRunnerUnInstall);
  489. RegisterPkgHandler('fpmakeclean',TFPMakeRunnerClean);
  490. RegisterPkgHandler('fpmakemanifest',TFPMakeRunnerManifest);
  491. RegisterPkgHandler('fpmakearchive',TFPMakeRunnerArchive);
  492. end.