globals.pas 36 KB

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