pkgropts.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. {
  2. This file is part of the Free Pascal Utilities
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {$mode objfpc}
  11. {$h+}
  12. unit pkgropts;
  13. interface
  14. uses Classes, Sysutils, Inifiles, fpmktype;
  15. Type
  16. { TPackagerOptions }
  17. TPackagerOptions = Class(TPersistent)
  18. private
  19. FDirty: Boolean;
  20. // Global options
  21. FRemoteMirrorsLocation : String;
  22. FLocalMirrorsLocation : String;
  23. FRemoteRepository : String;
  24. FLocalRepository : String;
  25. FCompilerConfigDir,
  26. FPackagesDir,
  27. FBuildDir,
  28. FLocalDir : String;
  29. FDefaultVerbosity,
  30. FDefaultCompilerConfig : String;
  31. // Compiler specific options
  32. FCompiler : String;
  33. FCompilerCPU: TCPU;
  34. FCompilerOS: TOS;
  35. FCompilerVersion : String;
  36. FInstallDir : String;
  37. function GetOptString(Index: integer): String;
  38. procedure SetOptString(Index: integer; const AValue: String);
  39. procedure SetCompilerCPU(const AValue: TCPU);
  40. procedure SetCompilerOS(const AValue: TOS);
  41. protected
  42. Property LocalDir : String Read FLocalDir;
  43. Public
  44. Constructor Create;
  45. Procedure InitGlobalDefaults;
  46. Procedure LoadGlobalFromIni(Ini : TCustomIniFile); virtual;
  47. Procedure SaveGlobalToIni(Ini : TCustomIniFile); virtual;
  48. Procedure LoadGlobalFromFile(FileName : String);
  49. Procedure SaveGlobalToFile(FileName : String);
  50. Procedure InitCompilerDefaults;
  51. Procedure LoadCompilerFromIni(Ini : TCustomIniFile); virtual;
  52. Procedure SaveCompilerToIni(Ini : TCustomIniFile); virtual;
  53. Procedure LoadCompilerFromFile(FileName : String);
  54. Procedure SaveCompilerToFile(FileName : String);
  55. Property Dirty : Boolean Read FDirty;
  56. function LocalVersions(CompilerConfig:String):string;
  57. Published
  58. Property RemoteMirrorsLocation : String Index 0 Read GetOptString Write SetOptString;
  59. Property LocalMirrorsLocation : String Index 1 Read GetOptString Write SetOptString;
  60. Property RemoteRepository : String Index 2 Read GetOptString Write SetOptString;
  61. Property LocalRepository : String Index 3 Read GetOptString Write SetOptString;
  62. Property BuildDir : String Index 5 Read GetOptString Write SetOptString;
  63. Property Compiler : String Index 6 Read GetOptString Write SetOptString;
  64. Property CompilerTarget : String Index 7 Read GetOptString Write SetOptString;
  65. Property DefaultCompilerConfig : String Index 8 Read GetOptString Write SetOptString;
  66. Property CompilerVersion : String Index 9 Read GetOptString Write SetOptString;
  67. Property InstallDir : String Index 10 Read GetOptString Write SetOptString;
  68. Property DefaultVerbosity : String Index 11 Read GetOptString Write SetOptString;
  69. Property PackagesDir : String Index 12 Read GetOptString Write SetOptString;
  70. Property CompilerConfigDir : String Index 13 Read GetOptString Write SetOptString;
  71. Property CompilerOS : TOS Read FCompilerOS Write SetCompilerOS;
  72. Property CompilerCPU : TCPU Read FCompilerCPU Write SetCompilerCPU;
  73. end;
  74. Const
  75. DefaultCPU = {$I %FPCTARGETCPU%};
  76. DefaultOS = {$I %FPCTARGETOS%};
  77. DefaultMirrorsLocation = 'http://www.freepascal.org/repository/mirrors.xml';
  78. DefaultCompiler = 'fpc';
  79. DefaultRemoteRepository = 'fpc';
  80. DefaultUnixPrefix = '/usr/local/lib/fpc/fppkg';
  81. DefaultUnixBuildDir = '/tmp/fppkg/';
  82. DefaultMirrors = 'mirrors.xml';
  83. DefaultRepository = 'packages.xml';
  84. DefaultVersions = 'versions-%s.dat';
  85. // ini file keys
  86. SDefaults = 'Defaults';
  87. // Global config
  88. KeyLocalMirrorsLocation = 'LocalMirrors';
  89. KeyRemoteMirrorsLocation = 'RemoteMirrors';
  90. KeyRemoteRepository = 'RemoteRepository';
  91. KeyLocalRepository = 'LocalRepository';
  92. KeyDefaultConfig = 'DefaultCompilerConfig';
  93. KeyCompilerConfigDir = 'CompilerConfigDir';
  94. KeyPackagesDir = 'PackagesDir';
  95. KeyBuildDir = 'BuildDir';
  96. KeyVerbosity = 'Verbosity';
  97. // Compiler dependent config
  98. KeyInstallDir = 'InstallDir';
  99. KeyCompiler = 'Compiler' ;
  100. KeyCompilerOS = 'OS';
  101. KeyCompilerCPU = 'CPU';
  102. KeyCompilerVersion = 'Version';
  103. Implementation
  104. uses
  105. {$ifdef unix}
  106. baseunix,
  107. {$endif}
  108. pkghandler,
  109. pkgmessages;
  110. Function FixPath(S : String) : string;
  111. begin
  112. If (S<>'') then
  113. Result:=IncludeTrailingPathDelimiter(S)
  114. else
  115. Result:='';
  116. end;
  117. { TPackagerOptions }
  118. constructor TPackagerOptions.Create;
  119. begin
  120. InitGlobalDefaults;
  121. end;
  122. function TPackagerOptions.GetOptString(Index: integer): String;
  123. begin
  124. Case Index of
  125. 0 : Result:=FRemoteMirrorsLocation;
  126. 1 : Result:=FLocalMirrorsLocation;
  127. 2 : Result:=FRemoteRepository;
  128. 3 : Result:=FLocalRepository;
  129. 5 : Result:=FBuildDir;
  130. 6 : Result:=FCompiler;
  131. 7 : Result:=MakeTargetString(CompilerCPU,CompilerOS);
  132. 8 : Result:=FDefaultCompilerConfig;
  133. 9 : Result:=FCompilerVersion;
  134. 10 : Result:=FInstallDir;
  135. 11 : Result:=FDefaultVerbosity;
  136. 12 : Result:=FPackagesDir;
  137. 13 : Result:=FCompilerConfigDir;
  138. end;
  139. end;
  140. procedure TPackagerOptions.SetOptString(Index: integer; const AValue: String);
  141. begin
  142. If AValue=GetOptString(Index) then
  143. Exit;
  144. Case Index of
  145. 0 : FLocalMirrorsLocation:=AValue;
  146. 1 : FRemoteMirrorsLocation:=AValue;
  147. 2 : FRemoteRepository:=AValue;
  148. 3 : FLocalRepository:=AValue;
  149. 5 : FBuildDir:=FixPath(AValue);
  150. 6 : FCompiler:=AValue;
  151. 7 : StringToCPUOS(AValue,FCompilerCPU,FCompilerOS);
  152. 8 : FDefaultCompilerConfig:=AValue;
  153. 9 : FCompilerVersion:=AValue;
  154. 10 : FInstallDir:=FixPath(AValue);
  155. 11 : FDefaultVerbosity:=AValue;
  156. 12 : FPackagesDir:=FixPath(AValue);
  157. 13 : FCompilerConfigDir:=FixPath(AValue);
  158. end;
  159. FDirty:=True;
  160. end;
  161. procedure TPackagerOptions.SetCompilerCPU(const AValue: TCPU);
  162. begin
  163. if FCompilerCPU=AValue then
  164. exit;
  165. FCompilerCPU:=AValue;
  166. FDirty:=True;
  167. end;
  168. procedure TPackagerOptions.SetCompilerOS(const AValue: TOS);
  169. begin
  170. if FCompilerOS=AValue then
  171. exit;
  172. FCompilerOS:=AValue;
  173. FDirty:=True;
  174. end;
  175. function TPackagerOptions.LocalVersions(CompilerConfig:String):string;
  176. begin
  177. Result:=ExtractFilePath(FLocalRepository)+Format(DefaultVersions,[CompilerConfig]);
  178. end;
  179. Procedure TPackagerOptions.InitGlobalDefaults;
  180. begin
  181. FRemoteMirrorsLocation:=DefaultMirrorsLocation;
  182. FRemoteRepository:=DefaultRemoteRepository;
  183. {$ifdef unix}
  184. if (fpGetUID=0) then
  185. FLocalDir:=DefaultUnixPrefix
  186. else
  187. FLocalDir:=IncludeTrailingPathDelimiter(GetEnvironmentVariable('HOME'))+'.fppkg/';
  188. {$else}
  189. // Change as needed on all OS-es...
  190. FLocalDir:=ExtractFilePath(Paramstr(0))+'fppkg'+PathDelim;
  191. {$endif}
  192. FBuildDir:=FLocalDir+'build'+PathDelim;
  193. FPackagesDir:=FLocalDir+'packages'+PathDelim;
  194. FCompilerConfigDir:=FLocalDir+'config'+PathDelim;
  195. FLocalMirrorsLocation:=FLocalDir+DefaultMirrors;
  196. FLocalRepository:=FLocalDir+DefaultRepository;
  197. FDefaultCompilerConfig:='default';
  198. FDefaultVerbosity:='error,info,debug,commands';
  199. end;
  200. Procedure TPackagerOptions.InitCompilerDefaults;
  201. begin
  202. FCompiler:=FileSearch(DefaultCompiler+ExeExt,GetEnvironmentVariable('PATH'));
  203. if FCompiler='' then
  204. Raise EPackageHandler.Create(SErrMissingFPC);
  205. {$warning TODO detect compiler version/target from -i options }
  206. FCompilerVersion:='2.0.4';
  207. FCompilerCPU:=StringToCPU(DefaultCPU);
  208. FCompilerOS:=StringToOS(DefaultOS);
  209. // Use the same algorithm as the compiler, see options.pas
  210. {$ifdef Unix}
  211. FInstallDir:=FixPath(GetEnvironmentVariable('FPCDIR'));
  212. if FInstallDir='' then
  213. begin
  214. if DirectoryExists('/usr/local/lib/fpc/'+FCompilerVersion) then
  215. FInstallDir:='/usr/local/lib/fpc/'+FCompilerVersion+'/'
  216. else
  217. FInstallDir:='/usr/lib/fpc/'+FCompilerVersion+'/';
  218. end;
  219. {$else unix}
  220. FInstallDir:=FixPath(GetEnvironmentVariable('FPCDIR'));
  221. if FInstallDir='' then
  222. begin
  223. FInstallDir:=ExtractFilePath(FCompiler)+'../';
  224. if not(DirectoryExists(FInstallDir+'/units')) and
  225. not(DirectoryExists(FInstallDir+'/rtl')) then
  226. FInstallDir:=FInstallDir+'../';
  227. end;
  228. {$endif unix}
  229. end;
  230. procedure TPackagerOptions.LoadGlobalFromIni(Ini: TCustomIniFile);
  231. begin
  232. With Ini do
  233. begin
  234. FLocalMirrorsLocation:=ReadString(SDefaults,KeyLocalMirrorsLocation,FLocalMirrorsLocation);
  235. FRemoteMirrorsLocation:=ReadString(SDefaults,KeyRemoteMirrorsLocation,FRemoteMirrorsLocation);
  236. FRemoteRepository:=ReadString(SDefaults,KeyRemoteRepository,FRemoteRepository);
  237. FLocalRepository:=ReadString(SDefaults,KeyLocalRepository,FLocalRepository);
  238. FBuildDir:=FixPath(ReadString(SDefaults,KeyBuildDir,FBuildDir));
  239. FDefaultCompilerConfig:=ReadString(SDefaults,KeyDefaultConfig,FDefaultCompilerConfig);
  240. end;
  241. end;
  242. procedure TPackagerOptions.SaveGlobalToIni(Ini: TCustomIniFile);
  243. begin
  244. With Ini do
  245. begin
  246. WriteString(SDefaults,KeyLocalMirrorsLocation,FLocalMirrorsLocation);
  247. WriteString(SDefaults,KeyRemoteMirrorsLocation,FRemoteMirrorsLocation);
  248. WriteString(SDefaults,KeyRemoteRepository,FRemoteRepository);
  249. WriteString(SDefaults,KeyLocalRepository,FLocalRepository);
  250. WriteString(SDefaults,KeyBuildDir,FBuildDir);
  251. WriteString(SDefaults,KeyDefaultConfig,FDefaultCompilerConfig);
  252. end;
  253. end;
  254. procedure TPackagerOptions.LoadGlobalFromFile(FileName: String);
  255. Var
  256. Ini : TMemIniFile;
  257. begin
  258. Ini:=TMemIniFile.Create(FileName);
  259. try
  260. LoadGlobalFromIni(Ini);
  261. finally
  262. Ini.Free;
  263. end;
  264. end;
  265. procedure TPackagerOptions.SaveGlobalToFile(FileName: String);
  266. Var
  267. Ini : TIniFile;
  268. begin
  269. Ini:=TIniFile.Create(FileName);
  270. try
  271. SaveGlobalToIni(Ini);
  272. Ini.UpdateFile;
  273. finally
  274. Ini.Free;
  275. end;
  276. end;
  277. procedure TPackagerOptions.LoadCompilerFromIni(Ini: TCustomIniFile);
  278. begin
  279. With Ini do
  280. begin
  281. FInstallDir:=FixPath(ReadString(SDefaults,KeyInstallDir,FInstallDir));
  282. FCompiler:=ReadString(SDefaults,KeyCompiler,FCompiler);
  283. FCompilerOS:=StringToOS(ReadString(SDefaults,KeyCompilerOS,OSToString(CompilerOS)));
  284. FCompilerCPU:=StringToCPU(ReadString(SDefaults,KeyCompilerCPU,CPUtoString(CompilerCPU)));
  285. FCompilerVersion:=ReadString(SDefaults,KeyCompilerVersion,FCompilerVersion);
  286. end;
  287. end;
  288. procedure TPackagerOptions.SaveCompilerToIni(Ini: TCustomIniFile);
  289. begin
  290. With Ini do
  291. begin
  292. WriteString(SDefaults,KeyInstallDir,FInstallDir);
  293. WriteString(SDefaults,KeyCompiler,FCompiler);
  294. WriteString(SDefaults,KeyCompilerOS,OSToString(CompilerOS));
  295. WriteString(SDefaults,KeyCompilerCPU,CPUtoString(CompilerCPU));
  296. WriteString(SDefaults,KeyCompilerVersion,FCompilerVersion);
  297. end;
  298. end;
  299. procedure TPackagerOptions.LoadCompilerFromFile(FileName: String);
  300. Var
  301. Ini : TMemIniFile;
  302. begin
  303. Ini:=TMemIniFile.Create(FileName);
  304. try
  305. LoadCompilerFromIni(Ini);
  306. finally
  307. Ini.Free;
  308. end;
  309. end;
  310. procedure TPackagerOptions.SaveCompilerToFile(FileName: String);
  311. Var
  312. Ini : TIniFile;
  313. begin
  314. Ini:=TIniFile.Create(FileName);
  315. try
  316. SaveCompilerToIni(Ini);
  317. Ini.UpdateFile;
  318. finally
  319. Ini.Free;
  320. end;
  321. end;
  322. end.