2
0

files.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 by Florian Klaempfl
  4. This unit implements an extended file management and the first loading
  5. and searching of the modules (ppufiles)
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit files;
  20. interface
  21. uses
  22. cobjects,globals;
  23. const
  24. {$ifdef FPC}
  25. maxunits = 1024;
  26. extbufsize = 65535;
  27. {$else}
  28. maxunits = 128;
  29. extbufsize = 2000;
  30. {$endif}
  31. type
  32. { this isn't a text file, this is t-ext-file }
  33. { which means a extended file this files can }
  34. { be handled by a file manager }
  35. pextfile = ^textfile;
  36. textfile = object(tbufferedfile)
  37. path,name,ext : pstring;
  38. _next : pextfile; { else conflicts with tinputstack }
  39. ref_index : word; { 65000 input files for a unit should be enough !! }
  40. { p must be the complete path (with ending \ (or / for unix ...) }
  41. constructor init(const p,n,e : string);
  42. destructor done;virtual;
  43. end;
  44. pinputfile = ^tinputfile;
  45. tinputfile = object(textfile)
  46. filenotatend : boolean;
  47. line_no : longint;
  48. line_count : longint; { second counter for unimportant tokens }
  49. next : pinputfile; { next input file in the stack of input files }
  50. ref_count : longint; { to handle the browser refs }
  51. constructor init(const p,n,e : string);
  52. procedure write_file_line(var t : text); { writes the file name and line number to t }
  53. function get_file_line : string;
  54. end;
  55. pfilemanager = ^tfilemanager;
  56. tfilemanager = object
  57. files : pextfile;
  58. last_ref_index : word;
  59. constructor init;
  60. destructor done;
  61. procedure close_all;
  62. procedure register_file(f : pextfile);
  63. function get_file(w : word) : pextfile;
  64. end;
  65. type
  66. tunitmap = array[0..maxunits-1] of pointer;
  67. punitmap = ^tunitmap;
  68. pmodule = ^tmodule;
  69. tmodule = object(tlinkedlist_item)
  70. ppufile : pextfile; { the PPU file }
  71. ppuversion, { PPU version, handle different versions }
  72. crc, { check sum written to the file }
  73. flags : longint; { flags }
  74. compiled, { unit is already compiled }
  75. do_assemble, { only assemble the object, don't recompile }
  76. do_compile, { need to compile the sources }
  77. sources_avail, { if all sources are reachable }
  78. in_implementation, { processing the implementation part? }
  79. in_main : boolean; { global, after uses else false }
  80. map : punitmap; { mapping of all used units }
  81. unitcount : word; { local unit counter }
  82. symtable : pointer; { pointer to the psymtable of this unit }
  83. output_format : tof; { how to write this file }
  84. uses_imports : boolean; { Set if the module imports from DLL's.}
  85. imports : plinkedlist;
  86. sourcefiles : tfilemanager;
  87. linksharedlibs,
  88. linkstaticlibs,
  89. linkofiles : tstringcontainer;
  90. used_units : tlinkedlist;
  91. current_inputfile : pinputfile;
  92. { used in firstpass for faster settings }
  93. current_index : word;
  94. unitname, { name of the (unit) module in uppercase }
  95. objfilename, { fullname of the objectfile }
  96. asmfilename, { fullname of the assemblerfile }
  97. ppufilename, { fullname of the ppufile }
  98. arfilename, { fullname of the archivefile }
  99. mainsource : pstring; { name of the main sourcefile }
  100. constructor init(const s:string;is_unit:boolean);
  101. destructor special_done;virtual; { this is to be called only when compiling again }
  102. procedure setfilename(const path,name:string);
  103. function load_ppu(const unit_path,n,ext:string):boolean;
  104. procedure search_unit(const n : string);
  105. end;
  106. pused_unit = ^tused_unit;
  107. tused_unit = object(tlinkedlist_item)
  108. u : pmodule;
  109. in_uses,
  110. in_interface,
  111. is_stab_written : boolean;
  112. unitid : word;
  113. constructor init(_u : pmodule;f : byte);
  114. destructor done;virtual;
  115. end;
  116. tunitheader = array[0..19] of char;
  117. const
  118. { compiler version }
  119. { format | }
  120. { signature | | }
  121. { | | | }
  122. { /-------\ /-------\ /----\ }
  123. unitheader : tunitheader = ('P','P','U','0','1','4',#0,#99,
  124. #0,#0,#0,#0,#0,#0,#255,#255,
  125. { | | \---------/ \-------/ }
  126. { | | | | }
  127. { | | check sum | }
  128. { | \--flags unused }
  129. { target system }
  130. #0,#0,#0,#0);
  131. {\---------/ }
  132. { | }
  133. { start of machine language }
  134. ibloadunit = 1;
  135. iborddef = 2;
  136. ibpointerdef = 3;
  137. ibtypesym = 4;
  138. ibarraydef = 5;
  139. ibprocdef = 6;
  140. ibprocsym = 7;
  141. iblinkofile = 8;
  142. ibstringdef = 9;
  143. ibvarsym = 10;
  144. ibconstsym = 11;
  145. ibinitunit = 12;
  146. ibaufzaehlsym = 13;
  147. ibtypedconstsym = 14;
  148. ibrecorddef = 15;
  149. ibfiledef = 16;
  150. ibformaldef = 17;
  151. ibobjectdef = 18;
  152. ibenumdef = 19;
  153. ibsetdef = 20;
  154. ibprocvardef = 21;
  155. ibsourcefile = 22;
  156. ibdbxcount = 23;
  157. ibfloatdef = 24;
  158. ibref = 25;
  159. ibextsymref = 26;
  160. ibextdefref = 27;
  161. ibabsolutesym = 28;
  162. ibclassrefdef = 29;
  163. ibpropertysym = 30;
  164. ibsharedlibs = 31;
  165. iblongstringdef = 32;
  166. ibansistringdef = 33;
  167. ibunitname = 34;
  168. ibwidestringdef = 35;
  169. ibstaticlibs = 36;
  170. ibend = 255;
  171. { unit flags }
  172. uf_init = $1;
  173. uf_uses_dbx = $2;
  174. uf_uses_browser = $4;
  175. uf_in_library = $8;
  176. uf_shared_library = $10;
  177. uf_big_endian = $20;
  178. uf_smartlink = $40;
  179. const
  180. main_module : pmodule = nil;
  181. current_module : pmodule = nil;
  182. var
  183. loaded_units : tlinkedlist;
  184. implementation
  185. uses
  186. dos,verbose,systems;
  187. {****************************************************************************
  188. TFILE
  189. ****************************************************************************}
  190. constructor textfile.init(const p,n,e : string);
  191. begin
  192. inherited init(p+n+e,extbufsize);
  193. path:=stringdup(p);
  194. name:=stringdup(n);
  195. ext:=stringdup(e);
  196. end;
  197. destructor textfile.done;
  198. begin
  199. inherited done;
  200. end;
  201. {****************************************************************************
  202. TINPUTFILE
  203. ****************************************************************************}
  204. constructor tinputfile.init(const p,n,e : string);
  205. begin
  206. inherited init(p,n,e);
  207. filenotatend:=true;
  208. line_no:=1;
  209. line_count:=0;
  210. next:=nil;
  211. end;
  212. procedure tinputfile.write_file_line(var t : text);
  213. begin
  214. write(t,get_file_line);
  215. end;
  216. function tinputfile.get_file_line : string;
  217. begin
  218. if Use_Rhide then
  219. get_file_line:=lowercase(bstoslash(path^)+name^+ext^)+':'+tostr(line_no)+':'
  220. else
  221. get_file_line:=name^+ext^+'('+tostr(line_no)+')'
  222. end;
  223. {****************************************************************************
  224. TFILEMANAGER
  225. ****************************************************************************}
  226. constructor tfilemanager.init;
  227. begin
  228. files:=nil;
  229. last_ref_index:=0;
  230. end;
  231. destructor tfilemanager.done;
  232. var
  233. hp : pextfile;
  234. begin
  235. hp:=files;
  236. while assigned(hp) do
  237. begin
  238. files:=files^._next;
  239. dispose(hp,done);
  240. hp:=files;
  241. end;
  242. end;
  243. procedure tfilemanager.close_all;
  244. begin
  245. end;
  246. procedure tfilemanager.register_file(f : pextfile);
  247. begin
  248. inc(last_ref_index);
  249. f^._next:=files;
  250. f^.ref_index:=last_ref_index;
  251. files:=f;
  252. end;
  253. function tfilemanager.get_file(w : word) : pextfile;
  254. var
  255. ff : pextfile;
  256. begin
  257. ff:=files;
  258. while assigned(ff) and (ff^.ref_index<>w) do
  259. ff:=ff^._next;
  260. get_file:=ff;
  261. end;
  262. {****************************************************************************
  263. TMODULE
  264. ****************************************************************************}
  265. procedure tmodule.setfilename(const path,name:string);
  266. var
  267. s : string;
  268. begin
  269. stringdispose(objfilename);
  270. stringdispose(asmfilename);
  271. stringdispose(ppufilename);
  272. stringdispose(arfilename);
  273. s:=FixFileName(FixPath(path)+name);
  274. objfilename:=stringdup(s+target_info.objext);
  275. asmfilename:=stringdup(s+target_info.asmext);
  276. ppufilename:=stringdup(s+target_info.unitext);
  277. arfilename:=stringdup(s+target_os.staticlibext);
  278. end;
  279. function tmodule.load_ppu(const unit_path,n,ext : string):boolean;
  280. var
  281. header : tunitheader;
  282. count : longint;
  283. temp,hs : string;
  284. b : byte;
  285. incfile_found : boolean;
  286. code : word;
  287. objfiletime,
  288. ppufiletime,
  289. asmfiletime,
  290. source_time : longint;
  291. {$ifdef UseBrowser}
  292. hp : pextfile;
  293. _d : dirstr;
  294. _n : namestr;
  295. _e : extstr;
  296. {$endif UseBrowser}
  297. begin
  298. load_ppu:=false;
  299. Message1(unit_u_ppu_loading,ppufilename^);
  300. ppufile:=new(pextfile,init(unit_path,n,ext));
  301. ppufile^.reset;
  302. ppufile^.flush;
  303. {Get ppufile time}
  304. ppufiletime:=getnamedfiletime(ppufilename^);
  305. Message1(unit_d_ppu_time,filetimestring(ppufiletime));
  306. { load the header }
  307. ppufile^.read_data(header,sizeof(header),count);
  308. if count<>sizeof(header) then
  309. begin
  310. ppufile^.done;
  311. Message(unit_d_ppu_file_too_short);
  312. exit;
  313. end;
  314. { check for a valid PPU file }
  315. if (header[0]<>'P') or (header[1]<>'P') or (header[2]<>'U') then
  316. begin
  317. ppufile^.done;
  318. Message(unit_d_ppu_invalid_header);
  319. exit;
  320. end;
  321. { load ppu version }
  322. val(header[3]+header[4]+header[5],ppuversion,code);
  323. if not(ppuversion in [13..14]) then
  324. begin
  325. ppufile^.done;
  326. Message1(unit_d_ppu_invalid_version,tostr(ppuversion));
  327. exit;
  328. end;
  329. flags:=byte(header[9]);
  330. Message1(unit_d_ppu_flags,tostr(flags));
  331. crc:=plongint(@header[10])^;
  332. Message1(unit_d_ppu_crc,tostr(crc));
  333. { read name if its there }
  334. ppufile^.read_data(b,1,count);
  335. if b=ibunitname then
  336. begin
  337. ppufile^.read_data(hs[0],1,count);
  338. ppufile^.read_data(hs[1],ord(hs[0]),count);
  339. stringdispose(unitname);
  340. unitname:=stringdup(hs);
  341. ppufile^.read_data(b,1,count);
  342. end;
  343. { search source files there is at least one source file }
  344. do_compile:=false;
  345. sources_avail:=true;
  346. while b<>ibend do
  347. begin
  348. ppufile^.read_data(hs[0],1,count);
  349. ppufile^.read_data(hs[1],ord(hs[0]),count);
  350. ppufile^.read_data(b,1,count);
  351. if (flags and uf_in_library)<>0 then
  352. begin
  353. sources_avail:=false;
  354. temp:=' library';
  355. end
  356. else
  357. begin
  358. { check the date of the source files }
  359. Source_Time:=GetNamedFileTime(unit_path+hs);
  360. if Source_Time=-1 then
  361. begin
  362. { search for include files in the includepathlist }
  363. if b<>ibend then
  364. temp:=search(hs,includesearchpath,incfile_found);
  365. if incfile_found then
  366. begin
  367. hs:=temp+hs;
  368. Source_Time:=GetNamedFileTime(hs);
  369. end;
  370. end
  371. else
  372. hs:=unit_path+hs;
  373. if Source_Time=-1 then
  374. begin
  375. sources_avail:=false;
  376. temp:=' not found';
  377. end
  378. else
  379. begin
  380. temp:=' time '+filetimestring(source_time);
  381. if (source_time>ppufiletime) then
  382. begin
  383. do_compile:=true;
  384. temp:=temp+' *'
  385. end;
  386. end;
  387. end;
  388. Message1(unit_t_ppu_source,hs+temp);
  389. {$ifdef UseBrowser}
  390. fsplit(hs,_d,_n,_e);
  391. new(hp,init(_d,_n,_e));
  392. { the indexing should match what is done in writeasunit }
  393. sourcefiles.register_file(hp);
  394. {$endif UseBrowser}
  395. end;
  396. { main source is always the last }
  397. stringdispose(mainsource);
  398. mainsource:=stringdup(hs);
  399. { check the object and assembler file if not a library }
  400. if (flags and uf_smartlink)<>0 then
  401. begin
  402. objfiletime:=getnamedfiletime(arfilename^);
  403. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  404. do_compile:=true;
  405. end
  406. else
  407. begin
  408. if (flags and uf_in_library)=0 then
  409. begin
  410. { the objectfile should be newer than the ppu file }
  411. objfiletime:=getnamedfiletime(objfilename^);
  412. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  413. begin
  414. { check if assembler file is older than ppu file }
  415. asmfileTime:=GetNamedFileTime(asmfilename^);
  416. if (asmfiletime<0) or (ppufiletime>asmfiletime) then
  417. begin
  418. Message(unit_d_obj_and_asm_are_older_than_ppu);
  419. do_compile:=true;
  420. end
  421. else
  422. begin
  423. Message(unit_d_obj_is_older_than_asm);
  424. do_assemble:=true;
  425. end;
  426. end;
  427. end;
  428. end;
  429. load_ppu:=true;
  430. end;
  431. procedure tmodule.search_unit(const n : string);
  432. var
  433. ext : string[8];
  434. singlepathstring,
  435. Path,
  436. filename : string;
  437. found : boolean;
  438. start,i : longint;
  439. Function UnitExists(const ext:string):boolean;
  440. begin
  441. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  442. UnitExists:=FileExists(Singlepathstring+FileName+ext);
  443. end;
  444. begin
  445. start:=1;
  446. filename:=FixFileName(n);
  447. path:=UnitSearchPath;
  448. Found:=false;
  449. repeat
  450. {Create current path to check}
  451. i:=pos(';',path);
  452. if i=0 then
  453. i:=length(path)+1;
  454. singlepathstring:=FixPath(copy(path,start,i-start));
  455. delete(path,start,i-start+1);
  456. { Check for PPL file }
  457. if not (cs_link_static in aktswitches) then
  458. begin
  459. Found:=UnitExists(target_info.unitlibext);
  460. if Found then
  461. Begin
  462. SetFileName(SinglePathString,FileName);
  463. Found:=Load_PPU(singlepathstring,filename,target_info.unitlibext);
  464. End;
  465. end;
  466. { Check for PPU file }
  467. if not (cs_link_dynamic in aktswitches) and not Found then
  468. begin
  469. Found:=UnitExists(target_info.unitext);
  470. if Found then
  471. Begin
  472. SetFileName(SinglePathString,FileName);
  473. Found:=Load_PPU(singlepathstring,filename,target_info.unitext);
  474. End;
  475. end;
  476. { Check for Sources }
  477. if not Found then
  478. begin
  479. ppufile:=nil;
  480. do_compile:=true;
  481. {Check for .pp file}
  482. Found:=UnitExists(target_os.sourceext);
  483. if Found then
  484. Ext:=target_os.sourceext
  485. else
  486. begin
  487. {Check for .pas}
  488. Found:=UnitExists(target_os.pasext);
  489. if Found then
  490. Ext:=target_os.pasext;
  491. end;
  492. stringdispose(mainsource);
  493. if Found then
  494. begin
  495. sources_avail:=true;
  496. {Load Filenames when found}
  497. mainsource:=StringDup(SinglePathString+FileName+Ext);
  498. SetFileName(SinglePathString,FileName);
  499. end
  500. else
  501. sources_avail:=false;
  502. end;
  503. until Found or (path='');
  504. end;
  505. constructor tmodule.init(const s:string;is_unit:boolean);
  506. var
  507. p : dirstr;
  508. n : namestr;
  509. e : extstr;
  510. begin
  511. FSplit(s,p,n,e);
  512. unitname:=stringdup(Upper(n));
  513. mainsource:=stringdup(s);
  514. objfilename:=nil;
  515. asmfilename:=nil;
  516. arfilename:=nil;
  517. ppufilename:=nil;
  518. setfilename(p,n);
  519. used_units.init;
  520. sourcefiles.init;
  521. linkofiles.init;
  522. linkstaticlibs.init;
  523. linksharedlibs.init;
  524. ppufile:=nil;
  525. current_inputfile:=nil;
  526. map:=nil;
  527. symtable:=nil;
  528. flags:=0;
  529. crc:=0;
  530. unitcount:=1;
  531. do_assemble:=false;
  532. do_compile:=false;
  533. sources_avail:=true;
  534. compiled:=false;
  535. in_implementation:=false;
  536. in_main:=false;
  537. uses_imports:=false;
  538. imports:=new(plinkedlist,init);
  539. output_format:=commandline_output_format;
  540. { set smartlink flag }
  541. if smartlink then
  542. flags:=flags or uf_smartlink;
  543. { search the PPU file if it is an unit }
  544. if is_unit then
  545. search_unit(unitname^);
  546. end;
  547. destructor tmodule.special_done;
  548. begin
  549. if assigned(map) then
  550. dispose(map);
  551. { cannot remove that because it is linked
  552. in the global chain of used_objects
  553. used_units.done; }
  554. sourcefiles.done;
  555. linkofiles.done;
  556. linkstaticlibs.done;
  557. linksharedlibs.done;
  558. if assigned(ppufile) then
  559. dispose(ppufile,done);
  560. if assigned(imports) then
  561. dispose(imports,done);
  562. inherited done;
  563. end;
  564. {****************************************************************************
  565. TUSED_UNIT
  566. ****************************************************************************}
  567. constructor tused_unit.init(_u : pmodule;f : byte);
  568. begin
  569. u:=_u;
  570. in_interface:=false;
  571. in_uses:=false;
  572. is_stab_written:=false;
  573. unitid:=f;
  574. end;
  575. destructor tused_unit.done;
  576. begin
  577. inherited done;
  578. end;
  579. end.
  580. {
  581. $Log$
  582. Revision 1.9 1998-05-06 15:04:20 pierre
  583. + when trying to find source files of a ppufile
  584. check the includepathlist for included files
  585. the main file must still be in the same directory
  586. Revision 1.8 1998/05/04 17:54:25 peter
  587. + smartlinking works (only case jumptable left todo)
  588. * redesign of systems.pas to support assemblers and linkers
  589. + Unitname is now also in the PPU-file, increased version to 14
  590. Revision 1.7 1998/05/01 16:38:44 florian
  591. * handling of private and protected fixed
  592. + change_keywords_to_tp implemented to remove
  593. keywords which aren't supported by tp
  594. * break and continue are now symbols of the system unit
  595. + widestring, longstring and ansistring type released
  596. Revision 1.6 1998/05/01 07:43:53 florian
  597. + basics for rtti implemented
  598. + switch $m (generate rtti for published sections)
  599. Revision 1.5 1998/04/30 15:59:40 pierre
  600. * GDB works again better :
  601. correct type info in one pass
  602. + UseTokenInfo for better source position
  603. * fixed one remaining bug in scanner for line counts
  604. * several little fixes
  605. Revision 1.4 1998/04/29 10:33:52 pierre
  606. + added some code for ansistring (not complete nor working yet)
  607. * corrected operator overloading
  608. * corrected nasm output
  609. + started inline procedures
  610. + added starstarn : use ** for exponentiation (^ gave problems)
  611. + started UseTokenInfo cond to get accurate positions
  612. Revision 1.3 1998/04/27 23:10:28 peter
  613. + new scanner
  614. * $makelib -> if smartlink
  615. * small filename fixes pmodule.setfilename
  616. * moved import from files.pas -> import.pas
  617. Revision 1.2 1998/04/21 10:16:47 peter
  618. * patches from strasbourg
  619. * objects is not used anymore in the fpc compiled version
  620. }