fppkg.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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, cthreads,
  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. pkgPackagesStructure,
  19. fpmkunit
  20. // Downloaders
  21. {$if (defined(unix) and not defined(android)) or defined(windows)}
  22. ,pkgwget
  23. ,pkglnet
  24. ,pkgfphttp
  25. {$endif}
  26. ;
  27. Type
  28. { TMakeTool }
  29. TMakeTool = Class(TCustomApplication)
  30. Private
  31. ParaAction : string;
  32. ParaPackages : TStringList;
  33. procedure MaybeCreateLocalDirs;
  34. procedure ShowUsage;
  35. Public
  36. Constructor Create;
  37. Destructor Destroy;override;
  38. Procedure LoadGlobalDefaults;
  39. Procedure ProcessCommandLine(FirstPass: boolean);
  40. Procedure DoRun; Override;
  41. end;
  42. EMakeToolError = Class(Exception);
  43. { TMakeTool }
  44. procedure TMakeTool.LoadGlobalDefaults;
  45. var
  46. i : integer;
  47. cfgfile : String;
  48. begin
  49. // Default verbosity
  50. LogLevels:=DefaultLogLevels;
  51. for i:=1 to ParamCount do
  52. begin
  53. if (ParamStr(i)='-d') or (ParamStr(i)='--debug') then
  54. begin
  55. LogLevels:=AllLogLevels+[llDebug];
  56. break;
  57. end;
  58. if (ParamStr(i)='-v') or (ParamStr(i)='--verbose') then
  59. begin
  60. LogLevels:=AllLogLevels+[llDebug];
  61. break;
  62. end;
  63. end;
  64. // First try config file from command line
  65. if HasOption('C','config-file') then
  66. cfgfile:=GetOptionValue('C','config-file')
  67. else
  68. cfgfile:='';
  69. GFPpkg.InitializeGlobalOptions(CfgFile);
  70. end;
  71. procedure TMakeTool.MaybeCreateLocalDirs;
  72. begin
  73. ForceDirectories(GFPpkg.Options.GlobalSection.BuildDir);
  74. ForceDirectories(GFPpkg.Options.GlobalSection.ArchivesDir);
  75. ForceDirectories(GFPpkg.Options.GlobalSection.CompilerConfigDir);
  76. end;
  77. procedure TMakeTool.ShowUsage;
  78. begin
  79. Writeln('Usage: ',Paramstr(0),' [options] <action> <package>');
  80. Writeln('Options:');
  81. Writeln(' -C --config-file Specify the configuration file to use');
  82. Writeln(' -c --config Set compiler configuration to use');
  83. Writeln(' -h --help This help');
  84. Writeln(' -v --verbose Show more information');
  85. Writeln(' -d --debug Show debugging information');
  86. Writeln(' -f --force Force installation also if the package is already installed');
  87. Writeln(' -r --recovery Recovery mode, use always internal fpmkunit');
  88. Writeln(' -b --broken Do not stop on broken packages');
  89. Writeln(' -l --showlocation Show in which repository the the packages are installed');
  90. Writeln(' -o --options=value Pass extra options to the compiler');
  91. Writeln(' -n Do not read the default configuration files');
  92. Writeln(' -p --prefix=value Specify the prefix');
  93. Writeln(' -s --skipbroken Skip the rebuild of depending packages after installation');
  94. Writeln(' -i --installlocation Specify the repository to install packages into');
  95. Writeln(' --compiler=value Specify the compiler-executable');
  96. Writeln(' --cpu=value Specify the target cpu to compile for');
  97. Writeln(' --os=value Specify the target operating system to compile for');
  98. Writeln('Actions:');
  99. Writeln(' update Update packages list');
  100. Writeln(' list List available and installed packages');
  101. Writeln(' build Build package');
  102. Writeln(' compile Compile package');
  103. Writeln(' install Install package');
  104. Writeln(' uninstall Uninstall package');
  105. Writeln(' clean Clean package');
  106. Writeln(' archive Create archive of package');
  107. Writeln(' download Download package');
  108. Writeln(' convertmk Convert Makefile.fpc to fpmake.pp');
  109. Writeln(' info Show more information about a package');
  110. Writeln(' fixbroken Recompile all (broken) packages with changed dependencies');
  111. Writeln(' listsettings Show the values for all fppkg settings');
  112. // Writeln(' addconfig Add a compiler configuration for the supplied compiler');
  113. Halt(0);
  114. end;
  115. Constructor TMakeTool.Create;
  116. begin
  117. inherited Create(nil);
  118. ParaPackages:=TStringList.Create;
  119. end;
  120. Destructor TMakeTool.Destroy;
  121. begin
  122. FreeAndNil(ParaPackages);
  123. inherited Destroy;
  124. end;
  125. procedure TMakeTool.ProcessCommandLine(FirstPass: boolean);
  126. Function CheckOption(Index : Integer;Short,Long : String): Boolean;
  127. var
  128. O : String;
  129. begin
  130. O:=Paramstr(Index);
  131. Result:=(O='-'+short) or (O='--'+long) or (copy(O,1,Length(Long)+3)=('--'+long+'='));
  132. end;
  133. Function OptionArg(Var Index : Integer) : String;
  134. Var
  135. P : Integer;
  136. begin
  137. if (Length(ParamStr(Index))>1) and (Paramstr(Index)[2]<>'-') then
  138. begin
  139. If Index<ParamCount then
  140. begin
  141. Inc(Index);
  142. Result:=Paramstr(Index);
  143. end
  144. else
  145. Error(SErrNeedArgument,[Index,ParamStr(Index)]);
  146. end
  147. else If length(ParamStr(Index))>2 then
  148. begin
  149. P:=Pos('=',Paramstr(Index));
  150. If (P=0) then
  151. Error(SErrNeedArgument,[Index,ParamStr(Index)])
  152. else
  153. begin
  154. Result:=Paramstr(Index);
  155. Delete(Result,1,P);
  156. end;
  157. end;
  158. end;
  159. function SplitSpaces(var SplitString: string) : string;
  160. var i : integer;
  161. begin
  162. i := pos(' ',SplitString);
  163. if i > 0 then
  164. begin
  165. result := copy(SplitString,1,i-1);
  166. delete(SplitString,1,i);
  167. end
  168. else
  169. begin
  170. result := SplitString;
  171. SplitString:='';
  172. end;
  173. end;
  174. Var
  175. I : Integer;
  176. HasAction : Boolean;
  177. OptString : String;
  178. begin
  179. I:=0;
  180. HasAction:=false;
  181. // We can't use the TCustomApplication option handling,
  182. // because they cannot handle [general opts] [command] [cmd-opts] [args]
  183. While (I<ParamCount) do
  184. begin
  185. Inc(I);
  186. // Check options.
  187. if CheckOption(I,'C','config-file') then
  188. begin
  189. // Do nothing, the config-file has already been read.
  190. OptionArg(I);
  191. end
  192. else if CheckOption(I,'c','config') then
  193. GFPpkg.Options.CommandLineSection.CompilerConfig:=OptionArg(I)
  194. else if CheckOption(I,'v','verbose') then
  195. LogLevels:=AllLogLevels
  196. else if CheckOption(I,'d','debug') then
  197. LogLevels:=AllLogLevels+[llDebug]
  198. else if CheckOption(I,'i','installrepository') then
  199. GFPpkg.Options.CommandLineSection.InstallRepository:=OptionArg(I)
  200. else if CheckOption(I,'r','recovery') then
  201. GFPpkg.Options.CommandLineSection.RecoveryMode:=true
  202. else if CheckOption(I,'n','') then
  203. GFPpkg.Options.CommandLineSection.SkipConfigurationFiles:=true
  204. else if CheckOption(I,'b','broken') then
  205. GFPpkg.Options.CommandLineSection.AllowBroken:=true
  206. else if CheckOption(I,'l','showlocation') then
  207. GFPpkg.Options.CommandLineSection.ShowLocation:=true
  208. else if CheckOption(I,'s','skipbroken') then
  209. GFPpkg.Options.CommandLineSection.SkipFixBrokenAfterInstall:=true
  210. else if CheckOption(I,'o','options') and FirstPass then
  211. begin
  212. OptString := OptionArg(I);
  213. while OptString <> '' do
  214. GFPpkg.CompilerOptions.Options.Add(SplitSpaces(OptString));
  215. end
  216. else if CheckOption(I,'p','prefix') then
  217. begin
  218. GFPpkg.CompilerOptions.GlobalPrefix := OptionArg(I);
  219. GFPpkg.CompilerOptions.LocalPrefix := OptionArg(I);
  220. GFPpkg.FPMakeCompilerOptions.GlobalPrefix := OptionArg(I);
  221. GFPpkg.FPMakeCompilerOptions.LocalPrefix := OptionArg(I);
  222. end
  223. else if CheckOption(I,'','compiler') then
  224. begin
  225. GFPpkg.CompilerOptions.Compiler := OptionArg(I);
  226. GFPpkg.FPMakeCompilerOptions.Compiler := OptionArg(I);
  227. end
  228. else if CheckOption(I,'','os') then
  229. GFPpkg.CompilerOptions.CompilerOS := StringToOS(OptionArg(I))
  230. else if CheckOption(I,'','cpu') then
  231. GFPpkg.CompilerOptions.CompilerCPU := StringToCPU(OptionArg(I))
  232. else if CheckOption(I,'h','help') then
  233. begin
  234. ShowUsage;
  235. halt(0);
  236. end
  237. else if (Length(Paramstr(i))>0) and (Paramstr(I)[1]='-') then
  238. begin
  239. if FirstPass then
  240. Raise EMakeToolError.CreateFmt(SErrInvalidArgument,[I,ParamStr(i)])
  241. end
  242. else
  243. // It's a command or target.
  244. begin
  245. if HasAction then
  246. begin
  247. if FirstPass then
  248. ParaPackages.Add(Paramstr(i))
  249. end
  250. else
  251. begin
  252. ParaAction:=Paramstr(i);
  253. HasAction:=true;
  254. end;
  255. end;
  256. end;
  257. if not HasAction then
  258. ShowUsage;
  259. end;
  260. procedure TMakeTool.DoRun;
  261. var
  262. OldCurrDir : String;
  263. i : Integer;
  264. SL : TStringList;
  265. Repo: TFPRepository;
  266. InstPackages: TFPCurrentDirectoryPackagesStructure;
  267. ArchivePackages: TFPArchiveFilenamePackagesStructure;
  268. begin
  269. OldCurrDir:=GetCurrentDir;
  270. Try
  271. InitializeFppkg;
  272. LoadGlobalDefaults;
  273. ProcessCommandLine(true);
  274. SetLength(FPMKUnitDeps,FPMKUnitDepDefaultCount);
  275. for i := 0 to FPMKUnitDepDefaultCount-1 do
  276. FPMKUnitDeps[i]:=FPMKUnitDepsDefaults[i];
  277. MaybeCreateLocalDirs;
  278. if not GFPpkg.Options.CommandLineSection.SkipConfigurationFiles then
  279. begin
  280. GFPpkg.InitializeCompilerOptions;
  281. if GFPpkg.Options.GlobalSection.ConfigVersion = 4 then
  282. begin
  283. // This version did not have any repository configured, but used a
  284. // 'local' and 'global' compiler-setting.
  285. GFPpkg.Options.AddRepositoriesForCompilerSettings(GFPpkg.CompilerOptions);
  286. end;
  287. end
  288. else
  289. begin
  290. GFPpkg.FPMakeCompilerOptions.InitCompilerDefaults;
  291. GFPpkg.CompilerOptions.InitCompilerDefaults;
  292. end;
  293. // The command-line is parsed for the second time, to make it possible
  294. // to override the values in the compiler-configuration file. (like prefix)
  295. ProcessCommandLine(false);
  296. // If CompilerVersion, CompilerOS or CompilerCPU is still empty, use the
  297. // compiler-executable to get them
  298. GFPpkg.FPMakeCompilerOptions.CheckCompilerValues;
  299. GFPpkg.CompilerOptions.CheckCompilerValues;
  300. LoadLocalAvailableMirrors;
  301. // Load local repository, update first if this is a new installation
  302. // errors will only be reported as warning. The user can be bootstrapping
  303. // and do an update later
  304. if not FileExists(GFPpkg.Options.GlobalSection.LocalPackagesFile) then
  305. begin
  306. try
  307. pkghandler.ExecuteAction('','update', GFPpkg);
  308. except
  309. on E: Exception do
  310. pkgglobals.Log(llWarning,E.Message);
  311. end;
  312. end;
  313. FindInstalledPackages(GFPpkg.FPMakeCompilerOptions,true);
  314. // Check for broken dependencies
  315. if not GFPpkg.Options.CommandLineSection.AllowBroken and
  316. (((ParaAction='fixbroken') and (ParaPackages.Count>0)) or
  317. (ParaAction='compile') or
  318. (ParaAction='build') or
  319. (ParaAction='install') or
  320. (ParaAction='archive')) then
  321. begin
  322. pkgglobals.Log(llDebug,SLogCheckBrokenDependenvies);
  323. SL:=TStringList.Create;
  324. if FindBrokenPackages(SL) then
  325. Error(SErrBrokenPackagesFound);
  326. FreeAndNil(SL);
  327. end;
  328. if (ParaAction='install') or (ParaAction='uninstall') or
  329. (ParaAction='fixbroken') then
  330. GFPpkg.ScanInstalledPackagesForAvailablePackages;
  331. if ParaPackages.Count=0 then
  332. begin
  333. // Do not add the fake-repository with the contents of the current directory
  334. // when a list of packages is shown. (The fake repository should not be shown)
  335. if ParaAction<>'list' then
  336. begin
  337. Repo := TFPRepository.Create(GFPpkg);
  338. GFPpkg.RepositoryList.Add(Repo);
  339. Repo.RepositoryType := fprtAvailable;
  340. Repo.RepositoryName := 'CurrentDirectory';
  341. Repo.Description := 'Package in current directory';
  342. InstPackages := TFPCurrentDirectoryPackagesStructure.Create(GFPpkg);
  343. InstPackages.InitializeWithOptions(nil, GFPpkg.Options, GFPpkg.CompilerOptions);
  344. InstPackages.Path := OldCurrDir;
  345. InstPackages.AddPackagesToRepository(Repo);
  346. Repo.DefaultPackagesStructure := InstPackages;
  347. end;
  348. pkghandler.ExecuteAction(CurrentDirPackageName,ParaAction,GFPpkg);
  349. end
  350. else
  351. begin
  352. // Process packages
  353. for i:=0 to ParaPackages.Count-1 do
  354. begin
  355. if sametext(ExtractFileExt(ParaPackages[i]),'.zip') and FileExists(ParaPackages[i]) then
  356. begin
  357. Repo := TFPRepository.Create(GFPpkg);
  358. GFPpkg.RepositoryList.Add(Repo);
  359. Repo.RepositoryType := fprtAvailable;
  360. Repo.RepositoryName := 'ArchiveFile';
  361. Repo.Description := 'Package in archive-file';
  362. ArchivePackages := TFPArchiveFilenamePackagesStructure.Create(GFPpkg);
  363. ArchivePackages.InitializeWithOptions(nil, GFPpkg.Options, GFPpkg.CompilerOptions);
  364. ArchivePackages.ArchiveFileName := ParaPackages[i];
  365. ArchivePackages.AddPackagesToRepository(Repo);
  366. Repo.DefaultPackagesStructure := ArchivePackages;
  367. pkgglobals.Log(llDebug,SLogCommandLineAction,['['+CmdLinePackageName+']',ParaAction]);
  368. pkghandler.ExecuteAction(CmdLinePackageName,ParaAction,GFPpkg);
  369. end
  370. else
  371. begin
  372. pkgglobals.Log(llDebug,SLogCommandLineAction,['['+ParaPackages[i]+']',ParaAction]);
  373. pkghandler.ExecuteAction(ParaPackages[i],ParaAction,GFPpkg);
  374. end;
  375. end;
  376. end;
  377. // Recompile all packages dependent on this package
  378. if (ParaAction='install') and not GFPpkg.Options.CommandLineSection.SkipFixBrokenAfterInstall then
  379. pkghandler.ExecuteAction('','fixbroken',GFPpkg);
  380. Terminate;
  381. except
  382. On E : Exception do
  383. begin
  384. Writeln(StdErr,SErrException);
  385. Writeln(StdErr,E.Message);
  386. Halt(1);
  387. end;
  388. end;
  389. SetCurrentDir(OldCurrDir);
  390. end;
  391. begin
  392. With TMakeTool.Create do
  393. try
  394. run;
  395. finally
  396. Free;
  397. end;
  398. end.