globals.pas 35 KB

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