globals.pas 66 KB

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