globals.pas 65 KB

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