pkgoptions.pp 15 KB

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