fppkg.pp 15 KB

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