globals.pas 65 KB

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