fppkg.pp 12 KB

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