globals.pas 66 KB

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