globals.pas 65 KB

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