globals.pas 45 KB

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