fpcfgs.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Integrated Development Environment
  4. Copyright (c) 1998 by Berczi Gabor
  5. Options/config routines for the IDE
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit FPCfgs;
  13. interface
  14. uses
  15. Objects,
  16. Systems;
  17. const
  18. MinStackSize = 1024;
  19. MaxStackSize = 67107840;
  20. MinHeapSize = 1024;
  21. MaxHeapSize = 67107840;
  22. type
  23. TOptionMode = (om_Normal,om_Debug,om_Release);
  24. TOptionToggle = array[TOptionMode] of boolean;
  25. TOptionString = array[TOptionMode] of string;
  26. TOptionValue = array[TOptionMode] of longint;
  27. POptionItem = ^TOptionItem;
  28. TOptionItem = record
  29. Name : string[30];
  30. Param : string[10];
  31. IsSet : TOptionToggle;
  32. end;
  33. POptionItemCollection = ^TOptionItemCollection;
  34. TOptionItemCollection = object(TCollection)
  35. function At(Index: Integer): POptionItem;
  36. procedure FreeItem(Item: Pointer); virtual;
  37. end;
  38. POptions = ^TOptions;
  39. TOptions = object
  40. constructor InitToggle(ch:char);
  41. constructor InitSel(ch:char);
  42. destructor Done;
  43. { items }
  44. procedure AddItem(const name,param:string);
  45. function ItemCount:integer;
  46. function ItemName(index:integer):string;
  47. function ItemParam(index:integer):string;
  48. function ItemIsSet(index:integer):boolean;
  49. procedure ItemSet(index:integer;b:boolean);
  50. function GetCurrSel:integer;
  51. procedure SetCurrSel(index:integer);
  52. { read / write to cfgfile which must be open }
  53. procedure WriteItemsCfg;
  54. function ReadItemsCfg(const s:string):boolean;
  55. private
  56. IsSel : boolean;
  57. Prefix : char;
  58. SelNr : integer;
  59. Items : POptionItemCollection;
  60. end;
  61. PDirectories = ^TDirectories;
  62. TDirectories = record
  63. LibraryDirs,
  64. IncludeDirs,
  65. UnitDirs,
  66. ObjectDirs : TOptionString;
  67. end;
  68. const
  69. OptionMode : TOptionMode = om_Normal;
  70. var
  71. SyntaxOptions,
  72. VerboseOptions,
  73. CodegenOptions,
  74. OptimizationOptions,
  75. ProcessorOptions,
  76. AsmReaderOptions,
  77. TargetOptions : POptions;
  78. Dirs : PDirectories;
  79. CondDefs : TOptionString;
  80. { other settings }
  81. function GetConditionalDefines: string;
  82. procedure SetConditionalDefines(const Defs: string);
  83. { write/read the options to ppc.cfg file }
  84. procedure WriteOptions(const fn:string);
  85. procedure ReadOptions(const fn:string);
  86. { initialize }
  87. procedure InitOptions;
  88. procedure DoneOptions;
  89. implementation
  90. uses
  91. GlobType,Tokens,Compiler;
  92. var
  93. CfgFile : text;
  94. {*****************************************************************************
  95. TOptionItemCollection
  96. *****************************************************************************}
  97. function TOptionItemCollection.At(Index: Integer): POptionItem;
  98. begin
  99. At:=inherited At(Index);
  100. end;
  101. procedure TOptionItemCollection.FreeItem(Item: Pointer);
  102. begin
  103. if assigned(Item) then
  104. Dispose(POptionItem(Item));
  105. end;
  106. {*****************************************************************************
  107. TOption
  108. *****************************************************************************}
  109. function NewOptionItem(const Name,Param:string):POptionItem;
  110. var
  111. P : POptionItem;
  112. begin
  113. New(P);
  114. P^.Name:=Name;
  115. P^.Param:=Param;
  116. FillChar(P^.IsSet,sizeof(P^.IsSet),0);
  117. NewOptionItem:=P;
  118. end;
  119. constructor TOptions.InitToggle(ch:char);
  120. begin
  121. new(Items,Init(10,5));
  122. Prefix:=ch;
  123. SelNr:=0;
  124. IsSel:=false;
  125. end;
  126. constructor TOptions.InitSel(ch:char);
  127. begin
  128. new(Items,Init(10,5));
  129. Prefix:=ch;
  130. SelNr:=0;
  131. IsSel:=true;
  132. end;
  133. destructor TOptions.Done;
  134. begin
  135. dispose(Items,Done);
  136. end;
  137. procedure TOptions.AddItem(const name,param:string);
  138. begin
  139. Items^.Insert(NewOptionItem(name,Param));
  140. end;
  141. function TOptions.ItemCount:integer;
  142. begin
  143. ItemCount:=Items^.Count;
  144. end;
  145. function TOptions.ItemName(index:integer):string;
  146. var
  147. P : POptionItem;
  148. begin
  149. P:=Items^.At(Index);
  150. if assigned(P) then
  151. ItemName:=P^.Name
  152. else
  153. ItemName:='';
  154. end;
  155. function TOptions.ItemParam(index:integer):string;
  156. var
  157. P : POptionItem;
  158. begin
  159. P:=Items^.At(Index);
  160. if assigned(P) then
  161. ItemParam:='-'+Prefix+P^.Param
  162. else
  163. ItemParam:='';
  164. end;
  165. function TOptions.ItemIsSet(index:integer):boolean;
  166. var
  167. P : POptionItem;
  168. begin
  169. if not IsSel then
  170. begin
  171. P:=Items^.At(Index);
  172. if assigned(P) then
  173. ItemIsSet:=P^.IsSet[OptionMode]
  174. else
  175. ItemIsSet:=false;
  176. end
  177. else
  178. ItemIsSet:=false;
  179. end;
  180. procedure TOptions.ItemSet(index:integer;b:boolean);
  181. var
  182. P : POptionItem;
  183. begin
  184. if not IsSel then
  185. begin
  186. P:=Items^.At(Index);
  187. if assigned(P) then
  188. P^.IsSet[OptionMode]:=b;
  189. end;
  190. end;
  191. function TOptions.GetCurrSel:integer;
  192. begin
  193. if IsSel then
  194. GetCurrSel:=SelNr
  195. else
  196. GetCurrSel:=-1;
  197. end;
  198. procedure TOptions.SetCurrSel(index:integer);
  199. begin
  200. if IsSel then
  201. SelNr:=index;
  202. end;
  203. procedure TOptions.WriteItemsCfg;
  204. var
  205. Pref : char;
  206. procedure writeitem(P:POptionItem);{$ifndef FPC}far;{$endif}
  207. begin
  208. if (P^.Param<>'') and (P^.IsSet[OptionMode]) then
  209. Writeln(CfgFile,'-'+Pref+P^.Param)
  210. end;
  211. begin
  212. Pref:=Prefix;
  213. if IsSel then
  214. begin
  215. writeln(CfgFile,ItemParam(SelNr));
  216. end
  217. else
  218. begin
  219. Items^.ForEach(@writeitem);
  220. end;
  221. end;
  222. function TOptions.ReadItemsCfg(const s:string):boolean;
  223. var
  224. FoundP : POptionItem;
  225. function checkitem(P:POptionItem):boolean;{$ifndef FPC}far;{$endif}
  226. begin
  227. CheckItem:=(P^.Param=s);
  228. end;
  229. begin
  230. FoundP:=Items^.FirstThat(@checkitem);
  231. if assigned(FoundP) then
  232. begin
  233. if IsSel then
  234. SelNr:=Items^.IndexOf(FoundP)
  235. else
  236. FoundP^.IsSet[OptionMode]:=true;
  237. ReadItemsCfg:=true;
  238. end
  239. else
  240. ReadItemsCfg:=false;
  241. end;
  242. {*****************************************************************************
  243. Others
  244. *****************************************************************************}
  245. function GetConditionalDefines: string;
  246. begin
  247. GetConditionalDefines:=CondDefs[OptionMode];
  248. end;
  249. procedure SetConditionalDefines(const Defs: string);
  250. begin
  251. CondDefs[OptionMode]:=Defs;
  252. end;
  253. procedure WriteConditionalDefines;
  254. var
  255. s,s1 : string;
  256. i : integer;
  257. begin
  258. s:=CondDefs[OptionMode];
  259. repeat
  260. i:=pos(' ',s);
  261. if i=0 then
  262. i:=255;
  263. s1:=Copy(s,1,i-1);
  264. if s1<>'' then
  265. writeln(CfgFile,'-d'+s1);
  266. Delete(s,1,i);
  267. until s='';
  268. end;
  269. procedure ReadConditionalDefines(const s:string);
  270. begin
  271. CondDefs[OptionMode]:=CondDefs[OptionMode]+s;
  272. end;
  273. {*****************************************************************************
  274. Read / Write
  275. *****************************************************************************}
  276. procedure WriteOptions(const fn:string);
  277. begin
  278. { create the switches }
  279. assign(CfgFile,fn);
  280. {$I-}
  281. rewrite(CfgFile);
  282. {$I+}
  283. if ioresult<>0 then
  284. exit;
  285. writeln(CfgFile,'# Automaticly created file, don''t edit.');
  286. TargetOptions^.WriteItemsCfg;
  287. VerboseOptions^.WriteItemsCfg;
  288. SyntaxOptions^.WriteItemsCfg;
  289. CodegenOptions^.WriteItemsCfg;
  290. OptimizationOptions^.WriteItemsCfg;
  291. ProcessorOptions^.WriteItemsCfg;
  292. AsmReaderOptions^.WriteItemsCfg;
  293. WriteConditionalDefines;
  294. close(CfgFile);
  295. end;
  296. procedure ReadOptions(const fn:string);
  297. var
  298. c : char;
  299. s : string;
  300. begin
  301. assign(CfgFile,fn);
  302. {$I-}
  303. reset(CfgFile);
  304. {$I+}
  305. if ioresult<>0 then
  306. exit;
  307. while not eof(CfgFile) do
  308. begin
  309. readln(CfgFile,s);
  310. if (length(s)>2) and (s[1]='-') then
  311. begin
  312. c:=s[2];
  313. Delete(s,1,2);
  314. case c of
  315. 'd' : ReadConditionalDefines(s);
  316. 'S' : SyntaxOptions^.ReadItemsCfg(s);
  317. 'T' : TargetOptions^.ReadItemsCfg(s);
  318. 'R' : AsmReaderOptions^.ReadItemsCfg(s);
  319. 'C' : CodegenOptions^.ReadItemsCfg(s);
  320. 'v' : VerboseOptions^.ReadItemsCfg(s);
  321. 'O' : begin
  322. if not OptimizationOptions^.ReadItemsCfg(s) then
  323. ProcessorOptions^.ReadItemsCfg(s);
  324. end;
  325. end;
  326. end;
  327. end;
  328. close(CfgFile);
  329. end;
  330. {*****************************************************************************
  331. Initialize
  332. *****************************************************************************}
  333. procedure InitOptions;
  334. begin
  335. New(SyntaxOptions,InitToggle('S'));
  336. with SyntaxOptions^ do
  337. begin
  338. AddItem('~D~elphi 2 extensions on','2');
  339. AddItem('~C~-like operators','c');
  340. AddItem('S~t~op after first error','e');
  341. AddItem('Allo~w~ LABEL and GOTO','g');
  342. AddItem('C++ styled ~i~nline','i');
  343. AddItem('Global C ~m~acros','m');
  344. AddItem('TP/BP ~7~.0 compatibility','o');
  345. AddItem('Del~p~hi compatibility','d');
  346. AddItem('A~l~low STATIC in objects','s');
  347. end;
  348. New(VerboseOptions,InitToggle('v'));
  349. with VerboseOptions^ do
  350. begin
  351. AddItem('~W~arnings','w');
  352. AddItem('~N~otes','n');
  353. AddItem('~H~ints','h');
  354. AddItem('General ~I~nfo','i');
  355. AddItem('~U~sed,tried info','ut');
  356. AddItem('~A~ll','a');
  357. AddItem('Show all ~P~rocedures if error','b');
  358. end;
  359. New(CodegenOptions,InitToggle('C'));
  360. with CodegenOptions^ do
  361. begin
  362. AddItem('~R~ange checking','r');
  363. AddItem('~S~tack checking','t');
  364. AddItem('~I~/O checking','i');
  365. AddItem('Integer ~o~verflow checking','o');
  366. end;
  367. New(OptimizationOptions,InitToggle('O'));
  368. with OptimizationOptions^ do
  369. begin
  370. AddItem('Generate ~s~maller code','g');
  371. AddItem('Generate ~f~aster code','G');
  372. AddItem('Use register-~v~ariables','r');
  373. AddItem('~U~ncertain optimizations','u');
  374. AddItem('Level ~1~ optimizations','1');
  375. AddItem('Level ~2~ optimizations','2');
  376. end;
  377. New(ProcessorOptions,InitSel('O'));
  378. with ProcessorOptions^ do
  379. begin
  380. AddItem('i~3~86/i486','p1');
  381. AddItem('Pentium/PentiumMM~X~ (tm)','p2');
  382. AddItem('P~P~ro/PII/c6x86/K6 (tm)','p3');
  383. end;
  384. New(TargetOptions,InitSel('T'));
  385. with TargetOptions^ do
  386. begin
  387. AddItem('DOS (GO32V~1~)','go32v1');
  388. AddItem('~D~OS (GO32V2)','go32v2');
  389. AddItem('~L~inux','linux');
  390. AddItem('~O~S/2','os2');
  391. AddItem('~W~IN32','win32');
  392. end;
  393. New(AsmReaderOptions,InitSel('R'));
  394. with AsmReaderOptions^ do
  395. begin
  396. AddItem('No preprocessin~g~','direct');
  397. AddItem('~A~T&T style assembler','att');
  398. AddItem('Int~e~l style assembler','intel');
  399. end;
  400. end;
  401. procedure DoneOptions;
  402. begin
  403. dispose(SyntaxOptions,Done);
  404. dispose(VerboseOptions,Done);
  405. dispose(CodegenOptions,Done);
  406. dispose(OptimizationOptions,Done);
  407. dispose(ProcessorOptions,Done);
  408. dispose(TargetOptions,Done);
  409. dispose(AsmReaderOptions,Done);
  410. end;
  411. end.
  412. {
  413. $Log$
  414. Revision 1.1 1998-12-22 14:27:54 peter
  415. * moved
  416. Revision 1.1 1998/12/22 10:39:40 peter
  417. + options are now written/read
  418. + find and replace routines
  419. }