fppkg.pp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. pkgarchive, pkgfpmake, pkgcommands
  15. // Downloaders
  16. {$if defined(unix) or defined(windows)}
  17. ,pkgwget
  18. {$endif}
  19. ;
  20. Type
  21. { TMakeTool }
  22. TMakeTool = Class(TCustomApplication)
  23. Private
  24. ActionStack : TActionStack;
  25. ParaAction : string;
  26. ParaPackages : TStringList;
  27. procedure GenerateParaActions;
  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. Defaults.LoadGlobalFromFile(cfgfile)
  69. else
  70. begin
  71. ForceDirectories(ExtractFilePath(cfgfile));
  72. Defaults.SaveGlobalToFile(cfgfile);
  73. GeneratedConfig:=true;
  74. end;
  75. // Load default verbosity from config
  76. SL:=TStringList.Create;
  77. SL.CommaText:=Defaults.DefaultVerbosity;
  78. for i:=0 to SL.Count-1 do
  79. Include(Verbosity,StringToVerbosity(SL[i]));
  80. SL.Free;
  81. Defaults.CurrentCompilerConfig:=Defaults.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(Defaults.BuildDir);
  91. ForceDirectories(Defaults.PackagesDir);
  92. ForceDirectories(Defaults.CompilerConfigDir);
  93. end;
  94. procedure TMakeTool.LoadCompilerDefaults;
  95. var
  96. S : String;
  97. begin
  98. S:=Defaults.CompilerConfigDir+Defaults.CurrentCompilerConfig;
  99. if FileExists(S) then
  100. begin
  101. Log(vDebug,SLogLoadingCompilerConfig,[S]);
  102. Defaults.LoadCompilerFromFile(S)
  103. end
  104. else
  105. begin
  106. Log(vDebug,SLogGeneratingCompilerConfig,[S]);
  107. Defaults.InitCompilerDefaults;
  108. Defaults.SaveCompilerToFile(S);
  109. end;
  110. end;
  111. procedure TMakeTool.ShowUsage;
  112. begin
  113. Writeln('Usage: ',Paramstr(0),' [options] <action> <package>');
  114. Writeln('Options:');
  115. Writeln(' -c --config Set compiler configuration to use');
  116. Writeln(' -h --help This help');
  117. Writeln(' -v --verbose Set verbosity');
  118. Writeln('Actions:');
  119. Writeln(' update Update packages list');
  120. Writeln(' avail List available packages');
  121. Writeln(' build Build package');
  122. Writeln(' install Install package');
  123. Writeln(' download Download package');
  124. Writeln(' convertmk Convert Makefile.fpc to fpmake.pp');
  125. Halt(0);
  126. end;
  127. Constructor TMakeTool.Create;
  128. begin
  129. inherited Create(nil);
  130. ParaPackages:=TStringList.Create;
  131. ActionStack:=TActionStack.Create;
  132. end;
  133. Destructor TMakeTool.Destroy;
  134. begin
  135. FreeAndNil(ActionStack);
  136. FreeAndNil(ParaPackages);
  137. inherited Destroy;
  138. end;
  139. procedure TMakeTool.ProcessCommandLine;
  140. Function CheckOption(Index : Integer;Short,Long : String): Boolean;
  141. var
  142. O : String;
  143. begin
  144. O:=Paramstr(Index);
  145. Result:=(O='-'+short) or (O='--'+long) or (copy(O,1,Length(Long)+3)=('--'+long+'='));
  146. end;
  147. Function OptionArg(Var Index : Integer) : String;
  148. Var
  149. P : Integer;
  150. begin
  151. if (Length(ParamStr(Index))>1) and (Paramstr(Index)[2]<>'-') then
  152. begin
  153. If Index<ParamCount then
  154. begin
  155. Inc(Index);
  156. Result:=Paramstr(Index);
  157. end
  158. else
  159. Error(SErrNeedArgument,[Index,ParamStr(Index)]);
  160. end
  161. else If length(ParamStr(Index))>2 then
  162. begin
  163. P:=Pos('=',Paramstr(Index));
  164. If (P=0) then
  165. Error(SErrNeedArgument,[Index,ParamStr(Index)])
  166. else
  167. begin
  168. Result:=Paramstr(Index);
  169. Delete(Result,1,P);
  170. end;
  171. end;
  172. end;
  173. Var
  174. I : Integer;
  175. HasAction : Boolean;
  176. begin
  177. I:=0;
  178. HasAction:=false;
  179. // We can't use the TCustomApplication option handling,
  180. // because they cannot handle [general opts] [command] [cmd-opts] [args]
  181. While (I<ParamCount) do
  182. begin
  183. Inc(I);
  184. // Check options.
  185. if CheckOption(I,'c','config') then
  186. Defaults.CurrentCompilerConfig:=OptionArg(I)
  187. else if CheckOption(I,'v','verbose') then
  188. Include(Verbosity,StringToVerbosity(OptionArg(I)))
  189. else if CheckOption(I,'h','help') then
  190. begin
  191. ShowUsage;
  192. halt(0);
  193. end
  194. else if (Length(Paramstr(i))>0) and (Paramstr(I)[1]='-') then
  195. Raise EMakeToolError.CreateFmt(SErrInvalidArgument,[I,ParamStr(i)])
  196. else
  197. // It's a command or target.
  198. begin
  199. if HasAction then
  200. ParaPackages.Add(Paramstr(i))
  201. else
  202. begin
  203. ParaAction:=Paramstr(i);
  204. HasAction:=true;
  205. end;
  206. end;
  207. end;
  208. if not HasAction then
  209. ShowUsage;
  210. end;
  211. procedure TMakeTool.GenerateParaActions;
  212. var
  213. ActionPackage : TFPPackage;
  214. i : integer;
  215. begin
  216. if GetPkgHandler(ParaAction)<>nil then
  217. begin
  218. if ParaPackages.Count=0 then
  219. begin
  220. Log(vDebug,SLogCommandLineAction,['[<currentdir>]',ParaAction]);
  221. ActionStack.Push(nil,ParaAction,[]);
  222. end
  223. else
  224. begin
  225. for i:=0 to ParaPackages.Count-1 do
  226. begin
  227. ActionPackage:=CurrentRepository.PackageByName(ParaPackages[i]);
  228. Log(vDebug,SLogCommandLineAction,['['+ActionPackage.Name+']',ParaAction]);
  229. ActionStack.Push(ActionPackage,ParaAction,[]);
  230. end;
  231. end;
  232. end
  233. else
  234. Raise EMakeToolError.CreateFmt(SErrInvalidCommand,[ParaAction]);
  235. end;
  236. procedure TMakeTool.DoRun;
  237. var
  238. Action : string;
  239. ActionPackage : TFPPackage;
  240. Args : TActionArgs;
  241. OldCurrDir : String;
  242. begin
  243. OldCurrDir:=GetCurrentDir;
  244. LoadGlobalDefaults;
  245. Try
  246. ProcessCommandLine;
  247. MaybeCreateLocalDirs;
  248. LoadCompilerDefaults;
  249. LoadLocalRepository;
  250. GenerateParaActions;
  251. repeat
  252. if not ActionStack.Pop(ActionPackage,Action,Args) then
  253. break;
  254. pkghandler.ExecuteAction(ActionPackage,Action,Args);
  255. until false;
  256. Terminate;
  257. except
  258. On E : Exception do
  259. begin
  260. Writeln(StdErr,SErrRunning);
  261. Writeln(StdErr,E.Message);
  262. Halt(1);
  263. end;
  264. end;
  265. SetCurrentDir(OldCurrDir);
  266. end;
  267. begin
  268. With TMakeTool.Create do
  269. try
  270. run;
  271. finally
  272. Free;
  273. end;
  274. end.