globals.pas 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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. type
  96. { this is written to ppus during token recording for generics so it must be packed }
  97. tsettings = packed record
  98. alignment : talignmentinfo;
  99. globalswitches : tglobalswitches;
  100. targetswitches : ttargetswitches;
  101. moduleswitches : tmoduleswitches;
  102. localswitches : tlocalswitches;
  103. modeswitches : tmodeswitches;
  104. optimizerswitches : toptimizerswitches;
  105. { generate information necessary to perform these wpo's during a subsequent compilation }
  106. genwpoptimizerswitches: twpoptimizerswitches;
  107. { perform these wpo's using information generated during a previous compilation }
  108. dowpoptimizerswitches: twpoptimizerswitches;
  109. debugswitches : tdebugswitches;
  110. { 0: old behaviour for sets <=256 elements
  111. >0: round to this size }
  112. setalloc,
  113. packenum : shortint;
  114. packrecords : shortint;
  115. maxfpuregisters : shortint;
  116. cputype,
  117. optimizecputype : tcputype;
  118. fputype : tfputype;
  119. asmmode : tasmmode;
  120. interfacetype : tinterfacetypes;
  121. defproccall : tproccalloption;
  122. sourcecodepage : tstringencoding;
  123. minfpconstprec : tfloattype;
  124. disabledircache : boolean;
  125. { CPU targets with microcontroller support can add a controller specific unit }
  126. {$if defined(ARM) or defined(AVR)}
  127. controllertype : tcontrollertype;
  128. {$endif defined(ARM) or defined(AVR)}
  129. { WARNING: this pointer cannot be written as such in record token }
  130. pmessage : pmessagestaterecord;
  131. end;
  132. const
  133. LinkMapWeightDefault = 1000;
  134. type
  135. TLinkRec = record
  136. Key : AnsiString;
  137. Value : AnsiString; // key expands to valuelist "value"
  138. Weight: longint;
  139. end;
  140. TLinkStrMap = class
  141. private
  142. itemcnt : longint;
  143. fmap : Array Of TLinkRec;
  144. function Lookup(key:Ansistring):longint;
  145. function getlinkrec(i:longint):TLinkRec;
  146. public
  147. procedure Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  148. procedure addseries(keys:AnsiString;weight:longint=LinkMapWeightDefault);
  149. function AddDep(keyvalue:String):boolean;
  150. function AddWeight(keyvalue:String):boolean;
  151. procedure SetValue(key:AnsiString;Weight:Integer);
  152. procedure SortonWeight;
  153. function Find(key:AnsiString):AnsiString;
  154. procedure Expand(src:TCmdStrList;dest: TLinkStrMap);
  155. procedure UpdateWeights(Weightmap:TLinkStrMap);
  156. constructor Create;
  157. property count : longint read itemcnt;
  158. property items[I:longint]:TLinkRec read getlinkrec; default;
  159. end;
  160. tpendingstate = record
  161. nextverbositystr : shortstring;
  162. nextlocalswitches : tlocalswitches;
  163. nextverbosityfullswitch: longint;
  164. nextcallingstr : shortstring;
  165. nextmessagerecord : pmessagestaterecord;
  166. verbosityfullswitched,
  167. localswitcheschanged : boolean;
  168. end;
  169. var
  170. { specified inputfile }
  171. inputfilepath : string;
  172. inputfilename : string;
  173. { specified outputfile with -o parameter }
  174. outputfilename : string;
  175. outputprefix : pshortstring;
  176. outputsuffix : pshortstring;
  177. { specified with -FE or -FU }
  178. outputexedir : TPathStr;
  179. outputunitdir : TPathStr;
  180. { specified with -FW and -Fw }
  181. wpofeedbackinput,
  182. wpofeedbackoutput : TPathStr;
  183. { things specified with parameters }
  184. paratarget : tsystem;
  185. paratargetdbg : tdbg;
  186. paratargetasm : tasm;
  187. paralinkoptions : TCmdStr;
  188. paradynamiclinker : string;
  189. paraprintnodetree : byte;
  190. parapreprocess : boolean;
  191. printnodefile : text;
  192. { typical cross compiling params}
  193. { directory where the utils can be found (options -FD) }
  194. utilsdirectory : TPathStr;
  195. { targetname specific prefix used by these utils (options -XP<path>) }
  196. utilsprefix : TCmdStr;
  197. cshared : boolean; { pass --shared to ld to link C libs shared}
  198. Dontlinkstdlibpath: Boolean; { Don't add std paths to linkpath}
  199. rlinkpath : TCmdStr; { rpath-link linkdir override}
  200. sysrootpath : TCmdStr; { target system root to search dyn linker }
  201. { some flags for global compiler switches }
  202. do_build,
  203. do_release,
  204. do_make : boolean;
  205. { path for searching units, different paths can be seperated by ; }
  206. exepath : TPathStr; { Path to ppc }
  207. librarysearchpath,
  208. unitsearchpath,
  209. objectsearchpath,
  210. includesearchpath,
  211. frameworksearchpath : TSearchPathList;
  212. autoloadunits : string;
  213. { linking }
  214. usegnubinutils : boolean;
  215. forceforwardslash : boolean;
  216. usewindowapi : boolean;
  217. description : string;
  218. SetPEFlagsSetExplicity,
  219. ImageBaseSetExplicity,
  220. MinStackSizeSetExplicity,
  221. MaxStackSizeSetExplicity,
  222. DescriptionSetExplicity : boolean;
  223. dllversion : string;
  224. dllmajor,
  225. dllminor,
  226. dllrevision : word; { revision only for netware }
  227. { win pe }
  228. peflags : longint;
  229. minstacksize,
  230. maxstacksize,
  231. imagebase : puint;
  232. UseDeffileForExports : boolean;
  233. UseDeffileForExportsSetExplicitly : boolean;
  234. GenerateImportSection,
  235. GenerateImportSectionSetExplicitly,
  236. RelocSection : boolean;
  237. MacOSXVersionMin,
  238. iPhoneOSVersionMin: string[15];
  239. RelocSectionSetExplicitly : boolean;
  240. LinkTypeSetExplicitly : boolean;
  241. current_tokenpos, { position of the last token }
  242. current_filepos : tfileposinfo; { current position }
  243. nwscreenname : string;
  244. nwthreadname : string;
  245. nwcopyright : string;
  246. codegenerror : boolean; { true if there is an error reported }
  247. block_type : tblock_type; { type of currently parsed block }
  248. compile_level : word;
  249. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  250. current_exceptblock : integer; { the exceptblock number of the current block (0 if none) }
  251. LinkLibraryAliases : TLinkStrMap;
  252. LinkLibraryOrder : TLinkStrMap;
  253. init_settings,
  254. current_settings : tsettings;
  255. pendingstate : tpendingstate;
  256. { Memory sizes }
  257. heapsize,
  258. stacksize,
  259. jmp_buf_size,
  260. jmp_buf_align : longint;
  261. {$Ifdef EXTDEBUG}
  262. { parameter switches }
  263. debugstop : boolean;
  264. {$EndIf EXTDEBUG}
  265. { windows / OS/2 application type }
  266. apptype : tapptype;
  267. features : tfeatures;
  268. const
  269. DLLsource : boolean = false;
  270. { used to set all registers used for each global function
  271. this should dramatically decrease the number of
  272. recompilations needed PM }
  273. simplify_ppu : boolean = true;
  274. Inside_asm_statement : boolean = false;
  275. global_unit_count : word = 0;
  276. { for error info in pp.pas }
  277. parser_current_file : string = '';
  278. {$if defined(m68k) or defined(arm)}
  279. { PalmOS resources }
  280. palmos_applicationname : string = 'FPC Application';
  281. palmos_applicationid : string[4] = 'FPCA';
  282. {$endif defined(m68k) or defined(arm)}
  283. {$ifdef powerpc}
  284. { default calling convention used on MorphOS }
  285. syscall_convention : string = 'LEGACY';
  286. {$endif powerpc}
  287. { default name of the C-style "main" procedure of the library/program }
  288. { (this will be prefixed with the target_info.cprefix) }
  289. defaultmainaliasname = 'main';
  290. mainaliasname : string = defaultmainaliasname;
  291. { by default no local variable trashing }
  292. localvartrashing: longint = -1;
  293. { actual values are defined in ncgutil.pas }
  294. nroftrashvalues = 4;
  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.