pkgfpmake.pp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. unit pkgfpmake;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,pkghandler;
  6. implementation
  7. uses
  8. fprepos,
  9. pkgoptions,
  10. pkgglobals,
  11. pkgmessages;
  12. type
  13. { TFPMakeCompiler }
  14. TFPMakeCompiler = Class(TPackagehandler)
  15. Private
  16. Procedure CompileFPMake;
  17. Public
  18. Function Execute(const Args:TActionArgs):boolean;override;
  19. end;
  20. { TFPMakeRunner }
  21. TFPMakeRunner = Class(TPackagehandler)
  22. Protected
  23. Function RunFPMake(const Command:string):Integer;
  24. end;
  25. { TFPMakeRunnerCompile }
  26. TFPMakeRunnerCompile = Class(TFPMakeRunner)
  27. Public
  28. Function Execute(const Args:TActionArgs):boolean;override;
  29. end;
  30. { TFPMakeRunnerBuild }
  31. TFPMakeRunnerBuild = Class(TFPMakeRunner)
  32. Public
  33. Function Execute(const Args:TActionArgs):boolean;override;
  34. end;
  35. { TFPMakeRunnerInstall }
  36. TFPMakeRunnerInstall = Class(TFPMakeRunner)
  37. Public
  38. Function Execute(const Args:TActionArgs):boolean;override;
  39. end;
  40. { TFPMakeRunnerManifest }
  41. TFPMakeRunnerManifest = Class(TFPMakeRunner)
  42. Public
  43. Function Execute(const Args:TActionArgs):boolean;override;
  44. end;
  45. { TFPMakeRunnerArchive }
  46. TFPMakeRunnerArchive = Class(TFPMakeRunner)
  47. Public
  48. Function Execute(const Args:TActionArgs):boolean;override;
  49. end;
  50. TMyMemoryStream=class(TMemoryStream)
  51. public
  52. constructor Create(p:pointer;mysize:integer);
  53. end;
  54. {$i fpmkunitsrc.inc}
  55. procedure CreateFPMKUnitSource(const AFileName:string);
  56. var
  57. InStream,
  58. OutStream : TStream;
  59. pend : pchar;
  60. begin
  61. try
  62. // Don't write trailing #0
  63. pend:=pchar(@fpmkunitsrc)+sizeof(fpmkunitsrc)-1;
  64. while pend^=#0 do
  65. dec(pend);
  66. InStream:=TMyMemoryStream.Create(@fpmkunitsrc,pend-pchar(@fpmkunitsrc));
  67. OutStream:=TFileStream.Create(AFileName,fmCreate);
  68. OutStream.CopyFrom(InStream,InStream.Size);
  69. finally
  70. InStream.Destroy;
  71. OutStream.Destroy;
  72. end;
  73. end;
  74. {*****************************************************************************
  75. TMyMemoryStream
  76. *****************************************************************************}
  77. constructor TMyMemoryStream.Create(p:pointer;mysize:integer);
  78. begin
  79. inherited Create;
  80. SetPointer(p,mysize);
  81. end;
  82. { TFPMakeCompiler }
  83. Procedure TFPMakeCompiler.CompileFPMake;
  84. function CheckUnitDir(const AUnitName:string;Out AUnitDir:string):boolean;
  85. begin
  86. Result:=false;
  87. if FPMakeCompilerOptions.LocalUnitDir<>'' then
  88. begin
  89. AUnitDir:=IncludeTrailingPathDelimiter(FPMakeCompilerOptions.LocalUnitDir+AUnitName);
  90. if DirectoryExistsLog(AUnitDir) then
  91. begin
  92. Result:=true;
  93. exit;
  94. end;
  95. end;
  96. AUnitDir:=IncludeTrailingPathDelimiter(FPMakeCompilerOptions.GlobalUnitDir+AUnitName);
  97. if DirectoryExistsLog(AUnitDir) then
  98. begin
  99. Result:=true;
  100. exit;
  101. end;
  102. AUnitDir:='';
  103. end;
  104. const
  105. TempBuildDir = 'build-fpmake';
  106. Var
  107. OOptions,
  108. DepDir,
  109. DepDir2,
  110. FPMakeBin,
  111. FPMakeSrc : string;
  112. NeedFPMKUnitSource,
  113. DoBootStrap,
  114. HaveFpmake : boolean;
  115. begin
  116. SetCurrentDir(PackageBuildPath);
  117. // Check for fpmake source
  118. FPMakeBin:='fpmake'+ExeExt;
  119. FPMakeSrc:='fpmake.pp';
  120. HaveFpmake:=FileExists(FPMakeSrc);
  121. If Not HaveFPMake then
  122. begin
  123. HaveFPMake:=FileExists('fpmake.pas');
  124. If HaveFPMake then
  125. FPMakeSrc:='fpmake.pas';
  126. end;
  127. // Need to compile fpmake executable?
  128. if not FileExists(FPMakeBin) or
  129. (FileAge(FPMakeBin)<FileAge(FPMakeSrc)) then
  130. begin
  131. if Not HaveFPMake then
  132. Error(SErrMissingFPMake);
  133. // Special bootstrapping mode to compile fpmake?
  134. DoBootStrap:=False;
  135. NeedFPMKUnitSource:=False;
  136. OOptions:='-n';
  137. // Add FPMKUnit unit dir, if not found we use the internal fpmkunit source
  138. if CheckUnitDir('fpmkunit',DepDir) then
  139. OOptions:=OOptions+' -Fu'+DepDir
  140. else
  141. begin
  142. Log(vWarning,SWarnFPMKUnitNotFound);
  143. DoBootStrap:=true;
  144. NeedFPMKUnitSource:=true;
  145. OOptions:=OOptions+' -Fu'+TempBuildDir;
  146. end;
  147. // Add PaszLib and Hash units dir
  148. // we need to check for the zipper.ppu that is not
  149. // delivered with fpc 2.0.4
  150. if not CheckUnitDir('hash',DepDir) then
  151. begin
  152. if DoBootStrap then
  153. DepDir:=''
  154. else
  155. Error(SErrMissingInstallPackage,['hash']);
  156. end;
  157. if not CheckUnitDir('paszlib',DepDir2) or
  158. not FileExists(DepDir2+'zipper.ppu') then
  159. begin
  160. if DoBootStrap then
  161. DepDir2:=''
  162. else
  163. Error(SErrMissingInstallPackage,['paszlib']);
  164. end;
  165. if (DepDir<>'') and (DepDir2<>'') then
  166. OOptions:=OOptions+' -Fu'+DepDir+' -Fu'+DepDir2
  167. else
  168. OOptions:=OOptions+' -dNO_UNIT_ZIPPER';
  169. // Add Process unit
  170. if CheckUnitDir('fcl-process',DepDir) then
  171. OOptions:=OOptions+' -Fu'+DepDir
  172. else
  173. begin
  174. if DoBootStrap then
  175. OOptions:=OOptions+' -dNO_UNIT_PROCESS'
  176. else
  177. Error(SErrMissingInstallPackage,['fcl-process']);
  178. end;
  179. // Add RTL unit dir
  180. if not CheckUnitDir('rtl',DepDir) then
  181. Error(SErrMissingInstallPackage,['rtl']);
  182. OOptions:=OOptions+' -Fu'+DepDir;
  183. // Units in a directory for easy cleaning
  184. DeleteDir(TempBuildDir);
  185. ForceDirectories(TempBuildDir);
  186. OOptions:=OOptions+' -FU'+TempBuildDir;
  187. // Compile options
  188. // -- bootstrapping compile with -g
  189. // -- default is to optimize, smartlink and strip to reduce
  190. // the executable size (there can be 100's of fpmake's on a system)
  191. if vInfo in Verbosity then
  192. OOptions:=OOptions+' -vi';
  193. if DoBootStrap then
  194. OOptions:=OOptions+' -g'
  195. else
  196. OOptions:=OOptions+' -O2 -XXs';
  197. // Create fpmkunit.pp if needed
  198. if NeedFPMKUnitSource then
  199. CreateFPMKUnitSource(TempBuildDir+PathDelim+'fpmkunit.pp');
  200. // Call compiler
  201. If ExecuteProcess(FPMakeCompilerOptions.Compiler,OOptions+' '+FPmakeSrc)<>0 then
  202. Error(SErrFailedToCompileFPCMake);
  203. // Cleanup units
  204. DeleteDir(TempBuildDir);
  205. end
  206. else
  207. Log(vCommands,SLogNotCompilingFPMake);
  208. end;
  209. function TFPMakeCompiler.Execute(const Args:TActionArgs):boolean;
  210. begin
  211. {$warning TODO Check arguments}
  212. CompileFPMake;
  213. result:=true;
  214. end;
  215. { TFPMakeRunner }
  216. Function TFPMakeRunner.RunFPMake(const Command:string) : Integer;
  217. Var
  218. FPMakeBin,
  219. OOptions : string;
  220. begin
  221. { Maybe compile fpmake executable? }
  222. ExecuteAction(CurrentPackage,'compilefpmake');
  223. { Create options }
  224. OOptions:=' --nofpccfg';
  225. if vInfo in Verbosity then
  226. OOptions:=OOptions+' --verbose';
  227. OOptions:=OOptions+' --compiler='+CompilerOptions.Compiler;
  228. OOptions:=OOptions+' --CPU='+CPUToString(CompilerOptions.CompilerCPU);
  229. OOptions:=OOptions+' --OS='+OSToString(CompilerOptions.CompilerOS);
  230. if IsSuperUser or GlobalOptions.InstallGlobal then
  231. OOptions:=OOptions+' --baseinstalldir='+CompilerOptions.GlobalInstallDir
  232. else
  233. OOptions:=OOptions+' --baseinstalldir='+CompilerOptions.LocalInstallDir;
  234. if CompilerOptions.LocalInstallDir<>'' then
  235. OOptions:=OOptions+' --localunitdir='+CompilerOptions.LocalUnitDir;
  236. OOptions:=OOptions+' --globalunitdir='+CompilerOptions.GlobalUnitDir;
  237. { Run FPMake }
  238. FPMakeBin:='fpmake'+ExeExt;
  239. SetCurrentDir(PackageBuildPath);
  240. Result:=ExecuteProcess(FPMakeBin,Command+OOptions);
  241. end;
  242. function TFPMakeRunnerCompile.Execute(const Args:TActionArgs):boolean;
  243. begin
  244. result:=(RunFPMake('compile')=0);
  245. end;
  246. function TFPMakeRunnerBuild.Execute(const Args:TActionArgs):boolean;
  247. begin
  248. result:=(RunFPMake('build')=0);
  249. end;
  250. function TFPMakeRunnerInstall.Execute(const Args:TActionArgs):boolean;
  251. begin
  252. result:=(RunFPMake('install')=0);
  253. end;
  254. function TFPMakeRunnerManifest.Execute(const Args:TActionArgs):boolean;
  255. begin
  256. result:=(RunFPMake('manifest')=0);
  257. end;
  258. function TFPMakeRunnerArchive.Execute(const Args:TActionArgs):boolean;
  259. begin
  260. result:=(RunFPMake('archive')=0);
  261. end;
  262. initialization
  263. RegisterPkgHandler('compilefpmake',TFPMakeCompiler);
  264. RegisterPkgHandler('fpmakecompile',TFPMakeRunnerCompile);
  265. RegisterPkgHandler('fpmakebuild',TFPMakeRunnerBuild);
  266. RegisterPkgHandler('fpmakeinstall',TFPMakeRunnerInstall);
  267. RegisterPkgHandler('fpmakemanifest',TFPMakeRunnerManifest);
  268. RegisterPkgHandler('fpmakearchive',TFPMakeRunnerArchive);
  269. end.