globals.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. {
  2. $Id$
  3. Copyright (C) 1998-2000 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 defines.inc}
  20. interface
  21. uses
  22. {$ifdef win32}
  23. windows,
  24. {$endif}
  25. {$ifdef unix}
  26. {$ifdef ver1_0}
  27. linux,
  28. {$else}
  29. unix,
  30. {$endif}
  31. {$endif}
  32. {$ifdef os2}
  33. doscalls,
  34. {$endif}
  35. {$ifdef Delphi}
  36. sysutils,
  37. dmisc,
  38. {$else}
  39. strings,
  40. dos,
  41. {$endif}
  42. cutils,cclasses,
  43. globtype,version,systems;
  44. const
  45. {$ifdef unix}
  46. DirSep = '/';
  47. {$else}
  48. {$ifdef amiga}
  49. DirSep = '/';
  50. {$else}
  51. DirSep = '\';
  52. {$endif}
  53. {$endif}
  54. {$ifdef Splitheap}
  55. testsplit : boolean = false;
  56. {$endif Splitheap}
  57. delphimodeswitches : tmodeswitches=
  58. [m_delphi,m_tp,m_all,m_class,m_objpas,m_result,m_string_pchar,
  59. m_pointer_2_procedure,m_autoderef,m_tp_procvar,m_initfinal,m_default_ansistring,
  60. m_out,m_default_para];
  61. fpcmodeswitches : tmodeswitches=
  62. [m_fpc,m_all,m_string_pchar,m_nested_comment,m_repeat_forward,
  63. m_cvar_support,m_initfinal,m_add_pointer];
  64. objfpcmodeswitches : tmodeswitches=
  65. [m_objfpc,m_fpc,m_all,m_class,m_objpas,m_result,m_string_pchar,m_nested_comment,
  66. m_repeat_forward,m_cvar_support,m_initfinal,m_add_pointer,m_out,m_default_para];
  67. tpmodeswitches : tmodeswitches=
  68. [m_tp7,m_tp,m_all,m_tp_procvar];
  69. gpcmodeswitches : tmodeswitches=
  70. [m_gpc,m_all];
  71. type
  72. pfileposinfo = ^tfileposinfo;
  73. tfileposinfo = record
  74. line : longint;
  75. column : word;
  76. fileindex : word;
  77. end;
  78. TSearchPathList = class(TStringList)
  79. procedure AddPath(s:string;addfirst:boolean);
  80. procedure AddList(list:TSearchPathList;addfirst:boolean);
  81. function FindFile(const f : string;var foundfile:string):boolean;
  82. end;
  83. var
  84. { specified inputfile }
  85. inputdir : dirstr;
  86. inputfile : namestr;
  87. inputextension : extstr;
  88. { specified outputfile with -o parameter }
  89. outputfile : namestr;
  90. { specified with -FE or -FU }
  91. outputexedir : dirstr;
  92. outputunitdir : dirstr;
  93. { things specified with parameters }
  94. paralinkoptions,
  95. paradynamiclinker : string;
  96. parapreprocess : boolean;
  97. { directory where the utils can be found (options -FD) }
  98. utilsdirectory : dirstr;
  99. { some flags for global compiler switches }
  100. do_build,
  101. do_make : boolean;
  102. not_unit_proc : boolean;
  103. { path for searching units, different paths can be seperated by ; }
  104. exepath : dirstr; { Path to ppc }
  105. librarysearchpath,
  106. unitsearchpath,
  107. objectsearchpath,
  108. includesearchpath : TSearchPathList;
  109. { deffile }
  110. usewindowapi : boolean;
  111. description : string;
  112. dllversion : string;
  113. dllmajor,dllminor,dllrevision : word; { revision only for netware }
  114. akttokenpos, { position of the last token }
  115. aktfilepos : tfileposinfo; { current position }
  116. { type of currently parsed block }
  117. { isn't full implemented (FK) }
  118. block_type : tblock_type;
  119. in_args : boolean; { arguments must be checked especially }
  120. parsing_para_level : integer; { parameter level, used to convert
  121. proc calls to proc loads in firstcalln }
  122. compile_level : word;
  123. make_ref : boolean;
  124. resolving_forward : boolean; { used to add forward reference as second ref }
  125. use_esp_stackframe : boolean; { to test for call with ESP as stack frame }
  126. inlining_procedure : boolean; { are we inlining a procedure }
  127. statement_level : integer;
  128. aktexceptblock : integer; { each except block gets a number check gotos }
  129. { commandline values }
  130. initdefines : tstringlist;
  131. initglobalswitches : tglobalswitches;
  132. initmoduleswitches : tmoduleswitches;
  133. initlocalswitches : tlocalswitches;
  134. initmodeswitches : tmodeswitches;
  135. {$IFDEF testvarsets}
  136. Initsetalloc, {0=fixed, 1 =var}
  137. {$ENDIF}
  138. initpackenum : longint;
  139. initpackrecords : tpackrecords;
  140. initoptprocessor,
  141. initspecificoptprocessor : tprocessors;
  142. initasmmode : tasmmode;
  143. initinterfacetype : tinterfacetypes;
  144. initoutputformat : tasm;
  145. { current state values }
  146. aktglobalswitches : tglobalswitches;
  147. aktmoduleswitches : tmoduleswitches;
  148. aktlocalswitches : tlocalswitches;
  149. nextaktlocalswitches : tlocalswitches;
  150. localswitcheschanged : boolean;
  151. aktmodeswitches : tmodeswitches;
  152. {$IFDEF testvarsets}
  153. aktsetalloc,
  154. {$ENDIF}
  155. aktpackenum : longint;
  156. aktmaxfpuregisters : longint;
  157. aktpackrecords : tpackrecords;
  158. aktoptprocessor,
  159. aktspecificoptprocessor : tprocessors;
  160. aktasmmode : tasmmode;
  161. aktinterfacetype : tinterfacetypes;
  162. aktoutputformat : tasm;
  163. { Memory sizes }
  164. heapsize,
  165. maxheapsize,
  166. stacksize : longint;
  167. {$Ifdef EXTDEBUG}
  168. total_of_firstpass,
  169. firstpass_several : longint;
  170. {$ifdef FPC}
  171. EntryMemUsed : longint;
  172. {$endif FPC}
  173. { parameter switches }
  174. debugstop,
  175. only_one_pass : boolean;
  176. {$EndIf EXTDEBUG}
  177. { windows / OS/2 application type }
  178. apptype : tapptype;
  179. const
  180. RelocSection : boolean = true;
  181. RelocSectionSetExplicitly : boolean = false;
  182. LinkTypeSetExplicitly : boolean = false;
  183. IsExe : boolean = false;
  184. DLLsource : boolean = false;
  185. DLLImageBase : pstring = nil;
  186. UseDeffileForExport : boolean = true;
  187. ForceDeffileForExport : boolean = false;
  188. { used to set all registers used for each global function
  189. this should dramatically decrease the number of
  190. recompilations needed PM }
  191. simplify_ppu : boolean = false;
  192. { should we allow non static members ? }
  193. allow_only_static : boolean = false;
  194. Inside_asm_statement : boolean = false;
  195. global_unit_count : word = 0;
  196. { for error info in pp.pas }
  197. parser_current_file : string = '';
  198. procedure abstract;
  199. function bstoslash(const s : string) : string;
  200. function getdatestr:string;
  201. function gettimestr:string;
  202. function filetimestring( t : longint) : string;
  203. procedure DefaultReplacements(var s:string);
  204. function GetCurrentDir:string;
  205. function path_absolute(const s : string) : boolean;
  206. Function PathExists ( F : String) : Boolean;
  207. Function FileExists ( Const F : String) : Boolean;
  208. Function RemoveFile(const f:string):boolean;
  209. Function RemoveDir(d:string):boolean;
  210. Function GetFileTime ( Var F : File) : Longint;
  211. Function GetNamedFileTime ( Const F : String) : Longint;
  212. Function SplitPath(const s:string):string;
  213. Function SplitFileName(const s:string):string;
  214. Function SplitName(const s:string):string;
  215. Function SplitExtension(Const HStr:String):String;
  216. Function AddExtension(Const HStr,ext:String):String;
  217. Function ForceExtension(Const HStr,ext:String):String;
  218. Function FixPath(s:string;allowdot:boolean):string;
  219. function FixFileName(const s:string):string;
  220. procedure SplitBinCmd(const s:string;var bstr,cstr:string);
  221. function FindFile(const f : string;path : string;var foundfile:string):boolean;
  222. function FindExe(const bin:string;var foundfile:string):boolean;
  223. function GetShortName(const n:string):string;
  224. Procedure Shell(const command:string);
  225. function GetEnvPChar(const envname:string):pchar;
  226. procedure FreeEnvPChar(p:pchar);
  227. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  228. procedure InitGlobals;
  229. procedure DoneGlobals;
  230. function string2guid(const s: string; var GUID: TGUID): boolean;
  231. function guid2string(const GUID: TGUID): string;
  232. implementation
  233. uses
  234. comphook;
  235. procedure abstract;
  236. begin
  237. do_internalerror(255);
  238. end;
  239. function bstoslash(const s : string) : string;
  240. {
  241. return string s with all \ changed into /
  242. }
  243. var
  244. i : longint;
  245. begin
  246. for i:=1to length(s) do
  247. if s[i]='\' then
  248. bstoslash[i]:='/'
  249. else
  250. bstoslash[i]:=s[i];
  251. bstoslash[0]:=s[0];
  252. end;
  253. {****************************************************************************
  254. Time Handling
  255. ****************************************************************************}
  256. Function L0(l:longint):string;
  257. {
  258. return the string of value l, if l<10 then insert a zero, so
  259. the string is always at least 2 chars '01','02',etc
  260. }
  261. var
  262. s : string;
  263. begin
  264. Str(l,s);
  265. if l<10 then
  266. s:='0'+s;
  267. L0:=s;
  268. end;
  269. function gettimestr:string;
  270. {
  271. get the current time in a string HH:MM:SS
  272. }
  273. var
  274. hour,min,sec,hsec : word;
  275. begin
  276. {$ifdef delphi}
  277. dmisc.gettime(hour,min,sec,hsec);
  278. {$else delphi}
  279. dos.gettime(hour,min,sec,hsec);
  280. {$endif delphi}
  281. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  282. end;
  283. function getdatestr:string;
  284. {
  285. get the current date in a string YY/MM/DD
  286. }
  287. var
  288. Year,Month,Day,Wday : Word;
  289. begin
  290. {$ifdef delphi}
  291. dmisc.getdate(year,month,day,wday);
  292. {$else}
  293. dos.getdate(year,month,day,wday);
  294. {$endif}
  295. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  296. end;
  297. function filetimestring( t : longint) : string;
  298. {
  299. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  300. }
  301. var
  302. {$ifndef unix}
  303. DT : DateTime;
  304. {$endif}
  305. Year,Month,Day,Hour,Min,Sec : Word;
  306. begin
  307. if t=-1 then
  308. begin
  309. FileTimeString:='Not Found';
  310. exit;
  311. end;
  312. {$ifndef unix}
  313. unpacktime(t,DT);
  314. Year:=dT.year;month:=dt.month;day:=dt.day;
  315. Hour:=dt.hour;min:=dt.min;sec:=dt.sec;
  316. {$else}
  317. EpochToLocal (t,year,month,day,hour,min,sec);
  318. {$endif}
  319. filetimestring:=L0(Year)+'/'+L0(Month)+'/'+L0(Day)+' '+L0(Hour)+':'+L0(min)+':'+L0(sec);
  320. end;
  321. {****************************************************************************
  322. Default Macro Handling
  323. ****************************************************************************}
  324. procedure DefaultReplacements(var s:string);
  325. begin
  326. { Replace some macro's }
  327. Replace(s,'$FPCVER',version_string);
  328. Replace(s,'$VERSION',version_string);
  329. Replace(s,'$FULLVERSION',full_version_string);
  330. Replace(s,'$FPCDATE',date_string);
  331. Replace(s,'$FPCTARGET',target_cpu_string);
  332. Replace(s,'$FPCCPU',target_cpu_string);
  333. Replace(s,'$TARGET',target_path);
  334. Replace(s,'$FPCOS',target_path);
  335. end;
  336. {****************************************************************************
  337. File Handling
  338. ****************************************************************************}
  339. function GetCurrentDir:string;
  340. var
  341. CurrentDir : string;
  342. begin
  343. GetDir(0,CurrentDir);
  344. GetCurrentDir:=FixPath(CurrentDir,false);
  345. end;
  346. function path_absolute(const s : string) : boolean;
  347. {
  348. is path s an absolute path?
  349. }
  350. begin
  351. path_absolute:=false;
  352. {$ifdef unix}
  353. if (length(s)>0) and (s[1]='/') then
  354. path_absolute:=true;
  355. {$else unix}
  356. {$ifdef amiga}
  357. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or (Pos(':',s) = length(s)) then
  358. path_absolute:=true;
  359. {$else}
  360. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or
  361. ((length(s)>2) and (s[2]=':') and ((s[3]='\') or (s[3]='/'))) then
  362. path_absolute:=true;
  363. {$endif amiga}
  364. {$endif unix}
  365. end;
  366. {$ifndef FPC}
  367. Procedure FindClose(var Info : SearchRec);
  368. Begin
  369. End;
  370. {$endif not FPC}
  371. Function FileExists ( Const F : String) : Boolean;
  372. {$ifndef delphi}
  373. Var
  374. Info : SearchRec;
  375. {$endif}
  376. begin
  377. {$ifdef delphi}
  378. FileExists:=sysutils.FileExists(f);
  379. {$else}
  380. findfirst(F,readonly+archive+hidden,info);
  381. FileExists:=(doserror=0);
  382. findclose(Info);
  383. {$endif delphi}
  384. end;
  385. Function PathExists ( F : String) : Boolean;
  386. Var
  387. Info : SearchRec;
  388. begin
  389. if F[Length(f)] in ['/','\'] then
  390. Delete(f,length(f),1);
  391. findfirst(F,readonly+archive+hidden+directory,info);
  392. PathExists:=(doserror=0) and ((info.attr and directory)=directory);
  393. findclose(Info);
  394. end;
  395. Function RemoveFile(const f:string):boolean;
  396. var
  397. g : file;
  398. begin
  399. assign(g,f);
  400. {$I-}
  401. erase(g);
  402. {$I+}
  403. RemoveFile:=(ioresult=0);
  404. end;
  405. Function RemoveDir(d:string):boolean;
  406. begin
  407. if d[length(d)]=DirSep then
  408. Delete(d,length(d),1);
  409. {$I-}
  410. rmdir(d);
  411. {$I+}
  412. RemoveDir:=(ioresult=0);
  413. end;
  414. Function SplitPath(const s:string):string;
  415. var
  416. i : longint;
  417. begin
  418. i:=Length(s);
  419. while (i>0) and not(s[i] in ['/','\']) do
  420. dec(i);
  421. SplitPath:=Copy(s,1,i);
  422. end;
  423. Function SplitFileName(const s:string):string;
  424. var
  425. p : dirstr;
  426. n : namestr;
  427. e : extstr;
  428. begin
  429. FSplit(s,p,n,e);
  430. SplitFileName:=n+e;
  431. end;
  432. Function SplitName(const s:string):string;
  433. var
  434. i,j : longint;
  435. begin
  436. i:=Length(s);
  437. j:=Length(s);
  438. while (i>0) and not(s[i] in ['/','\']) do
  439. dec(i);
  440. while (j>0) and (s[j]<>'.') do
  441. dec(j);
  442. if j<=i then
  443. j:=255;
  444. SplitName:=Copy(s,i+1,j-(i+1));
  445. end;
  446. Function SplitExtension(Const HStr:String):String;
  447. var
  448. j : longint;
  449. begin
  450. j:=length(Hstr);
  451. while (j>0) and (Hstr[j]<>'.') do
  452. begin
  453. if hstr[j]=DirSep then
  454. j:=0
  455. else
  456. dec(j);
  457. end;
  458. if j=0 then
  459. j:=254;
  460. SplitExtension:=Copy(Hstr,j,255);
  461. end;
  462. Function AddExtension(Const HStr,ext:String):String;
  463. begin
  464. if (Ext<>'') and (SplitExtension(HStr)='') then
  465. AddExtension:=Hstr+Ext
  466. else
  467. AddExtension:=Hstr;
  468. end;
  469. Function ForceExtension(Const HStr,ext:String):String;
  470. var
  471. j : longint;
  472. begin
  473. j:=length(Hstr);
  474. while (j>0) and (Hstr[j]<>'.') do
  475. dec(j);
  476. if j=0 then
  477. j:=255;
  478. ForceExtension:=Copy(Hstr,1,j-1)+Ext;
  479. end;
  480. Function FixPath(s:string;allowdot:boolean):string;
  481. var
  482. i : longint;
  483. begin
  484. { Fix separator }
  485. for i:=1 to length(s) do
  486. if s[i] in ['/','\'] then
  487. s[i]:=DirSep;
  488. { Fix ending / }
  489. if (length(s)>0) and (s[length(s)]<>DirSep) and
  490. (s[length(s)]<>':') then
  491. s:=s+DirSep;
  492. { Remove ./ }
  493. if (not allowdot) and (s='.'+DirSep) then
  494. s:='';
  495. { return }
  496. {$ifdef unix}
  497. FixPath:=s;
  498. {$else}
  499. FixPath:=Lower(s);
  500. {$endif}
  501. end;
  502. function FixFileName(const s:string):string;
  503. var
  504. i : longint;
  505. begin
  506. for i:=length(s) downto 1 do
  507. begin
  508. case s[i] of
  509. {$ifdef Unix}
  510. '/','\' :
  511. FixFileName[i]:='/';
  512. {$else Unix}
  513. '/' :
  514. FixFileName[i]:='\';
  515. 'A'..'Z' :
  516. FixFileName[i]:=char(byte(s[i])+32);
  517. {$endif Unix}
  518. else
  519. FixFileName[i]:=s[i];
  520. end;
  521. end;
  522. FixFileName[0]:=s[0];
  523. end;
  524. procedure SplitBinCmd(const s:string;var bstr,cstr:string);
  525. var
  526. i : longint;
  527. begin
  528. i:=pos(' ',s);
  529. if i>0 then
  530. begin
  531. bstr:=Copy(s,1,i-1);
  532. cstr:=Copy(s,i+1,length(s)-i);
  533. end
  534. else
  535. begin
  536. bstr:='';
  537. cstr:='';
  538. end;
  539. end;
  540. procedure TSearchPathList.AddPath(s:string;addfirst:boolean);
  541. var
  542. j : longint;
  543. hs,hsd,
  544. CurrentDir,
  545. CurrPath : string;
  546. dir : searchrec;
  547. hp : TStringListItem;
  548. procedure addcurrpath;
  549. begin
  550. if addfirst then
  551. begin
  552. Remove(currPath);
  553. Insert(currPath);
  554. end
  555. else
  556. begin
  557. { Check if already in path, then we don't add it }
  558. hp:=Find(currPath);
  559. if not assigned(hp) then
  560. Concat(currPath);
  561. end;
  562. end;
  563. begin
  564. if s='' then
  565. exit;
  566. { Support default macro's }
  567. DefaultReplacements(s);
  568. { get current dir }
  569. CurrentDir:=GetCurrentDir;
  570. repeat
  571. { get currpath }
  572. if addfirst then
  573. begin
  574. j:=length(s);
  575. while (j>0) and (s[j]<>';') do
  576. dec(j);
  577. CurrPath:=FixPath(Copy(s,j+1,length(s)-j),false);
  578. if j=0 then
  579. s:=''
  580. else
  581. System.Delete(s,j,length(s)-j+1);
  582. end
  583. else
  584. begin
  585. j:=Pos(';',s);
  586. if j=0 then
  587. j:=255;
  588. CurrPath:=FixPath(Copy(s,1,j-1),false);
  589. System.Delete(s,1,j);
  590. end;
  591. { fix pathname }
  592. if CurrPath='' then
  593. CurrPath:='.'+DirSep
  594. else
  595. begin
  596. CurrPath:=FixPath(FExpand(CurrPath),false);
  597. if (CurrentDir<>'') and (Copy(CurrPath,1,length(CurrentDir))=CurrentDir) then
  598. CurrPath:='.'+DirSep+Copy(CurrPath,length(CurrentDir)+1,255);
  599. end;
  600. { wildcard adding ? }
  601. if pos('*',currpath)>0 then
  602. begin
  603. if currpath[length(currpath)]=dirsep then
  604. hs:=Copy(currpath,1,length(CurrPath)-1)
  605. else
  606. hs:=currpath;
  607. hsd:=SplitPath(hs);
  608. findfirst(hs,directory,dir);
  609. while doserror=0 do
  610. begin
  611. if (dir.name<>'.') and
  612. (dir.name<>'..') and
  613. ((dir.attr and directory)<>0) then
  614. begin
  615. currpath:=hsd+dir.name+dirsep;
  616. hp:=Find(currPath);
  617. if not assigned(hp) then
  618. AddCurrPath;
  619. end;
  620. findnext(dir);
  621. end;
  622. FindClose(dir);
  623. end
  624. else
  625. begin
  626. if PathExists(currpath) then
  627. addcurrpath;
  628. end;
  629. until (s='');
  630. end;
  631. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  632. var
  633. s : string;
  634. hl : TSearchPathList;
  635. hp,hp2 : TStringListItem;
  636. begin
  637. if list.empty then
  638. exit;
  639. { create temp and reverse the list }
  640. if addfirst then
  641. begin
  642. hl:=TSearchPathList.Create;
  643. hp:=TStringListItem(list.first);
  644. while assigned(hp) do
  645. begin
  646. hl.insert(hp.Str);
  647. hp:=TStringListItem(hp.next);
  648. end;
  649. while not hl.empty do
  650. begin
  651. s:=hl.GetFirst;
  652. Remove(s);
  653. Insert(s);
  654. end;
  655. hl.Free;
  656. end
  657. else
  658. begin
  659. hp:=TStringListItem(list.first);
  660. while assigned(hp) do
  661. begin
  662. hp2:=Find(hp.Str);
  663. { Check if already in path, then we don't add it }
  664. if not assigned(hp2) then
  665. Concat(hp.Str);
  666. hp:=TStringListItem(hp.next);
  667. end;
  668. end;
  669. end;
  670. function TSearchPathList.FindFile(const f : string;var foundfile:string):boolean;
  671. Var
  672. p : TStringListItem;
  673. begin
  674. FindFile:=false;
  675. p:=TStringListItem(first);
  676. while assigned(p) do
  677. begin
  678. {
  679. Search order for case sensitive systems:
  680. 1. lowercase
  681. 2. NormalCase
  682. 3. UPPERCASE
  683. None case sensitive only lowercase
  684. }
  685. FoundFile:=p.Str+Lower(f);
  686. If FileExists(FoundFile) then
  687. begin
  688. FindFile:=true;
  689. exit;
  690. end;
  691. {$ifdef UNIX}
  692. FoundFile:=p.Str+f;
  693. If FileExists(FoundFile) then
  694. begin
  695. FindFile:=true;
  696. exit;
  697. end;
  698. FoundFile:=p.Str+Upper(f);
  699. If FileExists(FoundFile) then
  700. begin
  701. FindFile:=true;
  702. exit;
  703. end;
  704. {$endif UNIX}
  705. p:=TStringListItem(p.next);
  706. end;
  707. { Return original filename if not found }
  708. FoundFile:=f;
  709. end;
  710. Function GetFileTime ( Var F : File) : Longint;
  711. Var
  712. {$ifdef unix}
  713. Info : Stat;
  714. {$endif}
  715. L : longint;
  716. begin
  717. {$ifdef unix}
  718. FStat (F,Info);
  719. L:=Info.Mtime;
  720. {$else}
  721. GetFTime(f,l);
  722. {$endif}
  723. GetFileTime:=L;
  724. end;
  725. Function GetNamedFileTime (Const F : String) : Longint;
  726. begin
  727. GetNamedFileTime:=do_getnamedfiletime(F);
  728. end;
  729. function FindFile(const f : string;path : string;var foundfile:string):boolean;
  730. Var
  731. singlepathstring : string;
  732. i : longint;
  733. begin
  734. {$ifdef Unix}
  735. for i:=1 to length(path) do
  736. if path[i]=':' then
  737. path[i]:=';';
  738. {$endif Unix}
  739. FindFile:=false;
  740. repeat
  741. i:=pos(';',path);
  742. if i=0 then
  743. i:=256;
  744. singlepathstring:=FixPath(copy(path,1,i-1),false);
  745. delete(path,1,i);
  746. {
  747. Search order for case sensitive systems:
  748. 1. lowercase
  749. 2. NormalCase
  750. 3. UPPERCASE
  751. None case sensitive only lowercase
  752. }
  753. FoundFile:=singlepathstring+Lower(f);
  754. If FileExists(FoundFile) then
  755. begin
  756. FindFile:=true;
  757. exit;
  758. end;
  759. {$ifdef UNIX}
  760. FoundFile:=singlepathstring+f;
  761. If FileExists(FoundFile) then
  762. begin
  763. FindFile:=true;
  764. exit;
  765. end;
  766. FoundFile:=singlepathstring+Upper(f);
  767. If FileExists(FoundFile) then
  768. begin
  769. FindFile:=true;
  770. exit;
  771. end;
  772. {$endif UNIX}
  773. until path='';
  774. FoundFile:=f;
  775. end;
  776. function FindExe(const bin:string;var foundfile:string):boolean;
  777. begin
  778. {$ifdef delphi}
  779. FindExe:=FindFile(FixFileName(AddExtension(bin,source_info.exeext)),'.;'+exepath+';'+dmisc.getenv('PATH'),foundfile);
  780. {$else delphi}
  781. FindExe:=FindFile(FixFileName(AddExtension(bin,source_info.exeext)),'.;'+exepath+';'+dos.getenv('PATH'),foundfile);
  782. {$endif delphi}
  783. end;
  784. function GetShortName(const n:string):string;
  785. {$ifdef win32}
  786. var
  787. hs,hs2 : string;
  788. i : longint;
  789. {$endif}
  790. {$ifdef go32v2}
  791. var
  792. hs : string;
  793. {$endif}
  794. begin
  795. GetShortName:=n;
  796. {$ifdef win32}
  797. hs:=n+#0;
  798. i:=Windows.GetShortPathName(@hs[1],@hs2[1],high(hs2));
  799. if (i>0) and (i<=high(hs2)) then
  800. begin
  801. hs2[0]:=chr(strlen(@hs2[1]));
  802. GetShortName:=hs2;
  803. end;
  804. {$endif}
  805. {$ifdef go32v2}
  806. hs:=n;
  807. if Dos.GetShortName(hs) then
  808. GetShortName:=hs;
  809. {$endif}
  810. end;
  811. {****************************************************************************
  812. OS Dependent things
  813. ****************************************************************************}
  814. function GetEnvPChar(const envname:string):pchar;
  815. {$ifdef win32}
  816. var
  817. s : string;
  818. i,len : longint;
  819. hp,p,p2 : pchar;
  820. {$endif}
  821. {$ifdef os2}
  822. var
  823. P1, P2: PChar;
  824. {$endif}
  825. begin
  826. {$ifdef unix}
  827. GetEnvPchar:={$ifdef ver1_0}Linux{$else}Unix{$endif}.Getenv(envname);
  828. {$define GETENVOK}
  829. {$endif}
  830. {$ifdef win32}
  831. GetEnvPchar:=nil;
  832. p:=GetEnvironmentStrings;
  833. hp:=p;
  834. while hp^<>#0 do
  835. begin
  836. s:=strpas(hp);
  837. i:=pos('=',s);
  838. len:=strlen(hp);
  839. if upper(copy(s,1,i-1))=upper(envname) then
  840. begin
  841. GetMem(p2,len-length(envname));
  842. Move(hp[i],p2^,len-length(envname));
  843. GetEnvPchar:=p2;
  844. break;
  845. end;
  846. { next string entry}
  847. hp:=hp+len+1;
  848. end;
  849. FreeEnvironmentStrings(p);
  850. {$define GETENVOK}
  851. {$endif}
  852. {$ifdef os2}
  853. P1 := StrPNew (EnvName);
  854. if Assigned (P1) then
  855. begin
  856. if DosCalls.DosScanEnv (P1, P2) = 0 then
  857. GetEnvPChar := P2
  858. else
  859. GetEnvPChar := nil;
  860. StrDispose (P1);
  861. end else GetEnvPChar := nil;
  862. {$define GETENVOK}
  863. {$endif}
  864. {$ifdef GETENVOK}
  865. {$undef GETENVOK}
  866. {$else}
  867. GetEnvPchar:=StrPNew(Dos.Getenv(envname));
  868. {$endif}
  869. end;
  870. procedure FreeEnvPChar(p:pchar);
  871. begin
  872. {$ifndef unix}
  873. {$ifndef os2}
  874. StrDispose(p);
  875. {$endif}
  876. {$endif}
  877. end;
  878. Procedure Shell(const command:string);
  879. { This is already defined in the linux.ppu for linux, need for the *
  880. expansion under linux }
  881. {$ifdef unix}
  882. begin
  883. {$ifdef ver1_0}Linux{$else}Unix{$endif}.Shell(command);
  884. end;
  885. {$else}
  886. var
  887. comspec : string;
  888. begin
  889. comspec:=getenv('COMSPEC');
  890. Exec(comspec,' /C '+command);
  891. end;
  892. {$endif}
  893. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  894. var
  895. b : boolean;
  896. begin
  897. b:=true;
  898. if s='DEFAULT' then
  899. aktmodeswitches:=initmodeswitches
  900. else
  901. if s='DELPHI' then
  902. aktmodeswitches:=delphimodeswitches
  903. else
  904. if s='TP' then
  905. aktmodeswitches:=tpmodeswitches
  906. else
  907. if s='FPC' then
  908. aktmodeswitches:=fpcmodeswitches
  909. else
  910. if s='OBJFPC' then
  911. aktmodeswitches:=objfpcmodeswitches
  912. else
  913. if s='GPC' then
  914. aktmodeswitches:=gpcmodeswitches
  915. else
  916. b:=false;
  917. if b and changeInit then
  918. initmodeswitches := aktmodeswitches;
  919. if b then
  920. begin
  921. { turn ansistrings on by default ? }
  922. if (m_delphi in aktmodeswitches) then
  923. begin
  924. include(aktlocalswitches,cs_ansistrings);
  925. if changeinit then
  926. include(initlocalswitches,cs_ansistrings);
  927. end
  928. else
  929. begin
  930. exclude(aktlocalswitches,cs_ansistrings);
  931. if changeinit then
  932. exclude(initlocalswitches,cs_ansistrings);
  933. end;
  934. { enum packing }
  935. if (m_tp7 in aktmodeswitches) then
  936. aktpackenum:=1
  937. else
  938. aktpackenum:=4;
  939. if changeinit then
  940. initpackenum:=aktpackenum;
  941. end;
  942. SetCompileMode:=b;
  943. end;
  944. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  945. function string2guid(const s: string; var GUID: TGUID): boolean;
  946. function ishexstr(const hs: string): boolean;
  947. var
  948. i: integer;
  949. begin
  950. ishexstr:=false;
  951. for i:=1 to Length(hs) do begin
  952. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  953. exit;
  954. end;
  955. ishexstr:=true;
  956. end;
  957. function hexstr2longint(const hexs: string): longint;
  958. var
  959. i: integer;
  960. rl: longint;
  961. begin
  962. rl:=0;
  963. for i:=1 to length(hexs) do begin
  964. rl:=rl shl 4;
  965. case hexs[i] of
  966. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  967. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  968. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  969. end
  970. end;
  971. hexstr2longint:=rl;
  972. end;
  973. var
  974. i: integer;
  975. begin
  976. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  977. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  978. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  979. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  980. ishexstr(copy(s,26,12)) then begin
  981. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  982. GUID.D2:=hexstr2longint(copy(s,11,4));
  983. GUID.D3:=hexstr2longint(copy(s,16,4));
  984. for i:=0 to 1 do
  985. GUID.D4[i]:=hexstr2longint(copy(s,21+i*2,2));
  986. for i:=2 to 7 do
  987. GUID.D4[i]:=hexstr2longint(copy(s,22+i*2,2));
  988. string2guid:=true;
  989. end
  990. else
  991. string2guid:=false;
  992. end;
  993. function guid2string(const GUID: TGUID): string;
  994. function long2hex(l, len: longint): string;
  995. const
  996. hextbl: array[0..15] of char = '0123456789ABCDEF';
  997. var
  998. rs: string;
  999. i: integer;
  1000. begin
  1001. rs[0]:=chr(len);
  1002. for i:=len downto 1 do begin
  1003. rs[i]:=hextbl[l and $F];
  1004. l:=l shr 4;
  1005. end;
  1006. long2hex:=rs;
  1007. end;
  1008. begin
  1009. guid2string:=
  1010. '{'+long2hex(GUID.D1,8)+
  1011. '-'+long2hex(GUID.D2,4)+
  1012. '-'+long2hex(GUID.D3,4)+
  1013. '-'+long2hex(GUID.D4[0],2)+long2hex(GUID.D4[1],2)+
  1014. '-'+long2hex(GUID.D4[2],2)+long2hex(GUID.D4[3],2)+
  1015. long2hex(GUID.D4[4],2)+long2hex(GUID.D4[5],2)+
  1016. long2hex(GUID.D4[6],2)+long2hex(GUID.D4[7],2)+
  1017. '}';
  1018. end;
  1019. {****************************************************************************
  1020. Init
  1021. ****************************************************************************}
  1022. {$ifdef unix}
  1023. {$define need_path_search}
  1024. {$endif unix}
  1025. {$ifdef os2}
  1026. {$define need_path_search}
  1027. {$endif os2}
  1028. procedure get_exepath;
  1029. var
  1030. hs1 : namestr;
  1031. hs2 : extstr;
  1032. begin
  1033. {$ifdef delphi}
  1034. exepath:=dmisc.getenv('PPC_EXEC_PATH');
  1035. {$else delphi}
  1036. exepath:=dos.getenv('PPC_EXEC_PATH');
  1037. {$endif delphi}
  1038. if exepath='' then
  1039. fsplit(FixFileName(system.paramstr(0)),exepath,hs1,hs2);
  1040. {$ifdef need_path_search}
  1041. if exepath='' then
  1042. begin
  1043. if pos(source_info.exeext,hs1) <>
  1044. (length(hs1) - length(source_info.exeext)+1) then
  1045. hs1 := hs1 + source_info.exeext;
  1046. {$ifdef delphi}
  1047. findfile(hs1,dmisc.getenv('PATH'),exepath);
  1048. {$else delphi}
  1049. findfile(hs1,dos.getenv('PATH'),exepath);
  1050. {$endif delphi}
  1051. exepath:=SplitPath(exepath);
  1052. end;
  1053. {$endif need_path_search}
  1054. exepath:=FixPath(exepath,false);
  1055. end;
  1056. procedure DoneGlobals;
  1057. begin
  1058. initdefines.free;
  1059. if assigned(DLLImageBase) then
  1060. StringDispose(DLLImageBase);
  1061. RelocSection:=true;
  1062. RelocSectionSetExplicitly:=false;
  1063. UseDeffileForExport:=true;
  1064. librarysearchpath.Free;
  1065. unitsearchpath.Free;
  1066. objectsearchpath.Free;
  1067. includesearchpath.Free;
  1068. end;
  1069. procedure InitGlobals;
  1070. begin
  1071. { set global switches }
  1072. do_build:=false;
  1073. do_make:=true;
  1074. compile_level:=0;
  1075. { these two should not be cleared in
  1076. DoneGlobals as the IDE might need their value }
  1077. IsExe:=false;
  1078. DLLsource:=false;
  1079. { Output }
  1080. OutputFile:='';
  1081. OutputExeDir:='';
  1082. OutputUnitDir:='';
  1083. { Utils directory }
  1084. utilsdirectory:='';
  1085. { Search Paths }
  1086. librarysearchpath:=TSearchPathList.Create;
  1087. unitsearchpath:=TSearchPathList.Create;
  1088. includesearchpath:=TSearchPathList.Create;
  1089. objectsearchpath:=TSearchPathList.Create;
  1090. { Def file }
  1091. usewindowapi:=false;
  1092. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1093. dllversion:='';
  1094. { Init values }
  1095. initmodeswitches:=fpcmodeswitches;
  1096. initlocalswitches:=[cs_check_io];
  1097. initmoduleswitches:=[cs_extsyntax,cs_browser];
  1098. initglobalswitches:=[cs_check_unit_name,cs_link_static];
  1099. initoutputformat:=as_none;
  1100. {$ifdef i386}
  1101. initoptprocessor:=Class386;
  1102. initspecificoptprocessor:=Class386;
  1103. initpackenum:=4;
  1104. {$IFDEF testvarsets}
  1105. initsetalloc:=0;
  1106. {$ENDIF}
  1107. initpackrecords:=packrecord_2;
  1108. initasmmode:=asmmode_i386_att;
  1109. {$else not i386}
  1110. {$ifdef m68k}
  1111. initoptprocessor:=MC68000;
  1112. include(initmoduleswitches,cs_fp_emulation);
  1113. initpackenum:=4;
  1114. {$IFDEF testvarsets}
  1115. initsetalloc:=0;
  1116. {$ENDIF}
  1117. initpackrecords:=packrecord_2;
  1118. initoutputformat:=as_m68k_as;
  1119. initasmmode:=asmmode_m68k_mot;
  1120. {$endif m68k}
  1121. {$endif i386}
  1122. initinterfacetype:=it_interfacecom;
  1123. initdefines:=TStringList.Create;
  1124. { memory sizes, will be overriden by parameter or default for target
  1125. in options or init_parser }
  1126. stacksize:=0;
  1127. heapsize:=0;
  1128. maxheapsize:=0;
  1129. { compile state }
  1130. in_args:=false;
  1131. { must_be_valid:=true; obsolete PM }
  1132. not_unit_proc:=true;
  1133. apptype:=app_cui;
  1134. end;
  1135. begin
  1136. get_exepath;
  1137. {$ifdef EXTDEBUG}
  1138. {$ifdef FPC}
  1139. EntryMemUsed:=system.HeapSize-MemAvail;
  1140. {$endif FPC}
  1141. {$endif}
  1142. end.
  1143. {
  1144. $Log$
  1145. Revision 1.32 2001-04-18 22:01:53 peter
  1146. * registration of targets and assemblers
  1147. Revision 1.31 2001/04/15 09:48:29 peter
  1148. * fixed crash in labelnode
  1149. * easier detection of goto and label in try blocks
  1150. Revision 1.30 2001/04/13 01:22:07 peter
  1151. * symtable change to classes
  1152. * range check generation and errors fixed, make cycle DEBUG=1 works
  1153. * memory leaks fixed
  1154. Revision 1.29 2001/04/04 21:30:42 florian
  1155. * applied several fixes to get the DD8 Delphi Unit compiled
  1156. e.g. "forward"-interfaces are working now
  1157. Revision 1.28 2001/02/20 21:41:16 peter
  1158. * new fixfilename, findfile for unix. Look first for lowercase, then
  1159. NormalCase and last for UPPERCASE names.
  1160. Revision 1.27 2001/02/09 23:05:45 peter
  1161. * default packenum=1 for tp7 mode
  1162. Revision 1.26 2001/02/05 20:47:00 peter
  1163. * support linux unit for ver1_0 compilers
  1164. Revision 1.25 2001/01/21 20:32:45 marco
  1165. * Renamefest. Compiler part. Not that hard.
  1166. Revision 1.24 2001/01/20 18:32:52 hajny
  1167. + APPTYPE support under OS/2, app_fs, GetEnvPChar for OS/2
  1168. Revision 1.23 2001/01/13 00:03:41 peter
  1169. * fixed findexe to also support already extension in name
  1170. Revision 1.22 2000/12/26 15:57:25 peter
  1171. * use system.paramstr()
  1172. Revision 1.21 2000/12/25 00:07:26 peter
  1173. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1174. tlinkedlist objects)
  1175. Revision 1.20 2000/11/13 15:26:12 marco
  1176. * Renamefest
  1177. Revision 1.19 2000/11/12 22:20:37 peter
  1178. * create generic toutputsection for binary writers
  1179. Revision 1.18 2000/11/04 14:25:19 florian
  1180. + merged Attila's changes for interfaces, not tested yet
  1181. Revision 1.17 2000/10/31 22:02:46 peter
  1182. * symtable splitted, no real code changes
  1183. Revision 1.16 2000/10/04 14:51:08 pierre
  1184. * IsExe restored
  1185. Revision 1.15 2000/09/27 21:20:56 peter
  1186. * also set initlocalswitches in setcompilemode (merged)
  1187. Revision 1.14 2000/09/26 10:50:41 jonas
  1188. * initmodeswitches is changed is you change the compiler mode from the
  1189. command line (the -S<x> switches didn't work anymore for changing the
  1190. compiler mode) (merged from fixes branch)
  1191. Revision 1.13 2000/09/24 21:33:46 peter
  1192. * message updates merges
  1193. Revision 1.12 2000/09/24 21:19:50 peter
  1194. * delphi compile fixes
  1195. Revision 1.11 2000/09/24 15:12:40 peter
  1196. * fixed typo
  1197. Revision 1.10 2000/09/24 15:06:16 peter
  1198. * use defines.inc
  1199. Revision 1.9 2000/09/24 10:33:07 peter
  1200. * searching of exe in path also for OS/2
  1201. * fixed searching of exe in path.
  1202. Revision 1.8 2000/09/11 17:00:22 florian
  1203. + first implementation of Netware Module support, thanks to
  1204. Armin Diehl ([email protected]) for providing the patches
  1205. Revision 1.7 2000/08/27 16:11:51 peter
  1206. * moved some util functions from globals,cobjects to cutils
  1207. * splitted files into finput,fmodule
  1208. Revision 1.6 2000/08/12 19:14:58 peter
  1209. * ELF writer works now also with -g
  1210. * ELF writer is default again for linux
  1211. Revision 1.5 2000/08/12 15:30:44 peter
  1212. * IDE patch for stream reading (merged)
  1213. Revision 1.4 2000/08/02 19:49:59 peter
  1214. * first things for default parameters
  1215. Revision 1.3 2000/07/13 12:08:25 michael
  1216. + patched to 1.1.0 with former 1.09patch from peter
  1217. Revision 1.2 2000/07/13 11:32:41 michael
  1218. + removed logs
  1219. }