globals.pas 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299
  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. {$ifdef USE_SYSUTILS}
  594. F := ExpandFileName(F);
  595. {$else USE_SYSUTILS}
  596. F := FExpand (F);
  597. {$endif USE_SYSUTILS}
  598. I := Pos (DriveSeparator, F);
  599. if (F [Length (F)] = DirectorySeparator)
  600. and (((I = 0) and (Length (F) > 1)) or (I <> Length (F) - 1))
  601. then
  602. Delete (F, Length (F), 1);
  603. {$IFDEF USE_SYSUTILS}
  604. PathExists := FileGetAttr(F) and faDirectory = faDirectory;
  605. {$ELSE USE_SYSUTILS}
  606. Assign (FF, FExpand (F));
  607. GetFAttr (FF, A);
  608. PathExists := (DosError = 0) and (A and Directory = Directory);
  609. {$ENDIF USE_SYSUTILS}
  610. end;
  611. Function RemoveFile(const f:string):boolean;
  612. var
  613. g : file;
  614. begin
  615. assign(g,f);
  616. {$I-}
  617. erase(g);
  618. {$I+}
  619. RemoveFile:=(ioresult=0);
  620. end;
  621. Function RemoveDir(d:string):boolean;
  622. begin
  623. if d[length(d)]=source_info.DirSep then
  624. Delete(d,length(d),1);
  625. {$I-}
  626. rmdir(d);
  627. {$I+}
  628. RemoveDir:=(ioresult=0);
  629. end;
  630. Function SplitPath(const s:string):string;
  631. var
  632. i : longint;
  633. begin
  634. i:=Length(s);
  635. {$ifdef macos}
  636. while (i>0) and not(s[i] in [':']) do
  637. dec(i);
  638. {$else macos}
  639. while (i>0) and not(s[i] in ['/','\']) do
  640. dec(i);
  641. {$endif macos}
  642. SplitPath:=Copy(s,1,i);
  643. end;
  644. Function SplitFileName(const s:string):string;
  645. {$IFDEF USE_SYSUTILS}
  646. {$ELSE USE_SYSUTILS}
  647. var
  648. p : dirstr;
  649. n : namestr;
  650. e : extstr;
  651. {$ENDIF USE_SYSUTILS}
  652. begin
  653. {$IFDEF USE_SYSUTILS}
  654. SplitFileName:=ExtractFileName(s);
  655. {$ELSE USE_SYSUTILS}
  656. FSplit(s,p,n,e);
  657. SplitFileName:=n+e;
  658. {$ENDIF USE_SYSUTILS}
  659. end;
  660. Function SplitName(const s:string):string;
  661. var
  662. i,j : longint;
  663. begin
  664. i:=Length(s);
  665. j:=Length(s);
  666. while (i>0) and not(s[i] in ['/','\']) do
  667. dec(i);
  668. while (j>0) and (s[j]<>'.') do
  669. dec(j);
  670. if j<=i then
  671. j:=255;
  672. SplitName:=Copy(s,i+1,j-(i+1));
  673. end;
  674. Function SplitExtension(Const HStr:String):String;
  675. var
  676. j : longint;
  677. begin
  678. j:=length(Hstr);
  679. while (j>0) and (Hstr[j]<>'.') do
  680. begin
  681. if hstr[j]=source_info.DirSep then
  682. j:=0
  683. else
  684. dec(j);
  685. end;
  686. if j=0 then
  687. j:=254;
  688. SplitExtension:=Copy(Hstr,j,255);
  689. end;
  690. Function AddExtension(Const HStr,ext:String):String;
  691. begin
  692. if (Ext<>'') and (SplitExtension(HStr)='') then
  693. AddExtension:=Hstr+Ext
  694. else
  695. AddExtension:=Hstr;
  696. end;
  697. Function ForceExtension(Const HStr,ext:String):String;
  698. var
  699. j : longint;
  700. begin
  701. j:=length(Hstr);
  702. while (j>0) and (Hstr[j]<>'.') do
  703. dec(j);
  704. if j=0 then
  705. j:=255;
  706. ForceExtension:=Copy(Hstr,1,j-1)+Ext;
  707. end;
  708. Function FixPath(s:string;allowdot:boolean):string;
  709. var
  710. i : longint;
  711. begin
  712. { Fix separator }
  713. for i:=1 to length(s) do
  714. if s[i] in ['/','\'] then
  715. s[i]:=source_info.DirSep;
  716. { Fix ending / }
  717. if (length(s)>0) and (s[length(s)]<>source_info.DirSep) and
  718. (s[length(s)]<>':') then
  719. s:=s+source_info.DirSep;
  720. { Remove ./ }
  721. if (not allowdot) and (s='.'+source_info.DirSep) then
  722. s:='';
  723. { return }
  724. if source_info.files_case_relevent then
  725. FixPath:=s
  726. else
  727. FixPath:=Lower(s);
  728. end;
  729. {Actually the version in macutils.pp could be used,
  730. but that would not work for crosscompiling, so this is a slightly modified
  731. version of it.}
  732. function TranslatePathToMac (const path: string; mpw: Boolean): string;
  733. function GetVolumeIdentifier: string;
  734. begin
  735. GetVolumeIdentifier := '{Boot}'
  736. (*
  737. if mpw then
  738. GetVolumeIdentifier := '{Boot}'
  739. else
  740. GetVolumeIdentifier := macosBootVolumeName;
  741. *)
  742. end;
  743. var
  744. slashPos, oldpos, newpos, oldlen, maxpos: Longint;
  745. begin
  746. oldpos := 1;
  747. slashPos := Pos('/', path);
  748. if (slashPos <> 0) then {its a unix path}
  749. begin
  750. if slashPos = 1 then
  751. begin {its a full path}
  752. oldpos := 2;
  753. TranslatePathToMac := GetVolumeIdentifier;
  754. end
  755. else {its a partial path}
  756. TranslatePathToMac := ':';
  757. end
  758. else
  759. begin
  760. slashPos := Pos('\', path);
  761. if (slashPos <> 0) then {its a dos path}
  762. begin
  763. if slashPos = 1 then
  764. begin {its a full path, without drive letter}
  765. oldpos := 2;
  766. TranslatePathToMac := GetVolumeIdentifier;
  767. end
  768. else if (Length(path) >= 2) and (path[2] = ':') then {its a full path, with drive letter}
  769. begin
  770. oldpos := 4;
  771. TranslatePathToMac := GetVolumeIdentifier;
  772. end
  773. else {its a partial path}
  774. TranslatePathToMac := ':';
  775. end;
  776. end;
  777. if (slashPos <> 0) then {its a unix or dos path}
  778. begin
  779. {Translate "/../" to "::" , "/./" to ":" and "/" to ":" }
  780. newpos := Length(TranslatePathToMac);
  781. oldlen := Length(path);
  782. SetLength(TranslatePathToMac, newpos + oldlen); {It will be no longer than what is already}
  783. {prepended plus length of path.}
  784. maxpos := Length(TranslatePathToMac); {Get real maxpos, can be short if String is ShortString}
  785. {There is never a slash in the beginning, because either it was an absolute path, and then the}
  786. {drive and slash was removed, or it was a relative path without a preceding slash.}
  787. while oldpos <= oldlen do
  788. begin
  789. {Check if special dirs, ./ or ../ }
  790. if path[oldPos] = '.' then
  791. if (oldpos + 1 <= oldlen) and (path[oldPos + 1] = '.') then
  792. begin
  793. if (oldpos + 2 > oldlen) or (path[oldPos + 2] in ['/', '\']) then
  794. begin
  795. {It is "../" or ".." translates to ":" }
  796. if newPos = maxPos then
  797. begin {Shouldn't actually happen, but..}
  798. Exit('');
  799. end;
  800. newPos := newPos + 1;
  801. TranslatePathToMac[newPos] := ':';
  802. oldPos := oldPos + 3;
  803. continue; {Start over again}
  804. end;
  805. end
  806. else if (oldpos + 1 > oldlen) or (path[oldPos + 1] in ['/', '\']) then
  807. begin
  808. {It is "./" or "." ignor it }
  809. oldPos := oldPos + 2;
  810. continue; {Start over again}
  811. end;
  812. {Collect file or dir name}
  813. while (oldpos <= oldlen) and not (path[oldPos] in ['/', '\']) do
  814. begin
  815. if newPos = maxPos then
  816. begin {Shouldn't actually happen, but..}
  817. Exit('');
  818. end;
  819. newPos := newPos + 1;
  820. TranslatePathToMac[newPos] := path[oldPos];
  821. oldPos := oldPos + 1;
  822. end;
  823. {When we come here there is either a slash or we are at the end.}
  824. if (oldpos <= oldlen) then
  825. begin
  826. if newPos = maxPos then
  827. begin {Shouldn't actually happen, but..}
  828. Exit('');
  829. end;
  830. newPos := newPos + 1;
  831. TranslatePathToMac[newPos] := ':';
  832. oldPos := oldPos + 1;
  833. end;
  834. end;
  835. SetLength(TranslatePathToMac, newpos);
  836. end
  837. else if (path = '.') then
  838. TranslatePathToMac := ':'
  839. else if (path = '..') then
  840. TranslatePathToMac := '::'
  841. else
  842. TranslatePathToMac := path; {its a mac path}
  843. end;
  844. function FixFileName(const s:string):string;
  845. var
  846. i : longint;
  847. begin
  848. if source_info.system = system_powerpc_MACOS then
  849. FixFileName:= TranslatePathToMac(s, true)
  850. else if source_info.files_case_relevent then
  851. begin
  852. for i:=1 to length(s) do
  853. begin
  854. case s[i] of
  855. '/','\' :
  856. FixFileName[i]:=source_info.dirsep;
  857. else
  858. FixFileName[i]:=s[i];
  859. end;
  860. end;
  861. FixFileName[0]:=s[0];
  862. end
  863. else
  864. begin
  865. for i:=1 to length(s) do
  866. begin
  867. case s[i] of
  868. '/','\' :
  869. FixFileName[i]:=source_info.dirsep;
  870. 'A'..'Z' :
  871. FixFileName[i]:=char(byte(s[i])+32);
  872. else
  873. FixFileName[i]:=s[i];
  874. end;
  875. end;
  876. FixFileName[0]:=s[0];
  877. end;
  878. end;
  879. Function TargetFixPath(s:string;allowdot:boolean):string;
  880. var
  881. i : longint;
  882. begin
  883. { Fix separator }
  884. for i:=1 to length(s) do
  885. if s[i] in ['/','\'] then
  886. s[i]:=target_info.DirSep;
  887. { Fix ending / }
  888. if (length(s)>0) and (s[length(s)]<>target_info.DirSep) and
  889. (s[length(s)]<>':') then
  890. s:=s+target_info.DirSep;
  891. { Remove ./ }
  892. if (not allowdot) and (s='.'+target_info.DirSep) then
  893. s:='';
  894. { return }
  895. if target_info.files_case_relevent then
  896. TargetFixPath:=s
  897. else
  898. TargetFixPath:=Lower(s);
  899. end;
  900. function TargetFixFileName(const s:string):string;
  901. var
  902. i : longint;
  903. begin
  904. if target_info.system = system_powerpc_MACOS then
  905. TargetFixFileName:= TranslatePathToMac(s, true)
  906. else if target_info.files_case_relevent then
  907. begin
  908. for i:=1 to length(s) do
  909. begin
  910. case s[i] of
  911. '/','\' :
  912. TargetFixFileName[i]:=target_info.dirsep;
  913. else
  914. TargetFixFileName[i]:=s[i];
  915. end;
  916. end;
  917. TargetFixFileName[0]:=s[0];
  918. end
  919. else
  920. begin
  921. for i:=1 to length(s) do
  922. begin
  923. case s[i] of
  924. '/','\' :
  925. TargetFixFileName[i]:=target_info.dirsep;
  926. 'A'..'Z' :
  927. TargetFixFileName[i]:=char(byte(s[i])+32);
  928. else
  929. TargetFixFileName[i]:=s[i];
  930. end;
  931. end;
  932. TargetFixFileName[0]:=s[0];
  933. end;
  934. end;
  935. procedure SplitBinCmd(const s:string;var bstr:String;var cstr:TCmdStr);
  936. var
  937. i : longint;
  938. begin
  939. i:=pos(' ',s);
  940. if i>0 then
  941. begin
  942. bstr:=Copy(s,1,i-1);
  943. cstr:=Copy(s,i+1,length(s)-i);
  944. end
  945. else
  946. begin
  947. bstr:=s;
  948. cstr:='';
  949. end;
  950. end;
  951. procedure TSearchPathList.AddPath(s:string;addfirst:boolean);
  952. begin
  953. AddPath('',s,AddFirst);
  954. end;
  955. procedure TSearchPathList.AddPath(SrcPath,s:string;addfirst:boolean);
  956. var
  957. staridx,
  958. j : longint;
  959. prefix,
  960. suffix,
  961. CurrentDir,
  962. currPath : string;
  963. subdirfound : boolean;
  964. {$IFDEF USE_SYSUTILS}
  965. dir : TSearchRec;
  966. {$ELSE USE_SYSUTILS}
  967. dir : searchrec;
  968. {$ENDIF USE_SYSUTILS}
  969. hp : TStringListItem;
  970. procedure AddCurrPath;
  971. begin
  972. if addfirst then
  973. begin
  974. Remove(currPath);
  975. Insert(currPath);
  976. end
  977. else
  978. begin
  979. { Check if already in path, then we don't add it }
  980. hp:=Find(currPath);
  981. if not assigned(hp) then
  982. Concat(currPath);
  983. end;
  984. end;
  985. begin
  986. if s='' then
  987. exit;
  988. { Support default macro's }
  989. DefaultReplacements(s);
  990. { get current dir }
  991. CurrentDir:=GetCurrentDir;
  992. repeat
  993. { get currpath }
  994. if addfirst then
  995. begin
  996. j:=length(s);
  997. while (j>0) and (s[j]<>';') do
  998. dec(j);
  999. currPath:= TrimSpace(Copy(s,j+1,length(s)-j));
  1000. DePascalQuote(currPath);
  1001. currPath:=FixPath(currPath,false);
  1002. if j=0 then
  1003. s:=''
  1004. else
  1005. System.Delete(s,j,length(s)-j+1);
  1006. end
  1007. else
  1008. begin
  1009. j:=Pos(';',s);
  1010. if j=0 then
  1011. j:=255;
  1012. currPath:= TrimSpace(Copy(s,1,j-1));
  1013. DePascalQuote(currPath);
  1014. currPath:=SrcPath+FixPath(currPath,false);
  1015. System.Delete(s,1,j);
  1016. end;
  1017. { fix pathname }
  1018. if currPath='' then
  1019. currPath:= CurDirRelPath(source_info)
  1020. else
  1021. begin
  1022. {$ifdef USE_SYSUTILS}
  1023. currPath:=FixPath(ExpandFileName(currpath),false);
  1024. {$else USE_SYSUTILS}
  1025. currPath:=FixPath(FExpand(currPath),false);
  1026. {$endif USE_SYSUTILS}
  1027. if (CurrentDir<>'') and (Copy(currPath,1,length(CurrentDir))=CurrentDir) then
  1028. begin
  1029. {$ifdef AMIGA}
  1030. currPath:= CurrentDir+Copy(currPath,length(CurrentDir)+1,255);
  1031. {$else}
  1032. currPath:= CurDirRelPath(source_info)+Copy(currPath,length(CurrentDir)+1,255);
  1033. {$endif}
  1034. end;
  1035. end;
  1036. { wildcard adding ? }
  1037. staridx:=pos('*',currpath);
  1038. if staridx>0 then
  1039. begin
  1040. prefix:=SplitPath(Copy(currpath,1,staridx));
  1041. suffix:=Copy(currpath,staridx+1,length(currpath));
  1042. subdirfound:=false;
  1043. {$IFDEF USE_SYSUTILS}
  1044. if findfirst(prefix+'*',faDirectory,dir) = 0 then
  1045. begin
  1046. repeat
  1047. if (dir.name<>'.') and
  1048. (dir.name<>'..') and
  1049. ((dir.attr and faDirectory)<>0) then
  1050. begin
  1051. subdirfound:=true;
  1052. currpath:=prefix+dir.name+suffix;
  1053. if (suffix='') or PathExists(currpath) then
  1054. begin
  1055. hp:=Find(currPath);
  1056. if not assigned(hp) then
  1057. AddCurrPath;
  1058. end;
  1059. end;
  1060. until findnext(dir) <> 0;
  1061. end;
  1062. {$ELSE USE_SYSUTILS}
  1063. findfirst(prefix+'*',directory,dir);
  1064. while doserror=0 do
  1065. begin
  1066. if (dir.name<>'.') and
  1067. (dir.name<>'..') and
  1068. ((dir.attr and directory)<>0) then
  1069. begin
  1070. subdirfound:=true;
  1071. currpath:=prefix+dir.name+suffix;
  1072. if (suffix='') or PathExists(currpath) then
  1073. begin
  1074. hp:=Find(currPath);
  1075. if not assigned(hp) then
  1076. AddCurrPath;
  1077. end;
  1078. end;
  1079. findnext(dir);
  1080. end;
  1081. {$ENDIF USE_SYSUTILS}
  1082. FindClose(dir);
  1083. if not subdirfound then
  1084. WarnNonExistingPath(currpath);
  1085. end
  1086. else
  1087. begin
  1088. if PathExists(currpath) then
  1089. AddCurrPath
  1090. else
  1091. WarnNonExistingPath(currpath);
  1092. end;
  1093. until (s='');
  1094. end;
  1095. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  1096. var
  1097. s : string;
  1098. hl : TSearchPathList;
  1099. hp,hp2 : TStringListItem;
  1100. begin
  1101. if list.empty then
  1102. exit;
  1103. { create temp and reverse the list }
  1104. if addfirst then
  1105. begin
  1106. hl:=TSearchPathList.Create;
  1107. hp:=TStringListItem(list.first);
  1108. while assigned(hp) do
  1109. begin
  1110. hl.insert(hp.Str);
  1111. hp:=TStringListItem(hp.next);
  1112. end;
  1113. while not hl.empty do
  1114. begin
  1115. s:=hl.GetFirst;
  1116. Remove(s);
  1117. Insert(s);
  1118. end;
  1119. hl.Free;
  1120. end
  1121. else
  1122. begin
  1123. hp:=TStringListItem(list.first);
  1124. while assigned(hp) do
  1125. begin
  1126. hp2:=Find(hp.Str);
  1127. { Check if already in path, then we don't add it }
  1128. if not assigned(hp2) then
  1129. Concat(hp.Str);
  1130. hp:=TStringListItem(hp.next);
  1131. end;
  1132. end;
  1133. end;
  1134. function TSearchPathList.FindFile(const f : string;var foundfile:string):boolean;
  1135. Var
  1136. p : TStringListItem;
  1137. begin
  1138. FindFile:=false;
  1139. p:=TStringListItem(first);
  1140. while assigned(p) do
  1141. begin
  1142. result:=FileExistsNonCase(p.Str,f,FoundFile);
  1143. if result then
  1144. exit;
  1145. p:=TStringListItem(p.next);
  1146. end;
  1147. { Return original filename if not found }
  1148. FoundFile:=f;
  1149. end;
  1150. Function GetFileTime ( Var F : File) : Longint;
  1151. Var
  1152. {$ifdef hasunix}
  1153. info: Stat;
  1154. {$endif}
  1155. L : longint;
  1156. begin
  1157. {$ifdef hasunix}
  1158. {$IFDEF havelinuxrtl10}
  1159. FStat (F,Info);
  1160. L:=Info.Mtime;
  1161. {$ELSE}
  1162. FPFStat (F,Info);
  1163. L:=Info.st_Mtime;
  1164. {$ENDIF}
  1165. {$else}
  1166. GetFTime(f,l);
  1167. {$endif}
  1168. GetFileTime:=L;
  1169. end;
  1170. Function GetNamedFileTime (Const F : String) : Longint;
  1171. begin
  1172. GetNamedFileTime:=do_getnamedfiletime(F);
  1173. end;
  1174. function FindFile(const f : string;path : string;var foundfile:string):boolean;
  1175. Var
  1176. singlepathstring : string;
  1177. i : longint;
  1178. begin
  1179. {$ifdef Unix}
  1180. for i:=1 to length(path) do
  1181. if path[i]=':' then
  1182. path[i]:=';';
  1183. {$endif Unix}
  1184. FindFile:=false;
  1185. repeat
  1186. i:=pos(';',path);
  1187. if i=0 then
  1188. i:=256;
  1189. singlepathstring:=FixPath(copy(path,1,i-1),false);
  1190. delete(path,1,i);
  1191. result:=FileExistsNonCase(singlepathstring,f,FoundFile);
  1192. if result then
  1193. exit;
  1194. until path='';
  1195. FoundFile:=f;
  1196. end;
  1197. function FindFilePchar(const f : string;path : pchar;var foundfile:string):boolean;
  1198. Var
  1199. singlepathstring : string;
  1200. startpc,pc : pchar;
  1201. sepch : char;
  1202. begin
  1203. {$ifdef Unix}
  1204. sepch:=':';
  1205. {$else}
  1206. {$ifdef macos}
  1207. sepch:=',';
  1208. {$else}
  1209. sepch:=';';
  1210. {$endif macos}
  1211. {$endif Unix}
  1212. FindFilePchar:=false;
  1213. pc:=path;
  1214. repeat
  1215. startpc:=pc;
  1216. while (pc^<>sepch) and (pc^<>';') and (pc^<>#0) do
  1217. inc(pc);
  1218. move(startpc^,singlepathstring[1],pc-startpc);
  1219. singlepathstring[0]:=char(longint(pc-startpc));
  1220. singlepathstring:=FixPath(singlepathstring,false);
  1221. result:=FileExistsNonCase(singlepathstring,f,FoundFile);
  1222. if result then
  1223. exit;
  1224. if (pc^=#0) then
  1225. break;
  1226. inc(pc);
  1227. until false;
  1228. foundfile:=f;
  1229. end;
  1230. function FindExe(const bin:string;var foundfile:string):boolean;
  1231. var
  1232. p : pchar;
  1233. found : boolean;
  1234. begin
  1235. found:=FindFile(FixFileName(AddExtension(bin,source_info.exeext)),'.;'+exepath,foundfile);
  1236. if not found then
  1237. begin
  1238. {$ifdef macos}
  1239. p:=GetEnvPchar('Commands');
  1240. {$else}
  1241. p:=GetEnvPchar('PATH');
  1242. {$endif}
  1243. found:=FindFilePChar(FixFileName(AddExtension(bin,source_info.exeext)),p,foundfile);
  1244. FreeEnvPChar(p);
  1245. end;
  1246. FindExe:=found;
  1247. end;
  1248. function GetShortName(const n:string):string;
  1249. {$ifdef win32}
  1250. var
  1251. hs,hs2 : string;
  1252. i : longint;
  1253. {$endif}
  1254. {$ifdef go32v2}
  1255. var
  1256. hs : string;
  1257. {$endif}
  1258. {$ifdef watcom}
  1259. var
  1260. hs : string;
  1261. {$endif}
  1262. begin
  1263. GetShortName:=n;
  1264. {$ifdef win32}
  1265. hs:=n+#0;
  1266. i:=Windows.GetShortPathName(@hs[1],@hs2[1],high(hs2));
  1267. if (i>0) and (i<=high(hs2)) then
  1268. begin
  1269. hs2[0]:=chr(strlen(@hs2[1]));
  1270. GetShortName:=hs2;
  1271. end;
  1272. {$endif}
  1273. {$ifdef go32v2}
  1274. hs:=n;
  1275. if Dos.GetShortName(hs) then
  1276. GetShortName:=hs;
  1277. {$endif}
  1278. {$ifdef watcom}
  1279. hs:=n;
  1280. if Dos.GetShortName(hs) then
  1281. GetShortName:=hs;
  1282. {$endif}
  1283. end;
  1284. function CleanPath(const s:string):String;
  1285. { Wrapper that encapsulate fexpand/expandfilename}
  1286. begin
  1287. {$IFDEF USE_SYSUTILS}
  1288. cleanpath:=ExpandFileName(s);
  1289. {$else}
  1290. cleanpath:=fexpand(s);
  1291. {$endif}
  1292. end;
  1293. {****************************************************************************
  1294. OS Dependent things
  1295. ****************************************************************************}
  1296. function GetEnvPChar(const envname:string):pchar;
  1297. {$ifdef win32}
  1298. var
  1299. s : string;
  1300. i,len : longint;
  1301. hp,p,p2 : pchar;
  1302. {$endif}
  1303. begin
  1304. {$ifdef hasunix}
  1305. GetEnvPchar:={$ifdef havelinuxrtl10}Linux.getenv{$else}BaseUnix.fpGetEnv{$endif}(envname);
  1306. {$define GETENVOK}
  1307. {$endif}
  1308. {$ifdef win32}
  1309. GetEnvPchar:=nil;
  1310. p:=GetEnvironmentStrings;
  1311. hp:=p;
  1312. while hp^<>#0 do
  1313. begin
  1314. s:=strpas(hp);
  1315. i:=pos('=',s);
  1316. len:=strlen(hp);
  1317. if upper(copy(s,1,i-1))=upper(envname) then
  1318. begin
  1319. GetMem(p2,len-length(envname));
  1320. Move(hp[i],p2^,len-length(envname));
  1321. GetEnvPchar:=p2;
  1322. break;
  1323. end;
  1324. { next string entry}
  1325. hp:=hp+len+1;
  1326. end;
  1327. FreeEnvironmentStrings(p);
  1328. {$define GETENVOK}
  1329. {$endif}
  1330. {$ifdef os2}
  1331. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  1332. {$define GETENVOK}
  1333. {$endif}
  1334. {$ifdef GETENVOK}
  1335. {$undef GETENVOK}
  1336. {$else}
  1337. GetEnvPchar:=StrPNew(Dos.Getenv(envname));
  1338. {$endif}
  1339. end;
  1340. procedure FreeEnvPChar(p:pchar);
  1341. begin
  1342. {$ifndef hasunix}
  1343. {$ifndef os2}
  1344. StrDispose(p);
  1345. {$endif}
  1346. {$endif}
  1347. end;
  1348. {$IFDEF MORPHOS}
  1349. {$DEFINE AMIGASHELL}
  1350. {$ENDIF}
  1351. {$IFDEF AMIGA}
  1352. {$DEFINE AMIGASHELL}
  1353. {$ENDIF}
  1354. function Shell(const command:string): longint;
  1355. { This is already defined in the linux.ppu for linux, need for the *
  1356. expansion under linux }
  1357. {$ifdef hasunix}
  1358. begin
  1359. result := {$ifdef havelinuxrtl10}Linux{$else}Unix{$endif}.Shell(command);
  1360. end;
  1361. {$else}
  1362. {$ifdef amigashell}
  1363. begin
  1364. {$IFDEF USE_SYSUTILS}
  1365. result := ExecuteProcess('',command);
  1366. {$ELSE USE_SYSUTILS}
  1367. exec('',command);
  1368. if (doserror <> 0) then
  1369. result := doserror
  1370. else
  1371. result := dosexitcode;
  1372. end;
  1373. {$ENDIF USE_SYSUTILS}
  1374. {$else}
  1375. var
  1376. comspec : string;
  1377. begin
  1378. comspec:=getenv('COMSPEC');
  1379. {$IFDEF USE_SYSUTILS}
  1380. result := ExecuteProcess(comspec,' /C '+command);
  1381. {$ELSE USE_SYSUTILS}
  1382. Exec(comspec,' /C '+command);
  1383. if (doserror <> 0) then
  1384. result := doserror
  1385. else
  1386. result := dosexitcode;
  1387. end;
  1388. {$ENDIF USE_SYSUTILS}
  1389. {$endif}
  1390. {$endif}
  1391. {$UNDEF AMIGASHELL}
  1392. {$ifdef CPUI386}
  1393. {$define HASSETFPUEXCEPTIONMASK}
  1394. { later, this should be replaced by the math unit }
  1395. const
  1396. Default8087CW : word = $1332;
  1397. procedure Set8087CW(cw:word);assembler;
  1398. asm
  1399. movw cw,%ax
  1400. movw %ax,default8087cw
  1401. fnclex
  1402. fldcw default8087cw
  1403. end;
  1404. function Get8087CW:word;assembler;
  1405. asm
  1406. pushl $0
  1407. fnstcw (%esp)
  1408. popl %eax
  1409. end;
  1410. procedure SetFPUExceptionMask(const Mask: TFPUExceptionMask);
  1411. var
  1412. CtlWord: Word;
  1413. begin
  1414. CtlWord:=Get8087CW;
  1415. Set8087CW( (CtlWord and $FFC0) or Byte(Longint(Mask)) );
  1416. end;
  1417. {$endif CPUI386}
  1418. {$ifdef CPUX86_64}
  1419. {$define HASSETFPUEXCEPTIONMASK}
  1420. { later, this should be replaced by the math unit }
  1421. const
  1422. Default8087CW : word = $1332;
  1423. procedure Set8087CW(cw:word);assembler;
  1424. asm
  1425. movw cw,%ax
  1426. movw %ax,default8087cw
  1427. fnclex
  1428. fldcw default8087cw
  1429. end;
  1430. function Get8087CW:word;assembler;
  1431. asm
  1432. pushq $0
  1433. fnstcw (%rsp)
  1434. popq %rax
  1435. end;
  1436. procedure SetSSECSR(w : dword);
  1437. var
  1438. _w : dword;
  1439. begin
  1440. _w:=w;
  1441. asm
  1442. ldmxcsr _w
  1443. end;
  1444. end;
  1445. function GetSSECSR : dword;
  1446. var
  1447. _w : dword;
  1448. begin
  1449. asm
  1450. stmxcsr _w
  1451. end;
  1452. result:=_w;
  1453. end;
  1454. procedure SetFPUExceptionMask(const Mask: TFPUExceptionMask);
  1455. var
  1456. CtlWord: Word;
  1457. newmask : dword;
  1458. const
  1459. MM_MaskInvalidOp = %0000000010000000;
  1460. MM_MaskDenorm = %0000000100000000;
  1461. MM_MaskDivZero = %0000001000000000;
  1462. MM_MaskOverflow = %0000010000000000;
  1463. MM_MaskUnderflow = %0000100000000000;
  1464. MM_MaskPrecision = %0001000000000000;
  1465. begin
  1466. { classic FPU }
  1467. CtlWord:=Get8087CW;
  1468. Set8087CW( (CtlWord and $FFC0) or Byte(Longint(Mask)) );
  1469. { SSE }
  1470. newmask:=GetSSECSR;
  1471. { invalid operation }
  1472. if (exInvalidOp in mask) then
  1473. newmask:=newmask or MM_MaskInvalidOp
  1474. else
  1475. newmask:=newmask and not(MM_MaskInvalidOp);
  1476. { denormals }
  1477. if (exDenormalized in mask) then
  1478. newmask:=newmask or MM_MaskDenorm
  1479. else
  1480. newmask:=newmask and not(MM_MaskDenorm);
  1481. { zero divide }
  1482. if (exZeroDivide in mask) then
  1483. newmask:=newmask or MM_MaskDivZero
  1484. else
  1485. newmask:=newmask and not(MM_MaskDivZero);
  1486. { overflow }
  1487. if (exOverflow in mask) then
  1488. newmask:=newmask or MM_MaskOverflow
  1489. else
  1490. newmask:=newmask and not(MM_MaskOverflow);
  1491. { underflow }
  1492. if (exUnderflow in mask) then
  1493. newmask:=newmask or MM_MaskUnderflow
  1494. else
  1495. newmask:=newmask and not(MM_MaskUnderflow);
  1496. { Precision (inexact result) }
  1497. if (exPrecision in mask) then
  1498. newmask:=newmask or MM_MaskPrecision
  1499. else
  1500. newmask:=newmask and not(MM_MaskPrecision);
  1501. SetSSECSR(newmask);
  1502. end;
  1503. {$endif CPUX86_64}
  1504. {$ifdef CPUPOWERPC}
  1505. {$define HASSETFPUEXCEPTIONMASK}
  1506. procedure SetFPUExceptionMask(const Mask: TFPUExceptionMask);
  1507. var
  1508. newmask: record
  1509. case byte of
  1510. 1: (d: double);
  1511. 2: (a,b: cardinal);
  1512. end;
  1513. begin
  1514. { load current control register contents }
  1515. asm
  1516. mffs f0
  1517. stfd f0,newmask.d
  1518. end;
  1519. { invalid operation: bit 24 (big endian, bit 0 = left-most bit) }
  1520. if (exInvalidOp in mask) then
  1521. newmask.b := newmask.b and not(1 shl (31-24))
  1522. else
  1523. newmask.b := newmask.b or (1 shl (31-24));
  1524. { denormals can not cause exceptions on the PPC }
  1525. { zero divide: bit 27 }
  1526. if (exZeroDivide in mask) then
  1527. newmask.b := newmask.b and not(1 shl (31-27))
  1528. else
  1529. newmask.b := newmask.b or (1 shl (31-27));
  1530. { overflow: bit 25 }
  1531. if (exOverflow in mask) then
  1532. newmask.b := newmask.b and not(1 shl (31-25))
  1533. else
  1534. newmask.b := newmask.b or (1 shl (31-25));
  1535. { underflow: bit 26 }
  1536. if (exUnderflow in mask) then
  1537. newmask.b := newmask.b and not(1 shl (31-26))
  1538. else
  1539. newmask.b := newmask.b or (1 shl (31-26));
  1540. { Precision (inexact result): bit 28 }
  1541. if (exPrecision in mask) then
  1542. newmask.b := newmask.b and not(1 shl (31-28))
  1543. else
  1544. newmask.b := newmask.b or (1 shl (31-28));
  1545. { update control register contents }
  1546. asm
  1547. lfd f0, newmask.d
  1548. mtfsf 255,f0
  1549. end;
  1550. end;
  1551. {$endif CPUPOWERPC}
  1552. {$ifdef CPUSPARC}
  1553. {$define HASSETFPUEXCEPTIONMASK}
  1554. procedure SetFPUExceptionMask(const Mask: TFPUExceptionMask);
  1555. var
  1556. fsr : cardinal;
  1557. begin
  1558. { load current control register contents }
  1559. asm
  1560. st %fsr,fsr
  1561. end;
  1562. { invalid operation: bit 27 }
  1563. if (exInvalidOp in mask) then
  1564. fsr:=fsr and not(1 shl 27)
  1565. else
  1566. fsr:=fsr or (1 shl 27);
  1567. { zero divide: bit 24 }
  1568. if (exZeroDivide in mask) then
  1569. fsr:=fsr and not(1 shl 24)
  1570. else
  1571. fsr:=fsr or (1 shl 24);
  1572. { overflow: bit 26 }
  1573. if (exOverflow in mask) then
  1574. fsr:=fsr and not(1 shl 26)
  1575. else
  1576. fsr:=fsr or (1 shl 26);
  1577. { underflow: bit 25 }
  1578. if (exUnderflow in mask) then
  1579. fsr:=fsr and not(1 shl 25)
  1580. else
  1581. fsr:=fsr or (1 shl 25);
  1582. { Precision (inexact result): bit 23 }
  1583. if (exPrecision in mask) then
  1584. fsr:=fsr and not(1 shl 23)
  1585. else
  1586. fsr:=fsr or (1 shl 23);
  1587. { update control register contents }
  1588. asm
  1589. ld fsr,%fsr
  1590. end;
  1591. end;
  1592. {$endif CPUSPARC}
  1593. {$ifndef HASSETFPUEXCEPTIONMASK}
  1594. procedure SetFPUExceptionMask(const Mask: TFPUExceptionMask);
  1595. begin
  1596. end;
  1597. {$endif HASSETFPUEXCEPTIONMASK}
  1598. function is_number_float(d : double) : boolean;
  1599. var
  1600. bytearray : array[0..7] of byte;
  1601. begin
  1602. move(d,bytearray,8);
  1603. { only 1.1 save, 1.0.x will use always little endian }
  1604. {$ifdef FPC_BIG_ENDIAN}
  1605. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  1606. {$else FPC_BIG_ENDIAN}
  1607. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  1608. {$endif FPC_BIG_ENDIAN}
  1609. end;
  1610. function convertdoublearray(d : tdoublearray) : tdoublearray;{$ifdef USEINLINE}inline;{$endif}
  1611. {$ifdef CPUARM}
  1612. var
  1613. i : longint;
  1614. begin
  1615. for i:=0 to 3 do
  1616. begin
  1617. result[i+4]:=d[i];
  1618. result[i]:=d[i+4];
  1619. end;
  1620. {$else CPUARM}
  1621. begin
  1622. result:=d;
  1623. {$endif CPUARM}
  1624. end;
  1625. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  1626. var
  1627. b : boolean;
  1628. begin
  1629. b:=true;
  1630. if s='DEFAULT' then
  1631. aktmodeswitches:=initmodeswitches
  1632. else
  1633. if s='DELPHI' then
  1634. aktmodeswitches:=delphimodeswitches
  1635. else
  1636. if s='TP' then
  1637. aktmodeswitches:=tpmodeswitches
  1638. else
  1639. if s='FPC' then
  1640. aktmodeswitches:=fpcmodeswitches
  1641. else
  1642. if s='OBJFPC' then
  1643. aktmodeswitches:=objfpcmodeswitches
  1644. else
  1645. if s='GPC' then
  1646. aktmodeswitches:=gpcmodeswitches
  1647. else
  1648. if s='MACPAS' then
  1649. aktmodeswitches:=macmodeswitches
  1650. else
  1651. b:=false;
  1652. if b and changeInit then
  1653. initmodeswitches := aktmodeswitches;
  1654. if b then
  1655. begin
  1656. { turn ansistrings on by default ? }
  1657. if (m_delphi in aktmodeswitches) then
  1658. begin
  1659. include(aktlocalswitches,cs_ansistrings);
  1660. if changeinit then
  1661. include(initlocalswitches,cs_ansistrings);
  1662. end
  1663. else
  1664. begin
  1665. exclude(aktlocalswitches,cs_ansistrings);
  1666. if changeinit then
  1667. exclude(initlocalswitches,cs_ansistrings);
  1668. end;
  1669. { Default enum packing for delphi/tp7 }
  1670. if (m_tp7 in aktmodeswitches) or
  1671. (m_delphi in aktmodeswitches) or
  1672. (m_mac in aktmodeswitches) then
  1673. aktpackenum:=1
  1674. else
  1675. aktpackenum:=4;
  1676. if changeinit then
  1677. initpackenum:=aktpackenum;
  1678. {$ifdef i386}
  1679. { Default to intel assembler for delphi/tp7 on i386 }
  1680. if (m_delphi in aktmodeswitches) or
  1681. (m_tp7 in aktmodeswitches) then
  1682. aktasmmode:=asmmode_i386_intel;
  1683. if changeinit then
  1684. initasmmode:=aktasmmode;
  1685. {$endif i386}
  1686. end;
  1687. SetCompileMode:=b;
  1688. end;
  1689. function SetAktProcCall(const s:string; changeInit:boolean):boolean;
  1690. const
  1691. DefProcCallName : array[tproccalloption] of string[12] = ('',
  1692. 'CDECL',
  1693. 'CPPDECL',
  1694. '', { compilerproc }
  1695. 'FAR16',
  1696. 'OLDFPCCALL',
  1697. 'INLINE',
  1698. '', { internproc }
  1699. '', { syscall }
  1700. 'PASCAL',
  1701. 'REGISTER',
  1702. 'SAFECALL',
  1703. 'STDCALL',
  1704. 'SOFTFLOAT',
  1705. 'MWPASCAL'
  1706. );
  1707. var
  1708. t : tproccalloption;
  1709. begin
  1710. result:=false;
  1711. for t:=low(tproccalloption) to high(tproccalloption) do
  1712. if DefProcCallName[t]=s then
  1713. begin
  1714. AktDefProcCall:=t;
  1715. result:=true;
  1716. break;
  1717. end;
  1718. if changeinit then
  1719. InitDefProcCall:=AktDefProcCall;
  1720. end;
  1721. function SetProcessor(const s:string; changeInit: boolean):boolean;
  1722. var
  1723. t : tprocessors;
  1724. begin
  1725. SetProcessor:=false;
  1726. for t:=low(tprocessors) to high(tprocessors) do
  1727. if processorsstr[t]=s then
  1728. begin
  1729. aktspecificoptprocessor:=t;
  1730. SetProcessor:=true;
  1731. break;
  1732. end;
  1733. if changeinit then
  1734. initspecificoptprocessor:=aktspecificoptprocessor;
  1735. end;
  1736. function SetFpuType(const s:string; changeInit: boolean):boolean;
  1737. var
  1738. t : tfputype;
  1739. begin
  1740. SetFpuType:=false;
  1741. for t:=low(tfputype) to high(tfputype) do
  1742. if fputypestr[t]=s then
  1743. begin
  1744. aktfputype:=t;
  1745. SetFpuType:=true;
  1746. break;
  1747. end;
  1748. if changeinit then
  1749. initfputype:=aktfputype;
  1750. end;
  1751. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  1752. function string2guid(const s: string; var GUID: TGUID): boolean;
  1753. function ishexstr(const hs: string): boolean;
  1754. var
  1755. i: integer;
  1756. begin
  1757. ishexstr:=false;
  1758. for i:=1 to Length(hs) do begin
  1759. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  1760. exit;
  1761. end;
  1762. ishexstr:=true;
  1763. end;
  1764. function hexstr2longint(const hexs: string): longint;
  1765. var
  1766. i: integer;
  1767. rl: longint;
  1768. begin
  1769. rl:=0;
  1770. for i:=1 to length(hexs) do begin
  1771. rl:=rl shl 4;
  1772. case hexs[i] of
  1773. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  1774. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  1775. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  1776. end
  1777. end;
  1778. hexstr2longint:=rl;
  1779. end;
  1780. var
  1781. i: integer;
  1782. begin
  1783. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  1784. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  1785. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  1786. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  1787. ishexstr(copy(s,26,12)) then begin
  1788. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  1789. { these values are arealdy in the correct range (4 chars = word) }
  1790. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  1791. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  1792. for i:=0 to 1 do
  1793. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  1794. for i:=2 to 7 do
  1795. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  1796. string2guid:=true;
  1797. end
  1798. else
  1799. string2guid:=false;
  1800. end;
  1801. function guid2string(const GUID: TGUID): string;
  1802. function long2hex(l, len: longint): string;
  1803. const
  1804. hextbl: array[0..15] of char = '0123456789ABCDEF';
  1805. var
  1806. rs: string;
  1807. i: integer;
  1808. begin
  1809. rs[0]:=chr(len);
  1810. for i:=len downto 1 do begin
  1811. rs[i]:=hextbl[l and $F];
  1812. l:=l shr 4;
  1813. end;
  1814. long2hex:=rs;
  1815. end;
  1816. begin
  1817. guid2string:=
  1818. '{'+long2hex(GUID.D1,8)+
  1819. '-'+long2hex(GUID.D2,4)+
  1820. '-'+long2hex(GUID.D3,4)+
  1821. '-'+long2hex(GUID.D4[0],2)+long2hex(GUID.D4[1],2)+
  1822. '-'+long2hex(GUID.D4[2],2)+long2hex(GUID.D4[3],2)+
  1823. long2hex(GUID.D4[4],2)+long2hex(GUID.D4[5],2)+
  1824. long2hex(GUID.D4[6],2)+long2hex(GUID.D4[7],2)+
  1825. '}';
  1826. end;
  1827. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  1828. var
  1829. tok : string;
  1830. vstr : string;
  1831. l : longint;
  1832. code : integer;
  1833. b : talignmentinfo;
  1834. begin
  1835. UpdateAlignmentStr:=true;
  1836. uppervar(s);
  1837. fillchar(b,sizeof(b),0);
  1838. repeat
  1839. tok:=GetToken(s,'=');
  1840. if tok='' then
  1841. break;
  1842. vstr:=GetToken(s,',');
  1843. val(vstr,l,code);
  1844. if tok='PROC' then
  1845. b.procalign:=l
  1846. else if tok='JUMP' then
  1847. b.jumpalign:=l
  1848. else if tok='LOOP' then
  1849. b.loopalign:=l
  1850. else if tok='CONSTMIN' then
  1851. b.constalignmin:=l
  1852. else if tok='CONSTMAX' then
  1853. b.constalignmax:=l
  1854. else if tok='VARMIN' then
  1855. b.varalignmin:=l
  1856. else if tok='VARMAX' then
  1857. b.varalignmax:=l
  1858. else if tok='LOCALMIN' then
  1859. b.localalignmin:=l
  1860. else if tok='LOCALMAX' then
  1861. b.localalignmax:=l
  1862. else if tok='RECORDMIN' then
  1863. b.recordalignmin:=l
  1864. else if tok='RECORDMAX' then
  1865. b.recordalignmax:=l
  1866. else { Error }
  1867. UpdateAlignmentStr:=false;
  1868. until false;
  1869. UpdateAlignment(a,b);
  1870. end;
  1871. function var_align(siz: longint): longint;
  1872. begin
  1873. siz := size_2_align(siz);
  1874. var_align := used_align(siz,aktalignment.varalignmin,aktalignment.varalignmax);
  1875. end;
  1876. function const_align(siz: longint): longint;
  1877. begin
  1878. siz := size_2_align(siz);
  1879. const_align := used_align(siz,aktalignment.constalignmin,aktalignment.constalignmax);
  1880. end;
  1881. {****************************************************************************
  1882. Init
  1883. ****************************************************************************}
  1884. {$ifdef unix}
  1885. {$define need_path_search}
  1886. {$endif unix}
  1887. {$ifdef os2}
  1888. {$define need_path_search}
  1889. {$endif os2}
  1890. {$ifdef macos}
  1891. {$define need_path_search}
  1892. {$endif macos}
  1893. procedure get_exepath;
  1894. var
  1895. hs1 : namestr;
  1896. hs2 : extstr;
  1897. {$IFDEF USE_SYSUTILS}
  1898. exeName:String;
  1899. {$ENDIF USE_SYSUTILS}
  1900. {$ifdef need_path_search}
  1901. p : pchar;
  1902. {$endif need_path_search}
  1903. begin
  1904. {$IFDEF USE_SYSUTILS}
  1905. exepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  1906. {$ELSE USE_SYSUTILS}
  1907. exepath:=dos.getenv('PPC_EXEC_PATH');
  1908. {$ENDIF USE_SYSUTILS}
  1909. if exepath='' then
  1910. {$IFDEF USE_SYSUTILS}
  1911. exeName := FixFileName(system.paramstr(0));
  1912. exepath := ExtractFilePath(exeName);
  1913. hs1 := ExtractFileName(exeName);
  1914. hs2 := ExtractFileExt(exeName);
  1915. {$ELSE USE_SYSUTILS}
  1916. fsplit(FixFileName(system.paramstr(0)),exepath,hs1,hs2);
  1917. {$ENDIF USE_SYSUTILS}
  1918. {$ifdef need_path_search}
  1919. if exepath='' then
  1920. begin
  1921. if pos(source_info.exeext,hs1) <>
  1922. (length(hs1) - length(source_info.exeext)+1) then
  1923. hs1 := hs1 + source_info.exeext;
  1924. {$ifdef macos}
  1925. p:=GetEnvPchar('Commands');
  1926. {$else macos}
  1927. p:=GetEnvPchar('PATH');
  1928. {$endif macos}
  1929. FindFilePChar(hs1,p,exepath);
  1930. FreeEnvPChar(p);
  1931. exepath:=SplitPath(exepath);
  1932. end;
  1933. {$endif need_path_search}
  1934. exepath:=FixPath(exepath,false);
  1935. end;
  1936. procedure DoneGlobals;
  1937. begin
  1938. if assigned(DLLImageBase) then
  1939. StringDispose(DLLImageBase);
  1940. librarysearchpath.Free;
  1941. unitsearchpath.Free;
  1942. objectsearchpath.Free;
  1943. includesearchpath.Free;
  1944. end;
  1945. procedure InitGlobals;
  1946. begin
  1947. get_exepath;
  1948. { reset globals }
  1949. do_build:=false;
  1950. do_release:=false;
  1951. do_make:=true;
  1952. compile_level:=0;
  1953. DLLsource:=false;
  1954. inlining_procedure:=false;
  1955. resolving_forward:=false;
  1956. make_ref:=false;
  1957. LinkTypeSetExplicitly:=false;
  1958. { Output }
  1959. OutputFile:='';
  1960. OutputPrefix:=Nil;
  1961. OutputSuffix:=Nil;
  1962. OutputExtension:='';
  1963. OutputExeDir:='';
  1964. OutputUnitDir:='';
  1965. { Utils directory }
  1966. utilsdirectory:='';
  1967. utilsprefix:='';
  1968. cshared:=false;
  1969. rlinkpath:='';
  1970. { Search Paths }
  1971. librarysearchpath:=TSearchPathList.Create;
  1972. unitsearchpath:=TSearchPathList.Create;
  1973. includesearchpath:=TSearchPathList.Create;
  1974. objectsearchpath:=TSearchPathList.Create;
  1975. { Def file }
  1976. usewindowapi:=false;
  1977. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1978. DescriptionSetExplicity:=false;
  1979. dllversion:='';
  1980. dllmajor:=1;
  1981. dllminor:=0;
  1982. dllrevision:=0;
  1983. nwscreenname := '';
  1984. nwthreadname := '';
  1985. nwcopyright := '';
  1986. UseDeffileForExports:=false;
  1987. UseDeffileForExportsSetExplicitly:=false;
  1988. RelocSection:=false;
  1989. RelocSectionSetExplicitly:=false;
  1990. LinkTypeSetExplicitly:=false;
  1991. { Init values }
  1992. initmodeswitches:=fpcmodeswitches;
  1993. initlocalswitches:=[cs_check_io,cs_typed_const_writable];
  1994. initmoduleswitches:=[cs_extsyntax,cs_implicit_exceptions];
  1995. initsourcecodepage:='8859-1';
  1996. initglobalswitches:=[cs_check_unit_name,cs_link_static{$ifdef INTERNALLINKER},cs_link_internal,cs_link_map{$endif}];
  1997. initoutputformat:=target_asm.id;
  1998. fillchar(initalignment,sizeof(talignmentinfo),0);
  1999. { might be overridden later }
  2000. initasmmode:=asmmode_standard;
  2001. {$ifdef i386}
  2002. initoptprocessor:=ClassPentium3;
  2003. initspecificoptprocessor:=Class386;
  2004. initfputype:=fpu_x87;
  2005. initpackenum:=4;
  2006. {$IFDEF testvarsets}
  2007. initsetalloc:=0;
  2008. {$ENDIF}
  2009. initasmmode:=asmmode_i386_att;
  2010. {$endif i386}
  2011. {$ifdef m68k}
  2012. initoptprocessor:=MC68020;
  2013. initpackenum:=4;
  2014. {$IFDEF testvarsets}
  2015. initsetalloc:=0;
  2016. {$ENDIF}
  2017. {$endif m68k}
  2018. {$ifdef powerpc}
  2019. initoptprocessor:=PPC604;
  2020. initpackenum:=4;
  2021. {$IFDEF testvarsets}
  2022. initsetalloc:=0;
  2023. {$ENDIF}
  2024. initfputype:=fpu_standard;
  2025. {$endif powerpc}
  2026. {$ifdef sparc}
  2027. initoptprocessor:=SPARC_V8;
  2028. initpackenum:=4;
  2029. {$IFDEF testvarsets}
  2030. initsetalloc:=0;
  2031. {$ENDIF}
  2032. {$endif sparc}
  2033. {$ifdef arm}
  2034. initpackenum:=4;
  2035. {$IFDEF testvarsets}
  2036. initsetalloc:=0;
  2037. {$ENDIF}
  2038. initfputype:=fpu_fpa;
  2039. {$endif arm}
  2040. {$ifdef x86_64}
  2041. initoptprocessor:=ClassAthlon64;
  2042. initspecificoptprocessor:=ClassAthlon64;
  2043. initfputype:=fpu_sse64;
  2044. initpackenum:=4;
  2045. {$IFDEF testvarsets}
  2046. initsetalloc:=0;
  2047. {$ENDIF}
  2048. initasmmode:=asmmode_x86_64_gas;
  2049. {$endif x86_64}
  2050. initinterfacetype:=it_interfacecom;
  2051. initdefproccall:=pocall_default;
  2052. { memory sizes, will be overriden by parameter or default for target
  2053. in options or init_parser }
  2054. stacksize:=0;
  2055. { not initialized yet }
  2056. jmp_buf_size:=-1;
  2057. apptype:=app_cui;
  2058. end;
  2059. end.