globals.pas 38 KB

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