fppkg.pp 12 KB

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