fppkg.pp 15 KB

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