globals.pas 66 KB

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