globals.pas 32 KB

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