globals.pas 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294
  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_repeat_forward,m_cvar_support,m_tp_procvar];
  59. { maximum number of locals in bytes before warning is emitted }
  60. maxlocalsize = high(smallint);
  61. { maximum number of paras in bytes before warning is emitted }
  62. maxparasize = high(word);
  63. { maximum nesting of routines }
  64. maxnesting = 32;
  65. { Filenames and extensions }
  66. sourceext = '.pp';
  67. pasext = '.pas';
  68. pext = '.p';
  69. treelogfilename = 'tree.log';
  70. {$if defined(CPUARM) and defined(FPUFPA)}
  71. MathQNaN : tdoublearray = (0,0,252,255,0,0,0,0);
  72. MathInf : tdoublearray = (0,0,240,127,0,0,0,0);
  73. MathNegInf : tdoublearray = (0,0,240,255,0,0,0,0);
  74. MathPi : tdoublearray = (251,33,9,64,24,45,68,84);
  75. {$else}
  76. {$ifdef FPC_LITTLE_ENDIAN}
  77. MathQNaN : tdoublearray = (0,0,0,0,0,0,252,255);
  78. MathInf : tdoublearray = (0,0,0,0,0,0,240,127);
  79. MathNegInf : tdoublearray = (0,0,0,0,0,0,240,255);
  80. MathPi : tdoublearray = (24,45,68,84,251,33,9,64);
  81. MathPiExtended : textendedarray = (53,194,104,33,162,218,15,201,0,64);
  82. {$else FPC_LITTLE_ENDIAN}
  83. MathQNaN : tdoublearray = (255,252,0,0,0,0,0,0);
  84. MathInf : tdoublearray = (127,240,0,0,0,0,0,0);
  85. MathNegInf : tdoublearray = (255,240,0,0,0,0,0,0);
  86. MathPi : tdoublearray = (64,9,33,251,84,68,45,24);
  87. MathPiExtended : textendedarray = (64,0,201,15,218,162,33,104,194,53);
  88. {$endif FPC_LITTLE_ENDIAN}
  89. {$endif}
  90. type
  91. TFPUException = (exInvalidOp, exDenormalized, exZeroDivide,
  92. exOverflow, exUnderflow, exPrecision);
  93. TFPUExceptionMask = set of TFPUException;
  94. pfileposinfo = ^tfileposinfo;
  95. tfileposinfo = record
  96. line : longint;
  97. column : word;
  98. fileindex : word;
  99. { moduleindex : word; }
  100. end;
  101. TSearchPathList = class(TStringList)
  102. procedure AddPath(s:string;addfirst:boolean);overload;
  103. procedure AddPath(SrcPath,s:string;addfirst:boolean);overload;
  104. procedure AddList(list:TSearchPathList;addfirst:boolean);
  105. function FindFile(const f : string;var foundfile:string):boolean;
  106. end;
  107. tcodepagestring = string[20];
  108. var
  109. { specified inputfile }
  110. inputdir : dirstr;
  111. inputfile : namestr;
  112. inputextension : extstr;
  113. { specified outputfile with -o parameter }
  114. outputfile : namestr;
  115. outputprefix : pstring;
  116. outputsuffix : pstring;
  117. outputextension : namestr;
  118. { specified with -FE or -FU }
  119. outputexedir : dirstr;
  120. outputunitdir : dirstr;
  121. { things specified with parameters }
  122. paralinkoptions,
  123. paradynamiclinker : string;
  124. paraprintnodetree : byte;
  125. parapreprocess : boolean;
  126. printnodefile : text;
  127. { typical cross compiling params}
  128. { directory where the utils can be found (options -FD) }
  129. utilsdirectory : dirstr;
  130. { targetname specific prefix used by these utils (options -XP<path>) }
  131. utilsprefix : dirstr;
  132. cshared : boolean; { pass --shared to ld to link C libs shared}
  133. Dontlinkstdlibpath: Boolean; { Don't add std paths to linkpath}
  134. rlinkpath : dirstr; { rpath-link linkdir override}
  135. { some flags for global compiler switches }
  136. do_build,
  137. do_release,
  138. do_make : boolean;
  139. { path for searching units, different paths can be seperated by ; }
  140. exepath : dirstr; { Path to ppc }
  141. librarysearchpath,
  142. unitsearchpath,
  143. objectsearchpath,
  144. includesearchpath : TSearchPathList;
  145. autoloadunits : string;
  146. { linking }
  147. usewindowapi : boolean;
  148. description : string;
  149. DescriptionSetExplicity : boolean;
  150. dllversion : string;
  151. dllmajor,
  152. dllminor,
  153. dllrevision : word; { revision only for netware }
  154. UseDeffileForExports : boolean;
  155. UseDeffileForExportsSetExplicitly : boolean;
  156. RelocSection : boolean;
  157. RelocSectionSetExplicitly : boolean;
  158. LinkTypeSetExplicitly : boolean;
  159. akttokenpos, { position of the last token }
  160. aktfilepos : tfileposinfo; { current position }
  161. nwscreenname : string;
  162. nwthreadname : string;
  163. nwcopyright : string;
  164. codegenerror : boolean; { true if there is an error reported }
  165. block_type : tblock_type; { type of currently parsed block }
  166. parsing_para_level : integer; { parameter level, used to convert
  167. proc calls to proc loads in firstcalln }
  168. compile_level : word;
  169. make_ref : boolean;
  170. resolving_forward : boolean; { used to add forward reference as second ref }
  171. inlining_procedure : boolean; { are we inlining a procedure }
  172. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  173. aktexceptblock : integer; { the exceptblock number of the current block (0 if none) }
  174. { commandline values }
  175. initglobalswitches : tglobalswitches;
  176. initmoduleswitches : tmoduleswitches;
  177. initlocalswitches : tlocalswitches;
  178. initmodeswitches : tmodeswitches;
  179. {$IFDEF testvarsets}
  180. Initsetalloc, {0=fixed, 1 =var}
  181. {$ENDIF}
  182. initpackenum : shortint;
  183. {$ifdef ansistring_bits}
  184. initansistring_bits: Tstringbits;
  185. {$endif}
  186. initalignment : talignmentinfo;
  187. initoptprocessor,
  188. initspecificoptprocessor : tprocessors;
  189. initfputype : tfputype;
  190. initasmmode : tasmmode;
  191. initinterfacetype : tinterfacetypes;
  192. initoutputformat : tasm;
  193. initdefproccall : tproccalloption;
  194. initsourcecodepage : tcodepagestring;
  195. { current state values }
  196. aktglobalswitches : tglobalswitches;
  197. aktmoduleswitches : tmoduleswitches;
  198. aktlocalswitches : tlocalswitches;
  199. nextaktlocalswitches : tlocalswitches;
  200. localswitcheschanged : boolean;
  201. aktmodeswitches : tmodeswitches;
  202. {$IFDEF testvarsets}
  203. aktsetalloc,
  204. {$ENDIF}
  205. aktpackrecords,
  206. aktpackenum : shortint;
  207. {$ifdef ansistring_bits}
  208. aktansistring_bits : Tstringbits;
  209. {$endif}
  210. aktmaxfpuregisters : longint;
  211. aktalignment : talignmentinfo;
  212. aktoptprocessor,
  213. aktspecificoptprocessor : tprocessors;
  214. aktfputype : tfputype;
  215. aktasmmode : tasmmode;
  216. aktinterfacetype : tinterfacetypes;
  217. aktoutputformat : tasm;
  218. aktdefproccall : tproccalloption;
  219. aktsourcecodepage : tcodepagestring;
  220. { Memory sizes }
  221. heapsize,
  222. stacksize,
  223. jmp_buf_size : longint;
  224. {$Ifdef EXTDEBUG}
  225. { parameter switches }
  226. debugstop : boolean;
  227. {$EndIf EXTDEBUG}
  228. { windows / OS/2 application type }
  229. apptype : tapptype;
  230. const
  231. DLLsource : boolean = false;
  232. DLLImageBase : pstring = nil;
  233. { used to set all registers used for each global function
  234. this should dramatically decrease the number of
  235. recompilations needed PM }
  236. simplify_ppu : boolean = true;
  237. { should we allow non static members ? }
  238. allow_only_static : boolean = false;
  239. Inside_asm_statement : boolean = false;
  240. global_unit_count : word = 0;
  241. { for error info in pp.pas }
  242. parser_current_file : string = '';
  243. {$ifdef m68k}
  244. { PalmOS resources }
  245. palmos_applicationname : string = 'FPC Application';
  246. palmos_applicationid : string[4] = 'FPCA';
  247. {$endif m68k}
  248. {$ifdef powerpc}
  249. { default calling convention used on MorphOS }
  250. syscall_convention : string = 'LEGACY';
  251. {$endif powerpc}
  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. F := FExpand (F);
  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. currPath:=FixPath(FExpand(currPath),false);
  1023. if (CurrentDir<>'') and (Copy(currPath,1,length(CurrentDir))=CurrentDir) then
  1024. begin
  1025. {$ifdef AMIGA}
  1026. currPath:= CurrentDir+Copy(currPath,length(CurrentDir)+1,255);
  1027. {$else}
  1028. currPath:= CurDirRelPath(source_info)+Copy(currPath,length(CurrentDir)+1,255);
  1029. {$endif}
  1030. end;
  1031. end;
  1032. { wildcard adding ? }
  1033. staridx:=pos('*',currpath);
  1034. if staridx>0 then
  1035. begin
  1036. prefix:=SplitPath(Copy(currpath,1,staridx));
  1037. suffix:=Copy(currpath,staridx+1,length(currpath));
  1038. subdirfound:=false;
  1039. {$IFDEF USE_SYSUTILS}
  1040. if findfirst(prefix+'*',faDirectory,dir) = 0 then
  1041. begin
  1042. repeat
  1043. if (dir.name<>'.') and
  1044. (dir.name<>'..') and
  1045. ((dir.attr and faDirectory)<>0) then
  1046. begin
  1047. subdirfound:=true;
  1048. currpath:=prefix+dir.name+suffix;
  1049. if (suffix='') or PathExists(currpath) then
  1050. begin
  1051. hp:=Find(currPath);
  1052. if not assigned(hp) then
  1053. AddCurrPath;
  1054. end;
  1055. end;
  1056. until findnext(dir) <> 0;
  1057. end
  1058. {$ELSE USE_SYSUTILS}
  1059. findfirst(prefix+'*',directory,dir);
  1060. while doserror=0 do
  1061. begin
  1062. if (dir.name<>'.') and
  1063. (dir.name<>'..') and
  1064. ((dir.attr and directory)<>0) then
  1065. begin
  1066. subdirfound:=true;
  1067. currpath:=prefix+dir.name+suffix;
  1068. if (suffix='') or PathExists(currpath) then
  1069. begin
  1070. hp:=Find(currPath);
  1071. if not assigned(hp) then
  1072. AddCurrPath;
  1073. end;
  1074. end;
  1075. findnext(dir);
  1076. end;
  1077. {$ENDIF USE_SYSUTILS}
  1078. FindClose(dir);
  1079. if not subdirfound then
  1080. WarnNonExistingPath(currpath);
  1081. end
  1082. else
  1083. begin
  1084. if PathExists(currpath) then
  1085. AddCurrPath
  1086. else
  1087. WarnNonExistingPath(currpath);
  1088. end;
  1089. until (s='');
  1090. end;
  1091. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  1092. var
  1093. s : string;
  1094. hl : TSearchPathList;
  1095. hp,hp2 : TStringListItem;
  1096. begin
  1097. if list.empty then
  1098. exit;
  1099. { create temp and reverse the list }
  1100. if addfirst then
  1101. begin
  1102. hl:=TSearchPathList.Create;
  1103. hp:=TStringListItem(list.first);
  1104. while assigned(hp) do
  1105. begin
  1106. hl.insert(hp.Str);
  1107. hp:=TStringListItem(hp.next);
  1108. end;
  1109. while not hl.empty do
  1110. begin
  1111. s:=hl.GetFirst;
  1112. Remove(s);
  1113. Insert(s);
  1114. end;
  1115. hl.Free;
  1116. end
  1117. else
  1118. begin
  1119. hp:=TStringListItem(list.first);
  1120. while assigned(hp) do
  1121. begin
  1122. hp2:=Find(hp.Str);
  1123. { Check if already in path, then we don't add it }
  1124. if not assigned(hp2) then
  1125. Concat(hp.Str);
  1126. hp:=TStringListItem(hp.next);
  1127. end;
  1128. end;
  1129. end;
  1130. function TSearchPathList.FindFile(const f : string;var foundfile:string):boolean;
  1131. Var
  1132. p : TStringListItem;
  1133. begin
  1134. FindFile:=false;
  1135. p:=TStringListItem(first);
  1136. while assigned(p) do
  1137. begin
  1138. result:=FileExistsNonCase(p.Str,f,FoundFile);
  1139. if result then
  1140. exit;
  1141. p:=TStringListItem(p.next);
  1142. end;
  1143. { Return original filename if not found }
  1144. FoundFile:=f;
  1145. end;
  1146. Function GetFileTime ( Var F : File) : Longint;
  1147. Var
  1148. {$ifdef hasunix}
  1149. info: Stat;
  1150. {$endif}
  1151. L : longint;
  1152. begin
  1153. {$ifdef hasunix}
  1154. {$IFDEF havelinuxrtl10}
  1155. FStat (F,Info);
  1156. L:=Info.Mtime;
  1157. {$ELSE}
  1158. FPFStat (F,Info);
  1159. L:=Info.st_Mtime;
  1160. {$ENDIF}
  1161. {$else}
  1162. GetFTime(f,l);
  1163. {$endif}
  1164. GetFileTime:=L;
  1165. end;
  1166. Function GetNamedFileTime (Const F : String) : Longint;
  1167. begin
  1168. GetNamedFileTime:=do_getnamedfiletime(F);
  1169. end;
  1170. function FindFile(const f : string;path : string;var foundfile:string):boolean;
  1171. Var
  1172. singlepathstring : string;
  1173. i : longint;
  1174. begin
  1175. {$ifdef Unix}
  1176. for i:=1 to length(path) do
  1177. if path[i]=':' then
  1178. path[i]:=';';
  1179. {$endif Unix}
  1180. FindFile:=false;
  1181. repeat
  1182. i:=pos(';',path);
  1183. if i=0 then
  1184. i:=256;
  1185. singlepathstring:=FixPath(copy(path,1,i-1),false);
  1186. delete(path,1,i);
  1187. result:=FileExistsNonCase(singlepathstring,f,FoundFile);
  1188. if result then
  1189. exit;
  1190. until path='';
  1191. FoundFile:=f;
  1192. end;
  1193. function FindFilePchar(const f : string;path : pchar;var foundfile:string):boolean;
  1194. Var
  1195. singlepathstring : string;
  1196. startpc,pc : pchar;
  1197. sepch : char;
  1198. begin
  1199. {$ifdef Unix}
  1200. sepch:=':';
  1201. {$else}
  1202. {$ifdef macos}
  1203. sepch:=',';
  1204. {$else}
  1205. sepch:=';';
  1206. {$endif macos}
  1207. {$endif Unix}
  1208. FindFilePchar:=false;
  1209. pc:=path;
  1210. repeat
  1211. startpc:=pc;
  1212. while (pc^<>sepch) and (pc^<>';') and (pc^<>#0) do
  1213. inc(pc);
  1214. move(startpc^,singlepathstring[1],pc-startpc);
  1215. singlepathstring[0]:=char(longint(pc-startpc));
  1216. singlepathstring:=FixPath(singlepathstring,false);
  1217. result:=FileExistsNonCase(singlepathstring,f,FoundFile);
  1218. if result then
  1219. exit;
  1220. if (pc^=#0) then
  1221. break;
  1222. inc(pc);
  1223. until false;
  1224. foundfile:=f;
  1225. end;
  1226. function FindExe(const bin:string;var foundfile:string):boolean;
  1227. var
  1228. p : pchar;
  1229. found : boolean;
  1230. begin
  1231. found:=FindFile(FixFileName(AddExtension(bin,source_info.exeext)),'.;'+exepath,foundfile);
  1232. if not found then
  1233. begin
  1234. {$ifdef macos}
  1235. p:=GetEnvPchar('Commands');
  1236. {$else}
  1237. p:=GetEnvPchar('PATH');
  1238. {$endif}
  1239. found:=FindFilePChar(FixFileName(AddExtension(bin,source_info.exeext)),p,foundfile);
  1240. FreeEnvPChar(p);
  1241. end;
  1242. FindExe:=found;
  1243. end;
  1244. function GetShortName(const n:string):string;
  1245. {$ifdef win32}
  1246. var
  1247. hs,hs2 : string;
  1248. i : longint;
  1249. {$endif}
  1250. {$ifdef go32v2}
  1251. var
  1252. hs : string;
  1253. {$endif}
  1254. {$ifdef watcom}
  1255. var
  1256. hs : string;
  1257. {$endif}
  1258. begin
  1259. GetShortName:=n;
  1260. {$ifdef win32}
  1261. hs:=n+#0;
  1262. i:=Windows.GetShortPathName(@hs[1],@hs2[1],high(hs2));
  1263. if (i>0) and (i<=high(hs2)) then
  1264. begin
  1265. hs2[0]:=chr(strlen(@hs2[1]));
  1266. GetShortName:=hs2;
  1267. end;
  1268. {$endif}
  1269. {$ifdef go32v2}
  1270. hs:=n;
  1271. if Dos.GetShortName(hs) then
  1272. GetShortName:=hs;
  1273. {$endif}
  1274. {$ifdef watcom}
  1275. hs:=n;
  1276. if Dos.GetShortName(hs) then
  1277. GetShortName:=hs;
  1278. {$endif}
  1279. end;
  1280. function CleanPath(const s:string):String;
  1281. { Wrapper that encapsulate fexpand/expandfilename}
  1282. begin
  1283. {$IFDEF USE_SYSUTILS}
  1284. cleanpath:=ExpandFileName(s);
  1285. {$else}
  1286. cleanpath:=fexpand(s);
  1287. {$endif}
  1288. end;
  1289. {****************************************************************************
  1290. OS Dependent things
  1291. ****************************************************************************}
  1292. function GetEnvPChar(const envname:string):pchar;
  1293. {$ifdef win32}
  1294. var
  1295. s : string;
  1296. i,len : longint;
  1297. hp,p,p2 : pchar;
  1298. {$endif}
  1299. begin
  1300. {$ifdef hasunix}
  1301. GetEnvPchar:={$ifdef havelinuxrtl10}Linux.getenv{$else}BaseUnix.fpGetEnv{$endif}(envname);
  1302. {$define GETENVOK}
  1303. {$endif}
  1304. {$ifdef win32}
  1305. GetEnvPchar:=nil;
  1306. p:=GetEnvironmentStrings;
  1307. hp:=p;
  1308. while hp^<>#0 do
  1309. begin
  1310. s:=strpas(hp);
  1311. i:=pos('=',s);
  1312. len:=strlen(hp);
  1313. if upper(copy(s,1,i-1))=upper(envname) then
  1314. begin
  1315. GetMem(p2,len-length(envname));
  1316. Move(hp[i],p2^,len-length(envname));
  1317. GetEnvPchar:=p2;
  1318. break;
  1319. end;
  1320. { next string entry}
  1321. hp:=hp+len+1;
  1322. end;
  1323. FreeEnvironmentStrings(p);
  1324. {$define GETENVOK}
  1325. {$endif}
  1326. {$ifdef os2}
  1327. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  1328. {$define GETENVOK}
  1329. {$endif}
  1330. {$ifdef GETENVOK}
  1331. {$undef GETENVOK}
  1332. {$else}
  1333. GetEnvPchar:=StrPNew(Dos.Getenv(envname));
  1334. {$endif}
  1335. end;
  1336. procedure FreeEnvPChar(p:pchar);
  1337. begin
  1338. {$ifndef hasunix}
  1339. {$ifndef os2}
  1340. StrDispose(p);
  1341. {$endif}
  1342. {$endif}
  1343. end;
  1344. {$IFDEF MORPHOS}
  1345. {$DEFINE AMIGASHELL}
  1346. {$ENDIF}
  1347. {$IFDEF AMIGA}
  1348. {$DEFINE AMIGASHELL}
  1349. {$ENDIF}
  1350. function Shell(const command:string): longint;
  1351. { This is already defined in the linux.ppu for linux, need for the *
  1352. expansion under linux }
  1353. {$ifdef hasunix}
  1354. begin
  1355. result := {$ifdef havelinuxrtl10}Linux{$else}Unix{$endif}.Shell(command);
  1356. end;
  1357. {$else}
  1358. {$ifdef amigashell}
  1359. begin
  1360. {$IFDEF USE_SYSUTILS}
  1361. result := ExecuteProcess('',command);
  1362. {$ELSE USE_SYSUTILS}
  1363. exec('',command);
  1364. if (doserror <> 0) then
  1365. result := doserror
  1366. else
  1367. result := dosexitcode;
  1368. end;
  1369. {$ENDIF USE_SYSUTILS}
  1370. {$else}
  1371. var
  1372. comspec : string;
  1373. begin
  1374. comspec:=getenv('COMSPEC');
  1375. {$IFDEF USE_SYSUTILS}
  1376. result := ExecuteProcess(comspec,' /C '+command);
  1377. {$ELSE USE_SYSUTILS}
  1378. Exec(comspec,' /C '+command);
  1379. if (doserror <> 0) then
  1380. result := doserror
  1381. else
  1382. result := dosexitcode;
  1383. end;
  1384. {$ENDIF USE_SYSUTILS}
  1385. {$endif}
  1386. {$endif}
  1387. {$UNDEF AMIGASHELL}
  1388. {$ifdef CPUI386}
  1389. {$define HASSETFPUEXCEPTIONMASK}
  1390. { later, this should be replaced by the math unit }
  1391. const
  1392. Default8087CW : word = $1332;
  1393. procedure Set8087CW(cw:word);assembler;
  1394. asm
  1395. movw cw,%ax
  1396. movw %ax,default8087cw
  1397. fnclex
  1398. fldcw default8087cw
  1399. end;
  1400. function Get8087CW:word;assembler;
  1401. asm
  1402. pushl $0
  1403. fnstcw (%esp)
  1404. popl %eax
  1405. end;
  1406. procedure SetFPUExceptionMask(const Mask: TFPUExceptionMask);
  1407. var
  1408. CtlWord: Word;
  1409. begin
  1410. CtlWord:=Get8087CW;
  1411. Set8087CW( (CtlWord and $FFC0) or Byte(Longint(Mask)) );
  1412. end;
  1413. {$endif CPUI386}
  1414. {$ifdef CPUX86_64}
  1415. {$define HASSETFPUEXCEPTIONMASK}
  1416. { later, this should be replaced by the math unit }
  1417. const
  1418. Default8087CW : word = $1332;
  1419. procedure Set8087CW(cw:word);assembler;
  1420. asm
  1421. movw cw,%ax
  1422. movw %ax,default8087cw
  1423. fnclex
  1424. fldcw default8087cw
  1425. end;
  1426. function Get8087CW:word;assembler;
  1427. asm
  1428. pushq $0
  1429. fnstcw (%rsp)
  1430. popq %rax
  1431. end;
  1432. procedure SetSSECSR(w : dword);
  1433. var
  1434. _w : dword;
  1435. begin
  1436. _w:=w;
  1437. asm
  1438. ldmxcsr _w
  1439. end;
  1440. end;
  1441. function GetSSECSR : dword;
  1442. var
  1443. _w : dword;
  1444. begin
  1445. asm
  1446. stmxcsr _w
  1447. end;
  1448. result:=_w;
  1449. end;
  1450. procedure SetFPUExceptionMask(const Mask: TFPUExceptionMask);
  1451. var
  1452. CtlWord: Word;
  1453. newmask : dword;
  1454. const
  1455. MM_MaskInvalidOp = %0000000010000000;
  1456. MM_MaskDenorm = %0000000100000000;
  1457. MM_MaskDivZero = %0000001000000000;
  1458. MM_MaskOverflow = %0000010000000000;
  1459. MM_MaskUnderflow = %0000100000000000;
  1460. MM_MaskPrecision = %0001000000000000;
  1461. begin
  1462. { classic FPU }
  1463. CtlWord:=Get8087CW;
  1464. Set8087CW( (CtlWord and $FFC0) or Byte(Longint(Mask)) );
  1465. { SSE }
  1466. newmask:=GetSSECSR;
  1467. { invalid operation }
  1468. if (exInvalidOp in mask) then
  1469. newmask:=newmask or MM_MaskInvalidOp
  1470. else
  1471. newmask:=newmask and not(MM_MaskInvalidOp);
  1472. { denormals }
  1473. if (exDenormalized in mask) then
  1474. newmask:=newmask or MM_MaskDenorm
  1475. else
  1476. newmask:=newmask and not(MM_MaskDenorm);
  1477. { zero divide }
  1478. if (exZeroDivide in mask) then
  1479. newmask:=newmask or MM_MaskDivZero
  1480. else
  1481. newmask:=newmask and not(MM_MaskDivZero);
  1482. { overflow }
  1483. if (exOverflow in mask) then
  1484. newmask:=newmask or MM_MaskOverflow
  1485. else
  1486. newmask:=newmask and not(MM_MaskOverflow);
  1487. { underflow }
  1488. if (exUnderflow in mask) then
  1489. newmask:=newmask or MM_MaskUnderflow
  1490. else
  1491. newmask:=newmask and not(MM_MaskUnderflow);
  1492. { Precision (inexact result) }
  1493. if (exPrecision in mask) then
  1494. newmask:=newmask or MM_MaskPrecision
  1495. else
  1496. newmask:=newmask and not(MM_MaskPrecision);
  1497. SetSSECSR(newmask);
  1498. end;
  1499. {$endif CPUX86_64}
  1500. {$ifdef CPUPOWERPC}
  1501. {$define HASSETFPUEXCEPTIONMASK}
  1502. procedure SetFPUExceptionMask(const Mask: TFPUExceptionMask);
  1503. var
  1504. newmask: record
  1505. case byte of
  1506. 1: (d: double);
  1507. 2: (a,b: cardinal);
  1508. end;
  1509. begin
  1510. { load current control register contents }
  1511. asm
  1512. mffs f0
  1513. stfd f0,newmask.d
  1514. end;
  1515. { invalid operation: bit 24 (big endian, bit 0 = left-most bit) }
  1516. if (exInvalidOp in mask) then
  1517. newmask.b := newmask.b and not(1 shl (31-24))
  1518. else
  1519. newmask.b := newmask.b or (1 shl (31-24));
  1520. { denormals can not cause exceptions on the PPC }
  1521. { zero divide: bit 27 }
  1522. if (exZeroDivide in mask) then
  1523. newmask.b := newmask.b and not(1 shl (31-27))
  1524. else
  1525. newmask.b := newmask.b or (1 shl (31-27));
  1526. { overflow: bit 25 }
  1527. if (exOverflow in mask) then
  1528. newmask.b := newmask.b and not(1 shl (31-25))
  1529. else
  1530. newmask.b := newmask.b or (1 shl (31-25));
  1531. { underflow: bit 26 }
  1532. if (exUnderflow in mask) then
  1533. newmask.b := newmask.b and not(1 shl (31-26))
  1534. else
  1535. newmask.b := newmask.b or (1 shl (31-26));
  1536. { Precision (inexact result): bit 28 }
  1537. if (exPrecision in mask) then
  1538. newmask.b := newmask.b and not(1 shl (31-28))
  1539. else
  1540. newmask.b := newmask.b or (1 shl (31-28));
  1541. { update control register contents }
  1542. asm
  1543. lfd f0, newmask.d
  1544. mtfsf 255,f0
  1545. end;
  1546. end;
  1547. {$endif CPUPOWERPC}
  1548. {$ifdef CPUSPARC}
  1549. {$define HASSETFPUEXCEPTIONMASK}
  1550. procedure SetFPUExceptionMask(const Mask: TFPUExceptionMask);
  1551. var
  1552. fsr : cardinal;
  1553. begin
  1554. { load current control register contents }
  1555. asm
  1556. st %fsr,fsr
  1557. end;
  1558. { invalid operation: bit 27 }
  1559. if (exInvalidOp in mask) then
  1560. fsr:=fsr and not(1 shl 27)
  1561. else
  1562. fsr:=fsr or (1 shl 27);
  1563. { zero divide: bit 24 }
  1564. if (exZeroDivide in mask) then
  1565. fsr:=fsr and not(1 shl 24)
  1566. else
  1567. fsr:=fsr or (1 shl 24);
  1568. { overflow: bit 26 }
  1569. if (exOverflow in mask) then
  1570. fsr:=fsr and not(1 shl 26)
  1571. else
  1572. fsr:=fsr or (1 shl 26);
  1573. { underflow: bit 25 }
  1574. if (exUnderflow in mask) then
  1575. fsr:=fsr and not(1 shl 25)
  1576. else
  1577. fsr:=fsr or (1 shl 25);
  1578. { Precision (inexact result): bit 23 }
  1579. if (exPrecision in mask) then
  1580. fsr:=fsr and not(1 shl 23)
  1581. else
  1582. fsr:=fsr or (1 shl 23);
  1583. { update control register contents }
  1584. asm
  1585. ld fsr,%fsr
  1586. end;
  1587. end;
  1588. {$endif CPUSPARC}
  1589. {$ifndef HASSETFPUEXCEPTIONMASK}
  1590. procedure SetFPUExceptionMask(const Mask: TFPUExceptionMask);
  1591. begin
  1592. end;
  1593. {$endif HASSETFPUEXCEPTIONMASK}
  1594. function is_number_float(d : double) : boolean;
  1595. var
  1596. bytearray : array[0..7] of byte;
  1597. begin
  1598. move(d,bytearray,8);
  1599. { only 1.1 save, 1.0.x will use always little endian }
  1600. {$ifdef FPC_BIG_ENDIAN}
  1601. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  1602. {$else FPC_BIG_ENDIAN}
  1603. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  1604. {$endif FPC_BIG_ENDIAN}
  1605. end;
  1606. function convertdoublearray(d : tdoublearray) : tdoublearray;{$ifdef USEINLINE}inline;{$endif}
  1607. {$ifdef CPUARM}
  1608. var
  1609. i : longint;
  1610. begin
  1611. for i:=0 to 3 do
  1612. begin
  1613. result[i+4]:=d[i];
  1614. result[i]:=d[i+4];
  1615. end;
  1616. {$else CPUARM}
  1617. begin
  1618. result:=d;
  1619. {$endif CPUARM}
  1620. end;
  1621. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  1622. var
  1623. b : boolean;
  1624. begin
  1625. b:=true;
  1626. if s='DEFAULT' then
  1627. aktmodeswitches:=initmodeswitches
  1628. else
  1629. if s='DELPHI' then
  1630. aktmodeswitches:=delphimodeswitches
  1631. else
  1632. if s='TP' then
  1633. aktmodeswitches:=tpmodeswitches
  1634. else
  1635. if s='FPC' then
  1636. aktmodeswitches:=fpcmodeswitches
  1637. else
  1638. if s='OBJFPC' then
  1639. aktmodeswitches:=objfpcmodeswitches
  1640. else
  1641. if s='GPC' then
  1642. aktmodeswitches:=gpcmodeswitches
  1643. else
  1644. if s='MACPAS' then
  1645. aktmodeswitches:=macmodeswitches
  1646. else
  1647. b:=false;
  1648. if b and changeInit then
  1649. initmodeswitches := aktmodeswitches;
  1650. if b then
  1651. begin
  1652. { turn ansistrings on by default ? }
  1653. if (m_delphi in aktmodeswitches) then
  1654. begin
  1655. include(aktlocalswitches,cs_ansistrings);
  1656. if changeinit then
  1657. include(initlocalswitches,cs_ansistrings);
  1658. end
  1659. else
  1660. begin
  1661. exclude(aktlocalswitches,cs_ansistrings);
  1662. if changeinit then
  1663. exclude(initlocalswitches,cs_ansistrings);
  1664. end;
  1665. { Default enum packing for delphi/tp7 }
  1666. if (m_tp7 in aktmodeswitches) or
  1667. (m_delphi in aktmodeswitches) then
  1668. aktpackenum:=1
  1669. else
  1670. aktpackenum:=4;
  1671. if changeinit then
  1672. initpackenum:=aktpackenum;
  1673. {$ifdef i386}
  1674. { Default to intel assembler for delphi/tp7 on i386 }
  1675. if (m_delphi in aktmodeswitches) or
  1676. (m_tp7 in aktmodeswitches) then
  1677. aktasmmode:=asmmode_i386_intel;
  1678. if changeinit then
  1679. initasmmode:=aktasmmode;
  1680. {$endif i386}
  1681. end;
  1682. SetCompileMode:=b;
  1683. end;
  1684. function SetAktProcCall(const s:string; changeInit:boolean):boolean;
  1685. const
  1686. DefProcCallName : array[tproccalloption] of string[12] = ('',
  1687. 'CDECL',
  1688. 'CPPDECL',
  1689. '', { compilerproc }
  1690. 'FAR16',
  1691. 'OLDFPCCALL',
  1692. 'INLINE',
  1693. '', { internproc }
  1694. '', { syscall }
  1695. 'PASCAL',
  1696. 'REGISTER',
  1697. 'SAFECALL',
  1698. 'STDCALL',
  1699. 'SOFTFLOAT',
  1700. 'MWPASCAL'
  1701. );
  1702. var
  1703. t : tproccalloption;
  1704. begin
  1705. result:=false;
  1706. for t:=low(tproccalloption) to high(tproccalloption) do
  1707. if DefProcCallName[t]=s then
  1708. begin
  1709. AktDefProcCall:=t;
  1710. result:=true;
  1711. break;
  1712. end;
  1713. if changeinit then
  1714. InitDefProcCall:=AktDefProcCall;
  1715. end;
  1716. function SetProcessor(const s:string; changeInit: boolean):boolean;
  1717. var
  1718. t : tprocessors;
  1719. begin
  1720. SetProcessor:=false;
  1721. for t:=low(tprocessors) to high(tprocessors) do
  1722. if processorsstr[t]=s then
  1723. begin
  1724. aktspecificoptprocessor:=t;
  1725. SetProcessor:=true;
  1726. break;
  1727. end;
  1728. if changeinit then
  1729. initspecificoptprocessor:=aktspecificoptprocessor;
  1730. end;
  1731. function SetFpuType(const s:string; changeInit: boolean):boolean;
  1732. var
  1733. t : tfputype;
  1734. begin
  1735. SetFpuType:=false;
  1736. for t:=low(tfputype) to high(tfputype) do
  1737. if fputypestr[t]=s then
  1738. begin
  1739. aktfputype:=t;
  1740. SetFpuType:=true;
  1741. break;
  1742. end;
  1743. if changeinit then
  1744. initfputype:=aktfputype;
  1745. end;
  1746. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  1747. function string2guid(const s: string; var GUID: TGUID): boolean;
  1748. function ishexstr(const hs: string): boolean;
  1749. var
  1750. i: integer;
  1751. begin
  1752. ishexstr:=false;
  1753. for i:=1 to Length(hs) do begin
  1754. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  1755. exit;
  1756. end;
  1757. ishexstr:=true;
  1758. end;
  1759. function hexstr2longint(const hexs: string): longint;
  1760. var
  1761. i: integer;
  1762. rl: longint;
  1763. begin
  1764. rl:=0;
  1765. for i:=1 to length(hexs) do begin
  1766. rl:=rl shl 4;
  1767. case hexs[i] of
  1768. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  1769. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  1770. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  1771. end
  1772. end;
  1773. hexstr2longint:=rl;
  1774. end;
  1775. var
  1776. i: integer;
  1777. begin
  1778. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  1779. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  1780. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  1781. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  1782. ishexstr(copy(s,26,12)) then begin
  1783. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  1784. { these values are arealdy in the correct range (4 chars = word) }
  1785. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  1786. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  1787. for i:=0 to 1 do
  1788. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  1789. for i:=2 to 7 do
  1790. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  1791. string2guid:=true;
  1792. end
  1793. else
  1794. string2guid:=false;
  1795. end;
  1796. function guid2string(const GUID: TGUID): string;
  1797. function long2hex(l, len: longint): string;
  1798. const
  1799. hextbl: array[0..15] of char = '0123456789ABCDEF';
  1800. var
  1801. rs: string;
  1802. i: integer;
  1803. begin
  1804. rs[0]:=chr(len);
  1805. for i:=len downto 1 do begin
  1806. rs[i]:=hextbl[l and $F];
  1807. l:=l shr 4;
  1808. end;
  1809. long2hex:=rs;
  1810. end;
  1811. begin
  1812. guid2string:=
  1813. '{'+long2hex(GUID.D1,8)+
  1814. '-'+long2hex(GUID.D2,4)+
  1815. '-'+long2hex(GUID.D3,4)+
  1816. '-'+long2hex(GUID.D4[0],2)+long2hex(GUID.D4[1],2)+
  1817. '-'+long2hex(GUID.D4[2],2)+long2hex(GUID.D4[3],2)+
  1818. long2hex(GUID.D4[4],2)+long2hex(GUID.D4[5],2)+
  1819. long2hex(GUID.D4[6],2)+long2hex(GUID.D4[7],2)+
  1820. '}';
  1821. end;
  1822. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  1823. var
  1824. tok : string;
  1825. vstr : string;
  1826. l : longint;
  1827. code : integer;
  1828. b : talignmentinfo;
  1829. begin
  1830. UpdateAlignmentStr:=true;
  1831. uppervar(s);
  1832. fillchar(b,sizeof(b),0);
  1833. repeat
  1834. tok:=GetToken(s,'=');
  1835. if tok='' then
  1836. break;
  1837. vstr:=GetToken(s,',');
  1838. val(vstr,l,code);
  1839. if tok='PROC' then
  1840. b.procalign:=l
  1841. else if tok='JUMP' then
  1842. b.jumpalign:=l
  1843. else if tok='LOOP' then
  1844. b.loopalign:=l
  1845. else if tok='CONSTMIN' then
  1846. b.constalignmin:=l
  1847. else if tok='CONSTMAX' then
  1848. b.constalignmax:=l
  1849. else if tok='VARMIN' then
  1850. b.varalignmin:=l
  1851. else if tok='VARMAX' then
  1852. b.varalignmax:=l
  1853. else if tok='LOCALMIN' then
  1854. b.localalignmin:=l
  1855. else if tok='LOCALMAX' then
  1856. b.localalignmax:=l
  1857. else if tok='RECORDMIN' then
  1858. b.recordalignmin:=l
  1859. else if tok='RECORDMAX' then
  1860. b.recordalignmax:=l
  1861. else { Error }
  1862. UpdateAlignmentStr:=false;
  1863. until false;
  1864. UpdateAlignment(a,b);
  1865. end;
  1866. function var_align(siz: longint): longint;
  1867. begin
  1868. siz := size_2_align(siz);
  1869. var_align := used_align(siz,aktalignment.varalignmin,aktalignment.varalignmax);
  1870. end;
  1871. function const_align(siz: longint): longint;
  1872. begin
  1873. siz := size_2_align(siz);
  1874. const_align := used_align(siz,aktalignment.constalignmin,aktalignment.constalignmax);
  1875. end;
  1876. {****************************************************************************
  1877. Init
  1878. ****************************************************************************}
  1879. {$ifdef unix}
  1880. {$define need_path_search}
  1881. {$endif unix}
  1882. {$ifdef os2}
  1883. {$define need_path_search}
  1884. {$endif os2}
  1885. {$ifdef macos}
  1886. {$define need_path_search}
  1887. {$endif macos}
  1888. procedure get_exepath;
  1889. var
  1890. hs1 : namestr;
  1891. hs2 : extstr;
  1892. {$IFDEF USE_SYSUTILS}
  1893. exeName:String;
  1894. {$ENDIF USE_SYSUTILS}
  1895. {$ifdef need_path_search}
  1896. p : pchar;
  1897. {$endif need_path_search}
  1898. begin
  1899. {$IFDEF USE_SYSUTILS}
  1900. exepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  1901. {$ELSE USE_SYSUTILS}
  1902. exepath:=dos.getenv('PPC_EXEC_PATH');
  1903. {$ENDIF USE_SYSUTILS}
  1904. if exepath='' then
  1905. {$IFDEF USE_SYSUTILS}
  1906. exeName := FixFileName(system.paramstr(0));
  1907. exepath := ExtractFilePath(exeName);
  1908. hs1 := ExtractFileName(exeName);
  1909. hs2 := ExtractFileExt(exeName);
  1910. {$ELSE USE_SYSUTILS}
  1911. fsplit(FixFileName(system.paramstr(0)),exepath,hs1,hs2);
  1912. {$ENDIF USE_SYSUTILS}
  1913. {$ifdef need_path_search}
  1914. if exepath='' then
  1915. begin
  1916. if pos(source_info.exeext,hs1) <>
  1917. (length(hs1) - length(source_info.exeext)+1) then
  1918. hs1 := hs1 + source_info.exeext;
  1919. {$ifdef macos}
  1920. p:=GetEnvPchar('Commands');
  1921. {$else macos}
  1922. p:=GetEnvPchar('PATH');
  1923. {$endif macos}
  1924. FindFilePChar(hs1,p,exepath);
  1925. FreeEnvPChar(p);
  1926. exepath:=SplitPath(exepath);
  1927. end;
  1928. {$endif need_path_search}
  1929. exepath:=FixPath(exepath,false);
  1930. end;
  1931. procedure DoneGlobals;
  1932. begin
  1933. if assigned(DLLImageBase) then
  1934. StringDispose(DLLImageBase);
  1935. librarysearchpath.Free;
  1936. unitsearchpath.Free;
  1937. objectsearchpath.Free;
  1938. includesearchpath.Free;
  1939. end;
  1940. procedure InitGlobals;
  1941. begin
  1942. get_exepath;
  1943. { reset globals }
  1944. do_build:=false;
  1945. do_release:=false;
  1946. do_make:=true;
  1947. compile_level:=0;
  1948. DLLsource:=false;
  1949. inlining_procedure:=false;
  1950. resolving_forward:=false;
  1951. make_ref:=false;
  1952. LinkTypeSetExplicitly:=false;
  1953. { Output }
  1954. OutputFile:='';
  1955. OutputPrefix:=Nil;
  1956. OutputSuffix:=Nil;
  1957. OutputExtension:='';
  1958. OutputExeDir:='';
  1959. OutputUnitDir:='';
  1960. { Utils directory }
  1961. utilsdirectory:='';
  1962. utilsprefix:='';
  1963. cshared:=false;
  1964. rlinkpath:='';
  1965. { Search Paths }
  1966. librarysearchpath:=TSearchPathList.Create;
  1967. unitsearchpath:=TSearchPathList.Create;
  1968. includesearchpath:=TSearchPathList.Create;
  1969. objectsearchpath:=TSearchPathList.Create;
  1970. { Def file }
  1971. usewindowapi:=false;
  1972. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1973. DescriptionSetExplicity:=false;
  1974. dllversion:='';
  1975. dllmajor:=1;
  1976. dllminor:=0;
  1977. dllrevision:=0;
  1978. nwscreenname := '';
  1979. nwthreadname := '';
  1980. nwcopyright := '';
  1981. UseDeffileForExports:=false;
  1982. UseDeffileForExportsSetExplicitly:=false;
  1983. RelocSection:=false;
  1984. RelocSectionSetExplicitly:=false;
  1985. LinkTypeSetExplicitly:=false;
  1986. { Init values }
  1987. initmodeswitches:=fpcmodeswitches;
  1988. initlocalswitches:=[cs_check_io,cs_typed_const_writable];
  1989. initmoduleswitches:=[cs_extsyntax,cs_implicit_exceptions];
  1990. initsourcecodepage:='8859-1';
  1991. initglobalswitches:=[cs_check_unit_name,cs_link_static{$ifdef INTERNALLINKER},cs_link_internal,cs_link_map{$endif}];
  1992. initoutputformat:=target_asm.id;
  1993. fillchar(initalignment,sizeof(talignmentinfo),0);
  1994. { might be overridden later }
  1995. initasmmode:=asmmode_standard;
  1996. {$ifdef i386}
  1997. initoptprocessor:=ClassPentium3;
  1998. initspecificoptprocessor:=Class386;
  1999. initfputype:=fpu_x87;
  2000. initpackenum:=4;
  2001. {$IFDEF testvarsets}
  2002. initsetalloc:=0;
  2003. {$ENDIF}
  2004. initasmmode:=asmmode_i386_att;
  2005. {$endif i386}
  2006. {$ifdef m68k}
  2007. initoptprocessor:=MC68020;
  2008. initpackenum:=4;
  2009. {$IFDEF testvarsets}
  2010. initsetalloc:=0;
  2011. {$ENDIF}
  2012. {$endif m68k}
  2013. {$ifdef powerpc}
  2014. initoptprocessor:=PPC604;
  2015. initpackenum:=4;
  2016. {$IFDEF testvarsets}
  2017. initsetalloc:=0;
  2018. {$ENDIF}
  2019. initfputype:=fpu_standard;
  2020. {$endif powerpc}
  2021. {$ifdef sparc}
  2022. initoptprocessor:=SPARC_V8;
  2023. initpackenum:=4;
  2024. {$IFDEF testvarsets}
  2025. initsetalloc:=0;
  2026. {$ENDIF}
  2027. {$endif sparc}
  2028. {$ifdef arm}
  2029. initpackenum:=4;
  2030. {$IFDEF testvarsets}
  2031. initsetalloc:=0;
  2032. {$ENDIF}
  2033. initfputype:=fpu_fpa;
  2034. {$endif arm}
  2035. {$ifdef x86_64}
  2036. initoptprocessor:=ClassAthlon64;
  2037. initspecificoptprocessor:=ClassAthlon64;
  2038. initfputype:=fpu_sse64;
  2039. initpackenum:=4;
  2040. {$IFDEF testvarsets}
  2041. initsetalloc:=0;
  2042. {$ENDIF}
  2043. initasmmode:=asmmode_x86_64_gas;
  2044. {$endif x86_64}
  2045. initinterfacetype:=it_interfacecom;
  2046. initdefproccall:=pocall_default;
  2047. { memory sizes, will be overriden by parameter or default for target
  2048. in options or init_parser }
  2049. stacksize:=0;
  2050. { not initialized yet }
  2051. jmp_buf_size:=-1;
  2052. apptype:=app_cui;
  2053. end;
  2054. end.