globals.pas 46 KB

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