globals.pas 69 KB

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