fppkg.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. program fppkg;
  2. {$mode objfpc}{$H+}
  3. {$if defined(VER2_2) and (FPC_PATCH<1)}
  4. {$fatal At least FPC 2.2.1 is required to compile fppkg}
  5. {$endif}
  6. uses
  7. // General
  8. {$ifdef unix}
  9. baseunix,
  10. {$endif}
  11. Classes, SysUtils, TypInfo, custapp,
  12. // Repository handler objects
  13. fprepos, fpxmlrep,
  14. pkgmessages, pkgglobals, pkgoptions, pkgrepos,
  15. // Package Handler components
  16. pkghandler,pkgmkconv, pkgdownload,
  17. pkgfpmake, pkgcommands
  18. // Downloaders
  19. {$if defined(unix) or defined(windows)}
  20. ,pkgwget
  21. ,pkglnet
  22. {$endif}
  23. ;
  24. Type
  25. { TMakeTool }
  26. TMakeTool = Class(TCustomApplication)
  27. Private
  28. ParaAction : string;
  29. ParaPackages : TStringList;
  30. procedure MaybeCreateLocalDirs;
  31. procedure ShowUsage;
  32. Public
  33. Constructor Create;
  34. Destructor Destroy;override;
  35. Function GetConfigFileName : String;
  36. Procedure LoadGlobalDefaults;
  37. Procedure LoadCompilerDefaults;
  38. Procedure ProcessCommandLine;
  39. Procedure DoRun; Override;
  40. end;
  41. EMakeToolError = Class(Exception);
  42. { TMakeTool }
  43. function TMakeTool.GetConfigFileName: String;
  44. begin
  45. if HasOption('C','config-file') then
  46. Result:=GetOptionValue('C','config-file')
  47. else
  48. Result:=GetAppConfigFile(IsSuperUser,False);
  49. end;
  50. procedure TMakeTool.LoadGlobalDefaults;
  51. var
  52. i : integer;
  53. cfgfile : String;
  54. GeneratedConfig : boolean;
  55. begin
  56. // Default verbosity
  57. LogLevels:=DefaultLogLevels;
  58. for i:=1 to ParamCount do
  59. if (ParamStr(i)='-d') or (ParamStr(i)='--debug') then
  60. begin
  61. LogLevels:=AllLogLevels+[vlDebug];
  62. break;
  63. end;
  64. // Load file or create new default configuration
  65. cfgfile:=GetConfigFileName;
  66. GeneratedConfig:=false;
  67. if FileExists(cfgfile) then
  68. begin
  69. GlobalOptions.LoadGlobalFromFile(cfgfile);
  70. if GlobalOptions.Dirty then
  71. GlobalOptions.SaveGlobalToFile(cfgfile);
  72. end
  73. else
  74. begin
  75. ForceDirectories(ExtractFilePath(cfgfile));
  76. GlobalOptions.SaveGlobalToFile(cfgfile);
  77. GeneratedConfig:=true;
  78. end;
  79. GlobalOptions.CompilerConfig:=GlobalOptions.DefaultCompilerConfig;
  80. // Tracing of what we've done above, need to be done after the verbosity is set
  81. if GeneratedConfig then
  82. Log(vlDebug,SLogGeneratingGlobalConfig,[cfgfile])
  83. else
  84. Log(vlDebug,SLogLoadingGlobalConfig,[cfgfile]);
  85. // Log configuration
  86. GlobalOptions.LogValues;
  87. end;
  88. procedure TMakeTool.MaybeCreateLocalDirs;
  89. begin
  90. ForceDirectories(GlobalOptions.BuildDir);
  91. ForceDirectories(GlobalOptions.ArchivesDir);
  92. ForceDirectories(GlobalOptions.CompilerConfigDir);
  93. end;
  94. procedure TMakeTool.LoadCompilerDefaults;
  95. var
  96. S : String;
  97. begin
  98. // Load default compiler config
  99. S:=GlobalOptions.CompilerConfigDir+GlobalOptions.CompilerConfig;
  100. if FileExists(S) then
  101. begin
  102. Log(vlDebug,SLogLoadingCompilerConfig,[S]);
  103. CompilerOptions.LoadCompilerFromFile(S)
  104. end
  105. else
  106. begin
  107. // Generate a default configuration if it doesn't exists
  108. if GlobalOptions.CompilerConfig='default' then
  109. begin
  110. Log(vlDebug,SLogGeneratingCompilerConfig,[S]);
  111. CompilerOptions.InitCompilerDefaults;
  112. CompilerOptions.SaveCompilerToFile(S);
  113. if CompilerOptions.Dirty then
  114. CompilerOptions.SaveCompilerToFile(S);
  115. end
  116. else
  117. Error(SErrMissingCompilerConfig,[S]);
  118. end;
  119. // Log compiler configuration
  120. CompilerOptions.LogValues('');
  121. // Load FPMake compiler config, this is normally the same config as above
  122. S:=GlobalOptions.CompilerConfigDir+GlobalOptions.FPMakeCompilerConfig;
  123. if FileExists(S) then
  124. begin
  125. Log(vlDebug,SLogLoadingFPMakeCompilerConfig,[S]);
  126. FPMakeCompilerOptions.LoadCompilerFromFile(S);
  127. if FPMakeCompilerOptions.Dirty then
  128. FPMakeCompilerOptions.SaveCompilerToFile(S);
  129. end
  130. else
  131. Error(SErrMissingCompilerConfig,[S]);
  132. // Log compiler configuration
  133. FPMakeCompilerOptions.LogValues('fpmake-building ');
  134. end;
  135. procedure TMakeTool.ShowUsage;
  136. begin
  137. Writeln('Usage: ',Paramstr(0),' [options] <action> <package>');
  138. Writeln('Options:');
  139. Writeln(' -c --config Set compiler configuration to use');
  140. Writeln(' -h --help This help');
  141. Writeln(' -v --verbose Show more information');
  142. Writeln(' -d --debug Show debugging information');
  143. Writeln(' -g --global Force installation to global (system-wide) directory');
  144. Writeln(' -f --force Force installation also if the package is already installed');
  145. Writeln(' -r --recovery Recovery mode, use always internal fpmkunit');
  146. Writeln('Actions:');
  147. Writeln(' update Update packages list');
  148. Writeln(' list List available and installed packages');
  149. Writeln(' build Build package');
  150. Writeln(' compile Compile package');
  151. Writeln(' install Install package');
  152. Writeln(' clean Clean package');
  153. Writeln(' archive Create archive of package');
  154. Writeln(' download Download package');
  155. Writeln(' convertmk Convert Makefile.fpc to fpmake.pp');
  156. // Writeln(' addconfig Add a compiler configuration for the supplied compiler');
  157. Halt(0);
  158. end;
  159. Constructor TMakeTool.Create;
  160. begin
  161. inherited Create(nil);
  162. ParaPackages:=TStringList.Create;
  163. end;
  164. Destructor TMakeTool.Destroy;
  165. begin
  166. FreeAndNil(ParaPackages);
  167. inherited Destroy;
  168. end;
  169. procedure TMakeTool.ProcessCommandLine;
  170. Function CheckOption(Index : Integer;Short,Long : String): Boolean;
  171. var
  172. O : String;
  173. begin
  174. O:=Paramstr(Index);
  175. Result:=(O='-'+short) or (O='--'+long) or (copy(O,1,Length(Long)+3)=('--'+long+'='));
  176. end;
  177. Function OptionArg(Var Index : Integer) : String;
  178. Var
  179. P : Integer;
  180. begin
  181. if (Length(ParamStr(Index))>1) and (Paramstr(Index)[2]<>'-') then
  182. begin
  183. If Index<ParamCount then
  184. begin
  185. Inc(Index);
  186. Result:=Paramstr(Index);
  187. end
  188. else
  189. Error(SErrNeedArgument,[Index,ParamStr(Index)]);
  190. end
  191. else If length(ParamStr(Index))>2 then
  192. begin
  193. P:=Pos('=',Paramstr(Index));
  194. If (P=0) then
  195. Error(SErrNeedArgument,[Index,ParamStr(Index)])
  196. else
  197. begin
  198. Result:=Paramstr(Index);
  199. Delete(Result,1,P);
  200. end;
  201. end;
  202. end;
  203. Var
  204. I : Integer;
  205. HasAction : Boolean;
  206. begin
  207. I:=0;
  208. HasAction:=false;
  209. // We can't use the TCustomApplication option handling,
  210. // because they cannot handle [general opts] [command] [cmd-opts] [args]
  211. While (I<ParamCount) do
  212. begin
  213. Inc(I);
  214. // Check options.
  215. if CheckOption(I,'c','config') then
  216. GlobalOptions.CompilerConfig:=OptionArg(I)
  217. else if CheckOption(I,'v','verbose') then
  218. LogLevels:=AllLogLevels
  219. else if CheckOption(I,'d','debug') then
  220. LogLevels:=AllLogLevels+[vlDebug]
  221. else if CheckOption(I,'g','global') then
  222. GlobalOptions.InstallGlobal:=true
  223. else if CheckOption(I,'r','recovery') then
  224. GlobalOptions.RecoveryMode:=true
  225. else if CheckOption(I,'b','broken') then
  226. GlobalOptions.AllowBroken:=true
  227. else if CheckOption(I,'h','help') then
  228. begin
  229. ShowUsage;
  230. halt(0);
  231. end
  232. else if (Length(Paramstr(i))>0) and (Paramstr(I)[1]='-') then
  233. Raise EMakeToolError.CreateFmt(SErrInvalidArgument,[I,ParamStr(i)])
  234. else
  235. // It's a command or target.
  236. begin
  237. if HasAction then
  238. ParaPackages.Add(Paramstr(i))
  239. else
  240. begin
  241. ParaAction:=Paramstr(i);
  242. HasAction:=true;
  243. end;
  244. end;
  245. end;
  246. if not HasAction then
  247. ShowUsage;
  248. end;
  249. procedure TMakeTool.DoRun;
  250. var
  251. ActionPackage : TFPPackage;
  252. OldCurrDir : String;
  253. i : Integer;
  254. SL : TStringList;
  255. begin
  256. OldCurrDir:=GetCurrentDir;
  257. Try
  258. LoadGlobalDefaults;
  259. ProcessCommandLine;
  260. // Scan is special, it doesn't need a valid local setup
  261. if (ParaAction='scan') then
  262. begin
  263. RebuildRemoteRepository;
  264. ListRemoteRepository;
  265. SaveRemoteRepository;
  266. halt(0);
  267. end;
  268. MaybeCreateLocalDirs;
  269. LoadCompilerDefaults;
  270. LoadLocalAvailableMirrors;
  271. // Load local repository, update first if this is a new installation
  272. // errors will only be reported as warning. The user can be bootstrapping
  273. // and do an update later
  274. if not FileExists(GlobalOptions.LocalPackagesFile) then
  275. begin
  276. try
  277. pkghandler.ExecuteAction('','update');
  278. except
  279. on E: Exception do
  280. Log(vlWarning,E.Message);
  281. end;
  282. end;
  283. LoadLocalAvailableRepository;
  284. FindInstalledPackages(FPMakeCompilerOptions,true);
  285. CheckFPMakeDependencies;
  286. // We only need to reload the status when we use a different
  287. // configuration for compiling fpmake
  288. if GlobalOptions.CompilerConfig<>GlobalOptions.FPMakeCompilerConfig then
  289. FindInstalledPackages(CompilerOptions,true);
  290. // Check for broken dependencies
  291. if not GlobalOptions.AllowBroken and
  292. not((ParaPackages.Count=0) and (ParaAction='fixbroken')) then
  293. begin
  294. SL:=TStringList.Create;
  295. if FindBrokenPackages(SL) then
  296. Error(SErrBrokenPackagesFound);
  297. FreeAndNil(SL);
  298. end;
  299. if ParaPackages.Count=0 then
  300. begin
  301. ActionPackage:=AvailableRepository.AddPackage(CurrentDirPackageName);
  302. pkghandler.ExecuteAction(CurrentDirPackageName,ParaAction);
  303. end
  304. else
  305. begin
  306. // Process packages
  307. for i:=0 to ParaPackages.Count-1 do
  308. begin
  309. if FileExists(ParaPackages[i]) then
  310. begin
  311. ActionPackage:=AvailableRepository.AddPackage(CmdLinePackageName);
  312. ActionPackage.LocalFileName:=ExpandFileName(ParaPackages[i]);
  313. pkghandler.ExecuteAction(CmdLinePackageName,ParaAction);
  314. end
  315. else
  316. begin
  317. Log(vlDebug,SLogCommandLineAction,['['+ParaPackages[i]+']',ParaAction]);
  318. pkghandler.ExecuteAction(ParaPackages[i],ParaAction);
  319. end;
  320. end;
  321. end;
  322. // Recompile all packages dependent on this package
  323. if (ParaAction='install') then
  324. pkghandler.ExecuteAction('','fixbroken');
  325. Terminate;
  326. except
  327. On E : Exception do
  328. begin
  329. Writeln(StdErr,SErrException);
  330. Writeln(StdErr,E.Message);
  331. Halt(1);
  332. end;
  333. end;
  334. SetCurrentDir(OldCurrDir);
  335. end;
  336. begin
  337. With TMakeTool.Create do
  338. try
  339. run;
  340. finally
  341. Free;
  342. end;
  343. end.