globals.pas 65 KB

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