globals.pas 46 KB

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