pkgoptions.pp 17 KB

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