globals.pas 45 KB

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