globals.pas 35 KB

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