globals.pas 38 KB

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