fppkg.pp 15 KB

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