globals.pas 64 KB

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