globals.pas 35 KB

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