globals.pas 65 KB

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