fppkg.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. Function GetConfigFileName : String;
  36. Procedure LoadGlobalDefaults;
  37. Procedure LoadCompilerDefaults;
  38. Procedure ProcessCommandLine;
  39. Procedure DoRun; Override;
  40. end;
  41. EMakeToolError = Class(Exception);
  42. { TMakeTool }
  43. function TMakeTool.GetConfigFileName: String;
  44. begin
  45. if HasOption('C','config-file') then
  46. Result:=GetOptionValue('C','config-file')
  47. else
  48. Result:=GetAppConfigFile(IsSuperUser,False);
  49. end;
  50. procedure TMakeTool.LoadGlobalDefaults;
  51. var
  52. i : integer;
  53. cfgfile : String;
  54. GeneratedConfig : boolean;
  55. begin
  56. // Default verbosity
  57. LogLevels:=DefaultLogLevels;
  58. for i:=1 to ParamCount do
  59. if (ParamStr(i)='-d') or (ParamStr(i)='--debug') then
  60. begin
  61. LogLevels:=AllLogLevels+[vlDebug];
  62. break;
  63. end;
  64. // Load file or create new default configuration
  65. cfgfile:=GetConfigFileName;
  66. GeneratedConfig:=false;
  67. if FileExists(cfgfile) then
  68. begin
  69. GlobalOptions.LoadGlobalFromFile(cfgfile);
  70. if GlobalOptions.Dirty then
  71. GlobalOptions.SaveGlobalToFile(cfgfile);
  72. end
  73. else
  74. begin
  75. ForceDirectories(ExtractFilePath(cfgfile));
  76. GlobalOptions.SaveGlobalToFile(cfgfile);
  77. GeneratedConfig:=true;
  78. end;
  79. GlobalOptions.CompilerConfig:=GlobalOptions.DefaultCompilerConfig;
  80. // Tracing of what we've done above, need to be done after the verbosity is set
  81. if GeneratedConfig then
  82. Log(vlDebug,SLogGeneratingGlobalConfig,[cfgfile])
  83. else
  84. Log(vlDebug,SLogLoadingGlobalConfig,[cfgfile])
  85. end;
  86. procedure TMakeTool.MaybeCreateLocalDirs;
  87. begin
  88. ForceDirectories(GlobalOptions.BuildDir);
  89. ForceDirectories(GlobalOptions.ArchivesDir);
  90. ForceDirectories(GlobalOptions.CompilerConfigDir);
  91. end;
  92. procedure TMakeTool.LoadCompilerDefaults;
  93. var
  94. S : String;
  95. begin
  96. // Load default compiler config
  97. S:=GlobalOptions.CompilerConfigDir+GlobalOptions.CompilerConfig;
  98. if FileExists(S) then
  99. begin
  100. Log(vlDebug,SLogLoadingCompilerConfig,[S]);
  101. CompilerOptions.LoadCompilerFromFile(S)
  102. end
  103. else
  104. begin
  105. // Generate a default configuration if it doesn't exists
  106. if GlobalOptions.CompilerConfig='default' then
  107. begin
  108. Log(vlDebug,SLogGeneratingCompilerConfig,[S]);
  109. CompilerOptions.InitCompilerDefaults;
  110. CompilerOptions.SaveCompilerToFile(S);
  111. if CompilerOptions.Dirty then
  112. CompilerOptions.SaveCompilerToFile(S);
  113. end
  114. else
  115. Error(SErrMissingCompilerConfig,[S]);
  116. end;
  117. // Load FPMake compiler config, this is normally the same config as above
  118. S:=GlobalOptions.CompilerConfigDir+GlobalOptions.FPMakeCompilerConfig;
  119. if FileExists(S) then
  120. begin
  121. Log(vlDebug,SLogLoadingFPMakeCompilerConfig,[S]);
  122. FPMakeCompilerOptions.LoadCompilerFromFile(S);
  123. if FPMakeCompilerOptions.Dirty then
  124. FPMakeCompilerOptions.SaveCompilerToFile(S);
  125. end
  126. else
  127. Error(SErrMissingCompilerConfig,[S]);
  128. end;
  129. procedure TMakeTool.ShowUsage;
  130. begin
  131. Writeln('Usage: ',Paramstr(0),' [options] <action> <package>');
  132. Writeln('Options:');
  133. Writeln(' -c --config Set compiler configuration to use');
  134. Writeln(' -h --help This help');
  135. Writeln(' -v --verbose Show more information');
  136. Writeln(' -d --debug Show debugging information');
  137. Writeln(' -g --global Force installation to global (system-wide) directory');
  138. Writeln(' -f --force Force installation also if the package is already installed');
  139. Writeln(' -r --recovery Recovery mode, use always internal fpmkunit');
  140. Writeln('Actions:');
  141. Writeln(' update Update packages list');
  142. Writeln(' showavail List available packages');
  143. Writeln(' showall Show all (including local) packages');
  144. Writeln(' build Build package');
  145. Writeln(' compile Compile package');
  146. Writeln(' install Install package');
  147. Writeln(' clean Clean package');
  148. Writeln(' archive Create archive of package');
  149. Writeln(' download Download package');
  150. Writeln(' convertmk Convert Makefile.fpc to fpmake.pp');
  151. // Writeln(' addconfig Add a compiler configuration for the supplied compiler');
  152. Halt(0);
  153. end;
  154. Constructor TMakeTool.Create;
  155. begin
  156. inherited Create(nil);
  157. ParaPackages:=TStringList.Create;
  158. end;
  159. Destructor TMakeTool.Destroy;
  160. begin
  161. FreeAndNil(ParaPackages);
  162. inherited Destroy;
  163. end;
  164. procedure TMakeTool.ProcessCommandLine;
  165. Function CheckOption(Index : Integer;Short,Long : String): Boolean;
  166. var
  167. O : String;
  168. begin
  169. O:=Paramstr(Index);
  170. Result:=(O='-'+short) or (O='--'+long) or (copy(O,1,Length(Long)+3)=('--'+long+'='));
  171. end;
  172. Function OptionArg(Var Index : Integer) : String;
  173. Var
  174. P : Integer;
  175. begin
  176. if (Length(ParamStr(Index))>1) and (Paramstr(Index)[2]<>'-') then
  177. begin
  178. If Index<ParamCount then
  179. begin
  180. Inc(Index);
  181. Result:=Paramstr(Index);
  182. end
  183. else
  184. Error(SErrNeedArgument,[Index,ParamStr(Index)]);
  185. end
  186. else If length(ParamStr(Index))>2 then
  187. begin
  188. P:=Pos('=',Paramstr(Index));
  189. If (P=0) then
  190. Error(SErrNeedArgument,[Index,ParamStr(Index)])
  191. else
  192. begin
  193. Result:=Paramstr(Index);
  194. Delete(Result,1,P);
  195. end;
  196. end;
  197. end;
  198. Var
  199. I : Integer;
  200. HasAction : Boolean;
  201. begin
  202. I:=0;
  203. HasAction:=false;
  204. // We can't use the TCustomApplication option handling,
  205. // because they cannot handle [general opts] [command] [cmd-opts] [args]
  206. While (I<ParamCount) do
  207. begin
  208. Inc(I);
  209. // Check options.
  210. if CheckOption(I,'c','config') then
  211. GlobalOptions.CompilerConfig:=OptionArg(I)
  212. else if CheckOption(I,'v','verbose') then
  213. LogLevels:=AllLogLevels
  214. else if CheckOption(I,'d','debug') then
  215. LogLevels:=AllLogLevels+[vlDebug]
  216. else if CheckOption(I,'g','global') then
  217. GlobalOptions.InstallGlobal:=true
  218. else if CheckOption(I,'r','recovery') then
  219. GlobalOptions.RecoveryMode:=true
  220. else if CheckOption(I,'b','broken') then
  221. GlobalOptions.AllowBroken:=true
  222. else if CheckOption(I,'h','help') then
  223. begin
  224. ShowUsage;
  225. halt(0);
  226. end
  227. else if (Length(Paramstr(i))>0) and (Paramstr(I)[1]='-') then
  228. Raise EMakeToolError.CreateFmt(SErrInvalidArgument,[I,ParamStr(i)])
  229. else
  230. // It's a command or target.
  231. begin
  232. if HasAction then
  233. ParaPackages.Add(Paramstr(i))
  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;
  255. MaybeCreateLocalDirs;
  256. LoadCompilerDefaults;
  257. // Load local repository, update first if this is a new installation
  258. // errors will only be reported as warning. The user can be bootstrapping
  259. // and do an update later
  260. if not FileExists(GlobalOptions.LocalPackagesFile) then
  261. begin
  262. try
  263. pkghandler.ExecuteAction('','update');
  264. except
  265. on E: Exception do
  266. Log(vlWarning,E.Message);
  267. end;
  268. end;
  269. LoadLocalAvailableMirrors;
  270. LoadLocalAvailableRepository;
  271. FindInstalledPackages(FPMakeCompilerOptions,true);
  272. CheckFPMakeDependencies;
  273. // We only need to reload the status when we use a different
  274. // configuration for compiling fpmake
  275. if GlobalOptions.CompilerConfig<>GlobalOptions.FPMakeCompilerConfig then
  276. FindInstalledPackages(CompilerOptions,true);
  277. // Check for broken dependencies
  278. if not GlobalOptions.AllowBroken and
  279. not((ParaPackages.Count=0) and (ParaAction='fixbroken')) then
  280. begin
  281. SL:=TStringList.Create;
  282. if FindBrokenPackages(SL) then
  283. Error(SErrBrokenPackagesFound);
  284. FreeAndNil(SL);
  285. end;
  286. if ParaPackages.Count=0 then
  287. begin
  288. ActionPackage:=AvailableRepository.AddPackage(CurrentDirPackageName);
  289. pkghandler.ExecuteAction(CurrentDirPackageName,ParaAction);
  290. end
  291. else
  292. begin
  293. // Process packages
  294. for i:=0 to ParaPackages.Count-1 do
  295. begin
  296. if FileExists(ParaPackages[i]) then
  297. begin
  298. ActionPackage:=AvailableRepository.AddPackage(CmdLinePackageName);
  299. ActionPackage.LocalFileName:=ExpandFileName(ParaPackages[i]);
  300. pkghandler.ExecuteAction(CmdLinePackageName,ParaAction);
  301. end
  302. else
  303. begin
  304. Log(vlDebug,SLogCommandLineAction,['['+ParaPackages[i]+']',ParaAction]);
  305. pkghandler.ExecuteAction(ParaPackages[i],ParaAction);
  306. end;
  307. end;
  308. end;
  309. // Recompile all packages dependent on this package
  310. if (ParaAction='install') then
  311. pkghandler.ExecuteAction('','fixbroken');
  312. Terminate;
  313. except
  314. On E : Exception do
  315. begin
  316. Writeln(StdErr,SErrException);
  317. Writeln(StdErr,E.Message);
  318. Halt(1);
  319. end;
  320. end;
  321. SetCurrentDir(OldCurrDir);
  322. end;
  323. begin
  324. With TMakeTool.Create do
  325. try
  326. run;
  327. finally
  328. Free;
  329. end;
  330. end.