globals.pas 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements some support functions and global variables
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit globals;
  19. {$i fpcdefs.inc}
  20. { Use the internal linker by default }
  21. { define INTERNALLINKER}
  22. interface
  23. uses
  24. {$ifdef win32}
  25. windows,
  26. {$endif}
  27. {$ifdef hasunix}
  28. {$ifdef havelinuxrtl10}
  29. linux,
  30. {$else}
  31. Baseunix,unix,
  32. {$endif}
  33. {$endif}
  34. {$ifdef Delphi}
  35. SysUtils,
  36. dmisc,
  37. {$else}
  38. strings,
  39. dos,
  40. {$endif}
  41. cutils,cclasses,
  42. globtype,version,systems,cpuinfo;
  43. const
  44. {$ifdef Splitheap}
  45. testsplit : boolean = false;
  46. {$endif Splitheap}
  47. delphimodeswitches : tmodeswitches=
  48. [m_delphi,m_all,m_class,m_objpas,m_result,m_string_pchar,
  49. m_pointer_2_procedure,m_autoderef,m_tp_procvar,m_initfinal,m_default_ansistring,
  50. m_out,m_default_para,m_duplicate_names,m_hintdirective,m_add_pointer];
  51. fpcmodeswitches : tmodeswitches=
  52. [m_fpc,m_all,m_string_pchar,m_nested_comment,m_repeat_forward,
  53. m_cvar_support,m_initfinal,m_add_pointer,m_hintdirective];
  54. objfpcmodeswitches : tmodeswitches=
  55. [m_objfpc,m_fpc,m_all,m_class,m_objpas,m_result,m_string_pchar,m_nested_comment,
  56. m_repeat_forward,m_cvar_support,m_initfinal,m_add_pointer,m_out,m_default_para,m_hintdirective];
  57. tpmodeswitches : tmodeswitches=
  58. [m_tp7,m_all,m_tp_procvar,m_duplicate_names];
  59. gpcmodeswitches : tmodeswitches=
  60. [m_gpc,m_all];
  61. macmodeswitches : tmodeswitches=
  62. [m_mac,m_all,m_result,m_repeat_forward,m_cvar_support];
  63. { maximum number of locals in bytes before warning is emitted }
  64. maxlocalsize = high(smallint);
  65. { maximum number of paras in bytes before warning is emitted }
  66. maxparasize = high(word);
  67. { maximum nesting of routines }
  68. maxnesting = 32;
  69. treelogfilename = 'tree.log';
  70. { I don't know if this endian dependend }
  71. MathQNaN : array[0..7] of byte = (0,0,0,0,0,0,252,255);
  72. MathInf : array[0..7] of byte = (0,0,0,0,0,0,240,127);
  73. MathNegInf : array[0..7] of byte = (0,0,0,0,0,0,240,255);
  74. type
  75. TFPUException = (exInvalidOp, exDenormalized, exZeroDivide,
  76. exOverflow, exUnderflow, exPrecision);
  77. TFPUExceptionMask = set of TFPUException;
  78. pfileposinfo = ^tfileposinfo;
  79. tfileposinfo = record
  80. line : longint;
  81. column : word;
  82. fileindex : word;
  83. { moduleindex : word; }
  84. end;
  85. TSearchPathList = class(TStringList)
  86. procedure AddPath(s:string;addfirst:boolean);overload;
  87. procedure AddPath(SrcPath,s:string;addfirst:boolean);overload;
  88. procedure AddList(list:TSearchPathList;addfirst:boolean);
  89. function FindFile(const f : string;var foundfile:string):boolean;
  90. end;
  91. tcodepagestring = string[20];
  92. var
  93. { specified inputfile }
  94. inputdir : dirstr;
  95. inputfile : namestr;
  96. inputextension : extstr;
  97. { specified outputfile with -o parameter }
  98. outputfile : namestr;
  99. outputprefix : pstring;
  100. outputsuffix : pstring;
  101. outputextension : namestr;
  102. { specified with -FE or -FU }
  103. outputexedir : dirstr;
  104. outputunitdir : dirstr;
  105. { things specified with parameters }
  106. paralinkoptions,
  107. paradynamiclinker : string;
  108. paraprintnodetree : byte;
  109. parapreprocess : boolean;
  110. printnodefile : text;
  111. { typical cross compiling params}
  112. { directory where the utils can be found (options -FD) }
  113. utilsdirectory : dirstr;
  114. { targetname specific prefix used by these utils (options -XP<path>) }
  115. utilsprefix : dirstr;
  116. cshared : boolean; { pass --shared to ld to link C libs shared}
  117. Dontlinkstdlibpath: Boolean; { Don't add std paths to linkpath}
  118. rlinkpath : dirstr; { rpath-link linkdir override}
  119. { some flags for global compiler switches }
  120. do_build,
  121. do_release,
  122. do_make : boolean;
  123. { path for searching units, different paths can be seperated by ; }
  124. exepath : dirstr; { Path to ppc }
  125. librarysearchpath,
  126. unitsearchpath,
  127. objectsearchpath,
  128. includesearchpath : TSearchPathList;
  129. { linking }
  130. usewindowapi : boolean;
  131. description : string;
  132. DescriptionSetExplicity : boolean;
  133. dllversion : string;
  134. dllmajor,
  135. dllminor,
  136. dllrevision : word; { revision only for netware }
  137. UseDeffileForExports : boolean;
  138. UseDeffileForExportsSetExplicitly : boolean;
  139. RelocSection : boolean;
  140. RelocSectionSetExplicitly : boolean;
  141. LinkTypeSetExplicitly : boolean;
  142. akttokenpos, { position of the last token }
  143. aktfilepos : tfileposinfo; { current position }
  144. nwscreenname : string;
  145. nwthreadname : string;
  146. nwcopyright : string;
  147. codegenerror : boolean; { true if there is an error reported }
  148. block_type : tblock_type; { type of currently parsed block }
  149. parsing_para_level : integer; { parameter level, used to convert
  150. proc calls to proc loads in firstcalln }
  151. compile_level : word;
  152. make_ref : boolean;
  153. resolving_forward : boolean; { used to add forward reference as second ref }
  154. inlining_procedure : boolean; { are we inlining a procedure }
  155. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  156. aktexceptblock : integer; { the exceptblock number of the current block (0 if none) }
  157. { commandline values }
  158. initdefines : tstringlist;
  159. initglobalswitches : tglobalswitches;
  160. initmoduleswitches : tmoduleswitches;
  161. initlocalswitches : tlocalswitches;
  162. initmodeswitches : tmodeswitches;
  163. {$IFDEF testvarsets}
  164. Initsetalloc, {0=fixed, 1 =var}
  165. {$ENDIF}
  166. initpackenum : shortint;
  167. {$ifdef ansistring_bits}
  168. initansistring_bits: Tstringbits;
  169. {$endif}
  170. initalignment : talignmentinfo;
  171. initoptprocessor,
  172. initspecificoptprocessor : tprocessors;
  173. initfputype : tfputype;
  174. initasmmode : tasmmode;
  175. initinterfacetype : tinterfacetypes;
  176. initoutputformat : tasm;
  177. initdefproccall : tproccalloption;
  178. initsourcecodepage : tcodepagestring;
  179. { current state values }
  180. aktglobalswitches : tglobalswitches;
  181. aktmoduleswitches : tmoduleswitches;
  182. aktlocalswitches : tlocalswitches;
  183. nextaktlocalswitches : tlocalswitches;
  184. localswitcheschanged : boolean;
  185. aktmodeswitches : tmodeswitches;
  186. {$IFDEF testvarsets}
  187. aktsetalloc,
  188. {$ENDIF}
  189. aktpackrecords,
  190. aktpackenum : longint;
  191. {$ifdef ansistring_bits}
  192. aktansistring_bits : Tstringbits;
  193. {$endif}
  194. aktmaxfpuregisters : longint;
  195. aktalignment : talignmentinfo;
  196. aktoptprocessor,
  197. aktspecificoptprocessor : tprocessors;
  198. aktfputype : tfputype;
  199. aktasmmode : tasmmode;
  200. aktinterfacetype : tinterfacetypes;
  201. aktoutputformat : tasm;
  202. aktdefproccall : tproccalloption;
  203. aktsourcecodepage : tcodepagestring;
  204. { Memory sizes }
  205. heapsize,
  206. stacksize : longint;
  207. {$Ifdef EXTDEBUG}
  208. { parameter switches }
  209. debugstop : boolean;
  210. {$EndIf EXTDEBUG}
  211. { windows / OS/2 application type }
  212. apptype : tapptype;
  213. const
  214. DLLsource : boolean = false;
  215. DLLImageBase : pstring = nil;
  216. { used to set all registers used for each global function
  217. this should dramatically decrease the number of
  218. recompilations needed PM }
  219. simplify_ppu : boolean = true;
  220. { should we allow non static members ? }
  221. allow_only_static : boolean = false;
  222. Inside_asm_statement : boolean = false;
  223. global_unit_count : word = 0;
  224. { for error info in pp.pas }
  225. parser_current_file : string = '';
  226. {$ifdef m68k}
  227. { PalmOS resources }
  228. palmos_applicationname : string = 'FPC Application';
  229. palmos_applicationid : string[4] = 'FPCA';
  230. {$endif m68k}
  231. procedure abstract;
  232. function bstoslash(const s : string) : string;
  233. function getdatestr:string;
  234. function gettimestr:string;
  235. function filetimestring( t : longint) : string;
  236. procedure DefaultReplacements(var s:string);
  237. function GetCurrentDir:string;
  238. function path_absolute(const s : string) : boolean;
  239. Function PathExists ( F : String) : Boolean;
  240. Function FileExists ( Const F : String) : Boolean;
  241. function FileExistsNonCase(const path,fn:string;var foundfile:string):boolean;
  242. Function RemoveFile(const f:string):boolean;
  243. Function RemoveDir(d:string):boolean;
  244. Function GetFileTime ( Var F : File) : Longint;
  245. Function GetNamedFileTime ( Const F : String) : Longint;
  246. Function SplitPath(const s:string):string;
  247. Function SplitFileName(const s:string):string;
  248. Function SplitName(const s:string):string;
  249. Function SplitExtension(Const HStr:String):String;
  250. Function AddExtension(Const HStr,ext:String):String;
  251. Function ForceExtension(Const HStr,ext:String):String;
  252. Function FixPath(s:string;allowdot:boolean):string;
  253. function FixFileName(const s:string):string;
  254. function TargetFixPath(s:string;allowdot:boolean):string;
  255. function TargetFixFileName(const s:string):string;
  256. procedure SplitBinCmd(const s:string;var bstr,cstr:string);
  257. function FindFile(const f : string;path : string;var foundfile:string):boolean;
  258. function FindFilePchar(const f : string;path : pchar;var foundfile:string):boolean;
  259. function FindExe(const bin:string;var foundfile:string):boolean;
  260. function GetShortName(const n:string):string;
  261. function Shell(const command:string): longint;
  262. function GetEnvPChar(const envname:string):pchar;
  263. procedure FreeEnvPChar(p:pchar);
  264. function SetFPUExceptionMask(const Mask : TFPUExceptionMask) : TFPUExceptionMask;
  265. function is_number_float(d : double) : boolean;
  266. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  267. function SetAktProcCall(const s:string; changeInit: boolean):boolean;
  268. function SetProcessor(const s:string; changeInit: boolean):boolean;
  269. function SetFpuType(const s:string; changeInit: boolean):boolean;
  270. procedure InitGlobals;
  271. procedure DoneGlobals;
  272. function string2guid(const s: string; var GUID: TGUID): boolean;
  273. function guid2string(const GUID: TGUID): string;
  274. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  275. {# Routine to get the required alignment for size of data, which will
  276. be placed in bss segment, according to the current alignment requirements }
  277. function var_align(siz: longint): longint;
  278. {# Routine to get the required alignment for size of data, which will
  279. be placed in data/const segment, according to the current alignment requirements }
  280. function const_align(siz: longint): longint;
  281. implementation
  282. uses
  283. {$ifdef macos}
  284. macutils,
  285. {$endif}
  286. comphook;
  287. procedure abstract;
  288. begin
  289. do_internalerror(255);
  290. end;
  291. procedure WarnNonExistingPath(const path : string);
  292. begin
  293. if assigned({$ifndef FPCPROCVAR}@{$endif}do_comment) then
  294. do_comment(V_Hint,'Path "'+path+'" not found');
  295. end;
  296. function bstoslash(const s : string) : string;
  297. {
  298. return string s with all \ changed into /
  299. }
  300. var
  301. i : longint;
  302. begin
  303. for i:=1to length(s) do
  304. if s[i]='\' then
  305. bstoslash[i]:='/'
  306. else
  307. bstoslash[i]:=s[i];
  308. bstoslash[0]:=s[0];
  309. end;
  310. {****************************************************************************
  311. Time Handling
  312. ****************************************************************************}
  313. Function L0(l:longint):string;
  314. {
  315. return the string of value l, if l<10 then insert a zero, so
  316. the string is always at least 2 chars '01','02',etc
  317. }
  318. var
  319. s : string;
  320. begin
  321. Str(l,s);
  322. if l<10 then
  323. s:='0'+s;
  324. L0:=s;
  325. end;
  326. function gettimestr:string;
  327. {
  328. get the current time in a string HH:MM:SS
  329. }
  330. var
  331. hour,min,sec,hsec : word;
  332. begin
  333. {$ifdef delphi}
  334. dmisc.gettime(hour,min,sec,hsec);
  335. {$else delphi}
  336. dos.gettime(hour,min,sec,hsec);
  337. {$endif delphi}
  338. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  339. end;
  340. function getdatestr:string;
  341. {
  342. get the current date in a string YY/MM/DD
  343. }
  344. var
  345. Year,Month,Day,Wday : Word;
  346. begin
  347. {$ifdef delphi}
  348. dmisc.getdate(year,month,day,wday);
  349. {$else}
  350. dos.getdate(year,month,day,wday);
  351. {$endif}
  352. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  353. end;
  354. function filetimestring( t : longint) : string;
  355. {
  356. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  357. }
  358. var
  359. DT : DateTime;
  360. begin
  361. if t=-1 then
  362. begin
  363. FileTimeString:='Not Found';
  364. exit;
  365. end;
  366. unpacktime(t,DT);
  367. filetimestring:=L0(dt.Year)+'/'+L0(dt.Month)+'/'+L0(dt.Day)+' '+L0(dt.Hour)+':'+L0(dt.min)+':'+L0(dt.sec);
  368. end;
  369. {****************************************************************************
  370. Default Macro Handling
  371. ****************************************************************************}
  372. procedure DefaultReplacements(var s:string);
  373. begin
  374. { Replace some macros }
  375. Replace(s,'$FPCVER',version_string);
  376. Replace(s,'$VERSION',version_string);
  377. Replace(s,'$FULLVERSION',full_version_string);
  378. Replace(s,'$FPCDATE',date_string);
  379. Replace(s,'$FPCTARGET',target_cpu_string);
  380. Replace(s,'$FPCCPU',target_cpu_string);
  381. Replace(s,'$TARGET',target_path);
  382. Replace(s,'$FPCOS',target_path);
  383. end;
  384. {****************************************************************************
  385. File Handling
  386. ****************************************************************************}
  387. function GetCurrentDir:string;
  388. var
  389. CurrentDir : string;
  390. begin
  391. GetDir(0,CurrentDir);
  392. GetCurrentDir:=FixPath(CurrentDir,false);
  393. end;
  394. function path_absolute(const s : string) : boolean;
  395. {
  396. is path s an absolute path?
  397. }
  398. begin
  399. path_absolute:=false;
  400. {$ifdef unix}
  401. if (length(s)>0) and (s[1]='/') then
  402. path_absolute:=true;
  403. {$else unix}
  404. {$ifdef amiga}
  405. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or (Pos(':',s) = length(s)) then
  406. path_absolute:=true;
  407. {$else}
  408. {$ifdef macos}
  409. if IsMacFullPath(s) then
  410. path_absolute:=true;
  411. {$else}
  412. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or
  413. ((length(s)>2) and (s[2]=':') and ((s[3]='\') or (s[3]='/'))) then
  414. path_absolute:=true;
  415. {$endif macos}
  416. {$endif amiga}
  417. {$endif unix}
  418. end;
  419. {$ifndef FPC}
  420. Procedure FindClose(var Info : SearchRec);
  421. Begin
  422. End;
  423. {$endif not FPC}
  424. Function FileExists ( Const F : String) : Boolean;
  425. Var
  426. res : boolean;
  427. {$ifndef delphi}
  428. Info : SearchRec;
  429. {$endif}
  430. begin
  431. {$ifdef delphi}
  432. res:=sysutils.FileExists(f);
  433. {$else}
  434. findfirst(F,readonly+archive+hidden,info);
  435. res:=(doserror=0);
  436. findclose(Info);
  437. {$endif delphi}
  438. if assigned({$ifndef FPCPROVCAR}@{$endif}do_comment) then
  439. begin
  440. if res then
  441. do_comment(V_Tried,'Searching file '+F+'... found')
  442. else
  443. do_comment(V_Tried,'Searching file '+F+'... not found');
  444. end;
  445. FileExists:=res;
  446. end;
  447. function FileExistsNonCase(const path,fn:string;var foundfile:string):boolean;
  448. var
  449. fn2 : string;
  450. begin
  451. result:=false;
  452. if source_info.files_case_relevent then
  453. begin
  454. {
  455. Search order for case sensitive systems:
  456. 1. NormalCase
  457. 2. lowercase
  458. 3. UPPERCASE
  459. }
  460. FoundFile:=path+fn;
  461. If FileExists(FoundFile) then
  462. begin
  463. result:=true;
  464. exit;
  465. end;
  466. fn2:=Lower(fn);
  467. if fn2<>fn then
  468. begin
  469. FoundFile:=path+fn2;
  470. If FileExists(FoundFile) then
  471. begin
  472. result:=true;
  473. exit;
  474. end;
  475. end;
  476. fn2:=Upper(fn);
  477. if fn2<>fn then
  478. begin
  479. FoundFile:=path+fn2;
  480. If FileExists(FoundFile) then
  481. begin
  482. result:=true;
  483. exit;
  484. end;
  485. end;
  486. end
  487. else
  488. begin
  489. { None case sensitive only lowercase }
  490. FoundFile:=path+Lower(fn);
  491. If FileExists(FoundFile) then
  492. begin
  493. result:=true;
  494. exit;
  495. end;
  496. end;
  497. { Set foundfile to something usefull }
  498. FoundFile:=fn;
  499. end;
  500. Function PathExists ( F : String) : Boolean;
  501. Var
  502. Info : SearchRec;
  503. disk : byte;
  504. begin
  505. { these operating systems have dos type drives }
  506. if source_info.system in [system_m68k_atari,system_i386_go32v2,
  507. system_i386_win32,system_i386_os2,
  508. system_i386_emx,system_i386_wdosx] then
  509. Begin
  510. if (Length(f)=3) and (F[2]=':') and (F[3] in ['/','\']) then
  511. begin
  512. if F[1] in ['A'..'Z'] then
  513. disk:=ord(F[1])-ord('A')+1
  514. else if F[1] in ['a'..'z'] then
  515. disk:=ord(F[1])-ord('a')+1
  516. else
  517. disk:=255;
  518. if disk=255 then
  519. PathExists:=false
  520. else
  521. PathExists:=(DiskSize(disk)<>-1);
  522. exit;
  523. end;
  524. end;
  525. if F[Length(f)] in ['/','\'] then
  526. Delete(f,length(f),1);
  527. findfirst(F,readonly+archive+hidden+directory,info);
  528. PathExists:=(doserror=0) and ((info.attr and directory)=directory);
  529. findclose(Info);
  530. end;
  531. Function RemoveFile(const f:string):boolean;
  532. var
  533. g : file;
  534. begin
  535. assign(g,f);
  536. {$I-}
  537. erase(g);
  538. {$I+}
  539. RemoveFile:=(ioresult=0);
  540. end;
  541. Function RemoveDir(d:string):boolean;
  542. begin
  543. if d[length(d)]=source_info.DirSep then
  544. Delete(d,length(d),1);
  545. {$I-}
  546. rmdir(d);
  547. {$I+}
  548. RemoveDir:=(ioresult=0);
  549. end;
  550. Function SplitPath(const s:string):string;
  551. var
  552. i : longint;
  553. begin
  554. i:=Length(s);
  555. while (i>0) and not(s[i] in ['/','\']) do
  556. dec(i);
  557. SplitPath:=Copy(s,1,i);
  558. end;
  559. Function SplitFileName(const s:string):string;
  560. var
  561. p : dirstr;
  562. n : namestr;
  563. e : extstr;
  564. begin
  565. FSplit(s,p,n,e);
  566. SplitFileName:=n+e;
  567. end;
  568. Function SplitName(const s:string):string;
  569. var
  570. i,j : longint;
  571. begin
  572. i:=Length(s);
  573. j:=Length(s);
  574. while (i>0) and not(s[i] in ['/','\']) do
  575. dec(i);
  576. while (j>0) and (s[j]<>'.') do
  577. dec(j);
  578. if j<=i then
  579. j:=255;
  580. SplitName:=Copy(s,i+1,j-(i+1));
  581. end;
  582. Function SplitExtension(Const HStr:String):String;
  583. var
  584. j : longint;
  585. begin
  586. j:=length(Hstr);
  587. while (j>0) and (Hstr[j]<>'.') do
  588. begin
  589. if hstr[j]=source_info.DirSep then
  590. j:=0
  591. else
  592. dec(j);
  593. end;
  594. if j=0 then
  595. j:=254;
  596. SplitExtension:=Copy(Hstr,j,255);
  597. end;
  598. Function AddExtension(Const HStr,ext:String):String;
  599. begin
  600. if (Ext<>'') and (SplitExtension(HStr)='') then
  601. AddExtension:=Hstr+Ext
  602. else
  603. AddExtension:=Hstr;
  604. end;
  605. Function ForceExtension(Const HStr,ext:String):String;
  606. var
  607. j : longint;
  608. begin
  609. j:=length(Hstr);
  610. while (j>0) and (Hstr[j]<>'.') do
  611. dec(j);
  612. if j=0 then
  613. j:=255;
  614. ForceExtension:=Copy(Hstr,1,j-1)+Ext;
  615. end;
  616. {Translates a unix or dos path to a mac path for use in MPW.
  617. If already a mac path, it does nothing. The origin of this
  618. algorithm will be put in macos/dos.pp, please update this
  619. from that, because there is some flaws in the algo below.}
  620. procedure TranslatePathToMac (var path: string);
  621. var
  622. slashPos, oldpos, newpos, oldlen: Integer;
  623. inname: Boolean;
  624. begin
  625. slashPos := Pos('/', path);
  626. if (slashPos <> 0) then {its a unix path}
  627. begin
  628. if slashPos = 1 then {its a full path}
  629. begin
  630. Delete(path,1,1);
  631. Insert('{Boot}', path, 1);
  632. end
  633. else {its a partial path}
  634. Insert('/', path, 1);
  635. end
  636. else
  637. begin
  638. slashPos := Pos('\', path);
  639. if (slashPos <> 0) then {its a dos path}
  640. begin
  641. if slashPos = 1 then {its a full path, without drive letter}
  642. begin
  643. Delete(path,1,1);
  644. Insert('{Boot}', path, 1);
  645. end
  646. else if (Length(path) >= 2) and (path[2] = ':') then {its a full path, with drive letter}
  647. begin
  648. Delete(path, 1, 2);
  649. Insert('{Boot}', path, 1)
  650. end
  651. else {its a partial path}
  652. Insert('/', path, 1);
  653. end;
  654. end;
  655. if (slashPos <> 0) then {its a unix or dos path}
  656. begin
  657. {Translate "/../" to "::" , "/./" to ":" and "/" to ":" ) in place. }
  658. oldlen := Length(path);
  659. newpos := 0;
  660. oldpos := 0;
  661. inname := false;
  662. while oldpos < oldlen do
  663. begin
  664. oldpos := oldpos + 1;
  665. case path[oldpos] of
  666. '.':
  667. if (((oldpos < oldlen) and (path[oldpos + 1] in ['.', '/', '\'])) or (oldpos = oldlen)) and not inname then
  668. begin {its really a lonely ".." or "."}
  669. {Skip two chars in any case. }
  670. {For ".." then ".." is skiped and for "." then "./" is skiped, this}
  671. {reqires the next char is a "/". Thats why a "/" was }
  672. {appended on the end above.}
  673. oldpos := oldpos + 1;
  674. end
  675. else {its part of a filename (hidden unix file, e g ".nisse")}
  676. begin
  677. inname := true;
  678. newpos := newpos + 1;
  679. path[newpos] := path[oldpos];
  680. end;
  681. '/', '\':
  682. begin
  683. inname := false;
  684. newpos := newpos + 1;
  685. path[newpos] := ':'; {Exchange to mac dir separator.}
  686. end;
  687. 'A'..'Z' :
  688. begin
  689. inname := true;
  690. newpos := newpos + 1;
  691. path[newpos] :=char(byte(path[oldpos])+32);
  692. end;
  693. else
  694. begin
  695. inname := true;
  696. newpos := newpos + 1;
  697. path[newpos] := path[oldpos];
  698. end;
  699. end;
  700. end;
  701. SetLength(path,newpos);
  702. end;
  703. end;
  704. Function FixPath(s:string;allowdot:boolean):string;
  705. var
  706. i : longint;
  707. begin
  708. { Fix separator }
  709. for i:=1 to length(s) do
  710. if s[i] in ['/','\'] then
  711. s[i]:=source_info.DirSep;
  712. { Fix ending / }
  713. if (length(s)>0) and (s[length(s)]<>source_info.DirSep) and
  714. (s[length(s)]<>':') then
  715. s:=s+source_info.DirSep;
  716. { Remove ./ }
  717. if (not allowdot) and (s='.'+source_info.DirSep) then
  718. s:='';
  719. { return }
  720. if source_info.files_case_relevent then
  721. FixPath:=s
  722. else
  723. FixPath:=Lower(s);
  724. end;
  725. function FixFileName(const s:string):string;
  726. var
  727. i : longint;
  728. begin
  729. if source_info.system = system_powerpc_MACOS then
  730. begin
  731. FixFileName:= s;
  732. TranslatePathToMac(FixFileName);
  733. end
  734. else if source_info.files_case_relevent then
  735. begin
  736. for i:=1 to length(s) do
  737. begin
  738. case s[i] of
  739. '/','\' :
  740. FixFileName[i]:=source_info.dirsep;
  741. else
  742. FixFileName[i]:=s[i];
  743. end;
  744. end;
  745. FixFileName[0]:=s[0];
  746. end
  747. else
  748. begin
  749. for i:=1 to length(s) do
  750. begin
  751. case s[i] of
  752. '/','\' :
  753. FixFileName[i]:=source_info.dirsep;
  754. 'A'..'Z' :
  755. FixFileName[i]:=char(byte(s[i])+32);
  756. else
  757. FixFileName[i]:=s[i];
  758. end;
  759. end;
  760. FixFileName[0]:=s[0];
  761. end;
  762. end;
  763. Function TargetFixPath(s:string;allowdot:boolean):string;
  764. var
  765. i : longint;
  766. begin
  767. { Fix separator }
  768. for i:=1 to length(s) do
  769. if s[i] in ['/','\'] then
  770. s[i]:=target_info.DirSep;
  771. { Fix ending / }
  772. if (length(s)>0) and (s[length(s)]<>target_info.DirSep) and
  773. (s[length(s)]<>':') then
  774. s:=s+target_info.DirSep;
  775. { Remove ./ }
  776. if (not allowdot) and (s='.'+target_info.DirSep) then
  777. s:='';
  778. { return }
  779. if target_info.files_case_relevent then
  780. TargetFixPath:=s
  781. else
  782. TargetFixPath:=Lower(s);
  783. end;
  784. function TargetFixFileName(const s:string):string;
  785. var
  786. i : longint;
  787. begin
  788. if target_info.system = system_powerpc_MACOS then
  789. begin
  790. TargetFixFileName:= s;
  791. TranslatePathToMac(TargetFixFileName);
  792. end
  793. else if target_info.files_case_relevent then
  794. begin
  795. for i:=1 to length(s) do
  796. begin
  797. case s[i] of
  798. '/','\' :
  799. TargetFixFileName[i]:=target_info.dirsep;
  800. else
  801. TargetFixFileName[i]:=s[i];
  802. end;
  803. end;
  804. TargetFixFileName[0]:=s[0];
  805. end
  806. else
  807. begin
  808. for i:=1 to length(s) do
  809. begin
  810. case s[i] of
  811. '/','\' :
  812. TargetFixFileName[i]:=target_info.dirsep;
  813. 'A'..'Z' :
  814. TargetFixFileName[i]:=char(byte(s[i])+32);
  815. else
  816. TargetFixFileName[i]:=s[i];
  817. end;
  818. end;
  819. TargetFixFileName[0]:=s[0];
  820. end;
  821. end;
  822. procedure SplitBinCmd(const s:string;var bstr,cstr:string);
  823. var
  824. i : longint;
  825. begin
  826. i:=pos(' ',s);
  827. if i>0 then
  828. begin
  829. bstr:=Copy(s,1,i-1);
  830. cstr:=Copy(s,i+1,length(s)-i);
  831. end
  832. else
  833. begin
  834. bstr:=s;
  835. cstr:='';
  836. end;
  837. end;
  838. procedure TSearchPathList.AddPath(s:string;addfirst:boolean);
  839. begin
  840. AddPath('',s,AddFirst);
  841. end;
  842. procedure TSearchPathList.AddPath(SrcPath,s:string;addfirst:boolean);
  843. var
  844. j : longint;
  845. hs,hsd,
  846. CurrentDir,
  847. CurrPath : string;
  848. subdirfound : boolean;
  849. dir : searchrec;
  850. hp : TStringListItem;
  851. procedure addcurrpath;
  852. begin
  853. if addfirst then
  854. begin
  855. Remove(currPath);
  856. Insert(currPath);
  857. end
  858. else
  859. begin
  860. { Check if already in path, then we don't add it }
  861. hp:=Find(currPath);
  862. if not assigned(hp) then
  863. Concat(currPath);
  864. end;
  865. end;
  866. begin
  867. if s='' then
  868. exit;
  869. { Support default macro's }
  870. DefaultReplacements(s);
  871. { get current dir }
  872. CurrentDir:=GetCurrentDir;
  873. repeat
  874. { get currpath }
  875. if addfirst then
  876. begin
  877. j:=length(s);
  878. while (j>0) and (s[j]<>';') do
  879. dec(j);
  880. CurrPath:=FixPath(Copy(s,j+1,length(s)-j),false);
  881. if j=0 then
  882. s:=''
  883. else
  884. System.Delete(s,j,length(s)-j+1);
  885. end
  886. else
  887. begin
  888. j:=Pos(';',s);
  889. if j=0 then
  890. j:=255;
  891. CurrPath:=SrcPath+FixPath(Copy(s,1,j-1),false);
  892. System.Delete(s,1,j);
  893. end;
  894. { fix pathname }
  895. if CurrPath='' then
  896. CurrPath:='.'+source_info.DirSep
  897. else
  898. begin
  899. CurrPath:=FixPath(FExpand(CurrPath),false);
  900. if (CurrentDir<>'') and (Copy(CurrPath,1,length(CurrentDir))=CurrentDir) then
  901. begin
  902. {$ifdef AMIGA}
  903. CurrPath:=CurrentDir+Copy(CurrPath,length(CurrentDir)+1,255);
  904. {$else}
  905. CurrPath:='.'+source_info.DirSep+Copy(CurrPath,length(CurrentDir)+1,255);
  906. {$endif}
  907. end;
  908. end;
  909. { wildcard adding ? }
  910. if pos('*',currpath)>0 then
  911. begin
  912. if currpath[length(currpath)]=source_info.dirsep then
  913. hs:=Copy(currpath,1,length(CurrPath)-1)
  914. else
  915. hs:=currpath;
  916. hsd:=SplitPath(hs);
  917. findfirst(hs,directory,dir);
  918. subdirfound:=false;
  919. while doserror=0 do
  920. begin
  921. if (dir.name<>'.') and
  922. (dir.name<>'..') and
  923. ((dir.attr and directory)<>0) then
  924. begin
  925. subdirfound:=true;
  926. currpath:=hsd+dir.name+source_info.dirsep;
  927. hp:=Find(currPath);
  928. if not assigned(hp) then
  929. AddCurrPath;
  930. end;
  931. findnext(dir);
  932. if not subdirfound then
  933. WarnNonExistingPath(currpath);
  934. end;
  935. FindClose(dir);
  936. end
  937. else
  938. begin
  939. if PathExists(currpath) then
  940. addcurrpath
  941. else
  942. WarnNonExistingPath(currpath);
  943. end;
  944. until (s='');
  945. end;
  946. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  947. var
  948. s : string;
  949. hl : TSearchPathList;
  950. hp,hp2 : TStringListItem;
  951. begin
  952. if list.empty then
  953. exit;
  954. { create temp and reverse the list }
  955. if addfirst then
  956. begin
  957. hl:=TSearchPathList.Create;
  958. hp:=TStringListItem(list.first);
  959. while assigned(hp) do
  960. begin
  961. hl.insert(hp.Str);
  962. hp:=TStringListItem(hp.next);
  963. end;
  964. while not hl.empty do
  965. begin
  966. s:=hl.GetFirst;
  967. Remove(s);
  968. Insert(s);
  969. end;
  970. hl.Free;
  971. end
  972. else
  973. begin
  974. hp:=TStringListItem(list.first);
  975. while assigned(hp) do
  976. begin
  977. hp2:=Find(hp.Str);
  978. { Check if already in path, then we don't add it }
  979. if not assigned(hp2) then
  980. Concat(hp.Str);
  981. hp:=TStringListItem(hp.next);
  982. end;
  983. end;
  984. end;
  985. function TSearchPathList.FindFile(const f : string;var foundfile:string):boolean;
  986. Var
  987. p : TStringListItem;
  988. begin
  989. FindFile:=false;
  990. p:=TStringListItem(first);
  991. while assigned(p) do
  992. begin
  993. result:=FileExistsNonCase(p.Str,f,FoundFile);
  994. if result then
  995. exit;
  996. p:=TStringListItem(p.next);
  997. end;
  998. { Return original filename if not found }
  999. FoundFile:=f;
  1000. end;
  1001. Function GetFileTime ( Var F : File) : Longint;
  1002. Var
  1003. {$ifdef hasunix}
  1004. info: Stat;
  1005. {$endif}
  1006. L : longint;
  1007. begin
  1008. {$ifdef hasunix}
  1009. {$IFDEF havelinuxrtl10}
  1010. FStat (F,Info);
  1011. L:=Info.Mtime;
  1012. {$ELSE}
  1013. FPFStat (F,Info);
  1014. L:=Info.st_Mtime;
  1015. {$ENDIF}
  1016. {$else}
  1017. GetFTime(f,l);
  1018. {$endif}
  1019. GetFileTime:=L;
  1020. end;
  1021. Function GetNamedFileTime (Const F : String) : Longint;
  1022. begin
  1023. GetNamedFileTime:=do_getnamedfiletime(F);
  1024. end;
  1025. function FindFile(const f : string;path : string;var foundfile:string):boolean;
  1026. Var
  1027. singlepathstring : string;
  1028. i : longint;
  1029. begin
  1030. {$ifdef Unix}
  1031. for i:=1 to length(path) do
  1032. if path[i]=':' then
  1033. path[i]:=';';
  1034. {$endif Unix}
  1035. FindFile:=false;
  1036. repeat
  1037. i:=pos(';',path);
  1038. if i=0 then
  1039. i:=256;
  1040. singlepathstring:=FixPath(copy(path,1,i-1),false);
  1041. delete(path,1,i);
  1042. result:=FileExistsNonCase(singlepathstring,f,FoundFile);
  1043. if result then
  1044. exit;
  1045. until path='';
  1046. FoundFile:=f;
  1047. end;
  1048. function FindFilePchar(const f : string;path : pchar;var foundfile:string):boolean;
  1049. Var
  1050. flower,
  1051. singlepathstring : string;
  1052. startpc,pc : pchar;
  1053. sepch : char;
  1054. begin
  1055. {$ifdef Unix}
  1056. sepch:=':';
  1057. {$else}
  1058. {$ifdef macos}
  1059. sepch:=',';
  1060. {$else}
  1061. sepch:=';';
  1062. {$endif macos}
  1063. {$endif Unix}
  1064. FindFilePchar:=false;
  1065. pc:=path;
  1066. repeat
  1067. startpc:=pc;
  1068. while (pc^<>sepch) and (pc^<>';') and (pc^<>#0) do
  1069. inc(pc);
  1070. move(startpc^,singlepathstring[1],pc-startpc);
  1071. singlepathstring[0]:=char(longint(pc-startpc));
  1072. singlepathstring:=FixPath(singlepathstring,false);
  1073. result:=FileExistsNonCase(singlepathstring,f,FoundFile);
  1074. if result then
  1075. exit;
  1076. if (pc^=#0) then
  1077. break;
  1078. inc(pc);
  1079. until false;
  1080. foundfile:=f;
  1081. end;
  1082. function FindExe(const bin:string;var foundfile:string):boolean;
  1083. var
  1084. p : pchar;
  1085. found : boolean;
  1086. begin
  1087. found:=FindFile(FixFileName(AddExtension(bin,source_info.exeext)),'.;'+exepath,foundfile);
  1088. if not found then
  1089. begin
  1090. {$ifdef macos}
  1091. p:=GetEnvPchar('Commands');
  1092. {$else}
  1093. p:=GetEnvPchar('PATH');
  1094. {$endif}
  1095. found:=FindFilePChar(FixFileName(AddExtension(bin,source_info.exeext)),p,foundfile);
  1096. FreeEnvPChar(p);
  1097. end;
  1098. FindExe:=found;
  1099. end;
  1100. function GetShortName(const n:string):string;
  1101. {$ifdef win32}
  1102. var
  1103. hs,hs2 : string;
  1104. i : longint;
  1105. {$endif}
  1106. {$ifdef go32v2}
  1107. var
  1108. hs : string;
  1109. {$endif}
  1110. {$ifdef watcom}
  1111. var
  1112. hs : string;
  1113. {$endif}
  1114. begin
  1115. GetShortName:=n;
  1116. {$ifdef win32}
  1117. hs:=n+#0;
  1118. i:=Windows.GetShortPathName(@hs[1],@hs2[1],high(hs2));
  1119. if (i>0) and (i<=high(hs2)) then
  1120. begin
  1121. hs2[0]:=chr(strlen(@hs2[1]));
  1122. GetShortName:=hs2;
  1123. end;
  1124. {$endif}
  1125. {$ifdef go32v2}
  1126. hs:=n;
  1127. if Dos.GetShortName(hs) then
  1128. GetShortName:=hs;
  1129. {$endif}
  1130. {$ifdef watcom}
  1131. hs:=n;
  1132. if Dos.GetShortName(hs) then
  1133. GetShortName:=hs;
  1134. {$endif}
  1135. end;
  1136. {****************************************************************************
  1137. OS Dependent things
  1138. ****************************************************************************}
  1139. function GetEnvPChar(const envname:string):pchar;
  1140. {$ifdef win32}
  1141. var
  1142. s : string;
  1143. i,len : longint;
  1144. hp,p,p2 : pchar;
  1145. {$endif}
  1146. begin
  1147. {$ifdef hasunix}
  1148. GetEnvPchar:={$ifdef havelinuxrtl10}Linux.getenv{$else}BaseUnix.fpGetEnv{$endif}(envname);
  1149. {$define GETENVOK}
  1150. {$endif}
  1151. {$ifdef win32}
  1152. GetEnvPchar:=nil;
  1153. p:=GetEnvironmentStrings;
  1154. hp:=p;
  1155. while hp^<>#0 do
  1156. begin
  1157. s:=strpas(hp);
  1158. i:=pos('=',s);
  1159. len:=strlen(hp);
  1160. if upper(copy(s,1,i-1))=upper(envname) then
  1161. begin
  1162. GetMem(p2,len-length(envname));
  1163. Move(hp[i],p2^,len-length(envname));
  1164. GetEnvPchar:=p2;
  1165. break;
  1166. end;
  1167. { next string entry}
  1168. hp:=hp+len+1;
  1169. end;
  1170. FreeEnvironmentStrings(p);
  1171. {$define GETENVOK}
  1172. {$endif}
  1173. {$ifdef os2}
  1174. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  1175. {$define GETENVOK}
  1176. {$endif}
  1177. {$ifdef GETENVOK}
  1178. {$undef GETENVOK}
  1179. {$else}
  1180. GetEnvPchar:=StrPNew({$ifdef delphi}DMisc{$else}Dos{$endif}.Getenv(envname));
  1181. {$endif}
  1182. end;
  1183. procedure FreeEnvPChar(p:pchar);
  1184. begin
  1185. {$ifndef hasunix}
  1186. {$ifndef os2}
  1187. StrDispose(p);
  1188. {$endif}
  1189. {$endif}
  1190. end;
  1191. function Shell(const command:string): longint;
  1192. { This is already defined in the linux.ppu for linux, need for the *
  1193. expansion under linux }
  1194. {$ifdef hasunix}
  1195. begin
  1196. result := {$ifdef havelinuxrtl10}Linux{$else}Unix{$endif}.Shell(command);
  1197. end;
  1198. {$else}
  1199. {$ifdef amiga}
  1200. begin
  1201. exec('',command);
  1202. if (doserror <> 0) then
  1203. result := doserror
  1204. else
  1205. result := dosexitcode;
  1206. end;
  1207. {$else}
  1208. var
  1209. comspec : string;
  1210. begin
  1211. comspec:=getenv('COMSPEC');
  1212. Exec(comspec,' /C '+command);
  1213. if (doserror <> 0) then
  1214. result := doserror
  1215. else
  1216. result := dosexitcode;
  1217. end;
  1218. {$endif}
  1219. {$endif}
  1220. {$ifdef CPUI386}
  1221. { later, this should be replaced by the math unit }
  1222. const
  1223. Default8087CW : word = $1332;
  1224. {$ASMMODE ATT}
  1225. procedure Set8087CW(cw:word);assembler;
  1226. asm
  1227. movw cw,%ax
  1228. movw %ax,default8087cw
  1229. fnclex
  1230. fldcw default8087cw
  1231. end;
  1232. function Get8087CW:word;assembler;
  1233. asm
  1234. pushl $0
  1235. fnstcw (%esp)
  1236. popl %eax
  1237. end;
  1238. function SetFPUExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  1239. var
  1240. CtlWord: Word;
  1241. begin
  1242. CtlWord:=Get8087CW;
  1243. Set8087CW( (CtlWord and $FFC0) or Byte(Longint(Mask)) );
  1244. Result:=TFPUExceptionMask(Longint(CtlWord and $3F));
  1245. end;
  1246. {$else CPUI386}
  1247. {$ifdef CPUPOWERPC}
  1248. function SetFPUExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  1249. var
  1250. newmask: record
  1251. case byte of
  1252. 1: (d: double);
  1253. 2: (a,b: cardinal);
  1254. end;
  1255. begin
  1256. { load current control register contents }
  1257. asm
  1258. mffs f0
  1259. stfd f0,newmask.d
  1260. end;
  1261. { invalid operation: bit 24 (big endian, bit 0 = left-most bit) }
  1262. if (exInvalidOp in mask) then
  1263. newmask.b := newmask.b and not(1 shl (31-24))
  1264. else
  1265. newmask.b := newmask.b or (1 shl (31-24));
  1266. { denormals can not cause exceptions on the PPC }
  1267. { zero divide: bit 27 }
  1268. if (exZeroDivide in mask) then
  1269. newmask.b := newmask.b and not(1 shl (31-27))
  1270. else
  1271. newmask.b := newmask.b or (1 shl (31-27));
  1272. { overflow: bit 25 }
  1273. if (exOverflow in mask) then
  1274. newmask.b := newmask.b and not(1 shl (31-25))
  1275. else
  1276. newmask.b := newmask.b or (1 shl (31-25));
  1277. { underflow: bit 26 }
  1278. if (exUnderflow in mask) then
  1279. newmask.b := newmask.b and not(1 shl (31-26))
  1280. else
  1281. newmask.b := newmask.b or (1 shl (31-26));
  1282. { Precision (inexact result): bit 28 }
  1283. if (exPrecision in mask) then
  1284. newmask.b := newmask.b and not(1 shl (31-28))
  1285. else
  1286. newmask.b := newmask.b or (1 shl (31-28));
  1287. { update control register contents }
  1288. asm
  1289. lfd f0, newmask.d
  1290. mtfsf 255,f0
  1291. end;
  1292. end;
  1293. {$else CPUPOWERPC}
  1294. function SetFPUExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  1295. begin
  1296. end;
  1297. {$endif CPUPOWERPC}
  1298. {$endif CPUI386}
  1299. function is_number_float(d : double) : boolean;
  1300. var
  1301. bytearray : array[0..7] of byte;
  1302. begin
  1303. move(d,bytearray,8);
  1304. { only 1.1 save, 1.0.x will use always little endian }
  1305. {$ifdef FPC_BIG_ENDIAN}
  1306. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  1307. {$else FPC_BIG_ENDIAN}
  1308. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  1309. {$endif FPC_BIG_ENDIAN}
  1310. end;
  1311. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  1312. var
  1313. b : boolean;
  1314. begin
  1315. b:=true;
  1316. if s='DEFAULT' then
  1317. aktmodeswitches:=initmodeswitches
  1318. else
  1319. if s='DELPHI' then
  1320. aktmodeswitches:=delphimodeswitches
  1321. else
  1322. if s='TP' then
  1323. aktmodeswitches:=tpmodeswitches
  1324. else
  1325. if s='FPC' then
  1326. aktmodeswitches:=fpcmodeswitches
  1327. else
  1328. if s='OBJFPC' then
  1329. aktmodeswitches:=objfpcmodeswitches
  1330. else
  1331. if s='GPC' then
  1332. aktmodeswitches:=gpcmodeswitches
  1333. else
  1334. if s='MACPAS' then
  1335. aktmodeswitches:=macmodeswitches
  1336. else
  1337. b:=false;
  1338. if b and changeInit then
  1339. initmodeswitches := aktmodeswitches;
  1340. if b then
  1341. begin
  1342. { turn ansistrings on by default ? }
  1343. if (m_delphi in aktmodeswitches) then
  1344. begin
  1345. include(aktlocalswitches,cs_ansistrings);
  1346. if changeinit then
  1347. include(initlocalswitches,cs_ansistrings);
  1348. end
  1349. else
  1350. begin
  1351. exclude(aktlocalswitches,cs_ansistrings);
  1352. if changeinit then
  1353. exclude(initlocalswitches,cs_ansistrings);
  1354. end;
  1355. { Default enum packing for delphi/tp7 }
  1356. if (m_tp7 in aktmodeswitches) or
  1357. (m_delphi in aktmodeswitches) then
  1358. aktpackenum:=1
  1359. else
  1360. aktpackenum:=4;
  1361. if changeinit then
  1362. initpackenum:=aktpackenum;
  1363. {$ifdef i386}
  1364. { Default to intel assembler for delphi/tp7 on i386 }
  1365. if (m_delphi in aktmodeswitches) or
  1366. (m_tp7 in aktmodeswitches) then
  1367. aktasmmode:=asmmode_i386_intel;
  1368. if changeinit then
  1369. initasmmode:=aktasmmode;
  1370. {$endif i386}
  1371. end;
  1372. SetCompileMode:=b;
  1373. end;
  1374. function SetAktProcCall(const s:string; changeInit:boolean):boolean;
  1375. const
  1376. DefProcCallName : array[tproccalloption] of string[12] = ('',
  1377. 'CDECL',
  1378. 'CPPDECL',
  1379. '', { compilerproc }
  1380. 'FAR16',
  1381. 'FPCCALL',
  1382. 'INLINE',
  1383. '', { internproc }
  1384. '', { syscall }
  1385. 'PASCAL',
  1386. 'REGISTER',
  1387. 'SAFECALL',
  1388. 'STDCALL',
  1389. 'SOFTFLOAT'
  1390. );
  1391. var
  1392. t : tproccalloption;
  1393. begin
  1394. result:=false;
  1395. for t:=low(tproccalloption) to high(tproccalloption) do
  1396. if DefProcCallName[t]=s then
  1397. begin
  1398. AktDefProcCall:=t;
  1399. result:=true;
  1400. break;
  1401. end;
  1402. if changeinit then
  1403. InitDefProcCall:=AktDefProcCall;
  1404. end;
  1405. function SetProcessor(const s:string; changeInit: boolean):boolean;
  1406. var
  1407. t : tprocessors;
  1408. begin
  1409. SetProcessor:=false;
  1410. for t:=low(tprocessors) to high(tprocessors) do
  1411. if processorsstr[t]=s then
  1412. begin
  1413. aktspecificoptprocessor:=t;
  1414. SetProcessor:=true;
  1415. break;
  1416. end;
  1417. if changeinit then
  1418. initspecificoptprocessor:=aktspecificoptprocessor;
  1419. end;
  1420. function SetFpuType(const s:string; changeInit: boolean):boolean;
  1421. var
  1422. t : tfputype;
  1423. begin
  1424. SetFpuType:=false;
  1425. for t:=low(tfputype) to high(tfputype) do
  1426. if fputypestr[t]=s then
  1427. begin
  1428. aktfputype:=t;
  1429. SetFpuType:=true;
  1430. break;
  1431. end;
  1432. if changeinit then
  1433. initfputype:=aktfputype;
  1434. end;
  1435. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  1436. function string2guid(const s: string; var GUID: TGUID): boolean;
  1437. function ishexstr(const hs: string): boolean;
  1438. var
  1439. i: integer;
  1440. begin
  1441. ishexstr:=false;
  1442. for i:=1 to Length(hs) do begin
  1443. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  1444. exit;
  1445. end;
  1446. ishexstr:=true;
  1447. end;
  1448. function hexstr2longint(const hexs: string): longint;
  1449. var
  1450. i: integer;
  1451. rl: longint;
  1452. begin
  1453. rl:=0;
  1454. for i:=1 to length(hexs) do begin
  1455. rl:=rl shl 4;
  1456. case hexs[i] of
  1457. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  1458. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  1459. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  1460. end
  1461. end;
  1462. hexstr2longint:=rl;
  1463. end;
  1464. var
  1465. i: integer;
  1466. begin
  1467. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  1468. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  1469. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  1470. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  1471. ishexstr(copy(s,26,12)) then begin
  1472. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  1473. { these values are arealdy in the correct range (4 chars = word) }
  1474. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  1475. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  1476. for i:=0 to 1 do
  1477. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  1478. for i:=2 to 7 do
  1479. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  1480. string2guid:=true;
  1481. end
  1482. else
  1483. string2guid:=false;
  1484. end;
  1485. function guid2string(const GUID: TGUID): string;
  1486. function long2hex(l, len: longint): string;
  1487. const
  1488. hextbl: array[0..15] of char = '0123456789ABCDEF';
  1489. var
  1490. rs: string;
  1491. i: integer;
  1492. begin
  1493. rs[0]:=chr(len);
  1494. for i:=len downto 1 do begin
  1495. rs[i]:=hextbl[l and $F];
  1496. l:=l shr 4;
  1497. end;
  1498. long2hex:=rs;
  1499. end;
  1500. begin
  1501. guid2string:=
  1502. '{'+long2hex(GUID.D1,8)+
  1503. '-'+long2hex(GUID.D2,4)+
  1504. '-'+long2hex(GUID.D3,4)+
  1505. '-'+long2hex(GUID.D4[0],2)+long2hex(GUID.D4[1],2)+
  1506. '-'+long2hex(GUID.D4[2],2)+long2hex(GUID.D4[3],2)+
  1507. long2hex(GUID.D4[4],2)+long2hex(GUID.D4[5],2)+
  1508. long2hex(GUID.D4[6],2)+long2hex(GUID.D4[7],2)+
  1509. '}';
  1510. end;
  1511. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  1512. var
  1513. tok : string;
  1514. vstr : string;
  1515. l : longint;
  1516. code : integer;
  1517. b : talignmentinfo;
  1518. begin
  1519. UpdateAlignmentStr:=true;
  1520. uppervar(s);
  1521. fillchar(b,sizeof(b),0);
  1522. repeat
  1523. tok:=GetToken(s,'=');
  1524. if tok='' then
  1525. break;
  1526. vstr:=GetToken(s,',');
  1527. val(vstr,l,code);
  1528. if tok='PROC' then
  1529. b.procalign:=l
  1530. else if tok='JUMP' then
  1531. b.jumpalign:=l
  1532. else if tok='LOOP' then
  1533. b.loopalign:=l
  1534. else if tok='CONSTMIN' then
  1535. b.constalignmin:=l
  1536. else if tok='CONSTMAX' then
  1537. b.constalignmax:=l
  1538. else if tok='VARMIN' then
  1539. b.varalignmin:=l
  1540. else if tok='VARMAX' then
  1541. b.varalignmax:=l
  1542. else if tok='LOCALMIN' then
  1543. b.localalignmin:=l
  1544. else if tok='LOCALMAX' then
  1545. b.localalignmax:=l
  1546. else if tok='RECORDMIN' then
  1547. b.recordalignmin:=l
  1548. else if tok='RECORDMAX' then
  1549. b.recordalignmax:=l
  1550. else { Error }
  1551. UpdateAlignmentStr:=false;
  1552. until false;
  1553. UpdateAlignment(a,b);
  1554. end;
  1555. function var_align(siz: longint): longint;
  1556. begin
  1557. siz := size_2_align(siz);
  1558. var_align := used_align(siz,aktalignment.varalignmin,aktalignment.varalignmax);
  1559. end;
  1560. function const_align(siz: longint): longint;
  1561. begin
  1562. siz := size_2_align(siz);
  1563. const_align := used_align(siz,aktalignment.constalignmin,aktalignment.constalignmax);
  1564. end;
  1565. {****************************************************************************
  1566. Init
  1567. ****************************************************************************}
  1568. {$ifdef unix}
  1569. {$define need_path_search}
  1570. {$endif unix}
  1571. {$ifdef os2}
  1572. {$define need_path_search}
  1573. {$endif os2}
  1574. procedure get_exepath;
  1575. var
  1576. hs1 : namestr;
  1577. hs2 : extstr;
  1578. p : pchar;
  1579. begin
  1580. {$ifdef delphi}
  1581. exepath:=dmisc.getenv('PPC_EXEC_PATH');
  1582. {$else delphi}
  1583. exepath:=dos.getenv('PPC_EXEC_PATH');
  1584. {$endif delphi}
  1585. if exepath='' then
  1586. fsplit(FixFileName(system.paramstr(0)),exepath,hs1,hs2);
  1587. {$ifdef need_path_search}
  1588. if exepath='' then
  1589. begin
  1590. if pos(source_info.exeext,hs1) <>
  1591. (length(hs1) - length(source_info.exeext)+1) then
  1592. hs1 := hs1 + source_info.exeext;
  1593. p:=GetEnvPchar('PATH');
  1594. FindFilePChar(hs1,p,exepath);
  1595. FreeEnvPChar(p);
  1596. exepath:=SplitPath(exepath);
  1597. end;
  1598. {$endif need_path_search}
  1599. exepath:=FixPath(exepath,false);
  1600. end;
  1601. procedure DoneGlobals;
  1602. begin
  1603. initdefines.free;
  1604. if assigned(DLLImageBase) then
  1605. StringDispose(DLLImageBase);
  1606. librarysearchpath.Free;
  1607. unitsearchpath.Free;
  1608. objectsearchpath.Free;
  1609. includesearchpath.Free;
  1610. end;
  1611. procedure InitGlobals;
  1612. begin
  1613. get_exepath;
  1614. { reset globals }
  1615. do_build:=false;
  1616. do_release:=false;
  1617. do_make:=true;
  1618. compile_level:=0;
  1619. DLLsource:=false;
  1620. inlining_procedure:=false;
  1621. resolving_forward:=false;
  1622. make_ref:=false;
  1623. LinkTypeSetExplicitly:=false;
  1624. { Output }
  1625. OutputFile:='';
  1626. OutputPrefix:=Nil;
  1627. OutputSuffix:=Nil;
  1628. OutputExtension:='';
  1629. OutputExeDir:='';
  1630. OutputUnitDir:='';
  1631. { Utils directory }
  1632. utilsdirectory:='';
  1633. utilsprefix:='';
  1634. cshared:=false;
  1635. rlinkpath:='';
  1636. { Search Paths }
  1637. librarysearchpath:=TSearchPathList.Create;
  1638. unitsearchpath:=TSearchPathList.Create;
  1639. includesearchpath:=TSearchPathList.Create;
  1640. objectsearchpath:=TSearchPathList.Create;
  1641. { Def file }
  1642. usewindowapi:=false;
  1643. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1644. DescriptionSetExplicity:=false;
  1645. dllversion:='';
  1646. dllmajor:=1;
  1647. dllminor:=0;
  1648. dllrevision:=0;
  1649. nwscreenname := '';
  1650. nwthreadname := '';
  1651. nwcopyright := '';
  1652. UseDeffileForExports:=false;
  1653. UseDeffileForExportsSetExplicitly:=false;
  1654. RelocSection:=false;
  1655. RelocSectionSetExplicitly:=false;
  1656. LinkTypeSetExplicitly:=false;
  1657. { Init values }
  1658. initmodeswitches:=fpcmodeswitches;
  1659. initlocalswitches:=[cs_check_io,cs_typed_const_writable];
  1660. initmoduleswitches:=[cs_extsyntax,cs_implicit_exceptions];
  1661. initsourcecodepage:='8859-1';
  1662. initglobalswitches:=[cs_check_unit_name,cs_link_static{$ifdef INTERNALLINKER},cs_link_internal,cs_link_map{$endif}];
  1663. initoutputformat:=target_asm.id;
  1664. fillchar(initalignment,sizeof(talignmentinfo),0);
  1665. { might be overridden later }
  1666. initasmmode:=asmmode_standard;
  1667. {$ifdef i386}
  1668. initoptprocessor:=Class386;
  1669. initspecificoptprocessor:=Class386;
  1670. initfputype:=fpu_x87;
  1671. initpackenum:=4;
  1672. {$IFDEF testvarsets}
  1673. initsetalloc:=0;
  1674. {$ENDIF}
  1675. initasmmode:=asmmode_i386_att;
  1676. {$endif i386}
  1677. {$ifdef m68k}
  1678. initoptprocessor:=MC68020;
  1679. initpackenum:=4;
  1680. {$IFDEF testvarsets}
  1681. initsetalloc:=0;
  1682. {$ENDIF}
  1683. {$endif m68k}
  1684. {$ifdef powerpc}
  1685. initoptprocessor:=PPC604;
  1686. initpackenum:=4;
  1687. {$IFDEF testvarsets}
  1688. initsetalloc:=0;
  1689. {$ENDIF}
  1690. initfputype:=fpu_standard;
  1691. {$endif powerpc}
  1692. {$ifdef sparc}
  1693. initoptprocessor:=SPARC_V8;
  1694. initpackenum:=4;
  1695. {$IFDEF testvarsets}
  1696. initsetalloc:=0;
  1697. {$ENDIF}
  1698. {$endif sparc}
  1699. {$ifdef arm}
  1700. initpackenum:=4;
  1701. {$IFDEF testvarsets}
  1702. initsetalloc:=0;
  1703. {$ENDIF}
  1704. initfputype:=fpu_fpa;
  1705. {$endif arm}
  1706. {$ifdef x86_64}
  1707. initoptprocessor:=ClassAthlon64;
  1708. initspecificoptprocessor:=ClassAthlon64;
  1709. initfputype:=fpu_sse64;
  1710. initpackenum:=4;
  1711. {$IFDEF testvarsets}
  1712. initsetalloc:=0;
  1713. {$ENDIF}
  1714. initasmmode:=asmmode_x86_64_gas;
  1715. {$endif x86_64}
  1716. initinterfacetype:=it_interfacecom;
  1717. initdefproccall:=pocall_default;
  1718. initdefines:=TStringList.Create;
  1719. { memory sizes, will be overriden by parameter or default for target
  1720. in options or init_parser }
  1721. stacksize:=0;
  1722. heapsize:=0;
  1723. apptype:=app_cui;
  1724. end;
  1725. end.
  1726. {
  1727. $Log$
  1728. Revision 1.136 2004-08-28 20:25:25 peter
  1729. * optimized search for noncasesensitive names. It now searches
  1730. first for NormalCase and skips double tests
  1731. Revision 1.135 2004/08/20 10:29:31 olle
  1732. + made fpc work as an MPW tool, by itself calling asm and link.
  1733. * bugfix in fp exception flag settings routine
  1734. Revision 1.134 2004/08/02 07:15:54 michael
  1735. + Patch from Christian Iversen to implement LIBPREFIX/SUFFIX/EXTENSION directives
  1736. Revision 1.133 2004/07/17 15:51:57 jonas
  1737. * shell now returns an exitcode
  1738. * print an error if linking failed when linking was done using a script
  1739. Revision 1.132 2004/07/05 23:28:24 olle
  1740. + FixFileName now handles Mac OS paths
  1741. Revision 1.131 2004/06/20 08:55:29 florian
  1742. * logs truncated
  1743. Revision 1.130 2004/06/16 20:07:07 florian
  1744. * dwarf branch merged
  1745. Revision 1.129 2004/05/11 18:20:52 olle
  1746. * changed $mode mac to $mode macpas
  1747. * changed macmodeswitches to be more faithful to the mac dialect
  1748. Revision 1.128 2004/04/29 19:56:36 daniel
  1749. * Prepare compiler infrastructure for multiple ansistring types
  1750. Revision 1.127 2004/04/28 15:19:03 florian
  1751. + syscall directive support for MorphOS added
  1752. Revision 1.126.2.4 2004/05/09 15:47:21 peter
  1753. * fix typecast from word->set
  1754. }