globals.pas 37 KB

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