globals.pas 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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; changeInit: boolean):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; changeInit: boolean):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 and changeInit then
  907. initmodeswitches := aktmodeswitches;
  908. if b then
  909. begin
  910. { turn ansistrings on by default ? }
  911. if (m_default_ansistring in aktmodeswitches) then
  912. begin
  913. include(aktlocalswitches,cs_ansistrings);
  914. if changeinit then
  915. include(initlocalswitches,cs_ansistrings);
  916. end
  917. else
  918. begin
  919. exclude(aktlocalswitches,cs_ansistrings);
  920. if changeinit then
  921. exclude(initlocalswitches,cs_ansistrings);
  922. end;
  923. end;
  924. SetCompileMode:=b;
  925. end;
  926. {****************************************************************************
  927. Init
  928. ****************************************************************************}
  929. {$ifdef linux}
  930. {$define need_path_search}
  931. {$endif linux}
  932. {$ifdef os2}
  933. {$define need_path_search}
  934. {$endif os2}
  935. procedure get_exepath;
  936. var
  937. hs1 : namestr;
  938. hs2 : extstr;
  939. {$ifdef need_path_search}
  940. b : boolean;
  941. {$endif}
  942. begin
  943. {$ifdef delphi}
  944. exepath:=dmisc.getenv('PPC_EXEC_PATH');
  945. {$else delphi}
  946. exepath:=dos.getenv('PPC_EXEC_PATH');
  947. {$endif delphi}
  948. if exepath='' then
  949. fsplit(FixFileName(paramstr(0)),exepath,hs1,hs2);
  950. {$ifdef need_path_search}
  951. if exepath='' then
  952. begin
  953. if pos(source_os.exeext,hs1) <>
  954. (length(hs1) - length(source_os.exeext)+1) then
  955. hs1 := hs1 + source_os.exeext;
  956. {$ifdef delphi}
  957. exepath := findfile(hs1,dmisc.getenv('PATH'),b);
  958. {$else delphi}
  959. exepath := findfile(hs1,dos.getenv('PATH'),b);
  960. {$endif delphi}
  961. end;
  962. {$endif need_path_search}
  963. exepath:=FixPath(exepath,false);
  964. end;
  965. procedure DoneGlobals;
  966. begin
  967. initdefines.done;
  968. if assigned(DLLImageBase) then
  969. StringDispose(DLLImageBase);
  970. RelocSection:=true;
  971. RelocSectionSetExplicitly:=false;
  972. DLLsource:=false;
  973. UseDeffileForExport:=true;
  974. librarysearchpath.Done;
  975. unitsearchpath.Done;
  976. objectsearchpath.Done;
  977. includesearchpath.Done;
  978. end;
  979. procedure InitGlobals;
  980. begin
  981. { set global switches }
  982. do_build:=false;
  983. do_make:=true;
  984. compile_level:=0;
  985. { Output }
  986. OutputFile:='';
  987. OutputExeDir:='';
  988. OutputUnitDir:='';
  989. { Utils directory }
  990. utilsdirectory:='';
  991. { Search Paths }
  992. librarysearchpath.Init;
  993. unitsearchpath.Init;
  994. includesearchpath.Init;
  995. objectsearchpath.Init;
  996. { Def file }
  997. usewindowapi:=false;
  998. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  999. dllversion:='';
  1000. { Init values }
  1001. initmodeswitches:=fpcmodeswitches;
  1002. initlocalswitches:=[cs_check_io];
  1003. initmoduleswitches:=[cs_extsyntax,cs_browser];
  1004. initglobalswitches:=[cs_check_unit_name,cs_link_static];
  1005. {$ifdef i386}
  1006. initoptprocessor:=Class386;
  1007. initspecificoptprocessor:=Class386;
  1008. initpackenum:=4;
  1009. {$IFDEF testvarsets}
  1010. initsetalloc:=0;
  1011. {$ENDIF}
  1012. initpackrecords:=packrecord_2;
  1013. initoutputformat:=target_asm.id;
  1014. initasmmode:=asmmode_i386_att;
  1015. {$else not i386}
  1016. {$ifdef m68k}
  1017. initoptprocessor:=MC68000;
  1018. include(initmoduleswitches,cs_fp_emulation);
  1019. initpackenum:=4;
  1020. {$IFDEF testvarsets}
  1021. initsetalloc:=0;
  1022. {$ENDIF}
  1023. initpackrecords:=packrecord_2;
  1024. initoutputformat:=as_m68k_as;
  1025. initasmmode:=asmmode_m68k_mot;
  1026. {$endif m68k}
  1027. {$endif i386}
  1028. initdefines.init;
  1029. { memory sizes, will be overriden by parameter or default for target
  1030. in options or init_parser }
  1031. stacksize:=0;
  1032. heapsize:=0;
  1033. maxheapsize:=0;
  1034. { compile state }
  1035. in_args:=false;
  1036. { must_be_valid:=true; obsolete PM }
  1037. not_unit_proc:=true;
  1038. apptype:=at_cui;
  1039. end;
  1040. begin
  1041. get_exepath;
  1042. {$ifdef EXTDEBUG}
  1043. {$ifdef FPC}
  1044. EntryMemUsed:=system.HeapSize-MemAvail;
  1045. {$endif FPC}
  1046. {$endif}
  1047. end.
  1048. {
  1049. $Log$
  1050. Revision 1.15 2000-09-27 21:20:56 peter
  1051. * also set initlocalswitches in setcompilemode (merged)
  1052. Revision 1.14 2000/09/26 10:50:41 jonas
  1053. * initmodeswitches is changed is you change the compiler mode from the
  1054. command line (the -S<x> switches didn't work anymore for changing the
  1055. compiler mode) (merged from fixes branch)
  1056. Revision 1.13 2000/09/24 21:33:46 peter
  1057. * message updates merges
  1058. Revision 1.12 2000/09/24 21:19:50 peter
  1059. * delphi compile fixes
  1060. Revision 1.11 2000/09/24 15:12:40 peter
  1061. * fixed typo
  1062. Revision 1.10 2000/09/24 15:06:16 peter
  1063. * use defines.inc
  1064. Revision 1.9 2000/09/24 10:33:07 peter
  1065. * searching of exe in path also for OS/2
  1066. * fixed searching of exe in path.
  1067. Revision 1.8 2000/09/11 17:00:22 florian
  1068. + first implementation of Netware Module support, thanks to
  1069. Armin Diehl ([email protected]) for providing the patches
  1070. Revision 1.7 2000/08/27 16:11:51 peter
  1071. * moved some util functions from globals,cobjects to cutils
  1072. * splitted files into finput,fmodule
  1073. Revision 1.6 2000/08/12 19:14:58 peter
  1074. * ELF writer works now also with -g
  1075. * ELF writer is default again for linux
  1076. Revision 1.5 2000/08/12 15:30:44 peter
  1077. * IDE patch for stream reading (merged)
  1078. Revision 1.4 2000/08/02 19:49:59 peter
  1079. * first things for default parameters
  1080. Revision 1.3 2000/07/13 12:08:25 michael
  1081. + patched to 1.1.0 with former 1.09patch from peter
  1082. Revision 1.2 2000/07/13 11:32:41 michael
  1083. + removed logs
  1084. }