globals.pas 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297
  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. 'FAR16',
  1695. 'OLDFPCCALL',
  1696. '', { internproc }
  1697. '', { syscall }
  1698. 'PASCAL',
  1699. 'REGISTER',
  1700. 'SAFECALL',
  1701. 'STDCALL',
  1702. 'SOFTFLOAT',
  1703. 'MWPASCAL'
  1704. );
  1705. var
  1706. t : tproccalloption;
  1707. begin
  1708. result:=false;
  1709. for t:=low(tproccalloption) to high(tproccalloption) do
  1710. if DefProcCallName[t]=s then
  1711. begin
  1712. AktDefProcCall:=t;
  1713. result:=true;
  1714. break;
  1715. end;
  1716. if changeinit then
  1717. InitDefProcCall:=AktDefProcCall;
  1718. end;
  1719. function SetProcessor(const s:string; changeInit: boolean):boolean;
  1720. var
  1721. t : tprocessors;
  1722. begin
  1723. SetProcessor:=false;
  1724. for t:=low(tprocessors) to high(tprocessors) do
  1725. if processorsstr[t]=s then
  1726. begin
  1727. aktspecificoptprocessor:=t;
  1728. SetProcessor:=true;
  1729. break;
  1730. end;
  1731. if changeinit then
  1732. initspecificoptprocessor:=aktspecificoptprocessor;
  1733. end;
  1734. function SetFpuType(const s:string; changeInit: boolean):boolean;
  1735. var
  1736. t : tfputype;
  1737. begin
  1738. SetFpuType:=false;
  1739. for t:=low(tfputype) to high(tfputype) do
  1740. if fputypestr[t]=s then
  1741. begin
  1742. aktfputype:=t;
  1743. SetFpuType:=true;
  1744. break;
  1745. end;
  1746. if changeinit then
  1747. initfputype:=aktfputype;
  1748. end;
  1749. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  1750. function string2guid(const s: string; var GUID: TGUID): boolean;
  1751. function ishexstr(const hs: string): boolean;
  1752. var
  1753. i: integer;
  1754. begin
  1755. ishexstr:=false;
  1756. for i:=1 to Length(hs) do begin
  1757. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  1758. exit;
  1759. end;
  1760. ishexstr:=true;
  1761. end;
  1762. function hexstr2longint(const hexs: string): longint;
  1763. var
  1764. i: integer;
  1765. rl: longint;
  1766. begin
  1767. rl:=0;
  1768. for i:=1 to length(hexs) do begin
  1769. rl:=rl shl 4;
  1770. case hexs[i] of
  1771. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  1772. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  1773. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  1774. end
  1775. end;
  1776. hexstr2longint:=rl;
  1777. end;
  1778. var
  1779. i: integer;
  1780. begin
  1781. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  1782. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  1783. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  1784. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  1785. ishexstr(copy(s,26,12)) then begin
  1786. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  1787. { these values are arealdy in the correct range (4 chars = word) }
  1788. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  1789. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  1790. for i:=0 to 1 do
  1791. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  1792. for i:=2 to 7 do
  1793. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  1794. string2guid:=true;
  1795. end
  1796. else
  1797. string2guid:=false;
  1798. end;
  1799. function guid2string(const GUID: TGUID): string;
  1800. function long2hex(l, len: longint): string;
  1801. const
  1802. hextbl: array[0..15] of char = '0123456789ABCDEF';
  1803. var
  1804. rs: string;
  1805. i: integer;
  1806. begin
  1807. rs[0]:=chr(len);
  1808. for i:=len downto 1 do begin
  1809. rs[i]:=hextbl[l and $F];
  1810. l:=l shr 4;
  1811. end;
  1812. long2hex:=rs;
  1813. end;
  1814. begin
  1815. guid2string:=
  1816. '{'+long2hex(GUID.D1,8)+
  1817. '-'+long2hex(GUID.D2,4)+
  1818. '-'+long2hex(GUID.D3,4)+
  1819. '-'+long2hex(GUID.D4[0],2)+long2hex(GUID.D4[1],2)+
  1820. '-'+long2hex(GUID.D4[2],2)+long2hex(GUID.D4[3],2)+
  1821. long2hex(GUID.D4[4],2)+long2hex(GUID.D4[5],2)+
  1822. long2hex(GUID.D4[6],2)+long2hex(GUID.D4[7],2)+
  1823. '}';
  1824. end;
  1825. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  1826. var
  1827. tok : string;
  1828. vstr : string;
  1829. l : longint;
  1830. code : integer;
  1831. b : talignmentinfo;
  1832. begin
  1833. UpdateAlignmentStr:=true;
  1834. uppervar(s);
  1835. fillchar(b,sizeof(b),0);
  1836. repeat
  1837. tok:=GetToken(s,'=');
  1838. if tok='' then
  1839. break;
  1840. vstr:=GetToken(s,',');
  1841. val(vstr,l,code);
  1842. if tok='PROC' then
  1843. b.procalign:=l
  1844. else if tok='JUMP' then
  1845. b.jumpalign:=l
  1846. else if tok='LOOP' then
  1847. b.loopalign:=l
  1848. else if tok='CONSTMIN' then
  1849. b.constalignmin:=l
  1850. else if tok='CONSTMAX' then
  1851. b.constalignmax:=l
  1852. else if tok='VARMIN' then
  1853. b.varalignmin:=l
  1854. else if tok='VARMAX' then
  1855. b.varalignmax:=l
  1856. else if tok='LOCALMIN' then
  1857. b.localalignmin:=l
  1858. else if tok='LOCALMAX' then
  1859. b.localalignmax:=l
  1860. else if tok='RECORDMIN' then
  1861. b.recordalignmin:=l
  1862. else if tok='RECORDMAX' then
  1863. b.recordalignmax:=l
  1864. else { Error }
  1865. UpdateAlignmentStr:=false;
  1866. until false;
  1867. UpdateAlignment(a,b);
  1868. end;
  1869. function var_align(siz: longint): longint;
  1870. begin
  1871. siz := size_2_align(siz);
  1872. var_align := used_align(siz,aktalignment.varalignmin,aktalignment.varalignmax);
  1873. end;
  1874. function const_align(siz: longint): longint;
  1875. begin
  1876. siz := size_2_align(siz);
  1877. const_align := used_align(siz,aktalignment.constalignmin,aktalignment.constalignmax);
  1878. end;
  1879. {****************************************************************************
  1880. Init
  1881. ****************************************************************************}
  1882. {$ifdef unix}
  1883. {$define need_path_search}
  1884. {$endif unix}
  1885. {$ifdef os2}
  1886. {$define need_path_search}
  1887. {$endif os2}
  1888. {$ifdef macos}
  1889. {$define need_path_search}
  1890. {$endif macos}
  1891. procedure get_exepath;
  1892. var
  1893. hs1 : namestr;
  1894. hs2 : extstr;
  1895. {$IFDEF USE_SYSUTILS}
  1896. exeName:String;
  1897. {$ENDIF USE_SYSUTILS}
  1898. {$ifdef need_path_search}
  1899. p : pchar;
  1900. {$endif need_path_search}
  1901. begin
  1902. {$IFDEF USE_SYSUTILS}
  1903. exepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  1904. {$ELSE USE_SYSUTILS}
  1905. exepath:=dos.getenv('PPC_EXEC_PATH');
  1906. {$ENDIF USE_SYSUTILS}
  1907. if exepath='' then
  1908. {$IFDEF USE_SYSUTILS}
  1909. exeName := FixFileName(system.paramstr(0));
  1910. exepath := ExtractFilePath(exeName);
  1911. hs1 := ExtractFileName(exeName);
  1912. hs2 := ExtractFileExt(exeName);
  1913. {$ELSE USE_SYSUTILS}
  1914. fsplit(FixFileName(system.paramstr(0)),exepath,hs1,hs2);
  1915. {$ENDIF USE_SYSUTILS}
  1916. {$ifdef need_path_search}
  1917. if exepath='' then
  1918. begin
  1919. if pos(source_info.exeext,hs1) <>
  1920. (length(hs1) - length(source_info.exeext)+1) then
  1921. hs1 := hs1 + source_info.exeext;
  1922. {$ifdef macos}
  1923. p:=GetEnvPchar('Commands');
  1924. {$else macos}
  1925. p:=GetEnvPchar('PATH');
  1926. {$endif macos}
  1927. FindFilePChar(hs1,p,exepath);
  1928. FreeEnvPChar(p);
  1929. exepath:=SplitPath(exepath);
  1930. end;
  1931. {$endif need_path_search}
  1932. exepath:=FixPath(exepath,false);
  1933. end;
  1934. procedure DoneGlobals;
  1935. begin
  1936. if assigned(DLLImageBase) then
  1937. StringDispose(DLLImageBase);
  1938. librarysearchpath.Free;
  1939. unitsearchpath.Free;
  1940. objectsearchpath.Free;
  1941. includesearchpath.Free;
  1942. end;
  1943. procedure InitGlobals;
  1944. begin
  1945. get_exepath;
  1946. { reset globals }
  1947. do_build:=false;
  1948. do_release:=false;
  1949. do_make:=true;
  1950. compile_level:=0;
  1951. DLLsource:=false;
  1952. inlining_procedure:=false;
  1953. resolving_forward:=false;
  1954. make_ref:=false;
  1955. LinkTypeSetExplicitly:=false;
  1956. { Output }
  1957. OutputFile:='';
  1958. OutputPrefix:=Nil;
  1959. OutputSuffix:=Nil;
  1960. OutputExtension:='';
  1961. OutputExeDir:='';
  1962. OutputUnitDir:='';
  1963. { Utils directory }
  1964. utilsdirectory:='';
  1965. utilsprefix:='';
  1966. cshared:=false;
  1967. rlinkpath:='';
  1968. { Search Paths }
  1969. librarysearchpath:=TSearchPathList.Create;
  1970. unitsearchpath:=TSearchPathList.Create;
  1971. includesearchpath:=TSearchPathList.Create;
  1972. objectsearchpath:=TSearchPathList.Create;
  1973. { Def file }
  1974. usewindowapi:=false;
  1975. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1976. DescriptionSetExplicity:=false;
  1977. dllversion:='';
  1978. dllmajor:=1;
  1979. dllminor:=0;
  1980. dllrevision:=0;
  1981. nwscreenname := '';
  1982. nwthreadname := '';
  1983. nwcopyright := '';
  1984. UseDeffileForExports:=false;
  1985. UseDeffileForExportsSetExplicitly:=false;
  1986. RelocSection:=false;
  1987. RelocSectionSetExplicitly:=false;
  1988. LinkTypeSetExplicitly:=false;
  1989. { Init values }
  1990. initmodeswitches:=fpcmodeswitches;
  1991. initlocalswitches:=[cs_check_io,cs_typed_const_writable];
  1992. initmoduleswitches:=[cs_extsyntax,cs_implicit_exceptions];
  1993. initsourcecodepage:='8859-1';
  1994. initglobalswitches:=[cs_check_unit_name,cs_link_static{$ifdef INTERNALLINKER},cs_link_internal,cs_link_map{$endif}];
  1995. initoutputformat:=target_asm.id;
  1996. fillchar(initalignment,sizeof(talignmentinfo),0);
  1997. { might be overridden later }
  1998. initasmmode:=asmmode_standard;
  1999. {$ifdef i386}
  2000. initoptprocessor:=ClassPentium3;
  2001. initspecificoptprocessor:=Class386;
  2002. initfputype:=fpu_x87;
  2003. initpackenum:=4;
  2004. {$IFDEF testvarsets}
  2005. initsetalloc:=0;
  2006. {$ENDIF}
  2007. initasmmode:=asmmode_i386_att;
  2008. {$endif i386}
  2009. {$ifdef m68k}
  2010. initoptprocessor:=MC68020;
  2011. initpackenum:=4;
  2012. {$IFDEF testvarsets}
  2013. initsetalloc:=0;
  2014. {$ENDIF}
  2015. {$endif m68k}
  2016. {$ifdef powerpc}
  2017. initoptprocessor:=PPC604;
  2018. initpackenum:=4;
  2019. {$IFDEF testvarsets}
  2020. initsetalloc:=0;
  2021. {$ENDIF}
  2022. initfputype:=fpu_standard;
  2023. {$endif powerpc}
  2024. {$ifdef sparc}
  2025. initoptprocessor:=SPARC_V8;
  2026. initpackenum:=4;
  2027. {$IFDEF testvarsets}
  2028. initsetalloc:=0;
  2029. {$ENDIF}
  2030. {$endif sparc}
  2031. {$ifdef arm}
  2032. initpackenum:=4;
  2033. {$IFDEF testvarsets}
  2034. initsetalloc:=0;
  2035. {$ENDIF}
  2036. initfputype:=fpu_fpa;
  2037. {$endif arm}
  2038. {$ifdef x86_64}
  2039. initoptprocessor:=ClassAthlon64;
  2040. initspecificoptprocessor:=ClassAthlon64;
  2041. initfputype:=fpu_sse64;
  2042. initpackenum:=4;
  2043. {$IFDEF testvarsets}
  2044. initsetalloc:=0;
  2045. {$ENDIF}
  2046. initasmmode:=asmmode_x86_64_gas;
  2047. {$endif x86_64}
  2048. initinterfacetype:=it_interfacecom;
  2049. initdefproccall:=pocall_default;
  2050. { memory sizes, will be overriden by parameter or default for target
  2051. in options or init_parser }
  2052. stacksize:=0;
  2053. { not initialized yet }
  2054. jmp_buf_size:=-1;
  2055. apptype:=app_cui;
  2056. end;
  2057. end.