fppkg.pp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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,
  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. FRepository : TFPRepository;
  28. FCompilerConfig : String;
  29. procedure GenerateParaActions;
  30. procedure LoadRepository;
  31. procedure MaybeCreateLocalDirs;
  32. procedure ShowUsage;
  33. Public
  34. Constructor Create;
  35. Destructor Destroy;override;
  36. Function GetConfigFileName : String;
  37. Procedure LoadGlobalDefaults;
  38. Procedure LoadCompilerDefaults;
  39. Procedure ProcessCommandLine;
  40. Procedure DoRun; Override;
  41. end;
  42. EMakeToolError = Class(Exception);
  43. { TMakeTool }
  44. function TMakeTool.GetConfigFileName: String;
  45. var
  46. G : Boolean;
  47. begin
  48. if HasOption('C','config-file') then
  49. Result:=GetOptionValue('C','config-file')
  50. else
  51. begin
  52. {$ifdef unix}
  53. g:=(fpgetuid=0);
  54. {$else}
  55. g:=true;
  56. {$endif}
  57. Result:=GetAppConfigFile(G,False);
  58. end
  59. end;
  60. procedure TMakeTool.LoadGlobalDefaults;
  61. var
  62. SL : TStringList;
  63. i : integer;
  64. cfgfile : String;
  65. GeneratedConfig : boolean;
  66. begin
  67. cfgfile:=GetConfigFileName;
  68. GeneratedConfig:=false;
  69. // Load file or create new default configuration
  70. if FileExists(cfgfile) then
  71. Defaults.LoadGlobalFromFile(cfgfile)
  72. else
  73. begin
  74. ForceDirectories(ExtractFilePath(cfgfile));
  75. Defaults.SaveGlobalToFile(cfgfile);
  76. GeneratedConfig:=true;
  77. end;
  78. // Load default verbosity from config
  79. SL:=TStringList.Create;
  80. SL.CommaText:=Defaults.DefaultVerbosity;
  81. for i:=0 to SL.Count-1 do
  82. Include(Verbosity,StringToVerbosity(SL[i]));
  83. SL.Free;
  84. FCompilerConfig:=Defaults.DefaultCompilerConfig;
  85. // Tracing of what we've done above, need to be done after the verbosity is set
  86. if GeneratedConfig then
  87. Log(vDebug,SLogGeneratingGlobalConfig,[cfgfile])
  88. else
  89. Log(vDebug,SLogLoadingGlobalConfig,[cfgfile])
  90. end;
  91. procedure TMakeTool.MaybeCreateLocalDirs;
  92. begin
  93. ForceDirectories(Defaults.BuildDir);
  94. ForceDirectories(Defaults.PackagesDir);
  95. ForceDirectories(Defaults.CompilerConfigDir);
  96. end;
  97. procedure TMakeTool.LoadCompilerDefaults;
  98. var
  99. S : String;
  100. begin
  101. S:=Defaults.CompilerConfigDir+FCompilerConfig;
  102. if FileExists(S) then
  103. begin
  104. Log(vDebug,SLogLoadingCompilerConfig,[S]);
  105. Defaults.LoadCompilerFromFile(S)
  106. end
  107. else
  108. begin
  109. Log(vDebug,SLogGeneratingCompilerConfig,[S]);
  110. Defaults.InitCompilerDefaults;
  111. Defaults.SaveCompilerToFile(S);
  112. end;
  113. end;
  114. procedure TMakeTool.LoadRepository;
  115. var
  116. S : String;
  117. X : TFPXMLRepositoryHandler;
  118. begin
  119. FRepository:=TFPRepository.Create(Nil);
  120. // Repository
  121. Log(vDebug,SLogLoadingPackagesFile,[Defaults.LocalPackagesFile]);
  122. if FileExists(Defaults.LocalPackagesFile) then
  123. begin
  124. X:=TFPXMLRepositoryHandler.Create;
  125. With X do
  126. try
  127. LoadFromXml(FRepository,Defaults.LocalPackagesFile);
  128. finally
  129. Free;
  130. end;
  131. end;
  132. // Versions
  133. S:=Defaults.LocalVersionsFile(FCompilerConfig);
  134. Log(vDebug,SLogLoadingVersionsFile,[S]);
  135. if FileExists(S) then
  136. FRepository.LoadStatusFromFile(S);
  137. end;
  138. procedure TMakeTool.ShowUsage;
  139. begin
  140. Writeln('Usage: ',Paramstr(0),' [options] <action> <package>');
  141. Writeln('Options:');
  142. Writeln(' -c --config Set compiler configuration to use');
  143. Writeln(' -h --help This help');
  144. Writeln(' -v --verbose Set verbosity');
  145. Writeln('Actions:');
  146. Writeln(' update Update available packages');
  147. Writeln(' listpackages List available packages');
  148. Writeln(' build Build package');
  149. Writeln(' install Install package');
  150. Writeln(' download Download package');
  151. Writeln(' convertmk Convert Makefile.fpc to fpmake.pp');
  152. Halt(0);
  153. end;
  154. Constructor TMakeTool.Create;
  155. begin
  156. inherited Create(nil);
  157. ParaPackages:=TStringList.Create;
  158. ActionStack:=TActionStack.Create;
  159. end;
  160. Destructor TMakeTool.Destroy;
  161. begin
  162. FreeAndNil(ActionStack);
  163. FreeAndNil(ParaPackages);
  164. inherited Destroy;
  165. end;
  166. procedure TMakeTool.ProcessCommandLine;
  167. Function CheckOption(Index : Integer;Short,Long : String): Boolean;
  168. var
  169. O : String;
  170. begin
  171. O:=Paramstr(Index);
  172. Result:=(O='-'+short) or (O='--'+long) or (copy(O,1,Length(Long)+3)=('--'+long+'='));
  173. end;
  174. Function OptionArg(Var Index : Integer) : String;
  175. Var
  176. P : Integer;
  177. begin
  178. if (Length(ParamStr(Index))>1) and (Paramstr(Index)[2]<>'-') then
  179. begin
  180. If Index<ParamCount then
  181. begin
  182. Inc(Index);
  183. Result:=Paramstr(Index);
  184. end
  185. else
  186. Error(SErrNeedArgument,[Index,ParamStr(Index)]);
  187. end
  188. else If length(ParamStr(Index))>2 then
  189. begin
  190. P:=Pos('=',Paramstr(Index));
  191. If (P=0) then
  192. Error(SErrNeedArgument,[Index,ParamStr(Index)])
  193. else
  194. begin
  195. Result:=Paramstr(Index);
  196. Delete(Result,1,P);
  197. end;
  198. end;
  199. end;
  200. Var
  201. I : Integer;
  202. HasAction : Boolean;
  203. begin
  204. I:=0;
  205. HasAction:=false;
  206. // We can't use the TCustomApplication option handling,
  207. // because they cannot handle [general opts] [command] [cmd-opts] [args]
  208. While (I<ParamCount) do
  209. begin
  210. Inc(I);
  211. // Check options.
  212. if CheckOption(I,'c','config') then
  213. FCompilerConfig:=OptionArg(I)
  214. else if CheckOption(I,'v','verbose') then
  215. Include(Verbosity,StringToVerbosity(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. Raise EMakeToolError.CreateFmt(SErrInvalidArgument,[I,ParamStr(i)])
  223. else
  224. // It's a command or target.
  225. begin
  226. if HasAction then
  227. ParaPackages.Add(Paramstr(i))
  228. else
  229. begin
  230. ParaAction:=Paramstr(i);
  231. HasAction:=true;
  232. end;
  233. end;
  234. end;
  235. if not HasAction then
  236. ShowUsage;
  237. end;
  238. procedure TMakeTool.GenerateParaActions;
  239. var
  240. ActionPackage : TFPPackage;
  241. i : integer;
  242. begin
  243. if GetPkgHandler(ParaAction)<>nil then
  244. begin
  245. if ParaPackages.Count=0 then
  246. begin
  247. Log(vDebug,SLogCommandLineAction,['[<currentdir>]',ParaAction]);
  248. ActionStack.Push(nil,ParaAction,[]);
  249. end
  250. else
  251. begin
  252. for i:=0 to ParaPackages.Count-1 do
  253. begin
  254. ActionPackage:=FRepository.PackageByName(ParaPackages[i]);
  255. Log(vDebug,SLogCommandLineAction,['['+ActionPackage.Name+']',ParaAction]);
  256. ActionStack.Push(ActionPackage,ParaAction,[]);
  257. end;
  258. end;
  259. end
  260. else
  261. Raise EMakeToolError.CreateFmt(SErrInvalidCommand,[ParaAction]);
  262. end;
  263. procedure TMakeTool.DoRun;
  264. var
  265. Action : string;
  266. ActionPackage : TFPPackage;
  267. Args : TActionArgs;
  268. OldCurrDir : String;
  269. begin
  270. LoadGlobalDefaults;
  271. OldCurrDir:=GetCurrentDir;
  272. Try
  273. ProcessCommandLine;
  274. MaybeCreateLocalDirs;
  275. LoadCompilerDefaults;
  276. LoadRepository;
  277. GenerateParaActions;
  278. repeat
  279. if not ActionStack.Pop(ActionPackage,Action,Args) then
  280. break;
  281. pkghandler.ExecuteAction(ActionPackage,Action,Args);
  282. until false;
  283. Terminate;
  284. except
  285. On E : Exception do
  286. begin
  287. Writeln(StdErr,SErrRunning);
  288. Writeln(StdErr,E.Message);
  289. Halt(1);
  290. end;
  291. end;
  292. SetCurrentDir(OldCurrDir);
  293. end;
  294. begin
  295. With TMakeTool.Create do
  296. try
  297. run;
  298. finally
  299. Free;
  300. end;
  301. end.