fppkg.pp 12 KB

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