globals.pas 55 KB

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