globals.pas 31 KB

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