globals.pas 37 KB

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