globals.pas 64 KB

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