globals.pas 65 KB

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