globals.pas 64 KB

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