globals.pas 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  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. DT : DateTime;
  303. begin
  304. if t=-1 then
  305. begin
  306. FileTimeString:='Not Found';
  307. exit;
  308. end;
  309. unpacktime(t,DT);
  310. filetimestring:=L0(dt.Year)+'/'+L0(dt.Month)+'/'+L0(dt.Day)+' '+L0(dt.Hour)+':'+L0(dt.min)+':'+L0(dt.sec);
  311. end;
  312. {****************************************************************************
  313. Default Macro Handling
  314. ****************************************************************************}
  315. procedure DefaultReplacements(var s:string);
  316. begin
  317. { Replace some macro's }
  318. Replace(s,'$FPCVER',version_string);
  319. Replace(s,'$VERSION',version_string);
  320. Replace(s,'$FULLVERSION',full_version_string);
  321. Replace(s,'$FPCDATE',date_string);
  322. Replace(s,'$FPCTARGET',target_cpu_string);
  323. Replace(s,'$FPCCPU',target_cpu_string);
  324. Replace(s,'$TARGET',target_path);
  325. Replace(s,'$FPCOS',target_path);
  326. end;
  327. {****************************************************************************
  328. File Handling
  329. ****************************************************************************}
  330. function GetCurrentDir:string;
  331. var
  332. CurrentDir : string;
  333. begin
  334. GetDir(0,CurrentDir);
  335. GetCurrentDir:=FixPath(CurrentDir,false);
  336. end;
  337. function path_absolute(const s : string) : boolean;
  338. {
  339. is path s an absolute path?
  340. }
  341. begin
  342. path_absolute:=false;
  343. {$ifdef unix}
  344. if (length(s)>0) and (s[1]='/') then
  345. path_absolute:=true;
  346. {$else unix}
  347. {$ifdef amiga}
  348. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or (Pos(':',s) = length(s)) then
  349. path_absolute:=true;
  350. {$else}
  351. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or
  352. ((length(s)>2) and (s[2]=':') and ((s[3]='\') or (s[3]='/'))) then
  353. path_absolute:=true;
  354. {$endif amiga}
  355. {$endif unix}
  356. end;
  357. {$ifndef FPC}
  358. Procedure FindClose(var Info : SearchRec);
  359. Begin
  360. End;
  361. {$endif not FPC}
  362. Function FileExists ( Const F : String) : Boolean;
  363. {$ifndef delphi}
  364. Var
  365. Info : SearchRec;
  366. {$endif}
  367. begin
  368. {$ifdef delphi}
  369. FileExists:=sysutils.FileExists(f);
  370. {$else}
  371. findfirst(F,readonly+archive+hidden,info);
  372. FileExists:=(doserror=0);
  373. findclose(Info);
  374. {$endif delphi}
  375. end;
  376. Function PathExists ( F : String) : Boolean;
  377. Var
  378. Info : SearchRec;
  379. begin
  380. if F[Length(f)] in ['/','\'] then
  381. Delete(f,length(f),1);
  382. findfirst(F,readonly+archive+hidden+directory,info);
  383. PathExists:=(doserror=0) and ((info.attr and directory)=directory);
  384. findclose(Info);
  385. end;
  386. Function RemoveFile(const f:string):boolean;
  387. var
  388. g : file;
  389. begin
  390. assign(g,f);
  391. {$I-}
  392. erase(g);
  393. {$I+}
  394. RemoveFile:=(ioresult=0);
  395. end;
  396. Function RemoveDir(d:string):boolean;
  397. begin
  398. if d[length(d)]=DirSep then
  399. Delete(d,length(d),1);
  400. {$I-}
  401. rmdir(d);
  402. {$I+}
  403. RemoveDir:=(ioresult=0);
  404. end;
  405. Function SplitPath(const s:string):string;
  406. var
  407. i : longint;
  408. begin
  409. i:=Length(s);
  410. while (i>0) and not(s[i] in ['/','\']) do
  411. dec(i);
  412. SplitPath:=Copy(s,1,i);
  413. end;
  414. Function SplitFileName(const s:string):string;
  415. var
  416. p : dirstr;
  417. n : namestr;
  418. e : extstr;
  419. begin
  420. FSplit(s,p,n,e);
  421. SplitFileName:=n+e;
  422. end;
  423. Function SplitName(const s:string):string;
  424. var
  425. i,j : longint;
  426. begin
  427. i:=Length(s);
  428. j:=Length(s);
  429. while (i>0) and not(s[i] in ['/','\']) do
  430. dec(i);
  431. while (j>0) and (s[j]<>'.') do
  432. dec(j);
  433. if j<=i then
  434. j:=255;
  435. SplitName:=Copy(s,i+1,j-(i+1));
  436. end;
  437. Function SplitExtension(Const HStr:String):String;
  438. var
  439. j : longint;
  440. begin
  441. j:=length(Hstr);
  442. while (j>0) and (Hstr[j]<>'.') do
  443. begin
  444. if hstr[j]=DirSep then
  445. j:=0
  446. else
  447. dec(j);
  448. end;
  449. if j=0 then
  450. j:=254;
  451. SplitExtension:=Copy(Hstr,j,255);
  452. end;
  453. Function AddExtension(Const HStr,ext:String):String;
  454. begin
  455. if (Ext<>'') and (SplitExtension(HStr)='') then
  456. AddExtension:=Hstr+Ext
  457. else
  458. AddExtension:=Hstr;
  459. end;
  460. Function ForceExtension(Const HStr,ext:String):String;
  461. var
  462. j : longint;
  463. begin
  464. j:=length(Hstr);
  465. while (j>0) and (Hstr[j]<>'.') do
  466. dec(j);
  467. if j=0 then
  468. j:=255;
  469. ForceExtension:=Copy(Hstr,1,j-1)+Ext;
  470. end;
  471. Function FixPath(s:string;allowdot:boolean):string;
  472. var
  473. i : longint;
  474. begin
  475. { Fix separator }
  476. for i:=1 to length(s) do
  477. if s[i] in ['/','\'] then
  478. s[i]:=DirSep;
  479. { Fix ending / }
  480. if (length(s)>0) and (s[length(s)]<>DirSep) and
  481. (s[length(s)]<>':') then
  482. s:=s+DirSep;
  483. { Remove ./ }
  484. if (not allowdot) and (s='.'+DirSep) then
  485. s:='';
  486. { return }
  487. {$ifdef unix}
  488. FixPath:=s;
  489. {$else}
  490. FixPath:=Lower(s);
  491. {$endif}
  492. end;
  493. function FixFileName(const s:string):string;
  494. var
  495. i : longint;
  496. begin
  497. for i:=length(s) downto 1 do
  498. begin
  499. case s[i] of
  500. {$ifdef Unix}
  501. '/','\' :
  502. FixFileName[i]:='/';
  503. {$else Unix}
  504. '/' :
  505. FixFileName[i]:='\';
  506. 'A'..'Z' :
  507. FixFileName[i]:=char(byte(s[i])+32);
  508. {$endif Unix}
  509. else
  510. FixFileName[i]:=s[i];
  511. end;
  512. end;
  513. FixFileName[0]:=s[0];
  514. end;
  515. procedure SplitBinCmd(const s:string;var bstr,cstr:string);
  516. var
  517. i : longint;
  518. begin
  519. i:=pos(' ',s);
  520. if i>0 then
  521. begin
  522. bstr:=Copy(s,1,i-1);
  523. cstr:=Copy(s,i+1,length(s)-i);
  524. end
  525. else
  526. begin
  527. bstr:='';
  528. cstr:='';
  529. end;
  530. end;
  531. procedure TSearchPathList.AddPath(s:string;addfirst:boolean);
  532. var
  533. j : longint;
  534. hs,hsd,
  535. CurrentDir,
  536. CurrPath : string;
  537. dir : searchrec;
  538. hp : TStringListItem;
  539. procedure addcurrpath;
  540. begin
  541. if addfirst then
  542. begin
  543. Remove(currPath);
  544. Insert(currPath);
  545. end
  546. else
  547. begin
  548. { Check if already in path, then we don't add it }
  549. hp:=Find(currPath);
  550. if not assigned(hp) then
  551. Concat(currPath);
  552. end;
  553. end;
  554. begin
  555. if s='' then
  556. exit;
  557. { Support default macro's }
  558. DefaultReplacements(s);
  559. { get current dir }
  560. CurrentDir:=GetCurrentDir;
  561. repeat
  562. { get currpath }
  563. if addfirst then
  564. begin
  565. j:=length(s);
  566. while (j>0) and (s[j]<>';') do
  567. dec(j);
  568. CurrPath:=FixPath(Copy(s,j+1,length(s)-j),false);
  569. if j=0 then
  570. s:=''
  571. else
  572. System.Delete(s,j,length(s)-j+1);
  573. end
  574. else
  575. begin
  576. j:=Pos(';',s);
  577. if j=0 then
  578. j:=255;
  579. CurrPath:=FixPath(Copy(s,1,j-1),false);
  580. System.Delete(s,1,j);
  581. end;
  582. { fix pathname }
  583. if CurrPath='' then
  584. CurrPath:='.'+DirSep
  585. else
  586. begin
  587. CurrPath:=FixPath(FExpand(CurrPath),false);
  588. if (CurrentDir<>'') and (Copy(CurrPath,1,length(CurrentDir))=CurrentDir) then
  589. CurrPath:='.'+DirSep+Copy(CurrPath,length(CurrentDir)+1,255);
  590. end;
  591. { wildcard adding ? }
  592. if pos('*',currpath)>0 then
  593. begin
  594. if currpath[length(currpath)]=dirsep then
  595. hs:=Copy(currpath,1,length(CurrPath)-1)
  596. else
  597. hs:=currpath;
  598. hsd:=SplitPath(hs);
  599. findfirst(hs,directory,dir);
  600. while doserror=0 do
  601. begin
  602. if (dir.name<>'.') and
  603. (dir.name<>'..') and
  604. ((dir.attr and directory)<>0) then
  605. begin
  606. currpath:=hsd+dir.name+dirsep;
  607. hp:=Find(currPath);
  608. if not assigned(hp) then
  609. AddCurrPath;
  610. end;
  611. findnext(dir);
  612. end;
  613. FindClose(dir);
  614. end
  615. else
  616. begin
  617. if PathExists(currpath) then
  618. addcurrpath;
  619. end;
  620. until (s='');
  621. end;
  622. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  623. var
  624. s : string;
  625. hl : TSearchPathList;
  626. hp,hp2 : TStringListItem;
  627. begin
  628. if list.empty then
  629. exit;
  630. { create temp and reverse the list }
  631. if addfirst then
  632. begin
  633. hl:=TSearchPathList.Create;
  634. hp:=TStringListItem(list.first);
  635. while assigned(hp) do
  636. begin
  637. hl.insert(hp.Str);
  638. hp:=TStringListItem(hp.next);
  639. end;
  640. while not hl.empty do
  641. begin
  642. s:=hl.GetFirst;
  643. Remove(s);
  644. Insert(s);
  645. end;
  646. hl.Free;
  647. end
  648. else
  649. begin
  650. hp:=TStringListItem(list.first);
  651. while assigned(hp) do
  652. begin
  653. hp2:=Find(hp.Str);
  654. { Check if already in path, then we don't add it }
  655. if not assigned(hp2) then
  656. Concat(hp.Str);
  657. hp:=TStringListItem(hp.next);
  658. end;
  659. end;
  660. end;
  661. function TSearchPathList.FindFile(const f : string;var foundfile:string):boolean;
  662. Var
  663. p : TStringListItem;
  664. begin
  665. FindFile:=false;
  666. p:=TStringListItem(first);
  667. while assigned(p) do
  668. begin
  669. {
  670. Search order for case sensitive systems:
  671. 1. lowercase
  672. 2. NormalCase
  673. 3. UPPERCASE
  674. None case sensitive only lowercase
  675. }
  676. FoundFile:=p.Str+Lower(f);
  677. If FileExists(FoundFile) then
  678. begin
  679. FindFile:=true;
  680. exit;
  681. end;
  682. {$ifdef UNIX}
  683. FoundFile:=p.Str+f;
  684. If FileExists(FoundFile) then
  685. begin
  686. FindFile:=true;
  687. exit;
  688. end;
  689. FoundFile:=p.Str+Upper(f);
  690. If FileExists(FoundFile) then
  691. begin
  692. FindFile:=true;
  693. exit;
  694. end;
  695. {$endif UNIX}
  696. p:=TStringListItem(p.next);
  697. end;
  698. { Return original filename if not found }
  699. FoundFile:=f;
  700. end;
  701. Function GetFileTime ( Var F : File) : Longint;
  702. Var
  703. {$ifdef unix}
  704. Info : Stat;
  705. {$endif}
  706. L : longint;
  707. begin
  708. {$ifdef unix}
  709. FStat (F,Info);
  710. L:=Info.Mtime;
  711. {$else}
  712. GetFTime(f,l);
  713. {$endif}
  714. GetFileTime:=L;
  715. end;
  716. Function GetNamedFileTime (Const F : String) : Longint;
  717. begin
  718. GetNamedFileTime:=do_getnamedfiletime(F);
  719. end;
  720. function FindFile(const f : string;path : string;var foundfile:string):boolean;
  721. Var
  722. singlepathstring : string;
  723. i : longint;
  724. begin
  725. {$ifdef Unix}
  726. for i:=1 to length(path) do
  727. if path[i]=':' then
  728. path[i]:=';';
  729. {$endif Unix}
  730. FindFile:=false;
  731. repeat
  732. i:=pos(';',path);
  733. if i=0 then
  734. i:=256;
  735. singlepathstring:=FixPath(copy(path,1,i-1),false);
  736. delete(path,1,i);
  737. {
  738. Search order for case sensitive systems:
  739. 1. lowercase
  740. 2. NormalCase
  741. 3. UPPERCASE
  742. None case sensitive only lowercase
  743. }
  744. FoundFile:=singlepathstring+Lower(f);
  745. If FileExists(FoundFile) then
  746. begin
  747. FindFile:=true;
  748. exit;
  749. end;
  750. {$ifdef UNIX}
  751. FoundFile:=singlepathstring+f;
  752. If FileExists(FoundFile) then
  753. begin
  754. FindFile:=true;
  755. exit;
  756. end;
  757. FoundFile:=singlepathstring+Upper(f);
  758. If FileExists(FoundFile) then
  759. begin
  760. FindFile:=true;
  761. exit;
  762. end;
  763. {$endif UNIX}
  764. until path='';
  765. FoundFile:=f;
  766. end;
  767. function FindExe(const bin:string;var foundfile:string):boolean;
  768. begin
  769. {$ifdef delphi}
  770. FindExe:=FindFile(FixFileName(AddExtension(bin,source_info.exeext)),'.;'+exepath+';'+dmisc.getenv('PATH'),foundfile);
  771. {$else delphi}
  772. FindExe:=FindFile(FixFileName(AddExtension(bin,source_info.exeext)),'.;'+exepath+';'+dos.getenv('PATH'),foundfile);
  773. {$endif delphi}
  774. end;
  775. function GetShortName(const n:string):string;
  776. {$ifdef win32}
  777. var
  778. hs,hs2 : string;
  779. i : longint;
  780. {$endif}
  781. {$ifdef go32v2}
  782. var
  783. hs : string;
  784. {$endif}
  785. begin
  786. GetShortName:=n;
  787. {$ifdef win32}
  788. hs:=n+#0;
  789. i:=Windows.GetShortPathName(@hs[1],@hs2[1],high(hs2));
  790. if (i>0) and (i<=high(hs2)) then
  791. begin
  792. hs2[0]:=chr(strlen(@hs2[1]));
  793. GetShortName:=hs2;
  794. end;
  795. {$endif}
  796. {$ifdef go32v2}
  797. hs:=n;
  798. if Dos.GetShortName(hs) then
  799. GetShortName:=hs;
  800. {$endif}
  801. end;
  802. {****************************************************************************
  803. OS Dependent things
  804. ****************************************************************************}
  805. function GetEnvPChar(const envname:string):pchar;
  806. {$ifdef win32}
  807. var
  808. s : string;
  809. i,len : longint;
  810. hp,p,p2 : pchar;
  811. {$endif}
  812. {$ifdef os2}
  813. var
  814. P1, P2: PChar;
  815. {$endif}
  816. begin
  817. {$ifdef unix}
  818. GetEnvPchar:={$ifdef ver1_0}Linux{$else}Unix{$endif}.Getenv(envname);
  819. {$define GETENVOK}
  820. {$endif}
  821. {$ifdef win32}
  822. GetEnvPchar:=nil;
  823. p:=GetEnvironmentStrings;
  824. hp:=p;
  825. while hp^<>#0 do
  826. begin
  827. s:=strpas(hp);
  828. i:=pos('=',s);
  829. len:=strlen(hp);
  830. if upper(copy(s,1,i-1))=upper(envname) then
  831. begin
  832. GetMem(p2,len-length(envname));
  833. Move(hp[i],p2^,len-length(envname));
  834. GetEnvPchar:=p2;
  835. break;
  836. end;
  837. { next string entry}
  838. hp:=hp+len+1;
  839. end;
  840. FreeEnvironmentStrings(p);
  841. {$define GETENVOK}
  842. {$endif}
  843. {$ifdef os2}
  844. P1 := StrPNew (EnvName);
  845. if Assigned (P1) then
  846. begin
  847. if DosCalls.DosScanEnv (P1, P2) = 0 then
  848. GetEnvPChar := P2
  849. else
  850. GetEnvPChar := nil;
  851. StrDispose (P1);
  852. end else GetEnvPChar := nil;
  853. {$define GETENVOK}
  854. {$endif}
  855. {$ifdef GETENVOK}
  856. {$undef GETENVOK}
  857. {$else}
  858. GetEnvPchar:=StrPNew(Dos.Getenv(envname));
  859. {$endif}
  860. end;
  861. procedure FreeEnvPChar(p:pchar);
  862. begin
  863. {$ifndef unix}
  864. {$ifndef os2}
  865. StrDispose(p);
  866. {$endif}
  867. {$endif}
  868. end;
  869. Procedure Shell(const command:string);
  870. { This is already defined in the linux.ppu for linux, need for the *
  871. expansion under linux }
  872. {$ifdef unix}
  873. begin
  874. {$ifdef ver1_0}Linux{$else}Unix{$endif}.Shell(command);
  875. end;
  876. {$else}
  877. var
  878. comspec : string;
  879. begin
  880. comspec:=getenv('COMSPEC');
  881. Exec(comspec,' /C '+command);
  882. end;
  883. {$endif}
  884. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  885. var
  886. b : boolean;
  887. begin
  888. b:=true;
  889. if s='DEFAULT' then
  890. aktmodeswitches:=initmodeswitches
  891. else
  892. if s='DELPHI' then
  893. aktmodeswitches:=delphimodeswitches
  894. else
  895. if s='TP' then
  896. aktmodeswitches:=tpmodeswitches
  897. else
  898. if s='FPC' then
  899. aktmodeswitches:=fpcmodeswitches
  900. else
  901. if s='OBJFPC' then
  902. aktmodeswitches:=objfpcmodeswitches
  903. else
  904. if s='GPC' then
  905. aktmodeswitches:=gpcmodeswitches
  906. else
  907. b:=false;
  908. if b and changeInit then
  909. initmodeswitches := aktmodeswitches;
  910. if b then
  911. begin
  912. { turn ansistrings on by default ? }
  913. if (m_delphi in aktmodeswitches) then
  914. begin
  915. include(aktlocalswitches,cs_ansistrings);
  916. if changeinit then
  917. include(initlocalswitches,cs_ansistrings);
  918. end
  919. else
  920. begin
  921. exclude(aktlocalswitches,cs_ansistrings);
  922. if changeinit then
  923. exclude(initlocalswitches,cs_ansistrings);
  924. end;
  925. { enum packing }
  926. if (m_tp7 in aktmodeswitches) then
  927. aktpackenum:=1
  928. else
  929. aktpackenum:=4;
  930. if changeinit then
  931. initpackenum:=aktpackenum;
  932. end;
  933. SetCompileMode:=b;
  934. end;
  935. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  936. function string2guid(const s: string; var GUID: TGUID): boolean;
  937. function ishexstr(const hs: string): boolean;
  938. var
  939. i: integer;
  940. begin
  941. ishexstr:=false;
  942. for i:=1 to Length(hs) do begin
  943. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  944. exit;
  945. end;
  946. ishexstr:=true;
  947. end;
  948. function hexstr2longint(const hexs: string): longint;
  949. var
  950. i: integer;
  951. rl: longint;
  952. begin
  953. rl:=0;
  954. for i:=1 to length(hexs) do begin
  955. rl:=rl shl 4;
  956. case hexs[i] of
  957. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  958. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  959. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  960. end
  961. end;
  962. hexstr2longint:=rl;
  963. end;
  964. var
  965. i: integer;
  966. begin
  967. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  968. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  969. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  970. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  971. ishexstr(copy(s,26,12)) then begin
  972. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  973. GUID.D2:=hexstr2longint(copy(s,11,4));
  974. GUID.D3:=hexstr2longint(copy(s,16,4));
  975. for i:=0 to 1 do
  976. GUID.D4[i]:=hexstr2longint(copy(s,21+i*2,2));
  977. for i:=2 to 7 do
  978. GUID.D4[i]:=hexstr2longint(copy(s,22+i*2,2));
  979. string2guid:=true;
  980. end
  981. else
  982. string2guid:=false;
  983. end;
  984. function guid2string(const GUID: TGUID): string;
  985. function long2hex(l, len: longint): string;
  986. const
  987. hextbl: array[0..15] of char = '0123456789ABCDEF';
  988. var
  989. rs: string;
  990. i: integer;
  991. begin
  992. rs[0]:=chr(len);
  993. for i:=len downto 1 do begin
  994. rs[i]:=hextbl[l and $F];
  995. l:=l shr 4;
  996. end;
  997. long2hex:=rs;
  998. end;
  999. begin
  1000. guid2string:=
  1001. '{'+long2hex(GUID.D1,8)+
  1002. '-'+long2hex(GUID.D2,4)+
  1003. '-'+long2hex(GUID.D3,4)+
  1004. '-'+long2hex(GUID.D4[0],2)+long2hex(GUID.D4[1],2)+
  1005. '-'+long2hex(GUID.D4[2],2)+long2hex(GUID.D4[3],2)+
  1006. long2hex(GUID.D4[4],2)+long2hex(GUID.D4[5],2)+
  1007. long2hex(GUID.D4[6],2)+long2hex(GUID.D4[7],2)+
  1008. '}';
  1009. end;
  1010. {****************************************************************************
  1011. Init
  1012. ****************************************************************************}
  1013. {$ifdef unix}
  1014. {$define need_path_search}
  1015. {$endif unix}
  1016. {$ifdef os2}
  1017. {$define need_path_search}
  1018. {$endif os2}
  1019. procedure get_exepath;
  1020. var
  1021. hs1 : namestr;
  1022. hs2 : extstr;
  1023. begin
  1024. {$ifdef delphi}
  1025. exepath:=dmisc.getenv('PPC_EXEC_PATH');
  1026. {$else delphi}
  1027. exepath:=dos.getenv('PPC_EXEC_PATH');
  1028. {$endif delphi}
  1029. if exepath='' then
  1030. fsplit(FixFileName(system.paramstr(0)),exepath,hs1,hs2);
  1031. {$ifdef need_path_search}
  1032. if exepath='' then
  1033. begin
  1034. if pos(source_info.exeext,hs1) <>
  1035. (length(hs1) - length(source_info.exeext)+1) then
  1036. hs1 := hs1 + source_info.exeext;
  1037. {$ifdef delphi}
  1038. findfile(hs1,dmisc.getenv('PATH'),exepath);
  1039. {$else delphi}
  1040. findfile(hs1,dos.getenv('PATH'),exepath);
  1041. {$endif delphi}
  1042. exepath:=SplitPath(exepath);
  1043. end;
  1044. {$endif need_path_search}
  1045. exepath:=FixPath(exepath,false);
  1046. end;
  1047. procedure DoneGlobals;
  1048. begin
  1049. initdefines.free;
  1050. if assigned(DLLImageBase) then
  1051. StringDispose(DLLImageBase);
  1052. RelocSection:=true;
  1053. RelocSectionSetExplicitly:=false;
  1054. UseDeffileForExport:=true;
  1055. librarysearchpath.Free;
  1056. unitsearchpath.Free;
  1057. objectsearchpath.Free;
  1058. includesearchpath.Free;
  1059. end;
  1060. procedure InitGlobals;
  1061. begin
  1062. { set global switches }
  1063. do_build:=false;
  1064. do_make:=true;
  1065. compile_level:=0;
  1066. { these two should not be cleared in
  1067. DoneGlobals as the IDE might need their value }
  1068. IsExe:=false;
  1069. DLLsource:=false;
  1070. { Output }
  1071. OutputFile:='';
  1072. OutputExeDir:='';
  1073. OutputUnitDir:='';
  1074. { Utils directory }
  1075. utilsdirectory:='';
  1076. { Search Paths }
  1077. librarysearchpath:=TSearchPathList.Create;
  1078. unitsearchpath:=TSearchPathList.Create;
  1079. includesearchpath:=TSearchPathList.Create;
  1080. objectsearchpath:=TSearchPathList.Create;
  1081. { Def file }
  1082. usewindowapi:=false;
  1083. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1084. dllversion:='';
  1085. { Init values }
  1086. initmodeswitches:=fpcmodeswitches;
  1087. initlocalswitches:=[cs_check_io];
  1088. initmoduleswitches:=[cs_extsyntax,cs_browser];
  1089. initglobalswitches:=[cs_check_unit_name,cs_link_static];
  1090. initoutputformat:=as_none;
  1091. {$ifdef i386}
  1092. initoptprocessor:=Class386;
  1093. initspecificoptprocessor:=Class386;
  1094. initpackenum:=4;
  1095. {$IFDEF testvarsets}
  1096. initsetalloc:=0;
  1097. {$ENDIF}
  1098. initpackrecords:=packrecord_2;
  1099. initasmmode:=asmmode_i386_att;
  1100. {$else not i386}
  1101. {$ifdef m68k}
  1102. initoptprocessor:=MC68000;
  1103. include(initmoduleswitches,cs_fp_emulation);
  1104. initpackenum:=4;
  1105. {$IFDEF testvarsets}
  1106. initsetalloc:=0;
  1107. {$ENDIF}
  1108. initpackrecords:=packrecord_2;
  1109. initoutputformat:=as_m68k_as;
  1110. initasmmode:=asmmode_m68k_mot;
  1111. {$endif m68k}
  1112. {$endif i386}
  1113. initinterfacetype:=it_interfacecom;
  1114. initdefines:=TStringList.Create;
  1115. { memory sizes, will be overriden by parameter or default for target
  1116. in options or init_parser }
  1117. stacksize:=0;
  1118. heapsize:=0;
  1119. maxheapsize:=0;
  1120. { compile state }
  1121. in_args:=false;
  1122. { must_be_valid:=true; obsolete PM }
  1123. not_unit_proc:=true;
  1124. apptype:=app_cui;
  1125. end;
  1126. begin
  1127. get_exepath;
  1128. {$ifdef EXTDEBUG}
  1129. {$ifdef FPC}
  1130. EntryMemUsed:=system.HeapSize-MemAvail;
  1131. {$endif FPC}
  1132. {$endif}
  1133. end.
  1134. {
  1135. $Log$
  1136. Revision 1.33 2001-05-06 14:49:17 peter
  1137. * ppu object to class rewrite
  1138. * move ppu read and write stuff to fppu
  1139. Revision 1.32 2001/04/18 22:01:53 peter
  1140. * registration of targets and assemblers
  1141. Revision 1.31 2001/04/15 09:48:29 peter
  1142. * fixed crash in labelnode
  1143. * easier detection of goto and label in try blocks
  1144. Revision 1.30 2001/04/13 01:22:07 peter
  1145. * symtable change to classes
  1146. * range check generation and errors fixed, make cycle DEBUG=1 works
  1147. * memory leaks fixed
  1148. Revision 1.29 2001/04/04 21:30:42 florian
  1149. * applied several fixes to get the DD8 Delphi Unit compiled
  1150. e.g. "forward"-interfaces are working now
  1151. Revision 1.28 2001/02/20 21:41:16 peter
  1152. * new fixfilename, findfile for unix. Look first for lowercase, then
  1153. NormalCase and last for UPPERCASE names.
  1154. Revision 1.27 2001/02/09 23:05:45 peter
  1155. * default packenum=1 for tp7 mode
  1156. Revision 1.26 2001/02/05 20:47:00 peter
  1157. * support linux unit for ver1_0 compilers
  1158. Revision 1.25 2001/01/21 20:32:45 marco
  1159. * Renamefest. Compiler part. Not that hard.
  1160. Revision 1.24 2001/01/20 18:32:52 hajny
  1161. + APPTYPE support under OS/2, app_fs, GetEnvPChar for OS/2
  1162. Revision 1.23 2001/01/13 00:03:41 peter
  1163. * fixed findexe to also support already extension in name
  1164. Revision 1.22 2000/12/26 15:57:25 peter
  1165. * use system.paramstr()
  1166. Revision 1.21 2000/12/25 00:07:26 peter
  1167. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1168. tlinkedlist objects)
  1169. Revision 1.20 2000/11/13 15:26:12 marco
  1170. * Renamefest
  1171. Revision 1.19 2000/11/12 22:20:37 peter
  1172. * create generic toutputsection for binary writers
  1173. Revision 1.18 2000/11/04 14:25:19 florian
  1174. + merged Attila's changes for interfaces, not tested yet
  1175. Revision 1.17 2000/10/31 22:02:46 peter
  1176. * symtable splitted, no real code changes
  1177. Revision 1.16 2000/10/04 14:51:08 pierre
  1178. * IsExe restored
  1179. Revision 1.15 2000/09/27 21:20:56 peter
  1180. * also set initlocalswitches in setcompilemode (merged)
  1181. Revision 1.14 2000/09/26 10:50:41 jonas
  1182. * initmodeswitches is changed is you change the compiler mode from the
  1183. command line (the -S<x> switches didn't work anymore for changing the
  1184. compiler mode) (merged from fixes branch)
  1185. Revision 1.13 2000/09/24 21:33:46 peter
  1186. * message updates merges
  1187. Revision 1.12 2000/09/24 21:19:50 peter
  1188. * delphi compile fixes
  1189. Revision 1.11 2000/09/24 15:12:40 peter
  1190. * fixed typo
  1191. Revision 1.10 2000/09/24 15:06:16 peter
  1192. * use defines.inc
  1193. Revision 1.9 2000/09/24 10:33:07 peter
  1194. * searching of exe in path also for OS/2
  1195. * fixed searching of exe in path.
  1196. Revision 1.8 2000/09/11 17:00:22 florian
  1197. + first implementation of Netware Module support, thanks to
  1198. Armin Diehl ([email protected]) for providing the patches
  1199. Revision 1.7 2000/08/27 16:11:51 peter
  1200. * moved some util functions from globals,cobjects to cutils
  1201. * splitted files into finput,fmodule
  1202. Revision 1.6 2000/08/12 19:14:58 peter
  1203. * ELF writer works now also with -g
  1204. * ELF writer is default again for linux
  1205. Revision 1.5 2000/08/12 15:30:44 peter
  1206. * IDE patch for stream reading (merged)
  1207. Revision 1.4 2000/08/02 19:49:59 peter
  1208. * first things for default parameters
  1209. Revision 1.3 2000/07/13 12:08:25 michael
  1210. + patched to 1.1.0 with former 1.09patch from peter
  1211. Revision 1.2 2000/07/13 11:32:41 michael
  1212. + removed logs
  1213. }