globals.pas 37 KB

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