globals.pas 65 KB

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