files.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. code : word;
  286. objfiletime,
  287. ppufiletime,
  288. asmfiletime,
  289. source_time : longint;
  290. {$ifdef UseBrowser}
  291. hp : pextfile;
  292. _d : dirstr;
  293. _n : namestr;
  294. _e : extstr;
  295. {$endif UseBrowser}
  296. begin
  297. load_ppu:=false;
  298. Message1(unit_u_ppu_loading,ppufilename^);
  299. ppufile:=new(pextfile,init(unit_path,n,ext));
  300. ppufile^.reset;
  301. ppufile^.flush;
  302. {Get ppufile time}
  303. ppufiletime:=getnamedfiletime(ppufilename^);
  304. Message1(unit_d_ppu_time,filetimestring(ppufiletime));
  305. { load the header }
  306. ppufile^.read_data(header,sizeof(header),count);
  307. if count<>sizeof(header) then
  308. begin
  309. ppufile^.done;
  310. Message(unit_d_ppu_file_too_short);
  311. exit;
  312. end;
  313. { check for a valid PPU file }
  314. if (header[0]<>'P') or (header[1]<>'P') or (header[2]<>'U') then
  315. begin
  316. ppufile^.done;
  317. Message(unit_d_ppu_invalid_header);
  318. exit;
  319. end;
  320. { load ppu version }
  321. val(header[3]+header[4]+header[5],ppuversion,code);
  322. if not(ppuversion in [13..14]) then
  323. begin
  324. ppufile^.done;
  325. Message1(unit_d_ppu_invalid_version,tostr(ppuversion));
  326. exit;
  327. end;
  328. flags:=byte(header[9]);
  329. Message1(unit_d_ppu_flags,tostr(flags));
  330. crc:=plongint(@header[10])^;
  331. Message1(unit_d_ppu_crc,tostr(crc));
  332. { read name if its there }
  333. ppufile^.read_data(b,1,count);
  334. if b=ibunitname then
  335. begin
  336. ppufile^.read_data(hs[0],1,count);
  337. ppufile^.read_data(hs[1],ord(hs[0]),count);
  338. stringdispose(unitname);
  339. unitname:=stringdup(hs);
  340. ppufile^.read_data(b,1,count);
  341. end;
  342. { search source files there is at least one source file }
  343. do_compile:=false;
  344. sources_avail:=true;
  345. while b<>ibend do
  346. begin
  347. ppufile^.read_data(hs[0],1,count);
  348. ppufile^.read_data(hs[1],ord(hs[0]),count);
  349. if (flags and uf_in_library)<>0 then
  350. begin
  351. sources_avail:=false;
  352. temp:=' library';
  353. end
  354. else
  355. begin
  356. { check the date of the source files }
  357. Source_Time:=GetNamedFileTime(unit_path+hs);
  358. if Source_Time=-1 then
  359. begin
  360. sources_avail:=false;
  361. temp:=' not found';
  362. end
  363. else
  364. begin
  365. temp:=' time '+filetimestring(source_time);
  366. if (source_time>ppufiletime) then
  367. begin
  368. do_compile:=true;
  369. temp:=temp+' *'
  370. end;
  371. end;
  372. end;
  373. Message1(unit_t_ppu_source,unit_path+hs+temp);
  374. {$ifdef UseBrowser}
  375. fsplit(unit_path+hs,_d,_n,_e);
  376. new(hp,init(_d,_n,_e));
  377. { the indexing should match what is done in writeasunit }
  378. sourcefiles.register_file(hp);
  379. {$endif UseBrowser}
  380. ppufile^.read_data(b,1,count);
  381. end;
  382. { main source is always the last }
  383. stringdispose(mainsource);
  384. mainsource:=stringdup(ppufile^.path^+hs);
  385. { check the object and assembler file if not a library }
  386. if (flags and uf_smartlink)<>0 then
  387. begin
  388. objfiletime:=getnamedfiletime(arfilename^);
  389. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  390. do_compile:=true;
  391. end
  392. else
  393. begin
  394. if (flags and uf_in_library)=0 then
  395. begin
  396. { the objectfile should be newer than the ppu file }
  397. objfiletime:=getnamedfiletime(objfilename^);
  398. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  399. begin
  400. { check if assembler file is older than ppu file }
  401. asmfileTime:=GetNamedFileTime(asmfilename^);
  402. if (asmfiletime<0) or (ppufiletime>asmfiletime) then
  403. begin
  404. Message(unit_d_obj_and_asm_are_older_than_ppu);
  405. do_compile:=true;
  406. end
  407. else
  408. begin
  409. Message(unit_d_obj_is_older_than_asm);
  410. do_assemble:=true;
  411. end;
  412. end;
  413. end;
  414. end;
  415. load_ppu:=true;
  416. end;
  417. procedure tmodule.search_unit(const n : string);
  418. var
  419. ext : string[8];
  420. singlepathstring,
  421. Path,
  422. filename : string;
  423. found : boolean;
  424. start,i : longint;
  425. Function UnitExists(const ext:string):boolean;
  426. begin
  427. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  428. UnitExists:=FileExists(Singlepathstring+FileName+ext);
  429. end;
  430. begin
  431. start:=1;
  432. filename:=FixFileName(n);
  433. path:=UnitSearchPath;
  434. Found:=false;
  435. repeat
  436. {Create current path to check}
  437. i:=pos(';',path);
  438. if i=0 then
  439. i:=length(path)+1;
  440. singlepathstring:=FixPath(copy(path,start,i-start));
  441. delete(path,start,i-start+1);
  442. { Check for PPL file }
  443. if not (cs_link_static in aktswitches) then
  444. begin
  445. Found:=UnitExists(target_info.unitlibext);
  446. if Found then
  447. Begin
  448. SetFileName(SinglePathString,FileName);
  449. Found:=Load_PPU(singlepathstring,filename,target_info.unitlibext);
  450. End;
  451. end;
  452. { Check for PPU file }
  453. if not (cs_link_dynamic in aktswitches) and not Found then
  454. begin
  455. Found:=UnitExists(target_info.unitext);
  456. if Found then
  457. Begin
  458. SetFileName(SinglePathString,FileName);
  459. Found:=Load_PPU(singlepathstring,filename,target_info.unitext);
  460. End;
  461. end;
  462. { Check for Sources }
  463. if not Found then
  464. begin
  465. ppufile:=nil;
  466. do_compile:=true;
  467. {Check for .pp file}
  468. Found:=UnitExists(target_os.sourceext);
  469. if Found then
  470. Ext:=target_os.sourceext
  471. else
  472. begin
  473. {Check for .pas}
  474. Found:=UnitExists(target_os.pasext);
  475. if Found then
  476. Ext:=target_os.pasext;
  477. end;
  478. stringdispose(mainsource);
  479. if Found then
  480. begin
  481. sources_avail:=true;
  482. {Load Filenames when found}
  483. mainsource:=StringDup(SinglePathString+FileName+Ext);
  484. SetFileName(SinglePathString,FileName);
  485. end
  486. else
  487. sources_avail:=false;
  488. end;
  489. until Found or (path='');
  490. end;
  491. constructor tmodule.init(const s:string;is_unit:boolean);
  492. var
  493. p : dirstr;
  494. n : namestr;
  495. e : extstr;
  496. begin
  497. FSplit(s,p,n,e);
  498. unitname:=stringdup(Upper(n));
  499. mainsource:=stringdup(s);
  500. objfilename:=nil;
  501. asmfilename:=nil;
  502. arfilename:=nil;
  503. ppufilename:=nil;
  504. setfilename(p,n);
  505. used_units.init;
  506. sourcefiles.init;
  507. linkofiles.init;
  508. linkstaticlibs.init;
  509. linksharedlibs.init;
  510. ppufile:=nil;
  511. current_inputfile:=nil;
  512. map:=nil;
  513. symtable:=nil;
  514. flags:=0;
  515. crc:=0;
  516. unitcount:=1;
  517. do_assemble:=false;
  518. do_compile:=false;
  519. sources_avail:=true;
  520. compiled:=false;
  521. in_implementation:=false;
  522. in_main:=false;
  523. uses_imports:=false;
  524. imports:=new(plinkedlist,init);
  525. output_format:=commandline_output_format;
  526. { set smartlink flag }
  527. if smartlink then
  528. flags:=flags or uf_smartlink;
  529. { search the PPU file if it is an unit }
  530. if is_unit then
  531. search_unit(unitname^);
  532. end;
  533. destructor tmodule.special_done;
  534. begin
  535. if assigned(map) then
  536. dispose(map);
  537. { cannot remove that because it is linked
  538. in the global chain of used_objects
  539. used_units.done; }
  540. sourcefiles.done;
  541. linkofiles.done;
  542. linkstaticlibs.done;
  543. linksharedlibs.done;
  544. if assigned(ppufile) then
  545. dispose(ppufile,done);
  546. if assigned(imports) then
  547. dispose(imports,done);
  548. inherited done;
  549. end;
  550. {****************************************************************************
  551. TUSED_UNIT
  552. ****************************************************************************}
  553. constructor tused_unit.init(_u : pmodule;f : byte);
  554. begin
  555. u:=_u;
  556. in_interface:=false;
  557. in_uses:=false;
  558. is_stab_written:=false;
  559. unitid:=f;
  560. end;
  561. destructor tused_unit.done;
  562. begin
  563. inherited done;
  564. end;
  565. end.
  566. {
  567. $Log$
  568. Revision 1.8 1998-05-04 17:54:25 peter
  569. + smartlinking works (only case jumptable left todo)
  570. * redesign of systems.pas to support assemblers and linkers
  571. + Unitname is now also in the PPU-file, increased version to 14
  572. Revision 1.7 1998/05/01 16:38:44 florian
  573. * handling of private and protected fixed
  574. + change_keywords_to_tp implemented to remove
  575. keywords which aren't supported by tp
  576. * break and continue are now symbols of the system unit
  577. + widestring, longstring and ansistring type released
  578. Revision 1.6 1998/05/01 07:43:53 florian
  579. + basics for rtti implemented
  580. + switch $m (generate rtti for published sections)
  581. Revision 1.5 1998/04/30 15:59:40 pierre
  582. * GDB works again better :
  583. correct type info in one pass
  584. + UseTokenInfo for better source position
  585. * fixed one remaining bug in scanner for line counts
  586. * several little fixes
  587. Revision 1.4 1998/04/29 10:33:52 pierre
  588. + added some code for ansistring (not complete nor working yet)
  589. * corrected operator overloading
  590. * corrected nasm output
  591. + started inline procedures
  592. + added starstarn : use ** for exponentiation (^ gave problems)
  593. + started UseTokenInfo cond to get accurate positions
  594. Revision 1.3 1998/04/27 23:10:28 peter
  595. + new scanner
  596. * $makelib -> if smartlink
  597. * small filename fixes pmodule.setfilename
  598. * moved import from files.pas -> import.pas
  599. Revision 1.2 1998/04/21 10:16:47 peter
  600. * patches from strasbourg
  601. * objects is not used anymore in the fpc compiled version
  602. }