globals.pas 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  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 windows}
  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 =
  41. [m_delphi,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,
  44. m_property,m_default_inline,m_except,m_advanced_records];
  45. delphiunicodemodeswitches = delphimodeswitches + [m_systemcodepage,m_default_unicodestring];
  46. fpcmodeswitches =
  47. [m_fpc,m_string_pchar,m_nested_comment,m_repeat_forward,
  48. m_cvar_support,m_initfinal,m_hintdirective,
  49. m_property,m_default_inline];
  50. objfpcmodeswitches =
  51. [m_objfpc,m_fpc,m_class,m_objpas,m_result,m_string_pchar,m_nested_comment,
  52. m_repeat_forward,m_cvar_support,m_initfinal,m_out,m_default_para,m_hintdirective,
  53. m_property,m_default_inline,m_except];
  54. tpmodeswitches =
  55. [m_tp7,m_tp_procvar,m_duplicate_names];
  56. {$ifdef gpc_mode}
  57. gpcmodeswitches =
  58. [m_gpc,m_tp_procvar];
  59. {$endif}
  60. macmodeswitches =
  61. [m_mac,m_cvar_support,m_mac_procvar,m_nested_procvars,m_non_local_goto,m_isolike_unary_minus,m_default_inline];
  62. isomodeswitches =
  63. [m_iso,m_tp_procvar,m_duplicate_names,m_nested_procvars,m_non_local_goto,m_isolike_unary_minus];
  64. { maximum nesting of routines }
  65. maxnesting = 32;
  66. { Filenames and extensions }
  67. sourceext = '.pp';
  68. pasext = '.pas';
  69. pext = '.p';
  70. treelogfilename = 'tree.log';
  71. {$if defined(CPUARM) and defined(FPUFPA)}
  72. MathQNaN : tdoublerec = (bytes : (0,0,252,255,0,0,0,0));
  73. MathInf : tdoublerec = (bytes : (0,0,240,127,0,0,0,0));
  74. MathNegInf : tdoublerec = (bytes : (0,0,240,255,0,0,0,0));
  75. MathPi : tdoublerec = (bytes : (251,33,9,64,24,45,68,84));
  76. {$else}
  77. {$ifdef FPC_LITTLE_ENDIAN}
  78. MathQNaN : tdoublerec = (bytes : (0,0,0,0,0,0,252,255));
  79. MathInf : tdoublerec = (bytes : (0,0,0,0,0,0,240,127));
  80. MathNegInf : tdoublerec = (bytes : (0,0,0,0,0,0,240,255));
  81. MathPi : tdoublerec = (bytes : (24,45,68,84,251,33,9,64));
  82. MathPiExtended : textendedrec = (bytes : (53,194,104,33,162,218,15,201,0,64));
  83. {$else FPC_LITTLE_ENDIAN}
  84. MathQNaN : tdoublerec = (bytes : (255,252,0,0,0,0,0,0));
  85. MathInf : tdoublerec = (bytes : (127,240,0,0,0,0,0,0));
  86. MathNegInf : tdoublerec = (bytes : (255,240,0,0,0,0,0,0));
  87. MathPi : tdoublerec = (bytes : (64,9,33,251,84,68,45,24));
  88. MathPiExtended : textendedrec = (bytes : (64,0,201,15,218,162,33,104,194,53));
  89. {$endif FPC_LITTLE_ENDIAN}
  90. {$endif}
  91. CP_UTF8 = 65001;
  92. CP_UTF16LE = 1200;
  93. CP_UTF16BE = 1201;
  94. CP_NONE = 65535;
  95. { by default no local variable trashing }
  96. localvartrashing: longint = -1;
  97. nroftrashvalues = 4;
  98. trashintvalues: array[0..nroftrashvalues-1] of int64 = ($5555555555555555,$AAAAAAAAAAAAAAAA,$EFEFEFEFEFEFEFEF,0);
  99. type
  100. { this is written to ppus during token recording for generics,
  101. it used to required to be packed,
  102. but this requirement is now obsolete,
  103. as the fields are written one by one. PM 2012-06-13 }
  104. tsettings = record
  105. alignment : talignmentinfo;
  106. globalswitches : tglobalswitches;
  107. targetswitches : ttargetswitches;
  108. moduleswitches : tmoduleswitches;
  109. localswitches : tlocalswitches;
  110. modeswitches : tmodeswitches;
  111. optimizerswitches : toptimizerswitches;
  112. { generate information necessary to perform these wpo's during a subsequent compilation }
  113. genwpoptimizerswitches: twpoptimizerswitches;
  114. { perform these wpo's using information generated during a previous compilation }
  115. dowpoptimizerswitches: twpoptimizerswitches;
  116. debugswitches : tdebugswitches;
  117. { 0: old behaviour for sets <=256 elements
  118. >0: round to this size }
  119. setalloc,
  120. packenum : shortint;
  121. packrecords : shortint;
  122. maxfpuregisters : shortint;
  123. cputype,
  124. optimizecputype : tcputype;
  125. fputype : tfputype;
  126. asmmode : tasmmode;
  127. interfacetype : tinterfacetypes;
  128. defproccall : tproccalloption;
  129. sourcecodepage : tstringencoding;
  130. minfpconstprec : tfloattype;
  131. disabledircache : boolean;
  132. { CPU targets with microcontroller support can add a controller specific unit }
  133. {$if defined(ARM) or defined(AVR)}
  134. controllertype : tcontrollertype;
  135. {$endif defined(ARM) or defined(AVR)}
  136. { WARNING: this pointer cannot be written as such in record token }
  137. pmessage : pmessagestaterecord;
  138. end;
  139. const
  140. LinkMapWeightDefault = 1000;
  141. type
  142. TLinkRec = record
  143. Key : AnsiString;
  144. Value : AnsiString; // key expands to valuelist "value"
  145. Weight: longint;
  146. end;
  147. TLinkStrMap = class
  148. private
  149. itemcnt : longint;
  150. fmap : Array Of TLinkRec;
  151. function Lookup(key:Ansistring):longint;
  152. function getlinkrec(i:longint):TLinkRec;
  153. public
  154. procedure Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  155. procedure addseries(keys:AnsiString;weight:longint=LinkMapWeightDefault);
  156. function AddDep(keyvalue:String):boolean;
  157. function AddWeight(keyvalue:String):boolean;
  158. procedure SetValue(key:AnsiString;Weight:Integer);
  159. procedure SortonWeight;
  160. function Find(key:AnsiString):AnsiString;
  161. procedure Expand(src:TCmdStrList;dest: TLinkStrMap);
  162. procedure UpdateWeights(Weightmap:TLinkStrMap);
  163. constructor Create;
  164. property count : longint read itemcnt;
  165. property items[I:longint]:TLinkRec read getlinkrec; default;
  166. end;
  167. tpendingstate = record
  168. nextverbositystr : shortstring;
  169. nextlocalswitches : tlocalswitches;
  170. nextverbosityfullswitch: longint;
  171. nextcallingstr : shortstring;
  172. nextmessagerecord : pmessagestaterecord;
  173. verbosityfullswitched,
  174. localswitcheschanged : boolean;
  175. end;
  176. var
  177. { specified inputfile }
  178. inputfilepath : string;
  179. inputfilename : string;
  180. { specified outputfile with -o parameter }
  181. outputfilename : string;
  182. outputprefix : pshortstring;
  183. outputsuffix : pshortstring;
  184. { specified with -FE or -FU }
  185. outputexedir : TPathStr;
  186. outputunitdir : TPathStr;
  187. { specified with -FW and -Fw }
  188. wpofeedbackinput,
  189. wpofeedbackoutput : TPathStr;
  190. { things specified with parameters }
  191. paratarget : tsystem;
  192. paratargetdbg : tdbg;
  193. paratargetasm : tasm;
  194. paralinkoptions : TCmdStr;
  195. paradynamiclinker : string;
  196. paraprintnodetree : byte;
  197. parapreprocess : boolean;
  198. printnodefile : text;
  199. { typical cross compiling params}
  200. { directory where the utils can be found (options -FD) }
  201. utilsdirectory : TPathStr;
  202. { targetname specific prefix used by these utils (options -XP<path>) }
  203. utilsprefix : TCmdStr;
  204. cshared : boolean; { pass --shared to ld to link C libs shared}
  205. Dontlinkstdlibpath: Boolean; { Don't add std paths to linkpath}
  206. rlinkpath : TCmdStr; { rpath-link linkdir override}
  207. sysrootpath : TCmdStr; { target system root to search dyn linker }
  208. { some flags for global compiler switches }
  209. do_build,
  210. do_release,
  211. do_make : boolean;
  212. { path for searching units, different paths can be seperated by ; }
  213. exepath : TPathStr; { Path to ppc }
  214. librarysearchpath,
  215. unitsearchpath,
  216. objectsearchpath,
  217. includesearchpath,
  218. frameworksearchpath : TSearchPathList;
  219. autoloadunits : string;
  220. { linking }
  221. usegnubinutils : boolean;
  222. forceforwardslash : boolean;
  223. usewindowapi : boolean;
  224. description : string;
  225. SetPEFlagsSetExplicity,
  226. ImageBaseSetExplicity,
  227. MinStackSizeSetExplicity,
  228. MaxStackSizeSetExplicity,
  229. DescriptionSetExplicity : boolean;
  230. dllversion : string;
  231. dllmajor,
  232. dllminor,
  233. dllrevision : word; { revision only for netware }
  234. { win pe }
  235. peflags : longint;
  236. minstacksize,
  237. maxstacksize,
  238. imagebase : puint;
  239. UseDeffileForExports : boolean;
  240. UseDeffileForExportsSetExplicitly : boolean;
  241. GenerateImportSection,
  242. GenerateImportSectionSetExplicitly,
  243. RelocSection : boolean;
  244. MacOSXVersionMin,
  245. iPhoneOSVersionMin: string[15];
  246. RelocSectionSetExplicitly : boolean;
  247. LinkTypeSetExplicitly : boolean;
  248. current_tokenpos, { position of the last token }
  249. current_filepos : tfileposinfo; { current position }
  250. nwscreenname : string;
  251. nwthreadname : string;
  252. nwcopyright : string;
  253. codegenerror : boolean; { true if there is an error reported }
  254. block_type : tblock_type; { type of currently parsed block }
  255. compile_level : word;
  256. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  257. current_exceptblock : integer; { the exceptblock number of the current block (0 if none) }
  258. LinkLibraryAliases : TLinkStrMap;
  259. LinkLibraryOrder : TLinkStrMap;
  260. init_settings,
  261. current_settings : tsettings;
  262. pendingstate : tpendingstate;
  263. { Memory sizes }
  264. heapsize,
  265. stacksize,
  266. jmp_buf_size,
  267. jmp_buf_align : longint;
  268. {$Ifdef EXTDEBUG}
  269. { parameter switches }
  270. debugstop : boolean;
  271. {$EndIf EXTDEBUG}
  272. { windows / OS/2 application type }
  273. apptype : tapptype;
  274. features : tfeatures;
  275. const
  276. DLLsource : boolean = false;
  277. { used to set all registers used for each global function
  278. this should dramatically decrease the number of
  279. recompilations needed PM }
  280. simplify_ppu : boolean = true;
  281. Inside_asm_statement : boolean = false;
  282. global_unit_count : word = 0;
  283. { for error info in pp.pas }
  284. parser_current_file : string = '';
  285. {$if defined(m68k) or defined(arm)}
  286. { PalmOS resources }
  287. palmos_applicationname : string = 'FPC Application';
  288. palmos_applicationid : string[4] = 'FPCA';
  289. {$endif defined(m68k) or defined(arm)}
  290. {$ifdef powerpc}
  291. { default calling convention used on MorphOS }
  292. syscall_convention : string = 'LEGACY';
  293. {$endif powerpc}
  294. { default name of the C-style "main" procedure of the library/program }
  295. { (this will be prefixed with the target_info.cprefix) }
  296. defaultmainaliasname = 'main';
  297. mainaliasname : string = defaultmainaliasname;
  298. const
  299. default_settings : TSettings = (
  300. alignment : (
  301. procalign : 0;
  302. loopalign : 0;
  303. jumpalign : 0;
  304. constalignmin : 0;
  305. constalignmax : 0;
  306. varalignmin : 0;
  307. varalignmax : 0;
  308. localalignmin : 0;
  309. localalignmax : 0;
  310. recordalignmin : 0;
  311. recordalignmax : 0;
  312. maxCrecordalign : 0;
  313. );
  314. globalswitches : [cs_check_unit_name,cs_link_static];
  315. targetswitches : [];
  316. moduleswitches : [cs_extsyntax,cs_implicit_exceptions];
  317. localswitches : [cs_check_io,cs_typed_const_writable,cs_pointermath];
  318. modeswitches : fpcmodeswitches;
  319. optimizerswitches : [];
  320. genwpoptimizerswitches : [];
  321. dowpoptimizerswitches : [];
  322. debugswitches : [];
  323. setalloc : 0;
  324. packenum : 4;
  325. packrecords : 0;
  326. maxfpuregisters : 0;
  327. { Note: GENERIC_CPU is sued together with generic subdirectory to
  328. be able to compile some of the units without any real CPU.
  329. This is used to generate a CPU independant PPUDUMP utility. PM }
  330. {$ifdef GENERIC_CPU}
  331. cputype : cpu_none;
  332. optimizecputype : cpu_none;
  333. fputype : fpu_none;
  334. {$else not GENERIC_CPU}
  335. {$ifdef i386}
  336. cputype : cpu_Pentium;
  337. optimizecputype : cpu_Pentium3;
  338. fputype : fpu_x87;
  339. {$endif i386}
  340. {$ifdef m68k}
  341. cputype : cpu_MC68020;
  342. optimizecputype : cpu_MC68020;
  343. fputype : fpu_soft;
  344. {$endif m68k}
  345. {$ifdef powerpc}
  346. cputype : cpu_PPC604;
  347. optimizecputype : cpu_ppc7400;
  348. fputype : fpu_standard;
  349. {$endif powerpc}
  350. {$ifdef POWERPC64}
  351. cputype : cpu_PPC970;
  352. optimizecputype : cpu_ppc970;
  353. fputype : fpu_standard;
  354. {$endif POWERPC64}
  355. {$ifdef sparc}
  356. cputype : cpu_SPARC_V8;
  357. optimizecputype : cpu_SPARC_V8;
  358. fputype : fpu_hard;
  359. {$endif sparc}
  360. {$ifdef arm}
  361. cputype : cpu_armv3;
  362. optimizecputype : cpu_armv3;
  363. fputype : fpu_fpa;
  364. {$endif arm}
  365. {$ifdef x86_64}
  366. cputype : cpu_athlon64;
  367. optimizecputype : cpu_athlon64;
  368. fputype : fpu_sse64;
  369. {$endif x86_64}
  370. {$ifdef ia64}
  371. cputype : cpu_itanium;
  372. optimizecputype : cpu_itanium;
  373. fputype : fpu_itanium;
  374. {$endif ia64}
  375. {$ifdef avr}
  376. cputype : cpuinfo.cpu_avr5;
  377. optimizecputype : cpuinfo.cpu_avr5;
  378. fputype : fpu_none;
  379. {$endif avr}
  380. {$ifdef mips}
  381. cputype : cpu_mips32;
  382. optimizecputype : cpu_mips32;
  383. fputype : fpu_mips2;
  384. {$endif mips}
  385. {$ifdef jvm}
  386. cputype : cpu_none;
  387. optimizecputype : cpu_none;
  388. fputype : fpu_standard;
  389. {$endif jvm}
  390. {$endif not GENERIC_CPU}
  391. asmmode : asmmode_standard;
  392. {$ifndef jvm}
  393. interfacetype : it_interfacecom;
  394. {$else jvm}
  395. interfacetype : it_interfacejava;
  396. {$endif jvm}
  397. defproccall : pocall_default;
  398. sourcecodepage : 28591;
  399. minfpconstprec : s32real;
  400. disabledircache : false;
  401. {$if defined(ARM) or defined(AVR)}
  402. controllertype : ct_none;
  403. {$endif defined(ARM) or defined(AVR)}
  404. pmessage : nil;
  405. );
  406. var
  407. starttime : real;
  408. function getdatestr:string;
  409. function gettimestr:string;
  410. function filetimestring( t : longint) : string;
  411. function getrealtime : real;
  412. procedure DefaultReplacements(var s:ansistring);
  413. function GetEnvPChar(const envname:ansistring):pchar;
  414. procedure FreeEnvPChar(p:pchar);
  415. function is_number_float(d : double) : boolean;
  416. { discern +0.0 and -0.0 }
  417. function get_real_sign(r: bestreal): longint;
  418. procedure InitGlobals;
  419. procedure DoneGlobals;
  420. function string2guid(const s: string; var GUID: TGUID): boolean;
  421. function guid2string(const GUID: TGUID): string;
  422. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  423. function Setabitype(const s:string;var a:tabi):boolean;
  424. function Setcputype(const s:string;var a:tcputype):boolean;
  425. function SetFpuType(const s:string;var a:tfputype):boolean;
  426. {$if defined(arm) or defined(avr)}
  427. function SetControllerType(const s:string;var a:tcontrollertype):boolean;
  428. {$endif defined(arm) or defined(avr)}
  429. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  430. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  431. function UpdateWpoStr(s: string; var a: twpoptimizerswitches): boolean;
  432. function UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  433. function UpdateTargetSwitchStr(s: string; var a: ttargetswitches): boolean;
  434. function IncludeFeature(const s : string) : boolean;
  435. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  436. {# Routine to get the required alignment for size of data, which will
  437. be placed in bss segment, according to the current alignment requirements }
  438. function var_align(want_align: longint): shortint;
  439. function var_align_size(siz: longint): shortint;
  440. {# Routine to get the required alignment for size of data, which will
  441. be placed in data/const segment, according to the current alignment requirements }
  442. function const_align(want_align: longint): shortint;
  443. function const_align_size(siz: longint): shortint;
  444. {$ifdef ARM}
  445. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  446. {$endif ARM}
  447. function floating_point_range_check_error : boolean;
  448. { hide Sysutils.ExecuteProcess in units using this one after SysUtils}
  449. const
  450. ExecuteProcess = 'Do not use' deprecated 'Use cfileutil.RequotedExecuteProcess instead, ExecuteProcess cannot deal with single quotes as used by Unix command lines';
  451. implementation
  452. uses
  453. {$ifdef macos}
  454. macutils,
  455. {$endif}
  456. {$ifdef mswindows}
  457. {$ifdef VER2_4}
  458. cwindirs,
  459. {$else VER2_4}
  460. windirs,
  461. {$endif VER2_4}
  462. {$endif}
  463. comphook;
  464. {****************************************************************************
  465. TLinkStrMap
  466. ****************************************************************************}
  467. Constructor TLinkStrMap.create;
  468. begin
  469. inherited;
  470. itemcnt:=0;
  471. end;
  472. procedure TLinkStrMap.Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  473. begin
  474. if lookup(key)<>-1 Then
  475. exit;
  476. if itemcnt<=length(fmap) Then
  477. setlength(fmap,itemcnt+10);
  478. fmap[itemcnt].key:=key;
  479. fmap[itemcnt].value:=value;
  480. fmap[itemcnt].weight:=weight;
  481. inc(itemcnt);
  482. end;
  483. function TLinkStrMap.AddDep(keyvalue:String):boolean;
  484. var
  485. i : Longint;
  486. begin
  487. AddDep:=false;
  488. i:=pos('=',keyvalue);
  489. if i=0 then
  490. exit;
  491. Add(Copy(KeyValue,1,i-1),Copy(KeyValue,i+1,length(KeyValue)-i));
  492. AddDep:=True;
  493. end;
  494. function TLinkStrMap.AddWeight(keyvalue:String):boolean;
  495. var
  496. i,j : Longint;
  497. Code : Word;
  498. s : AnsiString;
  499. begin
  500. AddWeight:=false;
  501. i:=pos('=',keyvalue);
  502. if i=0 then
  503. exit;
  504. s:=Copy(KeyValue,i+1,length(KeyValue)-i);
  505. val(s,j,code);
  506. if code=0 Then
  507. begin
  508. Add(Copy(KeyValue,1,i-1),'',j);
  509. AddWeight:=True;
  510. end;
  511. end;
  512. procedure TLinkStrMap.addseries(keys:AnsiString;weight:longint);
  513. var
  514. i,j,k : longint;
  515. begin
  516. k:=length(keys);
  517. i:=1;
  518. while i<=k do
  519. begin
  520. j:=i;
  521. while (i<=k) and (keys[i]<>',') do
  522. inc(i);
  523. add(copy(keys,j,i-j),'',weight);
  524. inc(i);
  525. end;
  526. end;
  527. procedure TLinkStrMap.SetValue(Key:Ansistring;weight:Integer);
  528. var
  529. j : longint;
  530. begin
  531. j:=lookup(key);
  532. if j<>-1 then
  533. fmap[j].weight:=weight;
  534. end;
  535. function TLinkStrMap.find(key:Ansistring):Ansistring;
  536. var
  537. j : longint;
  538. begin
  539. find:='';
  540. j:=lookup(key);
  541. if j<>-1 then
  542. find:=fmap[j].value;
  543. end;
  544. function TLinkStrMap.lookup(key:Ansistring):longint;
  545. var
  546. i : longint;
  547. begin
  548. lookup:=-1;
  549. i:=0;
  550. while (i<itemcnt) and (fmap[i].key<>key) do
  551. inc(i);
  552. if i<>itemcnt then
  553. lookup:=i;
  554. end;
  555. procedure TLinkStrMap.SortOnWeight;
  556. var
  557. i, j : longint;
  558. m : TLinkRec;
  559. begin
  560. if itemcnt <2 then exit;
  561. for i:=0 to itemcnt-1 do
  562. for j:=i+1 to itemcnt-1 do
  563. begin
  564. if fmap[i].weight>fmap[j].weight Then
  565. begin
  566. m:=fmap[i];
  567. fmap[i]:=fmap[j];
  568. fmap[j]:=m;
  569. end;
  570. end;
  571. end;
  572. function TLinkStrMap.getlinkrec(i:longint):TLinkRec;
  573. begin
  574. result:=fmap[i];
  575. end;
  576. procedure TLinkStrMap.Expand(Src:TCmdStrList;Dest:TLinkStrMap);
  577. // expands every thing in Src to Dest for linkorder purposes.
  578. var
  579. r : longint;
  580. LibN : TCmdStr;
  581. begin
  582. while not src.empty do
  583. begin
  584. LibN:=src.getfirst;
  585. r:=lookup (LibN);
  586. if r=-1 then
  587. dest.add(LibN)
  588. else
  589. dest.addseries(fmap[r].value);
  590. end;
  591. end;
  592. procedure TLinkStrMap.UpdateWeights(Weightmap:TLinkStrMap);
  593. var
  594. l,r : longint;
  595. begin
  596. for l := 0 to itemcnt-1 do
  597. begin
  598. r:=weightmap.lookup (fmap[l].key);
  599. if r<>-1 then
  600. fmap[l].weight:=weightmap[r].weight;
  601. end;
  602. end;
  603. {****************************************************************************
  604. Time Handling
  605. ****************************************************************************}
  606. Function L0(l:longint):string;
  607. {
  608. return the string of value l, if l<10 then insert a zero, so
  609. the string is always at least 2 chars '01','02',etc
  610. }
  611. var
  612. s : string;
  613. begin
  614. Str(l,s);
  615. if l<10 then
  616. s:='0'+s;
  617. L0:=s;
  618. end;
  619. function gettimestr:string;
  620. {
  621. get the current time in a string HH:MM:SS
  622. }
  623. var
  624. hour,min,sec,hsec : word;
  625. begin
  626. DecodeTime(Time,hour,min,sec,hsec);
  627. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  628. end;
  629. function getdatestr:string;
  630. {
  631. get the current date in a string YY/MM/DD
  632. }
  633. var
  634. Year,Month,Day: Word;
  635. begin
  636. DecodeDate(Date,year,month,day);
  637. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  638. end;
  639. function filetimestring( t : longint) : string;
  640. {
  641. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  642. }
  643. var
  644. DT : TDateTime;
  645. hsec : word;
  646. Year,Month,Day: Word;
  647. hour,min,sec : word;
  648. begin
  649. if t=-1 then
  650. begin
  651. Result := 'Not Found';
  652. exit;
  653. end;
  654. DT := FileDateToDateTime(t);
  655. DecodeTime(DT,hour,min,sec,hsec);
  656. DecodeDate(DT,year,month,day);
  657. Result := L0(Year)+'/'+L0(Month)+'/'+L0(Day)+' '+L0(Hour)+':'+L0(min)+':'+L0(sec);
  658. end;
  659. function getrealtime : real;
  660. var
  661. h,m,s,s1000 : word;
  662. begin
  663. DecodeTime(Time,h,m,s,s1000);
  664. result:=h*3600.0+m*60.0+s+s1000/1000.0;
  665. end;
  666. {****************************************************************************
  667. Default Macro Handling
  668. ****************************************************************************}
  669. procedure DefaultReplacements(var s:ansistring);
  670. {$ifdef mswindows}
  671. procedure ReplaceSpecialFolder(const MacroName: string; const ID: integer);
  672. begin
  673. // Only try to receive the special folders (and thus dynamically
  674. // load shfolder.dll) when that's needed.
  675. if pos(MacroName,s)>0 then
  676. Replace(s,MacroName,GetWindowsSpecialDir(ID));
  677. end;
  678. {$endif mswindows}
  679. var
  680. envstr: string;
  681. envvalue: pchar;
  682. i: integer;
  683. begin
  684. { Replace some macros }
  685. Replace(s,'$FPCVERSION',version_string);
  686. Replace(s,'$FPCFULLVERSION',full_version_string);
  687. Replace(s,'$FPCDATE',date_string);
  688. Replace(s,'$FPCCPU',target_cpu_string);
  689. Replace(s,'$FPCOS',target_os_string);
  690. if (tf_use_8_3 in Source_Info.Flags) or
  691. (tf_use_8_3 in Target_Info.Flags) then
  692. Replace(s,'$FPCTARGET',target_os_string)
  693. else
  694. Replace(s,'$FPCTARGET',target_full_string);
  695. {$ifdef mswindows}
  696. ReplaceSpecialFolder('$LOCAL_APPDATA',CSIDL_LOCAL_APPDATA);
  697. ReplaceSpecialFolder('$APPDATA',CSIDL_APPDATA);
  698. ReplaceSpecialFolder('$COMMON_APPDATA',CSIDL_COMMON_APPDATA);
  699. ReplaceSpecialFolder('$PERSONAL',CSIDL_PERSONAL);
  700. ReplaceSpecialFolder('$PROGRAM_FILES',CSIDL_PROGRAM_FILES);
  701. ReplaceSpecialFolder('$PROGRAM_FILES_COMMON',CSIDL_PROGRAM_FILES_COMMON);
  702. ReplaceSpecialFolder('$PROFILE',CSIDL_PROFILE);
  703. {$endif mswindows}
  704. { Replace environment variables between dollar signs }
  705. i := pos('$',s);
  706. while i>0 do
  707. begin
  708. envstr:=copy(s,i+1,length(s)-i);
  709. i:=pos('$',envstr);
  710. if i>0 then
  711. begin
  712. envstr := copy(envstr,1,i-1);
  713. envvalue := GetEnvPChar(envstr);
  714. if assigned(envvalue) then
  715. begin
  716. Replace(s,'$'+envstr+'$',envvalue);
  717. // Look if there is another env.var in the string
  718. i:=pos('$',s);
  719. end
  720. else
  721. // if the env.var is not set, do not replace the env.variable
  722. // and stop looking for more env.var within the string
  723. i := 0;
  724. FreeEnvPChar(envvalue);
  725. end;
  726. end;
  727. end;
  728. {****************************************************************************
  729. OS Dependent things
  730. ****************************************************************************}
  731. function GetEnvPChar(const envname:ansistring):pchar;
  732. {$ifdef mswindows}
  733. var
  734. s : string;
  735. i,len : longint;
  736. hp,p,p2 : pchar;
  737. {$endif}
  738. begin
  739. {$ifdef hasunix}
  740. GetEnvPchar:=BaseUnix.fpGetEnv(pansichar(envname));
  741. {$define GETENVOK}
  742. {$endif}
  743. {$ifdef mswindows}
  744. GetEnvPchar:=nil;
  745. p:=GetEnvironmentStrings;
  746. hp:=p;
  747. while hp^<>#0 do
  748. begin
  749. s:=strpas(hp);
  750. i:=pos('=',s);
  751. len:=strlen(hp);
  752. if upper(copy(s,1,i-1))=upper(envname) then
  753. begin
  754. GetMem(p2,len-length(envname));
  755. Move(hp[i],p2^,len-length(envname));
  756. GetEnvPchar:=p2;
  757. break;
  758. end;
  759. { next string entry}
  760. hp:=hp+len+1;
  761. end;
  762. FreeEnvironmentStrings(p);
  763. {$define GETENVOK}
  764. {$endif}
  765. {$ifdef os2}
  766. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  767. {$define GETENVOK}
  768. {$endif}
  769. {$ifdef GETENVOK}
  770. {$undef GETENVOK}
  771. {$else}
  772. GetEnvPchar:=StrPNew(GetEnvironmentVariable(envname));
  773. {$endif}
  774. end;
  775. procedure FreeEnvPChar(p:pchar);
  776. begin
  777. {$ifndef hasunix}
  778. {$ifndef os2}
  779. freemem(p);
  780. {$endif}
  781. {$endif}
  782. end;
  783. {$if defined(MORPHOS) or defined(AMIGA)}
  784. {$define AMIGASHELL}
  785. {$endif}
  786. {$UNDEF AMIGASHELL}
  787. function is_number_float(d : double) : boolean;
  788. var
  789. bytearray : array[0..7] of byte;
  790. begin
  791. move(d,bytearray,8);
  792. { only 1.1 save, 1.0.x will use always little endian }
  793. {$ifdef FPC_BIG_ENDIAN}
  794. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  795. {$else FPC_BIG_ENDIAN}
  796. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  797. {$endif FPC_BIG_ENDIAN}
  798. end;
  799. function get_real_sign(r: bestreal): longint;
  800. var
  801. p: pbyte;
  802. begin
  803. p := pbyte(@r);
  804. {$ifdef CPU_ARM}
  805. inc(p,4);
  806. {$else}
  807. {$ifdef FPC_LITTLE_ENDIAN}
  808. inc(p,sizeof(r)-1);
  809. {$endif}
  810. {$endif}
  811. if (p^ and $80) = 0 then
  812. result := 1
  813. else
  814. result := -1;
  815. end;
  816. function convertdoublerec(d : tdoublerec) : tdoublerec;{$ifdef USEINLINE}inline;{$endif}
  817. {$ifdef CPUARM}
  818. var
  819. i : longint;
  820. begin
  821. for i:=0 to 3 do
  822. begin
  823. result.bytes[i+4]:=d.bytes[i];
  824. result.bytes[i]:=d.bytes[i+4];
  825. end;
  826. {$else CPUARM}
  827. begin
  828. result:=d;
  829. {$endif CPUARM}
  830. end;
  831. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  832. function string2guid(const s: string; var GUID: TGUID): boolean;
  833. function ishexstr(const hs: string): boolean;
  834. var
  835. i: integer;
  836. begin
  837. ishexstr:=false;
  838. for i:=1 to Length(hs) do begin
  839. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  840. exit;
  841. end;
  842. ishexstr:=true;
  843. end;
  844. function hexstr2longint(const hexs: string): longint;
  845. var
  846. i: integer;
  847. rl: longint;
  848. begin
  849. rl:=0;
  850. for i:=1 to length(hexs) do begin
  851. rl:=rl shl 4;
  852. case hexs[i] of
  853. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  854. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  855. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  856. end
  857. end;
  858. hexstr2longint:=rl;
  859. end;
  860. var
  861. i: integer;
  862. begin
  863. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  864. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  865. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  866. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  867. ishexstr(copy(s,26,12)) then begin
  868. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  869. { these values are arealdy in the correct range (4 chars = word) }
  870. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  871. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  872. for i:=0 to 1 do
  873. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  874. for i:=2 to 7 do
  875. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  876. string2guid:=true;
  877. end
  878. else if (length(s)=0) then
  879. begin
  880. FillChar(GUID,SizeOf(GUID),0);
  881. string2guid:=true;
  882. end
  883. else
  884. string2guid:=false;
  885. end;
  886. function guid2string(const GUID: TGUID): string;
  887. begin
  888. guid2string:=
  889. '{'+hexstr(GUID.D1,8)+
  890. '-'+hexstr(GUID.D2,4)+
  891. '-'+hexstr(GUID.D3,4)+
  892. '-'+hexstr(GUID.D4[0],2)+hexstr(GUID.D4[1],2)+
  893. '-'+hexstr(GUID.D4[2],2)+hexstr(GUID.D4[3],2)+
  894. hexstr(GUID.D4[4],2)+hexstr(GUID.D4[5],2)+
  895. hexstr(GUID.D4[6],2)+hexstr(GUID.D4[7],2)+
  896. '}';
  897. end;
  898. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  899. const
  900. DefProcCallName : array[tproccalloption] of string[12] = ('',
  901. 'CDECL',
  902. 'CPPDECL',
  903. 'FAR16',
  904. 'OLDFPCCALL',
  905. '', { internproc }
  906. '', { syscall }
  907. 'PASCAL',
  908. 'REGISTER',
  909. 'SAFECALL',
  910. 'STDCALL',
  911. 'SOFTFLOAT',
  912. 'MWPASCAL',
  913. 'INTERRUPT'
  914. );
  915. var
  916. t : tproccalloption;
  917. hs : string;
  918. begin
  919. result:=false;
  920. if (s = '') then
  921. exit;
  922. hs:=upper(s);
  923. if (hs = 'DEFAULT') then
  924. begin
  925. a := pocall_default;
  926. result := true;
  927. exit;
  928. end;
  929. for t:=low(tproccalloption) to high(tproccalloption) do
  930. if DefProcCallName[t]=hs then
  931. begin
  932. a:=t;
  933. result:=true;
  934. break;
  935. end;
  936. end;
  937. function Setabitype(const s:string;var a:tabi):boolean;
  938. var
  939. t : tabi;
  940. hs : string;
  941. begin
  942. result:=false;
  943. hs:=Upper(s);
  944. for t:=low(t) to high(t) do
  945. if abi2str[t]=hs then
  946. begin
  947. a:=t;
  948. result:=true;
  949. break;
  950. end;
  951. end;
  952. function Setcputype(const s:string;var a:tcputype):boolean;
  953. var
  954. t : tcputype;
  955. hs : string;
  956. begin
  957. result:=false;
  958. hs:=Upper(s);
  959. for t:=low(tcputype) to high(tcputype) do
  960. if cputypestr[t]=hs then
  961. begin
  962. a:=t;
  963. result:=true;
  964. break;
  965. end;
  966. end;
  967. function SetFpuType(const s:string;var a:tfputype):boolean;
  968. var
  969. t : tfputype;
  970. begin
  971. result:=false;
  972. for t:=low(tfputype) to high(tfputype) do
  973. if fputypestr[t]=s then
  974. begin
  975. a:=t;
  976. result:=true;
  977. break;
  978. end;
  979. end;
  980. {$if defined(arm) or defined(avr)}
  981. function SetControllerType(const s:string;var a:tcontrollertype):boolean;
  982. var
  983. t : tcontrollertype;
  984. hs : string;
  985. begin
  986. result:=false;
  987. hs:=Upper(s);
  988. for t:=low(tcontrollertype) to high(tcontrollertype) do
  989. if embedded_controllers[t].controllertypestr=hs then
  990. begin
  991. a:=t;
  992. result:=true;
  993. break;
  994. end;
  995. end;
  996. {$endif defined(arm) or defined(avr)}
  997. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  998. var
  999. tok : string;
  1000. vstr : string;
  1001. l : longint;
  1002. code : integer;
  1003. b : talignmentinfo;
  1004. begin
  1005. UpdateAlignmentStr:=true;
  1006. uppervar(s);
  1007. fillchar(b,sizeof(b),0);
  1008. repeat
  1009. tok:=GetToken(s,'=');
  1010. if tok='' then
  1011. break;
  1012. vstr:=GetToken(s,',');
  1013. val(vstr,l,code);
  1014. if tok='PROC' then
  1015. b.procalign:=l
  1016. else if tok='JUMP' then
  1017. b.jumpalign:=l
  1018. else if tok='LOOP' then
  1019. b.loopalign:=l
  1020. else if tok='CONSTMIN' then
  1021. begin
  1022. b.constalignmin:=l;
  1023. if l>b.constalignmax then
  1024. b.constalignmax:=l;
  1025. end
  1026. else if tok='CONSTMAX' then
  1027. b.constalignmax:=l
  1028. else if tok='VARMIN' then
  1029. begin
  1030. b.varalignmin:=l;
  1031. if l>b.varalignmax then
  1032. b.varalignmax:=l;
  1033. end
  1034. else if tok='VARMAX' then
  1035. b.varalignmax:=l
  1036. else if tok='LOCALMIN' then
  1037. begin
  1038. b.localalignmin:=l;
  1039. if l>b.localalignmax then
  1040. b.localalignmax:=l;
  1041. end
  1042. else if tok='LOCALMAX' then
  1043. b.localalignmax:=l
  1044. else if tok='RECORDMIN' then
  1045. begin
  1046. b.recordalignmin:=l;
  1047. if l>b.recordalignmax then
  1048. b.recordalignmax:=l;
  1049. end
  1050. else if tok='RECORDMAX' then
  1051. b.recordalignmax:=l
  1052. else { Error }
  1053. UpdateAlignmentStr:=false;
  1054. until false;
  1055. Result:=Result and UpdateAlignment(a,b);
  1056. end;
  1057. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  1058. var
  1059. tok : string;
  1060. doset,
  1061. found : boolean;
  1062. opt : toptimizerswitch;
  1063. begin
  1064. result:=true;
  1065. uppervar(s);
  1066. repeat
  1067. tok:=GetToken(s,',');
  1068. if tok='' then
  1069. break;
  1070. if Copy(tok,1,2)='NO' then
  1071. begin
  1072. delete(tok,1,2);
  1073. doset:=false;
  1074. end
  1075. else
  1076. doset:=true;
  1077. found:=false;
  1078. for opt:=low(toptimizerswitch) to high(toptimizerswitch) do
  1079. begin
  1080. if OptimizerSwitchStr[opt]=tok then
  1081. begin
  1082. found:=true;
  1083. break;
  1084. end;
  1085. end;
  1086. if found then
  1087. begin
  1088. if doset then
  1089. include(a,opt)
  1090. else
  1091. exclude(a,opt);
  1092. end
  1093. else
  1094. result:=false;
  1095. until false;
  1096. end;
  1097. function UpdateWpoStr(s: string; var a: twpoptimizerswitches): boolean;
  1098. var
  1099. tok : string;
  1100. doset,
  1101. found : boolean;
  1102. opt : twpoptimizerswitch;
  1103. begin
  1104. result:=true;
  1105. uppervar(s);
  1106. repeat
  1107. tok:=GetToken(s,',');
  1108. if tok='' then
  1109. break;
  1110. if Copy(tok,1,2)='NO' then
  1111. begin
  1112. delete(tok,1,2);
  1113. doset:=false;
  1114. end
  1115. else
  1116. doset:=true;
  1117. found:=false;
  1118. if (tok = 'ALL') then
  1119. begin
  1120. for opt:=low(twpoptimizerswitch) to high(twpoptimizerswitch) do
  1121. if doset then
  1122. include(a,opt)
  1123. else
  1124. exclude(a,opt);
  1125. end
  1126. else
  1127. begin
  1128. for opt:=low(twpoptimizerswitch) to high(twpoptimizerswitch) do
  1129. begin
  1130. if WPOptimizerSwitchStr[opt]=tok then
  1131. begin
  1132. found:=true;
  1133. break;
  1134. end;
  1135. end;
  1136. if found then
  1137. begin
  1138. if doset then
  1139. include(a,opt)
  1140. else
  1141. exclude(a,opt);
  1142. end
  1143. else
  1144. result:=false;
  1145. end;
  1146. until false;
  1147. end;
  1148. function UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  1149. var
  1150. tok : string;
  1151. doset,
  1152. found : boolean;
  1153. opt : tdebugswitch;
  1154. begin
  1155. result:=true;
  1156. uppervar(s);
  1157. repeat
  1158. tok:=GetToken(s,',');
  1159. if tok='' then
  1160. break;
  1161. if Copy(tok,1,2)='NO' then
  1162. begin
  1163. delete(tok,1,2);
  1164. doset:=false;
  1165. end
  1166. else
  1167. doset:=true;
  1168. found:=false;
  1169. for opt:=low(tdebugswitch) to high(tdebugswitch) do
  1170. begin
  1171. if DebugSwitchStr[opt]=tok then
  1172. begin
  1173. found:=true;
  1174. break;
  1175. end;
  1176. end;
  1177. if found then
  1178. begin
  1179. if doset then
  1180. include(a,opt)
  1181. else
  1182. exclude(a,opt);
  1183. end
  1184. else
  1185. result:=false;
  1186. until false;
  1187. end;
  1188. function UpdateTargetSwitchStr(s: string; var a: ttargetswitches): boolean;
  1189. var
  1190. tok : string;
  1191. doset,
  1192. found : boolean;
  1193. opt : ttargetswitch;
  1194. begin
  1195. result:=true;
  1196. uppervar(s);
  1197. repeat
  1198. tok:=GetToken(s,',');
  1199. if tok='' then
  1200. break;
  1201. if Copy(tok,1,2)='NO' then
  1202. begin
  1203. delete(tok,1,2);
  1204. doset:=false;
  1205. end
  1206. else
  1207. doset:=true;
  1208. found:=false;
  1209. for opt:=low(ttargetswitch) to high(ttargetswitch) do
  1210. begin
  1211. if TargetSwitchStr[opt]=tok then
  1212. begin
  1213. found:=true;
  1214. break;
  1215. end;
  1216. end;
  1217. if found then
  1218. begin
  1219. if doset then
  1220. include(a,opt)
  1221. else
  1222. exclude(a,opt);
  1223. end
  1224. else
  1225. result:=false;
  1226. until false;
  1227. end;
  1228. function IncludeFeature(const s : string) : boolean;
  1229. var
  1230. i : tfeature;
  1231. begin
  1232. result:=true;
  1233. for i:=low(tfeature) to high(tfeature) do
  1234. if s=featurestr[i] then
  1235. begin
  1236. include(features,i);
  1237. exit;
  1238. end;
  1239. result:=false;
  1240. end;
  1241. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  1242. var
  1243. value, error: longint;
  1244. begin
  1245. if (upper(s)='DEFAULT') then
  1246. begin
  1247. a:=s32real;
  1248. result:=true;
  1249. exit;
  1250. end;
  1251. result:=false;
  1252. val(s,value,error);
  1253. if (error<>0) then
  1254. exit;
  1255. case value of
  1256. 32: a:=s32real;
  1257. 64: a:=s64real;
  1258. { adding support for 80 bit here is tricky, since we can't really }
  1259. { check whether the target cpu+OS actually supports it }
  1260. else
  1261. exit;
  1262. end;
  1263. result:=true;
  1264. end;
  1265. function var_align(want_align: longint): shortint;
  1266. begin
  1267. var_align := used_align(want_align,current_settings.alignment.varalignmin,current_settings.alignment.varalignmax);
  1268. end;
  1269. function var_align_size(siz: longint): shortint;
  1270. begin
  1271. siz := size_2_align(siz);
  1272. var_align_size := var_align(siz);
  1273. end;
  1274. function const_align(want_align: longint): shortint;
  1275. begin
  1276. const_align := used_align(want_align,current_settings.alignment.constalignmin,current_settings.alignment.constalignmax);
  1277. end;
  1278. function const_align_size(siz: longint): shortint;
  1279. begin
  1280. siz := size_2_align(siz);
  1281. const_align_size := const_align(siz);
  1282. end;
  1283. {$ifdef ARM}
  1284. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  1285. begin
  1286. result := (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  1287. not(cs_fp_emulation in current_settings.moduleswitches);
  1288. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  1289. { inverse result if compiler was compiled with swapped hilo already }
  1290. result := not result;
  1291. {$endif FPC_DOUBLE_HILO_SWAPPED}
  1292. end;
  1293. {$endif ARM}
  1294. function floating_point_range_check_error : boolean;
  1295. begin
  1296. result:=cs_ieee_errors in current_settings.localswitches;
  1297. end;
  1298. {****************************************************************************
  1299. Init
  1300. ****************************************************************************}
  1301. {$ifdef unix}
  1302. {$define need_path_search}
  1303. {$endif unix}
  1304. {$ifdef os2}
  1305. {$define need_path_search}
  1306. {$endif os2}
  1307. {$ifdef macos}
  1308. {$define need_path_search}
  1309. {$endif macos}
  1310. procedure get_exepath;
  1311. var
  1312. localExepath : TCmdStr;
  1313. exeName:TCmdStr;
  1314. {$ifdef need_path_search}
  1315. hs1 : TPathStr;
  1316. {$endif need_path_search}
  1317. begin
  1318. localexepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  1319. if localexepath='' then
  1320. begin
  1321. exeName := FixFileName(system.paramstr(0));
  1322. localexepath := ExtractFilePath(exeName);
  1323. end;
  1324. {$ifdef need_path_search}
  1325. if localexepath='' then
  1326. begin
  1327. hs1 := ExtractFileName(exeName);
  1328. ChangeFileExt(hs1,source_info.exeext);
  1329. {$ifdef macos}
  1330. FindFile(hs1,GetEnvironmentVariable('Commands'),false,localExepath);
  1331. {$else macos}
  1332. FindFile(hs1,GetEnvironmentVariable('PATH'),false,localExepath);
  1333. {$endif macos}
  1334. localExepath:=ExtractFilePath(localExepath);
  1335. end;
  1336. {$endif need_path_search}
  1337. exepath:=FixPath(localExepath,false);
  1338. end;
  1339. procedure DoneGlobals;
  1340. begin
  1341. librarysearchpath.Free;
  1342. unitsearchpath.Free;
  1343. objectsearchpath.Free;
  1344. includesearchpath.Free;
  1345. frameworksearchpath.Free;
  1346. LinkLibraryAliases.Free;
  1347. LinkLibraryOrder.Free;
  1348. end;
  1349. procedure InitGlobals;
  1350. begin
  1351. get_exepath;
  1352. { reset globals }
  1353. do_build:=false;
  1354. do_release:=false;
  1355. do_make:=true;
  1356. compile_level:=0;
  1357. codegenerror:=false;
  1358. DLLsource:=false;
  1359. paratarget:=system_none;
  1360. paratargetasm:=as_none;
  1361. paratargetdbg:=dbg_none;
  1362. { Output }
  1363. OutputFileName:='';
  1364. OutputPrefix:=Nil;
  1365. OutputSuffix:=Nil;
  1366. OutputExeDir:='';
  1367. OutputUnitDir:='';
  1368. { Utils directory }
  1369. utilsdirectory:='';
  1370. utilsprefix:='';
  1371. cshared:=false;
  1372. rlinkpath:='';
  1373. sysrootpath:='';
  1374. { Search Paths }
  1375. librarysearchpath:=TSearchPathList.Create;
  1376. unitsearchpath:=TSearchPathList.Create;
  1377. includesearchpath:=TSearchPathList.Create;
  1378. objectsearchpath:=TSearchPathList.Create;
  1379. frameworksearchpath:=TSearchPathList.Create;
  1380. { Def file }
  1381. usewindowapi:=false;
  1382. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1383. DescriptionSetExplicity:=false;
  1384. SetPEFlagsSetExplicity:=false;
  1385. ImageBaseSetExplicity:=false;
  1386. MinStackSizeSetExplicity:=false;
  1387. MaxStackSizeSetExplicity:=false;
  1388. dllversion:='';
  1389. dllmajor:=1;
  1390. dllminor:=0;
  1391. dllrevision:=0;
  1392. nwscreenname := '';
  1393. nwthreadname := '';
  1394. nwcopyright := '';
  1395. UseDeffileForExports:=false;
  1396. UseDeffileForExportsSetExplicitly:=false;
  1397. GenerateImportSection:=false;
  1398. RelocSection:=false;
  1399. RelocSectionSetExplicitly:=false;
  1400. LinkTypeSetExplicitly:=false;
  1401. MacOSXVersionMin:='';
  1402. iPhoneOSVersionMin:='';
  1403. { memory sizes, will be overridden by parameter or default for target
  1404. in options or init_parser }
  1405. stacksize:=0;
  1406. { not initialized yet }
  1407. jmp_buf_size:=-1;
  1408. apptype:=app_cui;
  1409. { Init values }
  1410. init_settings:=default_settings;
  1411. if init_settings.optimizecputype=cpu_none then
  1412. init_settings.optimizecputype:=init_settings.cputype;
  1413. LinkLibraryAliases :=TLinkStrMap.Create;
  1414. LinkLibraryOrder :=TLinkStrMap.Create;
  1415. { enable all features by default }
  1416. features:=[low(Tfeature)..high(Tfeature)];
  1417. end;
  1418. end.