pkgoptions.pp 13 KB

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