globals.pas 66 KB

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