globals.pas 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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: shortint): 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: shortint): shortint;
  295. implementation
  296. uses
  297. {$ifdef macos}
  298. macutils,
  299. {$endif}
  300. comphook;
  301. {****************************************************************************
  302. TLinkStrMap
  303. ****************************************************************************}
  304. Constructor TLinkStrMap.create;
  305. begin
  306. inherited;
  307. itemcnt:=0;
  308. end;
  309. procedure TLinkStrMap.Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  310. begin
  311. if lookup(key)<>-1 Then
  312. exit;
  313. if itemcnt<=length(fmap) Then
  314. setlength(fmap,itemcnt+10);
  315. fmap[itemcnt].key:=key;
  316. fmap[itemcnt].value:=value;
  317. fmap[itemcnt].weight:=weight;
  318. inc(itemcnt);
  319. end;
  320. function TLinkStrMap.AddDep(keyvalue:String):boolean;
  321. var
  322. i : Longint;
  323. begin
  324. AddDep:=false;
  325. i:=pos('=',keyvalue);
  326. if i=0 then
  327. exit;
  328. Add(Copy(KeyValue,1,i-1),Copy(KeyValue,i+1,length(KeyValue)-i));
  329. AddDep:=True;
  330. end;
  331. function TLinkStrMap.AddWeight(keyvalue:String):boolean;
  332. var
  333. i,j : Longint;
  334. Code : Word;
  335. s : AnsiString;
  336. begin
  337. AddWeight:=false;
  338. i:=pos('=',keyvalue);
  339. if i=0 then
  340. exit;
  341. s:=Copy(KeyValue,i+1,length(KeyValue)-i);
  342. val(s,j,code);
  343. if code=0 Then
  344. begin
  345. Add(Copy(KeyValue,1,i-1),'',j);
  346. AddWeight:=True;
  347. end;
  348. end;
  349. procedure TLinkStrMap.addseries(keys:AnsiString;weight:longint);
  350. var
  351. i,j,k : longint;
  352. begin
  353. k:=length(keys);
  354. i:=1;
  355. while i<=k do
  356. begin
  357. j:=i;
  358. while (i<=k) and (keys[i]<>',') do
  359. inc(i);
  360. add(copy(keys,j,i-j),'',weight);
  361. inc(i);
  362. end;
  363. end;
  364. procedure TLinkStrMap.SetValue(Key:Ansistring;weight:Integer);
  365. var
  366. j : longint;
  367. begin
  368. j:=lookup(key);
  369. if j<>-1 then
  370. fmap[j].weight:=weight;
  371. end;
  372. function TLinkStrMap.find(key:Ansistring):Ansistring;
  373. var
  374. j : longint;
  375. begin
  376. find:='';
  377. j:=lookup(key);
  378. if j<>-1 then
  379. find:=fmap[j].value;
  380. end;
  381. function TLinkStrMap.lookup(key:Ansistring):longint;
  382. var
  383. i : longint;
  384. begin
  385. lookup:=-1;
  386. i:=0;
  387. while (i<itemcnt) and (fmap[i].key<>key) do
  388. inc(i);
  389. if i<>itemcnt then
  390. lookup:=i;
  391. end;
  392. procedure TLinkStrMap.SortOnWeight;
  393. var
  394. i, j : longint;
  395. m : TLinkRec;
  396. begin
  397. if itemcnt <2 then exit;
  398. for i:=0 to itemcnt-1 do
  399. for j:=i+1 to itemcnt-1 do
  400. begin
  401. if fmap[i].weight>fmap[j].weight Then
  402. begin
  403. m:=fmap[i];
  404. fmap[i]:=fmap[j];
  405. fmap[j]:=m;
  406. end;
  407. end;
  408. end;
  409. function TLinkStrMap.getlinkrec(i:longint):TLinkRec;
  410. begin
  411. result:=fmap[i];
  412. end;
  413. procedure TLinkStrMap.Expand(Src:TCmdStrList;Dest:TLinkStrMap);
  414. // expands every thing in Src to Dest for linkorder purposes.
  415. var
  416. l,r : longint;
  417. LibN : TCmdStr;
  418. begin
  419. while not src.empty do
  420. begin
  421. LibN:=src.getfirst;
  422. r:=lookup (LibN);
  423. if r=-1 then
  424. dest.add(LibN)
  425. else
  426. dest.addseries(fmap[r].value);
  427. end;
  428. end;
  429. procedure TLinkStrMap.UpdateWeights(Weightmap:TLinkStrMap);
  430. var
  431. l,r : longint;
  432. begin
  433. for l := 0 to itemcnt-1 do
  434. begin
  435. r:=weightmap.lookup (fmap[l].key);
  436. if r<>-1 then
  437. fmap[l].weight:=weightmap[r].weight;
  438. end;
  439. end;
  440. {****************************************************************************
  441. Time Handling
  442. ****************************************************************************}
  443. Function L0(l:longint):string;
  444. {
  445. return the string of value l, if l<10 then insert a zero, so
  446. the string is always at least 2 chars '01','02',etc
  447. }
  448. var
  449. s : string;
  450. begin
  451. Str(l,s);
  452. if l<10 then
  453. s:='0'+s;
  454. L0:=s;
  455. end;
  456. function gettimestr:string;
  457. {
  458. get the current time in a string HH:MM:SS
  459. }
  460. var
  461. hour,min,sec,hsec : word;
  462. begin
  463. DecodeTime(Time,hour,min,sec,hsec);
  464. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  465. end;
  466. function getdatestr:string;
  467. {
  468. get the current date in a string YY/MM/DD
  469. }
  470. var
  471. Year,Month,Day: Word;
  472. begin
  473. DecodeDate(Date,year,month,day);
  474. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  475. end;
  476. function filetimestring( t : longint) : string;
  477. {
  478. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  479. }
  480. var
  481. DT : TDateTime;
  482. hsec : word;
  483. Year,Month,Day: Word;
  484. hour,min,sec : word;
  485. begin
  486. if t=-1 then
  487. begin
  488. Result := 'Not Found';
  489. exit;
  490. end;
  491. DT := FileDateToDateTime(t);
  492. DecodeTime(DT,hour,min,sec,hsec);
  493. DecodeDate(DT,year,month,day);
  494. Result := L0(Year)+'/'+L0(Month)+'/'+L0(Day)+' '+L0(Hour)+':'+L0(min)+':'+L0(sec);
  495. end;
  496. function getrealtime : real;
  497. var
  498. h,m,s,s1000 : word;
  499. begin
  500. DecodeTime(Time,h,m,s,s1000);
  501. result:=h*3600.0+m*60.0+s+s1000/1000.0;
  502. end;
  503. {****************************************************************************
  504. Default Macro Handling
  505. ****************************************************************************}
  506. procedure DefaultReplacements(var s:ansistring);
  507. begin
  508. { Replace some macros }
  509. Replace(s,'$FPCVERSION',version_string);
  510. Replace(s,'$FPCFULLVERSION',full_version_string);
  511. Replace(s,'$FPCDATE',date_string);
  512. Replace(s,'$FPCCPU',target_cpu_string);
  513. Replace(s,'$FPCOS',target_os_string);
  514. if tf_use_8_3 in Source_Info.Flags then
  515. Replace(s,'$FPCTARGET',target_os_string)
  516. else
  517. Replace(s,'$FPCTARGET',target_full_string);
  518. end;
  519. {****************************************************************************
  520. OS Dependent things
  521. ****************************************************************************}
  522. function GetEnvPChar(const envname:string):pchar;
  523. {$ifdef win32}
  524. var
  525. s : string;
  526. i,len : longint;
  527. hp,p,p2 : pchar;
  528. {$endif}
  529. begin
  530. {$ifdef hasunix}
  531. GetEnvPchar:=BaseUnix.fpGetEnv(envname);
  532. {$define GETENVOK}
  533. {$endif}
  534. {$ifdef win32}
  535. GetEnvPchar:=nil;
  536. p:=GetEnvironmentStrings;
  537. hp:=p;
  538. while hp^<>#0 do
  539. begin
  540. s:=strpas(hp);
  541. i:=pos('=',s);
  542. len:=strlen(hp);
  543. if upper(copy(s,1,i-1))=upper(envname) then
  544. begin
  545. GetMem(p2,len-length(envname));
  546. Move(hp[i],p2^,len-length(envname));
  547. GetEnvPchar:=p2;
  548. break;
  549. end;
  550. { next string entry}
  551. hp:=hp+len+1;
  552. end;
  553. FreeEnvironmentStrings(p);
  554. {$define GETENVOK}
  555. {$endif}
  556. {$ifdef os2}
  557. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  558. {$define GETENVOK}
  559. {$endif}
  560. {$ifdef GETENVOK}
  561. {$undef GETENVOK}
  562. {$else}
  563. GetEnvPchar:=StrPNew(GetEnvironmentVariable(envname));
  564. {$endif}
  565. end;
  566. procedure FreeEnvPChar(p:pchar);
  567. begin
  568. {$ifndef hasunix}
  569. {$ifndef os2}
  570. freemem(p);
  571. {$endif}
  572. {$endif}
  573. end;
  574. {$if defined(MORPHOS) or defined(AMIGA)}
  575. {$define AMIGASHELL}
  576. {$endif}
  577. function Shell(const command:ansistring): longint;
  578. { This is already defined in the linux.ppu for linux, need for the *
  579. expansion under linux }
  580. {$ifdef hasunix}
  581. begin
  582. result := Unix.Shell(command);
  583. end;
  584. {$else hasunix}
  585. {$ifdef amigashell}
  586. begin
  587. result := ExecuteProcess('',command);
  588. end;
  589. {$else amigashell}
  590. var
  591. comspec : string;
  592. begin
  593. comspec:=GetEnvironmentVariable('COMSPEC');
  594. result := ExecuteProcess(comspec,' /C '+command);
  595. end;
  596. {$endif amigashell}
  597. {$endif hasunix}
  598. {$UNDEF AMIGASHELL}
  599. function is_number_float(d : double) : boolean;
  600. var
  601. bytearray : array[0..7] of byte;
  602. begin
  603. move(d,bytearray,8);
  604. { only 1.1 save, 1.0.x will use always little endian }
  605. {$ifdef FPC_BIG_ENDIAN}
  606. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  607. {$else FPC_BIG_ENDIAN}
  608. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  609. {$endif FPC_BIG_ENDIAN}
  610. end;
  611. function get_real_sign(r: bestreal): longint;
  612. var
  613. p: pbyte;
  614. begin
  615. p := pbyte(@r);
  616. {$ifdef CPU_ARM}
  617. inc(p,4);
  618. {$else}
  619. {$ifdef FPC_LITTLE_ENDIAN}
  620. inc(p,sizeof(r)-1);
  621. {$endif}
  622. {$endif}
  623. if (p^ and $80) = 0 then
  624. result := 1
  625. else
  626. result := -1;
  627. end;
  628. function convertdoublerec(d : tdoublerec) : tdoublerec;{$ifdef USEINLINE}inline;{$endif}
  629. {$ifdef CPUARM}
  630. var
  631. i : longint;
  632. begin
  633. for i:=0 to 3 do
  634. begin
  635. result.bytes[i+4]:=d.bytes[i];
  636. result.bytes[i]:=d.bytes[i+4];
  637. end;
  638. {$else CPUARM}
  639. begin
  640. result:=d;
  641. {$endif CPUARM}
  642. end;
  643. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  644. function string2guid(const s: string; var GUID: TGUID): boolean;
  645. function ishexstr(const hs: string): boolean;
  646. var
  647. i: integer;
  648. begin
  649. ishexstr:=false;
  650. for i:=1 to Length(hs) do begin
  651. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  652. exit;
  653. end;
  654. ishexstr:=true;
  655. end;
  656. function hexstr2longint(const hexs: string): longint;
  657. var
  658. i: integer;
  659. rl: longint;
  660. begin
  661. rl:=0;
  662. for i:=1 to length(hexs) do begin
  663. rl:=rl shl 4;
  664. case hexs[i] of
  665. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  666. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  667. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  668. end
  669. end;
  670. hexstr2longint:=rl;
  671. end;
  672. var
  673. i: integer;
  674. begin
  675. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  676. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  677. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  678. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  679. ishexstr(copy(s,26,12)) then begin
  680. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  681. { these values are arealdy in the correct range (4 chars = word) }
  682. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  683. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  684. for i:=0 to 1 do
  685. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  686. for i:=2 to 7 do
  687. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  688. string2guid:=true;
  689. end
  690. else
  691. string2guid:=false;
  692. end;
  693. function guid2string(const GUID: TGUID): string;
  694. function long2hex(l, len: longint): string;
  695. const
  696. hextbl: array[0..15] of char = '0123456789ABCDEF';
  697. var
  698. rs: string;
  699. i: integer;
  700. begin
  701. rs[0]:=chr(len);
  702. for i:=len downto 1 do begin
  703. rs[i]:=hextbl[l and $F];
  704. l:=l shr 4;
  705. end;
  706. long2hex:=rs;
  707. end;
  708. begin
  709. guid2string:=
  710. '{'+long2hex(GUID.D1,8)+
  711. '-'+long2hex(GUID.D2,4)+
  712. '-'+long2hex(GUID.D3,4)+
  713. '-'+long2hex(GUID.D4[0],2)+long2hex(GUID.D4[1],2)+
  714. '-'+long2hex(GUID.D4[2],2)+long2hex(GUID.D4[3],2)+
  715. long2hex(GUID.D4[4],2)+long2hex(GUID.D4[5],2)+
  716. long2hex(GUID.D4[6],2)+long2hex(GUID.D4[7],2)+
  717. '}';
  718. end;
  719. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  720. const
  721. DefProcCallName : array[tproccalloption] of string[12] = ('',
  722. 'CDECL',
  723. 'CPPDECL',
  724. 'FAR16',
  725. 'OLDFPCCALL',
  726. '', { internproc }
  727. '', { syscall }
  728. 'PASCAL',
  729. 'REGISTER',
  730. 'SAFECALL',
  731. 'STDCALL',
  732. 'SOFTFLOAT',
  733. 'MWPASCAL'
  734. );
  735. var
  736. t : tproccalloption;
  737. hs : string;
  738. begin
  739. result:=false;
  740. if (s = '') then
  741. exit;
  742. hs:=upper(s);
  743. if (hs = 'DEFAULT') then
  744. begin
  745. a := pocall_default;
  746. result := true;
  747. exit;
  748. end;
  749. for t:=low(tproccalloption) to high(tproccalloption) do
  750. if DefProcCallName[t]=hs then
  751. begin
  752. a:=t;
  753. result:=true;
  754. break;
  755. end;
  756. end;
  757. function Setcputype(const s:string;var a:tcputype):boolean;
  758. var
  759. t : tcputype;
  760. hs : string;
  761. begin
  762. result:=false;
  763. hs:=Upper(s);
  764. for t:=low(tcputype) to high(tcputype) do
  765. if cputypestr[t]=hs then
  766. begin
  767. a:=t;
  768. result:=true;
  769. break;
  770. end;
  771. end;
  772. function SetFpuType(const s:string;var a:tfputype):boolean;
  773. var
  774. t : tfputype;
  775. begin
  776. result:=false;
  777. for t:=low(tfputype) to high(tfputype) do
  778. if fputypestr[t]=s then
  779. begin
  780. a:=t;
  781. result:=true;
  782. break;
  783. end;
  784. end;
  785. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  786. var
  787. tok : string;
  788. vstr : string;
  789. l : longint;
  790. code : integer;
  791. b : talignmentinfo;
  792. begin
  793. UpdateAlignmentStr:=true;
  794. uppervar(s);
  795. fillchar(b,sizeof(b),0);
  796. repeat
  797. tok:=GetToken(s,'=');
  798. if tok='' then
  799. break;
  800. vstr:=GetToken(s,',');
  801. val(vstr,l,code);
  802. if tok='PROC' then
  803. b.procalign:=l
  804. else if tok='JUMP' then
  805. b.jumpalign:=l
  806. else if tok='LOOP' then
  807. b.loopalign:=l
  808. else if tok='CONSTMIN' then
  809. b.constalignmin:=l
  810. else if tok='CONSTMAX' then
  811. b.constalignmax:=l
  812. else if tok='VARMIN' then
  813. b.varalignmin:=l
  814. else if tok='VARMAX' then
  815. b.varalignmax:=l
  816. else if tok='LOCALMIN' then
  817. b.localalignmin:=l
  818. else if tok='LOCALMAX' then
  819. b.localalignmax:=l
  820. else if tok='RECORDMIN' then
  821. b.recordalignmin:=l
  822. else if tok='RECORDMAX' then
  823. b.recordalignmax:=l
  824. else { Error }
  825. UpdateAlignmentStr:=false;
  826. until false;
  827. UpdateAlignment(a,b);
  828. end;
  829. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  830. var
  831. tok : string;
  832. doset,
  833. found : boolean;
  834. opt : toptimizerswitch;
  835. begin
  836. result:=true;
  837. uppervar(s);
  838. repeat
  839. tok:=GetToken(s,',');
  840. if tok='' then
  841. break;
  842. if Copy(tok,1,2)='NO' then
  843. begin
  844. delete(tok,1,2);
  845. doset:=false;
  846. end
  847. else
  848. doset:=true;
  849. found:=false;
  850. for opt:=low(toptimizerswitch) to high(toptimizerswitch) do
  851. begin
  852. if OptimizerSwitchStr[opt]=tok then
  853. begin
  854. found:=true;
  855. break;
  856. end;
  857. end;
  858. if found then
  859. begin
  860. if doset then
  861. include(a,opt)
  862. else
  863. exclude(a,opt);
  864. end
  865. else
  866. result:=false;
  867. until false;
  868. end;
  869. function IncludeFeature(const s : string) : boolean;
  870. var
  871. i : tfeature;
  872. begin
  873. result:=true;
  874. for i:=low(tfeature) to high(tfeature) do
  875. if s=featurestr[i] then
  876. begin
  877. include(features,i);
  878. exit;
  879. end;
  880. result:=false;
  881. end;
  882. function var_align(siz: shortint): shortint;
  883. begin
  884. siz := size_2_align(siz);
  885. var_align := used_align(siz,current_settings.alignment.varalignmin,current_settings.alignment.varalignmax);
  886. end;
  887. function const_align(siz: shortint): shortint;
  888. begin
  889. siz := size_2_align(siz);
  890. const_align := used_align(siz,current_settings.alignment.constalignmin,current_settings.alignment.constalignmax);
  891. end;
  892. {****************************************************************************
  893. Init
  894. ****************************************************************************}
  895. {$ifdef unix}
  896. {$define need_path_search}
  897. {$endif unix}
  898. {$ifdef os2}
  899. {$define need_path_search}
  900. {$endif os2}
  901. {$ifdef macos}
  902. {$define need_path_search}
  903. {$endif macos}
  904. procedure get_exepath;
  905. var
  906. localExepath : TCmdStr;
  907. exeName:TCmdStr;
  908. {$ifdef need_path_search}
  909. hs1 : TPathStr;
  910. p : pchar;
  911. {$endif need_path_search}
  912. begin
  913. localexepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  914. if localexepath='' then
  915. begin
  916. exeName := FixFileName(system.paramstr(0));
  917. localexepath := ExtractFilePath(exeName);
  918. end;
  919. {$ifdef need_path_search}
  920. if localexepath='' then
  921. begin
  922. hs1 := ExtractFileName(exeName);
  923. ChangeFileExt(hs1,source_info.exeext);
  924. {$ifdef macos}
  925. p:=GetEnvPchar('Commands');
  926. {$else macos}
  927. p:=GetEnvPchar('PATH');
  928. {$endif macos}
  929. FindFilePChar(hs1,p,false,localExepath);
  930. FreeEnvPChar(p);
  931. localExepath:=ExtractFilePath(localExepath);
  932. end;
  933. {$endif need_path_search}
  934. exepath:=FixPath(localExepath,false);
  935. end;
  936. procedure DoneGlobals;
  937. begin
  938. if assigned(DLLImageBase) then
  939. StringDispose(DLLImageBase);
  940. librarysearchpath.Free;
  941. unitsearchpath.Free;
  942. objectsearchpath.Free;
  943. includesearchpath.Free;
  944. LinkLibraryAliases.Free;
  945. LinkLibraryOrder.Free;
  946. end;
  947. procedure InitGlobals;
  948. var
  949. i : tfeature;
  950. begin
  951. get_exepath;
  952. { reset globals }
  953. do_build:=false;
  954. do_release:=false;
  955. do_make:=true;
  956. compile_level:=0;
  957. DLLsource:=false;
  958. resolving_forward:=false;
  959. paratarget:=system_none;
  960. paratargetasm:=as_none;
  961. paratargetdbg:=dbg_none;
  962. { Output }
  963. OutputFileName:='';
  964. OutputPrefix:=Nil;
  965. OutputSuffix:=Nil;
  966. OutputExeDir:='';
  967. OutputUnitDir:='';
  968. { Utils directory }
  969. utilsdirectory:='';
  970. utilsprefix:='';
  971. cshared:=false;
  972. rlinkpath:='';
  973. { Search Paths }
  974. librarysearchpath:=TSearchPathList.Create;
  975. unitsearchpath:=TSearchPathList.Create;
  976. includesearchpath:=TSearchPathList.Create;
  977. objectsearchpath:=TSearchPathList.Create;
  978. { Def file }
  979. usewindowapi:=false;
  980. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  981. DescriptionSetExplicity:=false;
  982. SetPEFlagsSetExplicity:=false;
  983. ImageBaseSetExplicity:=false;
  984. MinStackSizeSetExplicity:=false;
  985. MaxStackSizeSetExplicity:=false;
  986. dllversion:='';
  987. dllmajor:=1;
  988. dllminor:=0;
  989. dllrevision:=0;
  990. nwscreenname := '';
  991. nwthreadname := '';
  992. nwcopyright := '';
  993. UseDeffileForExports:=false;
  994. UseDeffileForExportsSetExplicitly:=false;
  995. GenerateImportSection:=false;
  996. RelocSection:=false;
  997. RelocSectionSetExplicitly:=false;
  998. LinkTypeSetExplicitly:=false;
  999. { memory sizes, will be overriden by parameter or default for target
  1000. in options or init_parser }
  1001. stacksize:=0;
  1002. { not initialized yet }
  1003. jmp_buf_size:=-1;
  1004. apptype:=app_cui;
  1005. { Init values }
  1006. init_settings.modeswitches:=fpcmodeswitches;
  1007. init_settings.localswitches:=[cs_check_io,cs_typed_const_writable];
  1008. init_settings.moduleswitches:=[cs_extsyntax,cs_implicit_exceptions];
  1009. init_settings.globalswitches:=[cs_check_unit_name,cs_link_static];
  1010. init_settings.optimizerswitches:=[];
  1011. init_settings.sourcecodepage:='8859-1';
  1012. init_settings.packenum:=4;
  1013. init_settings.setalloc:=0;
  1014. fillchar(init_settings.alignment,sizeof(talignmentinfo),0);
  1015. { might be overridden later }
  1016. init_settings.asmmode:=asmmode_standard;
  1017. init_settings.cputype:=cpu_none;
  1018. init_settings.optimizecputype:=cpu_none;
  1019. init_settings.fputype:=fpu_none;
  1020. init_settings.interfacetype:=it_interfacecom;
  1021. init_settings.defproccall:=pocall_default;
  1022. { Target specific defaults, these can override previous default options }
  1023. {$ifdef i386}
  1024. init_settings.cputype:=cpu_Pentium;
  1025. init_settings.optimizecputype:=cpu_Pentium3;
  1026. init_settings.fputype:=fpu_x87;
  1027. {$endif i386}
  1028. {$ifdef m68k}
  1029. init_settings.cputype:=cpu_MC68020;
  1030. init_settings.fputype:=fpu_soft;
  1031. {$endif m68k}
  1032. {$ifdef powerpc}
  1033. init_settings.cputype:=cpu_PPC604;
  1034. init_settings.optimizecputype:=cpu_ppc7400;
  1035. init_settings.fputype:=fpu_standard;
  1036. {$endif powerpc}
  1037. {$ifdef POWERPC64}
  1038. init_settings.cputype:=cpu_PPC970;
  1039. init_settings.optimizecputype:=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. features:=[low(Tfeature)..high(Tfeature)];
  1060. end;
  1061. end.