fppkg.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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.SaveInifileChanges 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.SaveInifileChanges 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.SaveInifileChanges 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(' -o --options=value Pass extra options to the compiler');
  166. Writeln('Actions:');
  167. Writeln(' update Update packages list');
  168. Writeln(' list List available and installed packages');
  169. Writeln(' build Build package');
  170. Writeln(' compile Compile package');
  171. Writeln(' install Install package');
  172. Writeln(' clean Clean package');
  173. Writeln(' archive Create archive of package');
  174. Writeln(' download Download package');
  175. Writeln(' convertmk Convert Makefile.fpc to fpmake.pp');
  176. Writeln(' fixbroken Recompile all (broken) packages with changed dependencies');
  177. // Writeln(' addconfig Add a compiler configuration for the supplied compiler');
  178. Halt(0);
  179. end;
  180. Constructor TMakeTool.Create;
  181. begin
  182. inherited Create(nil);
  183. ParaPackages:=TStringList.Create;
  184. end;
  185. Destructor TMakeTool.Destroy;
  186. begin
  187. FreeAndNil(ParaPackages);
  188. inherited Destroy;
  189. end;
  190. procedure TMakeTool.ProcessCommandLine;
  191. Function CheckOption(Index : Integer;Short,Long : String): Boolean;
  192. var
  193. O : String;
  194. begin
  195. O:=Paramstr(Index);
  196. Result:=(O='-'+short) or (O='--'+long) or (copy(O,1,Length(Long)+3)=('--'+long+'='));
  197. end;
  198. Function OptionArg(Var Index : Integer) : String;
  199. Var
  200. P : Integer;
  201. begin
  202. if (Length(ParamStr(Index))>1) and (Paramstr(Index)[2]<>'-') then
  203. begin
  204. If Index<ParamCount then
  205. begin
  206. Inc(Index);
  207. Result:=Paramstr(Index);
  208. end
  209. else
  210. Error(SErrNeedArgument,[Index,ParamStr(Index)]);
  211. end
  212. else If length(ParamStr(Index))>2 then
  213. begin
  214. P:=Pos('=',Paramstr(Index));
  215. If (P=0) then
  216. Error(SErrNeedArgument,[Index,ParamStr(Index)])
  217. else
  218. begin
  219. Result:=Paramstr(Index);
  220. Delete(Result,1,P);
  221. end;
  222. end;
  223. end;
  224. function SplitSpaces(var SplitString: string) : string;
  225. var i : integer;
  226. begin
  227. i := pos(' ',SplitString);
  228. if i > 0 then
  229. begin
  230. result := copy(SplitString,1,i-1);
  231. delete(SplitString,1,i);
  232. end
  233. else
  234. begin
  235. result := SplitString;
  236. SplitString:='';
  237. end;
  238. end;
  239. Var
  240. I : Integer;
  241. HasAction : Boolean;
  242. OptString : String;
  243. begin
  244. I:=0;
  245. HasAction:=false;
  246. // We can't use the TCustomApplication option handling,
  247. // because they cannot handle [general opts] [command] [cmd-opts] [args]
  248. While (I<ParamCount) do
  249. begin
  250. Inc(I);
  251. // Check options.
  252. if CheckOption(I,'c','config') then
  253. GlobalOptions.CompilerConfig:=OptionArg(I)
  254. else if CheckOption(I,'v','verbose') then
  255. LogLevels:=AllLogLevels
  256. else if CheckOption(I,'d','debug') then
  257. LogLevels:=AllLogLevels+[vlDebug]
  258. else if CheckOption(I,'g','global') then
  259. GlobalOptions.InstallGlobal:=true
  260. else if CheckOption(I,'r','recovery') then
  261. GlobalOptions.RecoveryMode:=true
  262. else if CheckOption(I,'b','broken') then
  263. GlobalOptions.AllowBroken:=true
  264. else if CheckOption(I,'l','showlocation') then
  265. GlobalOptions.ShowLocation:=true
  266. else if CheckOption(I,'o','options') then
  267. begin
  268. OptString := OptionArg(I);
  269. while OptString <> '' do
  270. CompilerOptions.Options.Add(SplitSpaces(OptString));
  271. end
  272. else if CheckOption(I,'h','help') then
  273. begin
  274. ShowUsage;
  275. halt(0);
  276. end
  277. else if (Length(Paramstr(i))>0) and (Paramstr(I)[1]='-') then
  278. Raise EMakeToolError.CreateFmt(SErrInvalidArgument,[I,ParamStr(i)])
  279. else
  280. // It's a command or target.
  281. begin
  282. if HasAction then
  283. ParaPackages.Add(Paramstr(i))
  284. else
  285. begin
  286. ParaAction:=Paramstr(i);
  287. HasAction:=true;
  288. end;
  289. end;
  290. end;
  291. if not HasAction then
  292. ShowUsage;
  293. end;
  294. procedure TMakeTool.DoRun;
  295. var
  296. ActionPackage : TFPPackage;
  297. OldCurrDir : String;
  298. i : Integer;
  299. SL : TStringList;
  300. begin
  301. OldCurrDir:=GetCurrentDir;
  302. Try
  303. LoadGlobalDefaults;
  304. ProcessCommandLine;
  305. // Scan is special, it doesn't need a valid local setup
  306. if (ParaAction='scan') then
  307. begin
  308. RebuildRemoteRepository;
  309. ListRemoteRepository;
  310. SaveRemoteRepository;
  311. halt(0);
  312. end;
  313. MaybeCreateLocalDirs;
  314. LoadCompilerDefaults;
  315. LoadLocalAvailableMirrors;
  316. // Load local repository, update first if this is a new installation
  317. // errors will only be reported as warning. The user can be bootstrapping
  318. // and do an update later
  319. if not FileExists(GlobalOptions.LocalPackagesFile) then
  320. begin
  321. try
  322. pkghandler.ExecuteAction('','update');
  323. except
  324. on E: Exception do
  325. pkgglobals.Log(vlWarning,E.Message);
  326. end;
  327. end;
  328. LoadLocalAvailableRepository;
  329. FindInstalledPackages(FPMakeCompilerOptions,true);
  330. CheckFPMakeDependencies;
  331. // We only need to reload the status when we use a different
  332. // configuration for compiling fpmake
  333. if GlobalOptions.CompilerConfig<>GlobalOptions.FPMakeCompilerConfig then
  334. FindInstalledPackages(CompilerOptions,true);
  335. // Check for broken dependencies
  336. if not GlobalOptions.AllowBroken and
  337. (((ParaAction='fixbroken') and (ParaPackages.Count>0)) or
  338. (ParaAction='compile') or
  339. (ParaAction='build') or
  340. (ParaAction='install') or
  341. (ParaAction='archive')) then
  342. begin
  343. pkgglobals.Log(vlDebug,SLogCheckBrokenDependenvies);
  344. SL:=TStringList.Create;
  345. if FindBrokenPackages(SL) then
  346. Error(SErrBrokenPackagesFound);
  347. FreeAndNil(SL);
  348. end;
  349. if ParaPackages.Count=0 then
  350. begin
  351. ActionPackage:=AvailableRepository.AddPackage(CurrentDirPackageName);
  352. pkghandler.ExecuteAction(CurrentDirPackageName,ParaAction);
  353. end
  354. else
  355. begin
  356. // Process packages
  357. for i:=0 to ParaPackages.Count-1 do
  358. begin
  359. if sametext(ExtractFileExt(ParaPackages[i]),'.zip') and FileExists(ParaPackages[i]) then
  360. begin
  361. ActionPackage:=AvailableRepository.AddPackage(CmdLinePackageName);
  362. ActionPackage.LocalFileName:=ExpandFileName(ParaPackages[i]);
  363. pkghandler.ExecuteAction(CmdLinePackageName,ParaAction);
  364. end
  365. else
  366. begin
  367. pkgglobals.Log(vlDebug,SLogCommandLineAction,['['+ParaPackages[i]+']',ParaAction]);
  368. pkghandler.ExecuteAction(ParaPackages[i],ParaAction);
  369. end;
  370. end;
  371. end;
  372. // Recompile all packages dependent on this package
  373. if (ParaAction='install') then
  374. pkghandler.ExecuteAction('','fixbroken');
  375. Terminate;
  376. except
  377. On E : Exception do
  378. begin
  379. Writeln(StdErr,SErrException);
  380. Writeln(StdErr,E.Message);
  381. Halt(1);
  382. end;
  383. end;
  384. SetCurrentDir(OldCurrDir);
  385. end;
  386. begin
  387. With TMakeTool.Create do
  388. try
  389. run;
  390. finally
  391. Free;
  392. end;
  393. end.