globals.pas 38 KB

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