fppkg.pp 12 KB

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