fppkg.pp 13 KB

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