fppkg.pp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. program fppkg;
  2. {$mode objfpc}{$H+}
  3. uses
  4. // General
  5. {$ifdef unix}
  6. baseunix,
  7. {$endif}
  8. Classes, SysUtils, TypInfo, custapp,
  9. // Repository handler objects
  10. fprepos, fpxmlrep,
  11. pkgmessages, pkgglobals, pkgoptions, pkgrepos,
  12. // Package Handler components
  13. pkghandler,pkgmkconv, pkgdownload,
  14. pkgfpmake, pkgcommands
  15. // Downloaders
  16. {$if defined(unix) or defined(windows)}
  17. ,pkgwget
  18. ,pkglnet
  19. {$endif}
  20. ;
  21. Type
  22. { TMakeTool }
  23. TMakeTool = Class(TCustomApplication)
  24. Private
  25. ActionStack : TActionStack;
  26. ParaAction : string;
  27. ParaPackages : TStringList;
  28. procedure MaybeCreateLocalDirs;
  29. procedure ShowUsage;
  30. Public
  31. Constructor Create;
  32. Destructor Destroy;override;
  33. Function GetConfigFileName : String;
  34. Procedure LoadGlobalDefaults;
  35. Procedure LoadCompilerDefaults;
  36. Procedure ProcessCommandLine;
  37. Procedure DoRun; Override;
  38. end;
  39. EMakeToolError = Class(Exception);
  40. { TMakeTool }
  41. function TMakeTool.GetConfigFileName: String;
  42. var
  43. G : Boolean;
  44. begin
  45. if HasOption('C','config-file') then
  46. Result:=GetOptionValue('C','config-file')
  47. else
  48. begin
  49. {$ifdef unix}
  50. g:=(fpgetuid=0);
  51. {$else}
  52. g:=true;
  53. {$endif}
  54. Result:=GetAppConfigFile(G,False);
  55. end
  56. end;
  57. procedure TMakeTool.LoadGlobalDefaults;
  58. var
  59. SL : TStringList;
  60. i : integer;
  61. cfgfile : String;
  62. GeneratedConfig : boolean;
  63. begin
  64. cfgfile:=GetConfigFileName;
  65. GeneratedConfig:=false;
  66. // Load file or create new default configuration
  67. if FileExists(cfgfile) then
  68. GlobalOptions.LoadGlobalFromFile(cfgfile)
  69. else
  70. begin
  71. ForceDirectories(ExtractFilePath(cfgfile));
  72. GlobalOptions.SaveGlobalToFile(cfgfile);
  73. GeneratedConfig:=true;
  74. end;
  75. // Load default verbosity from config
  76. SL:=TStringList.Create;
  77. SL.CommaText:=GlobalOptions.DefaultVerbosity;
  78. for i:=0 to SL.Count-1 do
  79. Include(Verbosity,StringToVerbosity(SL[i]));
  80. SL.Free;
  81. GlobalOptions.CompilerConfig:=GlobalOptions.DefaultCompilerConfig;
  82. // Tracing of what we've done above, need to be done after the verbosity is set
  83. if GeneratedConfig then
  84. Log(vDebug,SLogGeneratingGlobalConfig,[cfgfile])
  85. else
  86. Log(vDebug,SLogLoadingGlobalConfig,[cfgfile])
  87. end;
  88. procedure TMakeTool.MaybeCreateLocalDirs;
  89. begin
  90. ForceDirectories(GlobalOptions.BuildDir);
  91. ForceDirectories(GlobalOptions.PackagesDir);
  92. ForceDirectories(GlobalOptions.CompilerConfigDir);
  93. end;
  94. procedure TMakeTool.LoadCompilerDefaults;
  95. var
  96. S : String;
  97. begin
  98. // Load default compiler config
  99. S:=GlobalOptions.CompilerConfigDir+GlobalOptions.CompilerConfig;
  100. if FileExists(S) then
  101. begin
  102. Log(vDebug,SLogLoadingCompilerConfig,[S]);
  103. CompilerOptions.LoadCompilerFromFile(S)
  104. end
  105. else
  106. begin
  107. // Generate a default configuration if it doesn't exists
  108. if GlobalOptions.CompilerConfig='default' then
  109. begin
  110. Log(vDebug,SLogGeneratingCompilerConfig,[S]);
  111. CompilerOptions.InitCompilerDefaults;
  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(vDebug,SLogLoadingFPMakeCompilerConfig,[S]);
  122. FPMakeCompilerOptions.LoadCompilerFromFile(S)
  123. end
  124. else
  125. Error(SErrMissingCompilerConfig,[S]);
  126. end;
  127. procedure TMakeTool.ShowUsage;
  128. begin
  129. Writeln('Usage: ',Paramstr(0),' [options] <action> <package>');
  130. Writeln('Options:');
  131. Writeln(' -c --config Set compiler configuration to use');
  132. Writeln(' -h --help This help');
  133. Writeln(' -v --verbose Set verbosity');
  134. Writeln(' -g --global Force installation to global (system-wide) directory');
  135. Writeln(' -f --force Force installation also if the package is already installed');
  136. Writeln('Actions:');
  137. Writeln(' update Update packages list');
  138. Writeln(' avail List available packages');
  139. Writeln(' build Build package');
  140. Writeln(' compile Compile package');
  141. Writeln(' install Install package');
  142. Writeln(' archive Create archive of package');
  143. Writeln(' download Download package');
  144. Writeln(' convertmk Convert Makefile.fpc to fpmake.pp');
  145. // Writeln(' addconfig Add a compiler configuration for the supplied compiler');
  146. Halt(0);
  147. end;
  148. Constructor TMakeTool.Create;
  149. begin
  150. inherited Create(nil);
  151. ParaPackages:=TStringList.Create;
  152. ActionStack:=TActionStack.Create;
  153. end;
  154. Destructor TMakeTool.Destroy;
  155. begin
  156. FreeAndNil(ActionStack);
  157. FreeAndNil(ParaPackages);
  158. inherited Destroy;
  159. end;
  160. procedure TMakeTool.ProcessCommandLine;
  161. Function CheckOption(Index : Integer;Short,Long : String): Boolean;
  162. var
  163. O : String;
  164. begin
  165. O:=Paramstr(Index);
  166. Result:=(O='-'+short) or (O='--'+long) or (copy(O,1,Length(Long)+3)=('--'+long+'='));
  167. end;
  168. Function OptionArg(Var Index : Integer) : String;
  169. Var
  170. P : Integer;
  171. begin
  172. if (Length(ParamStr(Index))>1) and (Paramstr(Index)[2]<>'-') then
  173. begin
  174. If Index<ParamCount then
  175. begin
  176. Inc(Index);
  177. Result:=Paramstr(Index);
  178. end
  179. else
  180. Error(SErrNeedArgument,[Index,ParamStr(Index)]);
  181. end
  182. else If length(ParamStr(Index))>2 then
  183. begin
  184. P:=Pos('=',Paramstr(Index));
  185. If (P=0) then
  186. Error(SErrNeedArgument,[Index,ParamStr(Index)])
  187. else
  188. begin
  189. Result:=Paramstr(Index);
  190. Delete(Result,1,P);
  191. end;
  192. end;
  193. end;
  194. Var
  195. I : Integer;
  196. HasAction : Boolean;
  197. begin
  198. I:=0;
  199. HasAction:=false;
  200. // We can't use the TCustomApplication option handling,
  201. // because they cannot handle [general opts] [command] [cmd-opts] [args]
  202. While (I<ParamCount) do
  203. begin
  204. Inc(I);
  205. // Check options.
  206. if CheckOption(I,'c','config') then
  207. GlobalOptions.CompilerConfig:=OptionArg(I)
  208. else if CheckOption(I,'v','verbose') then
  209. Include(Verbosity,StringToVerbosity(OptionArg(I)))
  210. else if CheckOption(I,'g','global') then
  211. GlobalOptions.InstallGlobal:=true
  212. else if CheckOption(I,'h','help') then
  213. begin
  214. ShowUsage;
  215. halt(0);
  216. end
  217. else if (Length(Paramstr(i))>0) and (Paramstr(I)[1]='-') then
  218. Raise EMakeToolError.CreateFmt(SErrInvalidArgument,[I,ParamStr(i)])
  219. else
  220. // It's a command or target.
  221. begin
  222. if HasAction then
  223. ParaPackages.Add(Paramstr(i))
  224. else
  225. begin
  226. ParaAction:=Paramstr(i);
  227. HasAction:=true;
  228. end;
  229. end;
  230. end;
  231. if not HasAction then
  232. ShowUsage;
  233. end;
  234. procedure TMakeTool.DoRun;
  235. var
  236. ActionPackage : TFPPackage;
  237. OldCurrDir : String;
  238. Res : Boolean;
  239. i : Integer;
  240. begin
  241. OldCurrDir:=GetCurrentDir;
  242. Try
  243. LoadGlobalDefaults;
  244. ProcessCommandLine;
  245. MaybeCreateLocalDirs;
  246. LoadCompilerDefaults;
  247. // Load local repository, update first if this is a new installation
  248. if not FileExists(GlobalOptions.LocalPackagesFile) then
  249. pkghandler.ExecuteAction(nil,'update');
  250. LoadLocalRepository;
  251. LoadFPMakeLocalStatus;
  252. // We only need to reload the status when we use a different
  253. // configuration for compiling fpmake
  254. if GlobalOptions.CompilerConfig<>GlobalOptions.FPMakeCompilerConfig then
  255. LoadLocalStatus;
  256. if ParaPackages.Count=0 then
  257. begin
  258. Log(vDebug,SLogCommandLineAction,['[<currentdir>]',ParaAction]);
  259. res:=pkghandler.ExecuteAction(nil,ParaAction);
  260. end
  261. else
  262. begin
  263. // Process packages
  264. for i:=0 to ParaPackages.Count-1 do
  265. begin
  266. ActionPackage:=CurrentRepository.PackageByName(ParaPackages[i]);
  267. Log(vDebug,SLogCommandLineAction,['['+ActionPackage.Name+']',ParaAction]);
  268. res:=pkghandler.ExecuteAction(ActionPackage,ParaAction);
  269. if not res then
  270. break;
  271. end;
  272. end;
  273. Terminate;
  274. except
  275. On E : Exception do
  276. begin
  277. Writeln(StdErr,SErrException);
  278. Writeln(StdErr,E.Message);
  279. Halt(1);
  280. end;
  281. end;
  282. SetCurrentDir(OldCurrDir);
  283. end;
  284. begin
  285. With TMakeTool.Create do
  286. try
  287. run;
  288. finally
  289. Free;
  290. end;
  291. end.