pkgoptions.pp 14 KB

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