globals.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements some support functions and global variables
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit globals;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. {$ifdef win32}
  22. windows,
  23. {$endif}
  24. {$ifdef os2}
  25. dos,
  26. {$endif os2}
  27. {$ifdef hasunix}
  28. Baseunix,unix,
  29. {$endif}
  30. {$IFNDEF USE_FAKE_SYSUTILS}
  31. sysutils,
  32. {$ELSE}
  33. fksysutl,
  34. {$ENDIF}
  35. { comphook pulls in sysutils anyways }
  36. cutils,cclasses,cfileutils,
  37. cpuinfo,
  38. globtype,version,systems;
  39. const
  40. delphimodeswitches : tmodeswitches=
  41. [m_delphi,m_all,m_class,m_objpas,m_result,m_string_pchar,
  42. m_pointer_2_procedure,m_autoderef,m_tp_procvar,m_initfinal,m_default_ansistring,
  43. m_out,m_default_para,m_duplicate_names,m_hintdirective,m_add_pointer,
  44. m_property,m_default_inline,m_except];
  45. fpcmodeswitches : tmodeswitches=
  46. [m_fpc,m_all,m_string_pchar,m_nested_comment,m_repeat_forward,
  47. m_cvar_support,m_initfinal,m_add_pointer,m_hintdirective,
  48. m_property,m_default_inline];
  49. objfpcmodeswitches : tmodeswitches=
  50. [m_objfpc,m_fpc,m_all,m_class,m_objpas,m_result,m_string_pchar,m_nested_comment,
  51. m_repeat_forward,m_cvar_support,m_initfinal,m_add_pointer,m_out,m_default_para,m_hintdirective,
  52. m_property,m_default_inline,m_except];
  53. tpmodeswitches : tmodeswitches=
  54. [m_tp7,m_all,m_tp_procvar,m_duplicate_names];
  55. gpcmodeswitches : tmodeswitches=
  56. [m_gpc,m_all,m_tp_procvar];
  57. macmodeswitches : tmodeswitches=
  58. [m_mac,m_all,m_result,m_cvar_support,m_mac_procvar];
  59. { maximum nesting of routines }
  60. maxnesting = 32;
  61. { Filenames and extensions }
  62. sourceext = '.pp';
  63. pasext = '.pas';
  64. pext = '.p';
  65. treelogfilename = 'tree.log';
  66. {$if defined(CPUARM) and defined(FPUFPA)}
  67. MathQNaN : tdoublerec = (bytes : (0,0,252,255,0,0,0,0));
  68. MathInf : tdoublerec = (bytes : (0,0,240,127,0,0,0,0));
  69. MathNegInf : tdoublerec = (bytes : (0,0,240,255,0,0,0,0));
  70. MathPi : tdoublerec = (bytes : (251,33,9,64,24,45,68,84));
  71. {$else}
  72. {$ifdef FPC_LITTLE_ENDIAN}
  73. MathQNaN : tdoublerec = (bytes : (0,0,0,0,0,0,252,255));
  74. MathInf : tdoublerec = (bytes : (0,0,0,0,0,0,240,127));
  75. MathNegInf : tdoublerec = (bytes : (0,0,0,0,0,0,240,255));
  76. MathPi : tdoublerec = (bytes : (24,45,68,84,251,33,9,64));
  77. MathPiExtended : textendedrec = (bytes : (53,194,104,33,162,218,15,201,0,64));
  78. {$else FPC_LITTLE_ENDIAN}
  79. MathQNaN : tdoublerec = (bytes : (255,252,0,0,0,0,0,0));
  80. MathInf : tdoublerec = (bytes : (127,240,0,0,0,0,0,0));
  81. MathNegInf : tdoublerec = (bytes : (255,240,0,0,0,0,0,0));
  82. MathPi : tdoublerec = (bytes : (64,9,33,251,84,68,45,24));
  83. MathPiExtended : textendedrec = (bytes : (64,0,201,15,218,162,33,104,194,53));
  84. {$endif FPC_LITTLE_ENDIAN}
  85. {$endif}
  86. type
  87. pfileposinfo = ^tfileposinfo;
  88. tfileposinfo = record
  89. line : longint;
  90. column : word;
  91. fileindex : word;
  92. moduleindex : word;
  93. end;
  94. tcodepagestring = string[20];
  95. tsettings = record
  96. globalswitches : tglobalswitches;
  97. moduleswitches : tmoduleswitches;
  98. localswitches : tlocalswitches;
  99. modeswitches : tmodeswitches;
  100. optimizerswitches : toptimizerswitches;
  101. { 0: old behaviour for sets <=256 elements
  102. >0: round to this size }
  103. setalloc,
  104. packenum : shortint;
  105. alignment : talignmentinfo;
  106. cputype,
  107. optimizecputype : tcputype;
  108. fputype : tfputype;
  109. asmmode : tasmmode;
  110. interfacetype : tinterfacetypes;
  111. defproccall : tproccalloption;
  112. sourcecodepage : tcodepagestring;
  113. packrecords : shortint;
  114. maxfpuregisters : shortint;
  115. end;
  116. const
  117. LinkMapWeightDefault = 1000;
  118. type
  119. TLinkRec = record
  120. Key : AnsiString;
  121. Value : AnsiString; // key expands to valuelist "value"
  122. Weight: longint;
  123. end;
  124. TLinkStrMap = class
  125. private
  126. itemcnt : longint;
  127. fmap : Array Of TLinkRec;
  128. function Lookup(key:Ansistring):longint;
  129. function getlinkrec(i:longint):TLinkRec;
  130. public
  131. procedure Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  132. procedure addseries(keys:AnsiString;weight:longint=LinkMapWeightDefault);
  133. function AddDep(keyvalue:String):boolean;
  134. function AddWeight(keyvalue:String):boolean;
  135. procedure SetValue(key:AnsiString;Weight:Integer);
  136. procedure SortonWeight;
  137. function Find(key:AnsiString):AnsiString;
  138. procedure Expand(src:TCmdStrList;dest: TLinkStrMap);
  139. procedure UpdateWeights(Weightmap:TLinkStrMap);
  140. constructor Create;
  141. property count : longint read itemcnt;
  142. property items[I:longint]:TLinkRec read getlinkrec; default;
  143. end;
  144. var
  145. { specified inputfile }
  146. inputfilepath : string;
  147. inputfilename : string;
  148. { specified outputfile with -o parameter }
  149. outputfilename : string;
  150. outputprefix : pshortstring;
  151. outputsuffix : pshortstring;
  152. { specified with -FE or -FU }
  153. outputexedir : TPathStr;
  154. outputunitdir : TPathStr;
  155. { things specified with parameters }
  156. paratarget : tsystem;
  157. paratargetdbg : tdbg;
  158. paratargetasm : tasm;
  159. paralinkoptions : TCmdStr;
  160. paradynamiclinker : string;
  161. paraprintnodetree : byte;
  162. parapreprocess : boolean;
  163. printnodefile : text;
  164. { typical cross compiling params}
  165. { directory where the utils can be found (options -FD) }
  166. utilsdirectory : TPathStr;
  167. { targetname specific prefix used by these utils (options -XP<path>) }
  168. utilsprefix : TCmdStr;
  169. cshared : boolean; { pass --shared to ld to link C libs shared}
  170. Dontlinkstdlibpath: Boolean; { Don't add std paths to linkpath}
  171. rlinkpath : TCmdStr; { rpath-link linkdir override}
  172. { some flags for global compiler switches }
  173. do_build,
  174. do_release,
  175. do_make : boolean;
  176. { path for searching units, different paths can be seperated by ; }
  177. exepath : TPathStr; { Path to ppc }
  178. librarysearchpath,
  179. unitsearchpath,
  180. objectsearchpath,
  181. includesearchpath : TSearchPathList;
  182. autoloadunits : string;
  183. { linking }
  184. usewindowapi : boolean;
  185. description : string;
  186. SetPEFlagsSetExplicity,
  187. ImageBaseSetExplicity,
  188. MinStackSizeSetExplicity,
  189. MaxStackSizeSetExplicity,
  190. DescriptionSetExplicity : boolean;
  191. dllversion : string;
  192. dllmajor,
  193. dllminor,
  194. dllrevision : word; { revision only for netware }
  195. { win pe }
  196. peflags : longint;
  197. minstacksize,
  198. maxstacksize,
  199. imagebase : aword;
  200. UseDeffileForExports : boolean;
  201. UseDeffileForExportsSetExplicitly : boolean;
  202. GenerateImportSection,
  203. GenerateImportSectionSetExplicitly,
  204. RelocSection : boolean;
  205. RelocSectionSetExplicitly : boolean;
  206. LinkTypeSetExplicitly : boolean;
  207. current_tokenpos, { position of the last token }
  208. current_filepos : tfileposinfo; { current position }
  209. nwscreenname : string;
  210. nwthreadname : string;
  211. nwcopyright : string;
  212. codegenerror : boolean; { true if there is an error reported }
  213. block_type : tblock_type; { type of currently parsed block }
  214. parsing_para_level : integer; { parameter level, used to convert
  215. proc calls to proc loads in firstcalln }
  216. compile_level : word;
  217. resolving_forward : boolean; { used to add forward reference as second ref }
  218. inlining_procedure : boolean; { are we inlining a procedure }
  219. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  220. aktexceptblock : integer; { the exceptblock number of the current block (0 if none) }
  221. LinkLibraryAliases : TLinkStrMap;
  222. LinkLibraryOrder : TLinkStrMap;
  223. init_settings,
  224. current_settings : tsettings;
  225. nextlocalswitches : tlocalswitches;
  226. localswitcheschanged : boolean;
  227. { Memory sizes }
  228. heapsize,
  229. stacksize,
  230. jmp_buf_size : longint;
  231. {$Ifdef EXTDEBUG}
  232. { parameter switches }
  233. debugstop : boolean;
  234. {$EndIf EXTDEBUG}
  235. { windows / OS/2 application type }
  236. apptype : tapptype;
  237. features : tfeatures;
  238. const
  239. DLLsource : boolean = false;
  240. DLLImageBase : pshortstring = nil;
  241. { used to set all registers used for each global function
  242. this should dramatically decrease the number of
  243. recompilations needed PM }
  244. simplify_ppu : boolean = true;
  245. { should we allow non static members ? }
  246. allow_only_static : boolean = false;
  247. Inside_asm_statement : boolean = false;
  248. global_unit_count : word = 0;
  249. { for error info in pp.pas }
  250. parser_current_file : string = '';
  251. {$if defined(m68k) or defined(arm)}
  252. { PalmOS resources }
  253. palmos_applicationname : string = 'FPC Application';
  254. palmos_applicationid : string[4] = 'FPCA';
  255. {$endif defined(m68k) or defined(arm)}
  256. {$ifdef powerpc}
  257. { default calling convention used on MorphOS }
  258. syscall_convention : string = 'LEGACY';
  259. {$endif powerpc}
  260. { default name of the C-style "main" procedure of the library/program }
  261. { (this will be prefixed with the target_info.cprefix) }
  262. mainaliasname : string = 'main';
  263. { by default no local variable trashing }
  264. localvartrashing: longint = -1;
  265. { actual values are defined in ncgutil.pas }
  266. nroftrashvalues = 4;
  267. var
  268. starttime : real;
  269. function getdatestr:string;
  270. function gettimestr:string;
  271. function filetimestring( t : longint) : string;
  272. function getrealtime : real;
  273. procedure DefaultReplacements(var s:ansistring);
  274. function Shell(const command:ansistring): longint;
  275. function GetEnvPChar(const envname:string):pchar;
  276. procedure FreeEnvPChar(p:pchar);
  277. function is_number_float(d : double) : boolean;
  278. { discern +0.0 and -0.0 }
  279. function get_real_sign(r: bestreal): longint;
  280. procedure InitGlobals;
  281. procedure DoneGlobals;
  282. function string2guid(const s: string; var GUID: TGUID): boolean;
  283. function guid2string(const GUID: TGUID): string;
  284. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  285. function Setcputype(const s:string;var a:tcputype):boolean;
  286. function SetFpuType(const s:string;var a:tfputype):boolean;
  287. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  288. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  289. function IncludeFeature(const s : string) : boolean;
  290. {# Routine to get the required alignment for size of data, which will
  291. be placed in bss segment, according to the current alignment requirements }
  292. function var_align(siz: shortint): shortint;
  293. {# Routine to get the required alignment for size of data, which will
  294. be placed in data/const segment, according to the current alignment requirements }
  295. function const_align(siz: shortint): shortint;
  296. implementation
  297. uses
  298. {$ifdef macos}
  299. macutils,
  300. {$endif}
  301. comphook;
  302. {****************************************************************************
  303. TLinkStrMap
  304. ****************************************************************************}
  305. Constructor TLinkStrMap.create;
  306. begin
  307. inherited;
  308. itemcnt:=0;
  309. end;
  310. procedure TLinkStrMap.Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  311. begin
  312. if lookup(key)<>-1 Then
  313. exit;
  314. if itemcnt<=length(fmap) Then
  315. setlength(fmap,itemcnt+10);
  316. fmap[itemcnt].key:=key;
  317. fmap[itemcnt].value:=value;
  318. fmap[itemcnt].weight:=weight;
  319. inc(itemcnt);
  320. end;
  321. function TLinkStrMap.AddDep(keyvalue:String):boolean;
  322. var
  323. i : Longint;
  324. begin
  325. AddDep:=false;
  326. i:=pos('=',keyvalue);
  327. if i=0 then
  328. exit;
  329. Add(Copy(KeyValue,1,i-1),Copy(KeyValue,i+1,length(KeyValue)-i));
  330. AddDep:=True;
  331. end;
  332. function TLinkStrMap.AddWeight(keyvalue:String):boolean;
  333. var
  334. i,j : Longint;
  335. Code : Word;
  336. s : AnsiString;
  337. begin
  338. AddWeight:=false;
  339. i:=pos('=',keyvalue);
  340. if i=0 then
  341. exit;
  342. s:=Copy(KeyValue,i+1,length(KeyValue)-i);
  343. val(s,j,code);
  344. if code=0 Then
  345. begin
  346. Add(Copy(KeyValue,1,i-1),'',j);
  347. AddWeight:=True;
  348. end;
  349. end;
  350. procedure TLinkStrMap.addseries(keys:AnsiString;weight:longint);
  351. var
  352. i,j,k : longint;
  353. begin
  354. k:=length(keys);
  355. i:=1;
  356. while i<=k do
  357. begin
  358. j:=i;
  359. while (i<=k) and (keys[i]<>',') do
  360. inc(i);
  361. add(copy(keys,j,i-j),'',weight);
  362. inc(i);
  363. end;
  364. end;
  365. procedure TLinkStrMap.SetValue(Key:Ansistring;weight:Integer);
  366. var
  367. j : longint;
  368. begin
  369. j:=lookup(key);
  370. if j<>-1 then
  371. fmap[j].weight:=weight;
  372. end;
  373. function TLinkStrMap.find(key:Ansistring):Ansistring;
  374. var
  375. j : longint;
  376. begin
  377. find:='';
  378. j:=lookup(key);
  379. if j<>-1 then
  380. find:=fmap[j].value;
  381. end;
  382. function TLinkStrMap.lookup(key:Ansistring):longint;
  383. var
  384. i : longint;
  385. begin
  386. lookup:=-1;
  387. i:=0;
  388. while (i<itemcnt) and (fmap[i].key<>key) do
  389. inc(i);
  390. if i<>itemcnt then
  391. lookup:=i;
  392. end;
  393. procedure TLinkStrMap.SortOnWeight;
  394. var
  395. i, j : longint;
  396. m : TLinkRec;
  397. begin
  398. if itemcnt <2 then exit;
  399. for i:=0 to itemcnt-1 do
  400. for j:=i+1 to itemcnt-1 do
  401. begin
  402. if fmap[i].weight>fmap[j].weight Then
  403. begin
  404. m:=fmap[i];
  405. fmap[i]:=fmap[j];
  406. fmap[j]:=m;
  407. end;
  408. end;
  409. end;
  410. function TLinkStrMap.getlinkrec(i:longint):TLinkRec;
  411. begin
  412. result:=fmap[i];
  413. end;
  414. procedure TLinkStrMap.Expand(Src:TCmdStrList;Dest:TLinkStrMap);
  415. // expands every thing in Src to Dest for linkorder purposes.
  416. var
  417. l,r : longint;
  418. LibN : TCmdStr;
  419. begin
  420. while not src.empty do
  421. begin
  422. LibN:=src.getfirst;
  423. r:=lookup (LibN);
  424. if r=-1 then
  425. dest.add(LibN)
  426. else
  427. dest.addseries(fmap[r].value);
  428. end;
  429. end;
  430. procedure TLinkStrMap.UpdateWeights(Weightmap:TLinkStrMap);
  431. var
  432. l,r : longint;
  433. begin
  434. for l := 0 to itemcnt-1 do
  435. begin
  436. r:=weightmap.lookup (fmap[l].key);
  437. if r<>-1 then
  438. fmap[l].weight:=weightmap[r].weight;
  439. end;
  440. end;
  441. {****************************************************************************
  442. Time Handling
  443. ****************************************************************************}
  444. Function L0(l:longint):string;
  445. {
  446. return the string of value l, if l<10 then insert a zero, so
  447. the string is always at least 2 chars '01','02',etc
  448. }
  449. var
  450. s : string;
  451. begin
  452. Str(l,s);
  453. if l<10 then
  454. s:='0'+s;
  455. L0:=s;
  456. end;
  457. function gettimestr:string;
  458. {
  459. get the current time in a string HH:MM:SS
  460. }
  461. var
  462. hour,min,sec,hsec : word;
  463. begin
  464. DecodeTime(Time,hour,min,sec,hsec);
  465. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  466. end;
  467. function getdatestr:string;
  468. {
  469. get the current date in a string YY/MM/DD
  470. }
  471. var
  472. Year,Month,Day: Word;
  473. begin
  474. DecodeDate(Date,year,month,day);
  475. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  476. end;
  477. function filetimestring( t : longint) : string;
  478. {
  479. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  480. }
  481. var
  482. DT : TDateTime;
  483. hsec : word;
  484. Year,Month,Day: Word;
  485. hour,min,sec : word;
  486. begin
  487. if t=-1 then
  488. begin
  489. Result := 'Not Found';
  490. exit;
  491. end;
  492. DT := FileDateToDateTime(t);
  493. DecodeTime(DT,hour,min,sec,hsec);
  494. DecodeDate(DT,year,month,day);
  495. Result := L0(Year)+'/'+L0(Month)+'/'+L0(Day)+' '+L0(Hour)+':'+L0(min)+':'+L0(sec);
  496. end;
  497. function getrealtime : real;
  498. var
  499. h,m,s,s1000 : word;
  500. begin
  501. DecodeTime(Time,h,m,s,s1000);
  502. result:=h*3600.0+m*60.0+s+s1000/1000.0;
  503. end;
  504. {****************************************************************************
  505. Default Macro Handling
  506. ****************************************************************************}
  507. procedure DefaultReplacements(var s:ansistring);
  508. begin
  509. { Replace some macros }
  510. Replace(s,'$FPCVERSION',version_string);
  511. Replace(s,'$FPCFULLVERSION',full_version_string);
  512. Replace(s,'$FPCDATE',date_string);
  513. Replace(s,'$FPCCPU',target_cpu_string);
  514. Replace(s,'$FPCOS',target_os_string);
  515. if tf_use_8_3 in Source_Info.Flags then
  516. Replace(s,'$FPCTARGET',target_os_string)
  517. else
  518. Replace(s,'$FPCTARGET',target_full_string);
  519. end;
  520. {****************************************************************************
  521. OS Dependent things
  522. ****************************************************************************}
  523. function GetEnvPChar(const envname:string):pchar;
  524. {$ifdef win32}
  525. var
  526. s : string;
  527. i,len : longint;
  528. hp,p,p2 : pchar;
  529. {$endif}
  530. begin
  531. {$ifdef hasunix}
  532. GetEnvPchar:=BaseUnix.fpGetEnv(envname);
  533. {$define GETENVOK}
  534. {$endif}
  535. {$ifdef win32}
  536. GetEnvPchar:=nil;
  537. p:=GetEnvironmentStrings;
  538. hp:=p;
  539. while hp^<>#0 do
  540. begin
  541. s:=strpas(hp);
  542. i:=pos('=',s);
  543. len:=strlen(hp);
  544. if upper(copy(s,1,i-1))=upper(envname) then
  545. begin
  546. GetMem(p2,len-length(envname));
  547. Move(hp[i],p2^,len-length(envname));
  548. GetEnvPchar:=p2;
  549. break;
  550. end;
  551. { next string entry}
  552. hp:=hp+len+1;
  553. end;
  554. FreeEnvironmentStrings(p);
  555. {$define GETENVOK}
  556. {$endif}
  557. {$ifdef os2}
  558. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  559. {$define GETENVOK}
  560. {$endif}
  561. {$ifdef GETENVOK}
  562. {$undef GETENVOK}
  563. {$else}
  564. GetEnvPchar:=StrPNew(GetEnvironmentVariable(envname));
  565. {$endif}
  566. end;
  567. procedure FreeEnvPChar(p:pchar);
  568. begin
  569. {$ifndef hasunix}
  570. {$ifndef os2}
  571. freemem(p);
  572. {$endif}
  573. {$endif}
  574. end;
  575. {$if defined(MORPHOS) or defined(AMIGA)}
  576. {$define AMIGASHELL}
  577. {$endif}
  578. function Shell(const command:ansistring): longint;
  579. { This is already defined in the linux.ppu for linux, need for the *
  580. expansion under linux }
  581. {$ifdef hasunix}
  582. begin
  583. result := Unix.Shell(command);
  584. end;
  585. {$else hasunix}
  586. {$ifdef amigashell}
  587. begin
  588. result := ExecuteProcess('',command);
  589. end;
  590. {$else amigashell}
  591. var
  592. comspec : string;
  593. begin
  594. comspec:=GetEnvironmentVariable('COMSPEC');
  595. result := ExecuteProcess(comspec,' /C '+command);
  596. end;
  597. {$endif amigashell}
  598. {$endif hasunix}
  599. {$UNDEF AMIGASHELL}
  600. function is_number_float(d : double) : boolean;
  601. var
  602. bytearray : array[0..7] of byte;
  603. begin
  604. move(d,bytearray,8);
  605. { only 1.1 save, 1.0.x will use always little endian }
  606. {$ifdef FPC_BIG_ENDIAN}
  607. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  608. {$else FPC_BIG_ENDIAN}
  609. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  610. {$endif FPC_BIG_ENDIAN}
  611. end;
  612. function get_real_sign(r: bestreal): longint;
  613. var
  614. p: pbyte;
  615. begin
  616. p := pbyte(@r);
  617. {$ifdef CPU_ARM}
  618. inc(p,4);
  619. {$else}
  620. {$ifdef FPC_LITTLE_ENDIAN}
  621. inc(p,sizeof(r)-1);
  622. {$endif}
  623. {$endif}
  624. if (p^ and $80) = 0 then
  625. result := 1
  626. else
  627. result := -1;
  628. end;
  629. function convertdoublerec(d : tdoublerec) : tdoublerec;{$ifdef USEINLINE}inline;{$endif}
  630. {$ifdef CPUARM}
  631. var
  632. i : longint;
  633. begin
  634. for i:=0 to 3 do
  635. begin
  636. result.bytes[i+4]:=d.bytes[i];
  637. result.bytes[i]:=d.bytes[i+4];
  638. end;
  639. {$else CPUARM}
  640. begin
  641. result:=d;
  642. {$endif CPUARM}
  643. end;
  644. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  645. function string2guid(const s: string; var GUID: TGUID): boolean;
  646. function ishexstr(const hs: string): boolean;
  647. var
  648. i: integer;
  649. begin
  650. ishexstr:=false;
  651. for i:=1 to Length(hs) do begin
  652. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  653. exit;
  654. end;
  655. ishexstr:=true;
  656. end;
  657. function hexstr2longint(const hexs: string): longint;
  658. var
  659. i: integer;
  660. rl: longint;
  661. begin
  662. rl:=0;
  663. for i:=1 to length(hexs) do begin
  664. rl:=rl shl 4;
  665. case hexs[i] of
  666. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  667. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  668. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  669. end
  670. end;
  671. hexstr2longint:=rl;
  672. end;
  673. var
  674. i: integer;
  675. begin
  676. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  677. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  678. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  679. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  680. ishexstr(copy(s,26,12)) then begin
  681. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  682. { these values are arealdy in the correct range (4 chars = word) }
  683. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  684. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  685. for i:=0 to 1 do
  686. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  687. for i:=2 to 7 do
  688. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  689. string2guid:=true;
  690. end
  691. else
  692. string2guid:=false;
  693. end;
  694. function guid2string(const GUID: TGUID): string;
  695. function long2hex(l, len: longint): string;
  696. const
  697. hextbl: array[0..15] of char = '0123456789ABCDEF';
  698. var
  699. rs: string;
  700. i: integer;
  701. begin
  702. rs[0]:=chr(len);
  703. for i:=len downto 1 do begin
  704. rs[i]:=hextbl[l and $F];
  705. l:=l shr 4;
  706. end;
  707. long2hex:=rs;
  708. end;
  709. begin
  710. guid2string:=
  711. '{'+long2hex(GUID.D1,8)+
  712. '-'+long2hex(GUID.D2,4)+
  713. '-'+long2hex(GUID.D3,4)+
  714. '-'+long2hex(GUID.D4[0],2)+long2hex(GUID.D4[1],2)+
  715. '-'+long2hex(GUID.D4[2],2)+long2hex(GUID.D4[3],2)+
  716. long2hex(GUID.D4[4],2)+long2hex(GUID.D4[5],2)+
  717. long2hex(GUID.D4[6],2)+long2hex(GUID.D4[7],2)+
  718. '}';
  719. end;
  720. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  721. const
  722. DefProcCallName : array[tproccalloption] of string[12] = ('',
  723. 'CDECL',
  724. 'CPPDECL',
  725. 'FAR16',
  726. 'OLDFPCCALL',
  727. '', { internproc }
  728. '', { syscall }
  729. 'PASCAL',
  730. 'REGISTER',
  731. 'SAFECALL',
  732. 'STDCALL',
  733. 'SOFTFLOAT',
  734. 'MWPASCAL'
  735. );
  736. var
  737. t : tproccalloption;
  738. hs : string;
  739. begin
  740. result:=false;
  741. if (s = '') then
  742. exit;
  743. hs:=upper(s);
  744. if (hs = 'DEFAULT') then
  745. begin
  746. a := pocall_default;
  747. result := true;
  748. exit;
  749. end;
  750. for t:=low(tproccalloption) to high(tproccalloption) do
  751. if DefProcCallName[t]=hs then
  752. begin
  753. a:=t;
  754. result:=true;
  755. break;
  756. end;
  757. end;
  758. function Setcputype(const s:string;var a:tcputype):boolean;
  759. var
  760. t : tcputype;
  761. hs : string;
  762. begin
  763. result:=false;
  764. hs:=Upper(s);
  765. for t:=low(tcputype) to high(tcputype) do
  766. if cputypestr[t]=hs then
  767. begin
  768. a:=t;
  769. result:=true;
  770. break;
  771. end;
  772. end;
  773. function SetFpuType(const s:string;var a:tfputype):boolean;
  774. var
  775. t : tfputype;
  776. begin
  777. result:=false;
  778. for t:=low(tfputype) to high(tfputype) do
  779. if fputypestr[t]=s then
  780. begin
  781. a:=t;
  782. result:=true;
  783. break;
  784. end;
  785. end;
  786. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  787. var
  788. tok : string;
  789. vstr : string;
  790. l : longint;
  791. code : integer;
  792. b : talignmentinfo;
  793. begin
  794. UpdateAlignmentStr:=true;
  795. uppervar(s);
  796. fillchar(b,sizeof(b),0);
  797. repeat
  798. tok:=GetToken(s,'=');
  799. if tok='' then
  800. break;
  801. vstr:=GetToken(s,',');
  802. val(vstr,l,code);
  803. if tok='PROC' then
  804. b.procalign:=l
  805. else if tok='JUMP' then
  806. b.jumpalign:=l
  807. else if tok='LOOP' then
  808. b.loopalign:=l
  809. else if tok='CONSTMIN' then
  810. b.constalignmin:=l
  811. else if tok='CONSTMAX' then
  812. b.constalignmax:=l
  813. else if tok='VARMIN' then
  814. b.varalignmin:=l
  815. else if tok='VARMAX' then
  816. b.varalignmax:=l
  817. else if tok='LOCALMIN' then
  818. b.localalignmin:=l
  819. else if tok='LOCALMAX' then
  820. b.localalignmax:=l
  821. else if tok='RECORDMIN' then
  822. b.recordalignmin:=l
  823. else if tok='RECORDMAX' then
  824. b.recordalignmax:=l
  825. else { Error }
  826. UpdateAlignmentStr:=false;
  827. until false;
  828. UpdateAlignment(a,b);
  829. end;
  830. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  831. var
  832. tok : string;
  833. doset,
  834. found : boolean;
  835. opt : toptimizerswitch;
  836. begin
  837. result:=true;
  838. uppervar(s);
  839. repeat
  840. tok:=GetToken(s,',');
  841. if tok='' then
  842. break;
  843. if Copy(tok,1,2)='NO' then
  844. begin
  845. delete(tok,1,2);
  846. doset:=false;
  847. end
  848. else
  849. doset:=true;
  850. found:=false;
  851. for opt:=low(toptimizerswitch) to high(toptimizerswitch) do
  852. begin
  853. if OptimizerSwitchStr[opt]=tok then
  854. begin
  855. found:=true;
  856. break;
  857. end;
  858. end;
  859. if found then
  860. begin
  861. if doset then
  862. include(a,opt)
  863. else
  864. exclude(a,opt);
  865. end
  866. else
  867. result:=false;
  868. until false;
  869. end;
  870. function IncludeFeature(const s : string) : boolean;
  871. var
  872. i : tfeature;
  873. begin
  874. result:=true;
  875. for i:=low(tfeature) to high(tfeature) do
  876. if s=featurestr[i] then
  877. begin
  878. include(features,i);
  879. exit;
  880. end;
  881. result:=false;
  882. end;
  883. function var_align(siz: shortint): shortint;
  884. begin
  885. siz := size_2_align(siz);
  886. var_align := used_align(siz,current_settings.alignment.varalignmin,current_settings.alignment.varalignmax);
  887. end;
  888. function const_align(siz: shortint): shortint;
  889. begin
  890. siz := size_2_align(siz);
  891. const_align := used_align(siz,current_settings.alignment.constalignmin,current_settings.alignment.constalignmax);
  892. end;
  893. {****************************************************************************
  894. Init
  895. ****************************************************************************}
  896. {$ifdef unix}
  897. {$define need_path_search}
  898. {$endif unix}
  899. {$ifdef os2}
  900. {$define need_path_search}
  901. {$endif os2}
  902. {$ifdef macos}
  903. {$define need_path_search}
  904. {$endif macos}
  905. procedure get_exepath;
  906. var
  907. localExepath : TCmdStr;
  908. exeName:TCmdStr;
  909. {$ifdef need_path_search}
  910. hs1 : TPathStr;
  911. p : pchar;
  912. {$endif need_path_search}
  913. begin
  914. localexepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  915. if localexepath='' then
  916. begin
  917. exeName := FixFileName(system.paramstr(0));
  918. localexepath := ExtractFilePath(exeName);
  919. end;
  920. {$ifdef need_path_search}
  921. if localexepath='' then
  922. begin
  923. hs1 := ExtractFileName(exeName);
  924. ChangeFileExt(hs1,source_info.exeext);
  925. {$ifdef macos}
  926. p:=GetEnvPchar('Commands');
  927. {$else macos}
  928. p:=GetEnvPchar('PATH');
  929. {$endif macos}
  930. FindFilePChar(hs1,p,false,localExepath);
  931. FreeEnvPChar(p);
  932. localExepath:=ExtractFilePath(localExepath);
  933. end;
  934. {$endif need_path_search}
  935. exepath:=FixPath(localExepath,false);
  936. end;
  937. procedure DoneGlobals;
  938. begin
  939. if assigned(DLLImageBase) then
  940. StringDispose(DLLImageBase);
  941. librarysearchpath.Free;
  942. unitsearchpath.Free;
  943. objectsearchpath.Free;
  944. includesearchpath.Free;
  945. LinkLibraryAliases.Free;
  946. LinkLibraryOrder.Free;
  947. end;
  948. procedure InitGlobals;
  949. var
  950. i : tfeature;
  951. begin
  952. get_exepath;
  953. { reset globals }
  954. do_build:=false;
  955. do_release:=false;
  956. do_make:=true;
  957. compile_level:=0;
  958. DLLsource:=false;
  959. inlining_procedure:=false;
  960. resolving_forward:=false;
  961. paratarget:=system_none;
  962. paratargetasm:=as_none;
  963. paratargetdbg:=dbg_none;
  964. { Output }
  965. OutputFileName:='';
  966. OutputPrefix:=Nil;
  967. OutputSuffix:=Nil;
  968. OutputExeDir:='';
  969. OutputUnitDir:='';
  970. { Utils directory }
  971. utilsdirectory:='';
  972. utilsprefix:='';
  973. cshared:=false;
  974. rlinkpath:='';
  975. { Search Paths }
  976. librarysearchpath:=TSearchPathList.Create;
  977. unitsearchpath:=TSearchPathList.Create;
  978. includesearchpath:=TSearchPathList.Create;
  979. objectsearchpath:=TSearchPathList.Create;
  980. { Def file }
  981. usewindowapi:=false;
  982. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  983. DescriptionSetExplicity:=false;
  984. SetPEFlagsSetExplicity:=false;
  985. ImageBaseSetExplicity:=false;
  986. MinStackSizeSetExplicity:=false;
  987. MaxStackSizeSetExplicity:=false;
  988. dllversion:='';
  989. dllmajor:=1;
  990. dllminor:=0;
  991. dllrevision:=0;
  992. nwscreenname := '';
  993. nwthreadname := '';
  994. nwcopyright := '';
  995. UseDeffileForExports:=false;
  996. UseDeffileForExportsSetExplicitly:=false;
  997. GenerateImportSection:=false;
  998. RelocSection:=false;
  999. RelocSectionSetExplicitly:=false;
  1000. LinkTypeSetExplicitly:=false;
  1001. { memory sizes, will be overriden by parameter or default for target
  1002. in options or init_parser }
  1003. stacksize:=0;
  1004. { not initialized yet }
  1005. jmp_buf_size:=-1;
  1006. apptype:=app_cui;
  1007. { Init values }
  1008. init_settings.modeswitches:=fpcmodeswitches;
  1009. init_settings.localswitches:=[cs_check_io,cs_typed_const_writable];
  1010. init_settings.moduleswitches:=[cs_extsyntax,cs_implicit_exceptions];
  1011. init_settings.globalswitches:=[cs_check_unit_name,cs_link_static];
  1012. init_settings.optimizerswitches:=[];
  1013. init_settings.sourcecodepage:='8859-1';
  1014. init_settings.packenum:=4;
  1015. init_settings.setalloc:=0;
  1016. fillchar(init_settings.alignment,sizeof(talignmentinfo),0);
  1017. { might be overridden later }
  1018. init_settings.asmmode:=asmmode_standard;
  1019. init_settings.cputype:=cpu_none;
  1020. init_settings.optimizecputype:=cpu_none;
  1021. init_settings.fputype:=fpu_none;
  1022. init_settings.interfacetype:=it_interfacecom;
  1023. init_settings.defproccall:=pocall_default;
  1024. { Target specific defaults, these can override previous default options }
  1025. {$ifdef i386}
  1026. init_settings.cputype:=cpu_Pentium;
  1027. init_settings.optimizecputype:=cpu_Pentium3;
  1028. init_settings.fputype:=fpu_x87;
  1029. {$endif i386}
  1030. {$ifdef m68k}
  1031. init_settings.cputype:=cpu_MC68020;
  1032. init_settings.fputype:=fpu_soft;
  1033. {$endif m68k}
  1034. {$ifdef powerpc}
  1035. init_settings.cputype:=cpu_PPC604;
  1036. init_settings.fputype:=fpu_standard;
  1037. {$endif powerpc}
  1038. {$ifdef POWERPC64}
  1039. init_settings.cputype:=cpu_PPC970;
  1040. init_settings.fputype:=fpu_standard;
  1041. {$endif POWERPC64}
  1042. {$ifdef sparc}
  1043. init_settings.cputype:=cpu_SPARC_V8;
  1044. init_settings.fputype:=fpu_hard;
  1045. {$endif sparc}
  1046. {$ifdef arm}
  1047. init_settings.cputype:=cpu_armv3;
  1048. init_settings.fputype:=fpu_fpa;
  1049. {$endif arm}
  1050. {$ifdef x86_64}
  1051. init_settings.cputype:=cpu_athlon64;
  1052. init_settings.fputype:=fpu_sse64;
  1053. {$endif x86_64}
  1054. if init_settings.optimizecputype=cpu_none then
  1055. init_settings.optimizecputype:=init_settings.cputype;
  1056. LinkLibraryAliases :=TLinkStrMap.Create;
  1057. LinkLibraryOrder :=TLinkStrMap.Create;
  1058. { enable all features by default }
  1059. for i:=low(tfeature) to high(tfeature) do
  1060. include(features,i);
  1061. end;
  1062. end.