globals.pas 65 KB

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