pkgoptions.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 pkgoptions;
  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 : String;
  28. FDefaultVerbosity,
  29. FDefaultCompilerConfig : String;
  30. // Compiler specific options
  31. FCompiler : String;
  32. FCompilerCPU: TCPU;
  33. FCompilerOS: TOS;
  34. FCompilerVersion : String;
  35. FInstallDir : String;
  36. function GetOptString(Index: integer): String;
  37. procedure SetOptString(Index: integer; const AValue: String);
  38. procedure SetCompilerCPU(const AValue: TCPU);
  39. procedure SetCompilerOS(const AValue: TOS);
  40. Public
  41. Constructor Create;
  42. Procedure InitGlobalDefaults;
  43. Procedure LoadGlobalFromIni(Ini : TCustomIniFile); virtual;
  44. Procedure SaveGlobalToIni(Ini : TCustomIniFile); virtual;
  45. Procedure LoadGlobalFromFile(FileName : String);
  46. Procedure SaveGlobalToFile(FileName : String);
  47. Procedure InitCompilerDefaults;
  48. Procedure LoadCompilerFromIni(Ini : TCustomIniFile); virtual;
  49. Procedure SaveCompilerToIni(Ini : TCustomIniFile); virtual;
  50. Procedure LoadCompilerFromFile(FileName : String);
  51. Procedure SaveCompilerToFile(FileName : String);
  52. Property Dirty : Boolean Read FDirty;
  53. function RemotePackagesFile:string;
  54. function LocalPackagesFile:string;
  55. function LocalVersionsFile(CompilerConfig:String):string;
  56. Published
  57. Property RemoteMirrorsLocation : String Index 0 Read GetOptString Write SetOptString;
  58. Property LocalMirrorsLocation : String Index 1 Read GetOptString Write SetOptString;
  59. Property RemoteRepository : String Index 2 Read GetOptString Write SetOptString;
  60. Property LocalRepository : String Index 3 Read GetOptString Write SetOptString;
  61. Property BuildDir : String Index 5 Read GetOptString Write SetOptString;
  62. Property Compiler : String Index 6 Read GetOptString Write SetOptString;
  63. Property CompilerTarget : String Index 7 Read GetOptString Write SetOptString;
  64. Property DefaultCompilerConfig : String Index 8 Read GetOptString Write SetOptString;
  65. Property CompilerVersion : String Index 9 Read GetOptString Write SetOptString;
  66. Property InstallDir : String Index 10 Read GetOptString Write SetOptString;
  67. Property DefaultVerbosity : String Index 11 Read GetOptString Write SetOptString;
  68. Property PackagesDir : String Index 12 Read GetOptString Write SetOptString;
  69. Property CompilerConfigDir : String Index 13 Read GetOptString Write SetOptString;
  70. Property CompilerOS : TOS Read FCompilerOS Write SetCompilerOS;
  71. Property CompilerCPU : TCPU Read FCompilerCPU Write SetCompilerCPU;
  72. end;
  73. var
  74. Defaults : TPackagerOptions;
  75. Implementation
  76. uses
  77. {$ifdef unix}
  78. baseunix,
  79. {$endif}
  80. pkgglobals,
  81. pkgmessages;
  82. Const
  83. DefaultMirrorFile = 'mirrors.xml';
  84. DefaultPackagesFile = 'packages.xml';
  85. DefaultVersionsFile = 'versions-%s.dat';
  86. DefaultMirrorsLocation = 'http://www.freepascal.org/repository/'+DefaultMirrorFile;
  87. {$warning TODO use real repository}
  88. {$ifdef unix}
  89. DefaultRemoteRepository = 'file://'+{$I %HOME%}+'/repository/';
  90. {$else}
  91. DefaultRemoteRepository = 'c:/repository/';
  92. {$endif}
  93. // ini file keys
  94. SDefaults = 'Defaults';
  95. // Global config
  96. KeyLocalMirrorsLocation = 'LocalMirrors';
  97. KeyRemoteMirrorsLocation = 'RemoteMirrors';
  98. KeyRemoteRepository = 'RemoteRepository';
  99. KeyLocalRepository = 'LocalRepository';
  100. KeyCompilerConfigDir = 'CompilerConfigDir';
  101. KeyPackagesDir = 'PackagesDir';
  102. KeyBuildDir = 'BuildDir';
  103. KeyCompilerConfig = 'CompilerConfig';
  104. KeyVerbosity = 'Verbosity';
  105. // Compiler dependent config
  106. KeyInstallDir = 'InstallDir';
  107. KeyCompiler = 'Compiler' ;
  108. KeyCompilerOS = 'OS';
  109. KeyCompilerCPU = 'CPU';
  110. KeyCompilerVersion = 'Version';
  111. { TPackagerOptions }
  112. constructor TPackagerOptions.Create;
  113. begin
  114. InitGlobalDefaults;
  115. end;
  116. function TPackagerOptions.GetOptString(Index: integer): String;
  117. begin
  118. Case Index of
  119. 0 : Result:=FRemoteMirrorsLocation;
  120. 1 : Result:=FLocalMirrorsLocation;
  121. 2 : Result:=FRemoteRepository;
  122. 3 : Result:=FLocalRepository;
  123. 5 : Result:=FBuildDir;
  124. 6 : Result:=FCompiler;
  125. 7 : Result:=MakeTargetString(CompilerCPU,CompilerOS);
  126. 8 : Result:=FDefaultCompilerConfig;
  127. 9 : Result:=FCompilerVersion;
  128. 10 : Result:=FInstallDir;
  129. 11 : Result:=FDefaultVerbosity;
  130. 12 : Result:=FPackagesDir;
  131. 13 : Result:=FCompilerConfigDir;
  132. end;
  133. end;
  134. procedure TPackagerOptions.SetOptString(Index: integer; const AValue: String);
  135. begin
  136. If AValue=GetOptString(Index) then
  137. Exit;
  138. Case Index of
  139. 0 : FLocalMirrorsLocation:=AValue;
  140. 1 : FRemoteMirrorsLocation:=AValue;
  141. 2 : FRemoteRepository:=AValue;
  142. 3 : FLocalRepository:=AValue;
  143. 5 : FBuildDir:=FixPath(AValue);
  144. 6 : FCompiler:=AValue;
  145. 7 : StringToCPUOS(AValue,FCompilerCPU,FCompilerOS);
  146. 8 : FDefaultCompilerConfig:=AValue;
  147. 9 : FCompilerVersion:=AValue;
  148. 10 : FInstallDir:=FixPath(AValue);
  149. 11 : FDefaultVerbosity:=AValue;
  150. 12 : FPackagesDir:=FixPath(AValue);
  151. 13 : FCompilerConfigDir:=FixPath(AValue);
  152. end;
  153. FDirty:=True;
  154. end;
  155. procedure TPackagerOptions.SetCompilerCPU(const AValue: TCPU);
  156. begin
  157. if FCompilerCPU=AValue then
  158. exit;
  159. FCompilerCPU:=AValue;
  160. FDirty:=True;
  161. end;
  162. procedure TPackagerOptions.SetCompilerOS(const AValue: TOS);
  163. begin
  164. if FCompilerOS=AValue then
  165. exit;
  166. FCompilerOS:=AValue;
  167. FDirty:=True;
  168. end;
  169. function TPackagerOptions.RemotePackagesFile:string;
  170. begin
  171. Result:=FRemoteRepository+DefaultPackagesFile;
  172. end;
  173. function TPackagerOptions.LocalPackagesFile:string;
  174. begin
  175. Result:=FLocalRepository+DefaultPackagesFile;
  176. end;
  177. function TPackagerOptions.LocalVersionsFile(CompilerConfig:String):string;
  178. begin
  179. Result:=FLocalRepository+Format(DefaultVersionsFile,[CompilerConfig]);
  180. end;
  181. Procedure TPackagerOptions.InitGlobalDefaults;
  182. var
  183. LocalDir : String;
  184. begin
  185. // Retrieve Local fppkg directory
  186. {$ifdef unix}
  187. if (fpGetUID=0) then
  188. begin
  189. if DirectoryExists('/usr/local/lib/fpc') then
  190. LocalDir:='/usr/local/lib/fpc/fppkg/'
  191. else
  192. LocalDir:='/usr/lib/fpc/fppkg/';
  193. end
  194. else
  195. LocalDir:=IncludeTrailingPathDelimiter(GetEnvironmentVariable('HOME'))+'.fppkg/';
  196. {$else}
  197. // Change as needed on all OS-es...
  198. LocalDir:=ExtractFilePath(Paramstr(0))+'fppkg'+PathDelim;
  199. {$endif}
  200. // Directories
  201. FBuildDir:=LocalDir+'build'+PathDelim;
  202. FPackagesDir:=LocalDir+'packages'+PathDelim;
  203. FCompilerConfigDir:=LocalDir+'config'+PathDelim;
  204. FLocalMirrorsLocation:=LocalDir+DefaultMirrorFile;
  205. FLocalRepository:=LocalDir;
  206. // Remote
  207. FRemoteMirrorsLocation:=DefaultMirrorsLocation;
  208. FRemoteRepository:=DefaultRemoteRepository;
  209. // Other config
  210. FDefaultCompilerConfig:='default';
  211. FDefaultVerbosity:='error,info,debug,commands';
  212. end;
  213. Procedure TPackagerOptions.InitCompilerDefaults;
  214. begin
  215. FCompiler:=FileSearch('fpc'+ExeExt,GetEnvironmentVariable('PATH'));
  216. if FCompiler='' then
  217. Raise EPackagerError.Create(SErrMissingFPC);
  218. {$warning TODO detect compiler version/target from -i options }
  219. FCompilerVersion:='2.0.4';
  220. FCompilerCPU:=StringToCPU({$I %FPCTARGETCPU%});
  221. FCompilerOS:=StringToOS({$I %FPCTARGETOS%});
  222. // Use the same algorithm as the compiler, see options.pas
  223. {$ifdef Unix}
  224. FInstallDir:=FixPath(GetEnvironmentVariable('FPCDIR'));
  225. if FInstallDir='' then
  226. begin
  227. if DirectoryExists('/usr/local/lib/fpc/'+FCompilerVersion) then
  228. FInstallDir:='/usr/local/lib/fpc/'+FCompilerVersion+'/'
  229. else
  230. FInstallDir:='/usr/lib/fpc/'+FCompilerVersion+'/';
  231. end;
  232. {$else unix}
  233. FInstallDir:=FixPath(GetEnvironmentVariable('FPCDIR'));
  234. if FInstallDir='' then
  235. begin
  236. FInstallDir:=ExtractFilePath(FCompiler)+'../';
  237. if not(DirectoryExists(FInstallDir+'/units')) and
  238. not(DirectoryExists(FInstallDir+'/rtl')) then
  239. FInstallDir:=FInstallDir+'../';
  240. end;
  241. {$endif unix}
  242. end;
  243. procedure TPackagerOptions.LoadGlobalFromIni(Ini: TCustomIniFile);
  244. begin
  245. With Ini do
  246. begin
  247. FLocalMirrorsLocation:=ReadString(SDefaults,KeyLocalMirrorsLocation,FLocalMirrorsLocation);
  248. FRemoteMirrorsLocation:=ReadString(SDefaults,KeyRemoteMirrorsLocation,FRemoteMirrorsLocation);
  249. FRemoteRepository:=ReadString(SDefaults,KeyRemoteRepository,FRemoteRepository);
  250. FLocalRepository:=ReadString(SDefaults,KeyLocalRepository,FLocalRepository);
  251. FBuildDir:=FixPath(ReadString(SDefaults,KeyBuildDir,FBuildDir));
  252. FPackagesDir:=FixPath(ReadString(SDefaults,KeyPackagesDir,FPackagesDir));
  253. FCompilerConfigDir:=FixPath(ReadString(SDefaults,KeyCompilerConfigDir,FCompilerConfigDir));
  254. FDefaultCompilerConfig:=ReadString(SDefaults,KeyCompilerConfig,FDefaultCompilerConfig);
  255. FDefaultVerbosity:=ReadString(SDefaults,KeyVerbosity,FDefaultVerbosity);
  256. end;
  257. end;
  258. procedure TPackagerOptions.SaveGlobalToIni(Ini: TCustomIniFile);
  259. begin
  260. With Ini do
  261. begin
  262. WriteString(SDefaults,KeyBuildDir,FBuildDir);
  263. WriteString(SDefaults,KeyPackagesDir,FPackagesDir);
  264. WriteString(SDefaults,KeyCompilerConfigDir,FCompilerConfigDir);
  265. WriteString(SDefaults,KeyLocalRepository,FLocalRepository);
  266. WriteString(SDefaults,KeyLocalMirrorsLocation,FLocalMirrorsLocation);
  267. WriteString(SDefaults,KeyRemoteMirrorsLocation,FRemoteMirrorsLocation);
  268. WriteString(SDefaults,KeyRemoteRepository,FRemoteRepository);
  269. WriteString(SDefaults,KeyCompilerConfig,FDefaultCompilerConfig);
  270. WriteString(SDefaults,KeyVerbosity,FDefaultVerbosity);
  271. end;
  272. end;
  273. procedure TPackagerOptions.LoadGlobalFromFile(FileName: String);
  274. Var
  275. Ini : TMemIniFile;
  276. begin
  277. Ini:=TMemIniFile.Create(FileName);
  278. try
  279. LoadGlobalFromIni(Ini);
  280. finally
  281. Ini.Free;
  282. end;
  283. end;
  284. procedure TPackagerOptions.SaveGlobalToFile(FileName: String);
  285. Var
  286. Ini : TIniFile;
  287. begin
  288. Ini:=TIniFile.Create(FileName);
  289. try
  290. SaveGlobalToIni(Ini);
  291. Ini.UpdateFile;
  292. finally
  293. Ini.Free;
  294. end;
  295. end;
  296. procedure TPackagerOptions.LoadCompilerFromIni(Ini: TCustomIniFile);
  297. begin
  298. With Ini do
  299. begin
  300. FInstallDir:=FixPath(ReadString(SDefaults,KeyInstallDir,FInstallDir));
  301. FCompiler:=ReadString(SDefaults,KeyCompiler,FCompiler);
  302. FCompilerOS:=StringToOS(ReadString(SDefaults,KeyCompilerOS,OSToString(CompilerOS)));
  303. FCompilerCPU:=StringToCPU(ReadString(SDefaults,KeyCompilerCPU,CPUtoString(CompilerCPU)));
  304. FCompilerVersion:=ReadString(SDefaults,KeyCompilerVersion,FCompilerVersion);
  305. end;
  306. end;
  307. procedure TPackagerOptions.SaveCompilerToIni(Ini: TCustomIniFile);
  308. begin
  309. With Ini do
  310. begin
  311. WriteString(SDefaults,KeyInstallDir,FInstallDir);
  312. WriteString(SDefaults,KeyCompiler,FCompiler);
  313. WriteString(SDefaults,KeyCompilerOS,OSToString(CompilerOS));
  314. WriteString(SDefaults,KeyCompilerCPU,CPUtoString(CompilerCPU));
  315. WriteString(SDefaults,KeyCompilerVersion,FCompilerVersion);
  316. end;
  317. end;
  318. procedure TPackagerOptions.LoadCompilerFromFile(FileName: String);
  319. Var
  320. Ini : TMemIniFile;
  321. begin
  322. Ini:=TMemIniFile.Create(FileName);
  323. try
  324. LoadCompilerFromIni(Ini);
  325. finally
  326. Ini.Free;
  327. end;
  328. end;
  329. procedure TPackagerOptions.SaveCompilerToFile(FileName: String);
  330. Var
  331. Ini : TIniFile;
  332. begin
  333. Ini:=TIniFile.Create(FileName);
  334. try
  335. SaveCompilerToIni(Ini);
  336. Ini.UpdateFile;
  337. finally
  338. Ini.Free;
  339. end;
  340. end;
  341. initialization
  342. Defaults:=TPackagerOptions.Create;
  343. finalization
  344. FreeAndNil(Defaults);
  345. end.