globals.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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,cobjects,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 b : boolean) : string;
  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 b : boolean) : string;
  223. function FindExe(bin:string;var found:boolean):string;
  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 ngraphsearchvalue(const s1,s2 : string) : double;
  241. const
  242. n = 3;
  243. var
  244. equals,i,j : longint;
  245. hs : string;
  246. begin
  247. equals:=0;
  248. { is the string long enough ? }
  249. if min(length(s1),length(s2))-n+1<1 then
  250. begin
  251. ngraphsearchvalue:=0.0;
  252. exit;
  253. end;
  254. for i:=1 to length(s1)-n+1 do
  255. begin
  256. hs:=copy(s1,i,n);
  257. for j:=1 to length(s2)-n+1 do
  258. if hs=copy(s2,j,n) then
  259. inc(equals);
  260. end;
  261. {$ifdef fpc}
  262. ngraphsearchvalue:=equals/double(max(length(s1),length(s2))-n+1);
  263. {$else}
  264. ngraphsearchvalue:=equals/(max(length(s1),length(s2))-n+1);
  265. {$endif}
  266. end;
  267. function bstoslash(const s : string) : string;
  268. {
  269. return string s with all \ changed into /
  270. }
  271. var
  272. i : longint;
  273. begin
  274. for i:=1to length(s) do
  275. if s[i]='\' then
  276. bstoslash[i]:='/'
  277. else
  278. bstoslash[i]:=s[i];
  279. bstoslash[0]:=s[0];
  280. end;
  281. {****************************************************************************
  282. Time Handling
  283. ****************************************************************************}
  284. Function L0(l:longint):string;
  285. {
  286. return the string of value l, if l<10 then insert a zero, so
  287. the string is always at least 2 chars '01','02',etc
  288. }
  289. var
  290. s : string;
  291. begin
  292. Str(l,s);
  293. if l<10 then
  294. s:='0'+s;
  295. L0:=s;
  296. end;
  297. function gettimestr:string;
  298. {
  299. get the current time in a string HH:MM:SS
  300. }
  301. var
  302. hour,min,sec,hsec : word;
  303. begin
  304. {$ifdef delphi}
  305. dmisc.gettime(hour,min,sec,hsec);
  306. {$else delphi}
  307. dos.gettime(hour,min,sec,hsec);
  308. {$endif delphi}
  309. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  310. end;
  311. function getdatestr:string;
  312. {
  313. get the current date in a string YY/MM/DD
  314. }
  315. var
  316. Year,Month,Day,Wday : Word;
  317. begin
  318. {$ifdef delphi}
  319. dmisc.getdate(year,month,day,wday);
  320. {$else}
  321. dos.getdate(year,month,day,wday);
  322. {$endif}
  323. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  324. end;
  325. function filetimestring( t : longint) : string;
  326. {
  327. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  328. }
  329. var
  330. {$ifndef unix}
  331. DT : DateTime;
  332. {$endif}
  333. Year,Month,Day,Hour,Min,Sec : Word;
  334. begin
  335. if t=-1 then
  336. begin
  337. FileTimeString:='Not Found';
  338. exit;
  339. end;
  340. {$ifndef unix}
  341. unpacktime(t,DT);
  342. Year:=dT.year;month:=dt.month;day:=dt.day;
  343. Hour:=dt.hour;min:=dt.min;sec:=dt.sec;
  344. {$else}
  345. EpochToLocal (t,year,month,day,hour,min,sec);
  346. {$endif}
  347. filetimestring:=L0(Year)+'/'+L0(Month)+'/'+L0(Day)+' '+L0(Hour)+':'+L0(min)+':'+L0(sec);
  348. end;
  349. {****************************************************************************
  350. Default Macro Handling
  351. ****************************************************************************}
  352. procedure DefaultReplacements(var s:string);
  353. begin
  354. { Replace some macro's }
  355. Replace(s,'$FPCVER',version_string);
  356. Replace(s,'$VERSION',version_string);
  357. Replace(s,'$FULLVERSION',full_version_string);
  358. Replace(s,'$FPCDATE',date_string);
  359. Replace(s,'$FPCTARGET',target_cpu_string);
  360. Replace(s,'$FPCCPU',target_cpu_string);
  361. Replace(s,'$TARGET',target_path);
  362. Replace(s,'$FPCOS',target_path);
  363. end;
  364. {****************************************************************************
  365. File Handling
  366. ****************************************************************************}
  367. function GetCurrentDir:string;
  368. var
  369. CurrentDir : string;
  370. begin
  371. GetDir(0,CurrentDir);
  372. GetCurrentDir:=FixPath(CurrentDir,false);
  373. end;
  374. function path_absolute(const s : string) : boolean;
  375. {
  376. is path s an absolute path?
  377. }
  378. begin
  379. path_absolute:=false;
  380. {$ifdef unix}
  381. if (length(s)>0) and (s[1]='/') then
  382. path_absolute:=true;
  383. {$else unix}
  384. {$ifdef amiga}
  385. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or (Pos(':',s) = length(s)) then
  386. path_absolute:=true;
  387. {$else}
  388. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or
  389. ((length(s)>2) and (s[2]=':') and ((s[3]='\') or (s[3]='/'))) then
  390. path_absolute:=true;
  391. {$endif amiga}
  392. {$endif unix}
  393. end;
  394. {$ifndef FPC}
  395. Procedure FindClose(var Info : SearchRec);
  396. Begin
  397. End;
  398. {$endif not FPC}
  399. Function FileExists ( Const F : String) : Boolean;
  400. {$ifndef delphi}
  401. Var
  402. Info : SearchRec;
  403. {$endif}
  404. begin
  405. {$ifdef delphi}
  406. FileExists:=sysutils.FileExists(f);
  407. {$else}
  408. findfirst(F,readonly+archive+hidden,info);
  409. FileExists:=(doserror=0);
  410. findclose(Info);
  411. {$endif delphi}
  412. end;
  413. Function PathExists ( F : String) : Boolean;
  414. Var
  415. Info : SearchRec;
  416. begin
  417. if F[Length(f)] in ['/','\'] then
  418. Delete(f,length(f),1);
  419. findfirst(F,readonly+archive+hidden+directory,info);
  420. PathExists:=(doserror=0) and ((info.attr and directory)=directory);
  421. findclose(Info);
  422. end;
  423. Function RemoveFile(const f:string):boolean;
  424. var
  425. g : file;
  426. begin
  427. assign(g,f);
  428. {$I-}
  429. erase(g);
  430. {$I+}
  431. RemoveFile:=(ioresult=0);
  432. end;
  433. Function RemoveDir(d:string):boolean;
  434. begin
  435. if d[length(d)]=DirSep then
  436. Delete(d,length(d),1);
  437. {$I-}
  438. rmdir(d);
  439. {$I+}
  440. RemoveDir:=(ioresult=0);
  441. end;
  442. Function SplitPath(const s:string):string;
  443. var
  444. i : longint;
  445. begin
  446. i:=Length(s);
  447. while (i>0) and not(s[i] in ['/','\']) do
  448. dec(i);
  449. SplitPath:=Copy(s,1,i);
  450. end;
  451. Function SplitFileName(const s:string):string;
  452. var
  453. p : dirstr;
  454. n : namestr;
  455. e : extstr;
  456. begin
  457. FSplit(s,p,n,e);
  458. SplitFileName:=n+e;
  459. end;
  460. Function SplitName(const s:string):string;
  461. var
  462. i,j : longint;
  463. begin
  464. i:=Length(s);
  465. j:=Length(s);
  466. while (i>0) and not(s[i] in ['/','\']) do
  467. dec(i);
  468. while (j>0) and (s[j]<>'.') do
  469. dec(j);
  470. if j<=i then
  471. j:=255;
  472. SplitName:=Copy(s,i+1,j-(i+1));
  473. end;
  474. Function SplitExtension(Const HStr:String):String;
  475. var
  476. j : longint;
  477. begin
  478. j:=length(Hstr);
  479. while (j>0) and (Hstr[j]<>'.') do
  480. begin
  481. if hstr[j]=DirSep then
  482. j:=0
  483. else
  484. dec(j);
  485. end;
  486. if j=0 then
  487. j:=254;
  488. SplitExtension:=Copy(Hstr,j,255);
  489. end;
  490. Function AddExtension(Const HStr,ext:String):String;
  491. begin
  492. if (Ext<>'') and (SplitExtension(HStr)='') then
  493. AddExtension:=Hstr+Ext
  494. else
  495. AddExtension:=Hstr;
  496. end;
  497. Function ForceExtension(Const HStr,ext:String):String;
  498. var
  499. j : longint;
  500. begin
  501. j:=length(Hstr);
  502. while (j>0) and (Hstr[j]<>'.') do
  503. dec(j);
  504. if j=0 then
  505. j:=255;
  506. ForceExtension:=Copy(Hstr,1,j-1)+Ext;
  507. end;
  508. Function FixPath(s:string;allowdot:boolean):string;
  509. var
  510. i : longint;
  511. begin
  512. { Fix separator }
  513. for i:=1 to length(s) do
  514. if s[i] in ['/','\'] then
  515. s[i]:=DirSep;
  516. { Fix ending / }
  517. if (length(s)>0) and (s[length(s)]<>DirSep) and
  518. (s[length(s)]<>':') then
  519. s:=s+DirSep;
  520. { Remove ./ }
  521. if (not allowdot) and (s='.'+DirSep) then
  522. s:='';
  523. { return }
  524. {$ifdef unix}
  525. FixPath:=s;
  526. {$else}
  527. FixPath:=Lower(s);
  528. {$endif}
  529. end;
  530. function FixFileName(const s:string):string;
  531. var
  532. i : longint;
  533. {$ifdef Linux}
  534. NoPath : boolean;
  535. {$endif Linux}
  536. begin
  537. {$ifdef Linux}
  538. NoPath:=true;
  539. {$endif Linux}
  540. for i:=length(s) downto 1 do
  541. begin
  542. case s[i] of
  543. {$ifdef Linux}
  544. '/','\' : begin
  545. FixFileName[i]:='/';
  546. NoPath:=false; {Skip lowercasing path: 'X11'<>'x11' }
  547. end;
  548. 'A'..'Z' : if NoPath then
  549. FixFileName[i]:=char(byte(s[i])+32)
  550. else
  551. FixFileName[i]:=s[i];
  552. {$else}
  553. '/' : FixFileName[i]:='\';
  554. 'A'..'Z' : FixFileName[i]:=char(byte(s[i])+32);
  555. {$endif}
  556. else
  557. FixFileName[i]:=s[i];
  558. end;
  559. end;
  560. FixFileName[0]:=s[0];
  561. end;
  562. procedure SplitBinCmd(const s:string;var bstr,cstr:string);
  563. var
  564. i : longint;
  565. begin
  566. i:=pos(' ',s);
  567. if i>0 then
  568. begin
  569. bstr:=Copy(s,1,i-1);
  570. cstr:=Copy(s,i+1,length(s)-i);
  571. end
  572. else
  573. begin
  574. bstr:='';
  575. cstr:='';
  576. end;
  577. end;
  578. procedure TSearchPathList.AddPath(s:string;addfirst:boolean);
  579. var
  580. j : longint;
  581. hs,hsd,
  582. CurrentDir,
  583. CurrPath : string;
  584. dir : searchrec;
  585. hp : TStringListItem;
  586. procedure addcurrpath;
  587. begin
  588. if addfirst then
  589. begin
  590. Remove(currPath);
  591. Insert(currPath);
  592. end
  593. else
  594. begin
  595. { Check if already in path, then we don't add it }
  596. hp:=Find(currPath);
  597. if not assigned(hp) then
  598. Concat(currPath);
  599. end;
  600. end;
  601. begin
  602. if s='' then
  603. exit;
  604. { Support default macro's }
  605. DefaultReplacements(s);
  606. { get current dir }
  607. CurrentDir:=GetCurrentDir;
  608. repeat
  609. { get currpath }
  610. if addfirst then
  611. begin
  612. j:=length(s);
  613. while (j>0) and (s[j]<>';') do
  614. dec(j);
  615. CurrPath:=FixPath(Copy(s,j+1,length(s)-j),false);
  616. if j=0 then
  617. s:=''
  618. else
  619. System.Delete(s,j,length(s)-j+1);
  620. end
  621. else
  622. begin
  623. j:=Pos(';',s);
  624. if j=0 then
  625. j:=255;
  626. CurrPath:=FixPath(Copy(s,1,j-1),false);
  627. System.Delete(s,1,j);
  628. end;
  629. { fix pathname }
  630. if CurrPath='' then
  631. CurrPath:='.'+DirSep
  632. else
  633. begin
  634. CurrPath:=FixPath(FExpand(CurrPath),false);
  635. if (CurrentDir<>'') and (Copy(CurrPath,1,length(CurrentDir))=CurrentDir) then
  636. CurrPath:='.'+DirSep+Copy(CurrPath,length(CurrentDir)+1,255);
  637. end;
  638. { wildcard adding ? }
  639. if pos('*',currpath)>0 then
  640. begin
  641. if currpath[length(currpath)]=dirsep then
  642. hs:=Copy(currpath,1,length(CurrPath)-1)
  643. else
  644. hs:=currpath;
  645. hsd:=SplitPath(hs);
  646. findfirst(hs,directory,dir);
  647. while doserror=0 do
  648. begin
  649. if (dir.name<>'.') and
  650. (dir.name<>'..') and
  651. ((dir.attr and directory)<>0) then
  652. begin
  653. currpath:=hsd+dir.name+dirsep;
  654. hp:=Find(currPath);
  655. if not assigned(hp) then
  656. AddCurrPath;
  657. end;
  658. findnext(dir);
  659. end;
  660. FindClose(dir);
  661. end
  662. else
  663. begin
  664. if PathExists(currpath) then
  665. addcurrpath;
  666. end;
  667. until (s='');
  668. end;
  669. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  670. var
  671. s : string;
  672. hl : TSearchPathList;
  673. hp,hp2 : TStringListItem;
  674. begin
  675. if list.empty then
  676. exit;
  677. { create temp and reverse the list }
  678. if addfirst then
  679. begin
  680. hl:=TSearchPathList.Create;
  681. hp:=TStringListItem(list.first);
  682. while assigned(hp) do
  683. begin
  684. hl.insert(hp.Str);
  685. hp:=TStringListItem(hp.next);
  686. end;
  687. while not hl.empty do
  688. begin
  689. s:=hl.GetFirst;
  690. Remove(s);
  691. Insert(s);
  692. end;
  693. hl.Free;
  694. end
  695. else
  696. begin
  697. hp:=TStringListItem(list.first);
  698. while assigned(hp) do
  699. begin
  700. hp2:=Find(hp.Str);
  701. { Check if already in path, then we don't add it }
  702. if not assigned(hp2) then
  703. Concat(hp.Str);
  704. hp:=TStringListItem(hp.next);
  705. end;
  706. end;
  707. end;
  708. function TSearchPathList.FindFile(const f : string;var b : boolean) : string;
  709. Var
  710. p : TStringListItem;
  711. begin
  712. FindFile:='';
  713. b:=false;
  714. p:=TStringListItem(first);
  715. while assigned(p) do
  716. begin
  717. If FileExists(p.Str+f) then
  718. begin
  719. FindFile:=p.Str;
  720. b:=true;
  721. exit;
  722. end;
  723. p:=TStringListItem(p.next);
  724. end;
  725. end;
  726. Function GetFileTime ( Var F : File) : Longint;
  727. Var
  728. {$ifdef unix}
  729. Info : Stat;
  730. {$endif}
  731. L : longint;
  732. begin
  733. {$ifdef unix}
  734. FStat (F,Info);
  735. L:=Info.Mtime;
  736. {$else}
  737. GetFTime(f,l);
  738. {$endif}
  739. GetFileTime:=L;
  740. end;
  741. Function GetNamedFileTime (Const F : String) : Longint;
  742. begin
  743. GetNamedFileTime:=do_getnamedfiletime(F);
  744. end;
  745. {Touch Assembler and object time to ppu time is there is a ppufilename}
  746. procedure SynchronizeFileTime(const fn1,fn2:string);
  747. var
  748. f : file;
  749. l : longint;
  750. begin
  751. Assign(f,fn1);
  752. {$I-}
  753. reset(f,1);
  754. {$I+}
  755. if ioresult=0 then
  756. begin
  757. getftime(f,l);
  758. { just to be sure in case there are rounding errors }
  759. setftime(f,l);
  760. close(f);
  761. assign(f,fn2);
  762. {$I-}
  763. reset(f,1);
  764. {$I+}
  765. if ioresult=0 then
  766. begin
  767. setftime(f,l);
  768. close(f);
  769. end;
  770. end;
  771. end;
  772. function FindFile(const f : string;path : string;var b : boolean) : string;
  773. Var
  774. singlepathstring : string;
  775. i : longint;
  776. begin
  777. {$ifdef unix}
  778. for i:=1 to length(path) do
  779. if path[i]=':' then
  780. path[i]:=';';
  781. {$endif}
  782. b:=false;
  783. FindFile:='';
  784. repeat
  785. i:=pos(';',path);
  786. if i=0 then
  787. i:=256;
  788. singlepathstring:=FixPath(copy(path,1,i-1),false);
  789. delete(path,1,i);
  790. If FileExists (singlepathstring+f) then
  791. begin
  792. FindFile:=singlepathstring;
  793. b:=true;
  794. exit;
  795. end;
  796. until path='';
  797. end;
  798. function FindExe(bin:string;var found:boolean):string;
  799. begin
  800. bin:=FixFileName(AddExtension(bin,source_os.exeext));
  801. {$ifdef delphi}
  802. FindExe:=FindFile(bin,'.;'+exepath+';'+dmisc.getenv('PATH'),found)+bin;
  803. {$else delphi}
  804. FindExe:=FindFile(bin,'.;'+exepath+';'+dos.getenv('PATH'),found)+bin;
  805. {$endif delphi}
  806. end;
  807. function GetShortName(const n:string):string;
  808. {$ifdef win32}
  809. var
  810. hs,hs2 : string;
  811. i : longint;
  812. {$endif}
  813. {$ifdef go32v2}
  814. var
  815. hs : string;
  816. {$endif}
  817. begin
  818. GetShortName:=n;
  819. {$ifdef win32}
  820. hs:=n+#0;
  821. i:=Windows.GetShortPathName(@hs[1],@hs2[1],high(hs2));
  822. if (i>0) and (i<=high(hs2)) then
  823. begin
  824. hs2[0]:=chr(strlen(@hs2[1]));
  825. GetShortName:=hs2;
  826. end;
  827. {$endif}
  828. {$ifdef go32v2}
  829. hs:=n;
  830. if Dos.GetShortName(hs) then
  831. GetShortName:=hs;
  832. {$endif}
  833. end;
  834. {****************************************************************************
  835. OS Dependent things
  836. ****************************************************************************}
  837. function GetEnvPChar(const envname:string):pchar;
  838. {$ifdef win32}
  839. var
  840. s : string;
  841. i,len : longint;
  842. hp,p,p2 : pchar;
  843. {$endif}
  844. {$ifdef os2}
  845. var
  846. P1, P2: PChar;
  847. {$endif}
  848. begin
  849. {$ifdef unix}
  850. GetEnvPchar:={$ifdef ver1_0}Linux{$else}Unix{$endif}.Getenv(envname);
  851. {$define GETENVOK}
  852. {$endif}
  853. {$ifdef win32}
  854. GetEnvPchar:=nil;
  855. p:=GetEnvironmentStrings;
  856. hp:=p;
  857. while hp^<>#0 do
  858. begin
  859. s:=strpas(hp);
  860. i:=pos('=',s);
  861. len:=strlen(hp);
  862. if upper(copy(s,1,i-1))=upper(envname) then
  863. begin
  864. GetMem(p2,len-length(envname));
  865. Move(hp[i],p2^,len-length(envname));
  866. GetEnvPchar:=p2;
  867. break;
  868. end;
  869. { next string entry}
  870. hp:=hp+len+1;
  871. end;
  872. FreeEnvironmentStrings(p);
  873. {$define GETENVOK}
  874. {$endif}
  875. {$ifdef os2}
  876. P1 := StrPNew (EnvName);
  877. if Assigned (P1) then
  878. begin
  879. if DosCalls.DosScanEnv (P1, P2) = 0 then
  880. GetEnvPChar := P2
  881. else
  882. GetEnvPChar := nil;
  883. StrDispose (P1);
  884. end else GetEnvPChar := nil;
  885. {$define GETENVOK}
  886. {$endif}
  887. {$ifdef GETENVOK}
  888. {$undef GETENVOK}
  889. {$else}
  890. GetEnvPchar:=StrPNew(Dos.Getenv(envname));
  891. {$endif}
  892. end;
  893. procedure FreeEnvPChar(p:pchar);
  894. begin
  895. {$ifndef unix}
  896. {$ifndef os2}
  897. StrDispose(p);
  898. {$endif}
  899. {$endif}
  900. end;
  901. Procedure Shell(const command:string);
  902. { This is already defined in the linux.ppu for linux, need for the *
  903. expansion under linux }
  904. {$ifdef unix}
  905. begin
  906. {$ifdef ver1_0}Linux{$else}Unix{$endif}.Shell(command);
  907. end;
  908. {$else}
  909. var
  910. comspec : string;
  911. begin
  912. comspec:=getenv('COMSPEC');
  913. Exec(comspec,' /C '+command);
  914. end;
  915. {$endif}
  916. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  917. var
  918. b : boolean;
  919. begin
  920. b:=true;
  921. if s='DEFAULT' then
  922. aktmodeswitches:=initmodeswitches
  923. else
  924. if s='DELPHI' then
  925. aktmodeswitches:=delphimodeswitches
  926. else
  927. if s='TP' then
  928. aktmodeswitches:=tpmodeswitches
  929. else
  930. if s='FPC' then
  931. aktmodeswitches:=fpcmodeswitches
  932. else
  933. if s='OBJFPC' then
  934. aktmodeswitches:=objfpcmodeswitches
  935. else
  936. if s='GPC' then
  937. aktmodeswitches:=gpcmodeswitches
  938. else
  939. b:=false;
  940. if b and changeInit then
  941. initmodeswitches := aktmodeswitches;
  942. if b then
  943. begin
  944. { turn ansistrings on by default ? }
  945. if (m_delphi in aktmodeswitches) then
  946. begin
  947. include(aktlocalswitches,cs_ansistrings);
  948. if changeinit then
  949. include(initlocalswitches,cs_ansistrings);
  950. end
  951. else
  952. begin
  953. exclude(aktlocalswitches,cs_ansistrings);
  954. if changeinit then
  955. exclude(initlocalswitches,cs_ansistrings);
  956. end;
  957. { enum packing }
  958. if (m_tp7 in aktmodeswitches) then
  959. aktpackenum:=1
  960. else
  961. aktpackenum:=4;
  962. if changeinit then
  963. initpackenum:=aktpackenum;
  964. end;
  965. SetCompileMode:=b;
  966. end;
  967. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  968. function string2guid(const s: string; var GUID: TGUID): boolean;
  969. function ishexstr(const hs: string): boolean;
  970. var
  971. i: integer;
  972. begin
  973. ishexstr:=false;
  974. for i:=1 to Length(hs) do begin
  975. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  976. exit;
  977. end;
  978. ishexstr:=true;
  979. end;
  980. function hexstr2longint(const hexs: string): longint;
  981. var
  982. i: integer;
  983. rl: longint;
  984. begin
  985. rl:=0;
  986. for i:=1 to length(hexs) do begin
  987. rl:=rl shl 4;
  988. case hexs[i] of
  989. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  990. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  991. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  992. end
  993. end;
  994. hexstr2longint:=rl;
  995. end;
  996. var
  997. i: integer;
  998. begin
  999. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  1000. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  1001. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  1002. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  1003. ishexstr(copy(s,26,12)) then begin
  1004. GUID.D1:=hexstr2longint(copy(s,2,8));
  1005. GUID.D2:=hexstr2longint(copy(s,11,4));
  1006. GUID.D3:=hexstr2longint(copy(s,16,4));
  1007. for i:=0 to 1 do
  1008. GUID.D4[i]:=hexstr2longint(copy(s,21+i*2,2));
  1009. for i:=2 to 7 do
  1010. GUID.D4[i]:=hexstr2longint(copy(s,22+i*2,2));
  1011. string2guid:=true;
  1012. end
  1013. else
  1014. string2guid:=false;
  1015. end;
  1016. function guid2string(const GUID: TGUID): string;
  1017. function long2hex(l, len: longint): string;
  1018. const
  1019. hextbl: array[0..15] of char = '0123456789ABCDEF';
  1020. var
  1021. rs: string;
  1022. i: integer;
  1023. begin
  1024. rs[0]:=chr(len);
  1025. for i:=len downto 1 do begin
  1026. rs[i]:=hextbl[l and $F];
  1027. l:=l shr 4;
  1028. end;
  1029. long2hex:=rs;
  1030. end;
  1031. begin
  1032. guid2string:=
  1033. '{'+long2hex(GUID.D1,8)+
  1034. '-'+long2hex(GUID.D2,4)+
  1035. '-'+long2hex(GUID.D3,4)+
  1036. '-'+long2hex(GUID.D4[0],2)+long2hex(GUID.D4[1],2)+
  1037. '-'+long2hex(GUID.D4[2],2)+long2hex(GUID.D4[3],2)+
  1038. long2hex(GUID.D4[4],2)+long2hex(GUID.D4[5],2)+
  1039. long2hex(GUID.D4[6],2)+long2hex(GUID.D4[7],2)+
  1040. '}';
  1041. end;
  1042. {****************************************************************************
  1043. Init
  1044. ****************************************************************************}
  1045. {$ifdef unix}
  1046. {$define need_path_search}
  1047. {$endif unix}
  1048. {$ifdef os2}
  1049. {$define need_path_search}
  1050. {$endif os2}
  1051. procedure get_exepath;
  1052. var
  1053. hs1 : namestr;
  1054. hs2 : extstr;
  1055. {$ifdef need_path_search}
  1056. b : boolean;
  1057. {$endif}
  1058. begin
  1059. {$ifdef delphi}
  1060. exepath:=dmisc.getenv('PPC_EXEC_PATH');
  1061. {$else delphi}
  1062. exepath:=dos.getenv('PPC_EXEC_PATH');
  1063. {$endif delphi}
  1064. if exepath='' then
  1065. fsplit(FixFileName(system.paramstr(0)),exepath,hs1,hs2);
  1066. {$ifdef need_path_search}
  1067. if exepath='' then
  1068. begin
  1069. if pos(source_os.exeext,hs1) <>
  1070. (length(hs1) - length(source_os.exeext)+1) then
  1071. hs1 := hs1 + source_os.exeext;
  1072. {$ifdef delphi}
  1073. exepath := findfile(hs1,dmisc.getenv('PATH'),b);
  1074. {$else delphi}
  1075. exepath := findfile(hs1,dos.getenv('PATH'),b);
  1076. {$endif delphi}
  1077. end;
  1078. {$endif need_path_search}
  1079. exepath:=FixPath(exepath,false);
  1080. end;
  1081. procedure DoneGlobals;
  1082. begin
  1083. initdefines.free;
  1084. if assigned(DLLImageBase) then
  1085. StringDispose(DLLImageBase);
  1086. RelocSection:=true;
  1087. RelocSectionSetExplicitly:=false;
  1088. UseDeffileForExport:=true;
  1089. librarysearchpath.Free;
  1090. unitsearchpath.Free;
  1091. objectsearchpath.Free;
  1092. includesearchpath.Free;
  1093. end;
  1094. procedure InitGlobals;
  1095. begin
  1096. { set global switches }
  1097. do_build:=false;
  1098. do_make:=true;
  1099. compile_level:=0;
  1100. { these two should not be cleared in
  1101. DoneGlobals as the IDE might need their value }
  1102. IsExe:=false;
  1103. DLLsource:=false;
  1104. { Output }
  1105. OutputFile:='';
  1106. OutputExeDir:='';
  1107. OutputUnitDir:='';
  1108. { Utils directory }
  1109. utilsdirectory:='';
  1110. { Search Paths }
  1111. librarysearchpath:=TSearchPathList.Create;
  1112. unitsearchpath:=TSearchPathList.Create;
  1113. includesearchpath:=TSearchPathList.Create;
  1114. objectsearchpath:=TSearchPathList.Create;
  1115. { Def file }
  1116. usewindowapi:=false;
  1117. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1118. dllversion:='';
  1119. { Init values }
  1120. initmodeswitches:=fpcmodeswitches;
  1121. initlocalswitches:=[cs_check_io];
  1122. initmoduleswitches:=[cs_extsyntax,cs_browser];
  1123. initglobalswitches:=[cs_check_unit_name,cs_link_static];
  1124. {$ifdef i386}
  1125. initoptprocessor:=Class386;
  1126. initspecificoptprocessor:=Class386;
  1127. initpackenum:=4;
  1128. {$IFDEF testvarsets}
  1129. initsetalloc:=0;
  1130. {$ENDIF}
  1131. initpackrecords:=packrecord_2;
  1132. initoutputformat:=target_asm.id;
  1133. initasmmode:=asmmode_i386_att;
  1134. {$else not i386}
  1135. {$ifdef m68k}
  1136. initoptprocessor:=MC68000;
  1137. include(initmoduleswitches,cs_fp_emulation);
  1138. initpackenum:=4;
  1139. {$IFDEF testvarsets}
  1140. initsetalloc:=0;
  1141. {$ENDIF}
  1142. initpackrecords:=packrecord_2;
  1143. initoutputformat:=as_m68k_as;
  1144. initasmmode:=asmmode_m68k_mot;
  1145. {$endif m68k}
  1146. {$endif i386}
  1147. initinterfacetype:=it_interfacecom;
  1148. initdefines:=TStringList.Create;
  1149. { memory sizes, will be overriden by parameter or default for target
  1150. in options or init_parser }
  1151. stacksize:=0;
  1152. heapsize:=0;
  1153. maxheapsize:=0;
  1154. { compile state }
  1155. in_args:=false;
  1156. { must_be_valid:=true; obsolete PM }
  1157. not_unit_proc:=true;
  1158. apptype:=app_cui;
  1159. end;
  1160. begin
  1161. get_exepath;
  1162. {$ifdef EXTDEBUG}
  1163. {$ifdef FPC}
  1164. EntryMemUsed:=system.HeapSize-MemAvail;
  1165. {$endif FPC}
  1166. {$endif}
  1167. end.
  1168. {
  1169. $Log$
  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. }