globals.pas 34 KB

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