files.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. {$ifdef NEWPPU}
  24. ,ppu
  25. {$endif}
  26. ;
  27. const
  28. {$ifdef FPC}
  29. maxunits = 1024;
  30. extbufsize = 65535;
  31. {$else}
  32. maxunits = 128;
  33. extbufsize = 2000;
  34. {$endif}
  35. type
  36. { this isn't a text file, this is t-ext-file }
  37. { which means a extended file this files can }
  38. { be handled by a file manager }
  39. pextfile = ^textfile;
  40. textfile = object(tbufferedfile)
  41. path,name,ext : pstring;
  42. _next : pextfile; { else conflicts with tinputstack }
  43. ref_index : word; { 65000 input files for a unit should be enough !! }
  44. { p must be the complete path (with ending \ (or / for unix ...) }
  45. constructor init(const p,n,e : string);
  46. destructor done;virtual;
  47. end;
  48. pinputfile = ^tinputfile;
  49. tinputfile = object(textfile)
  50. filenotatend : boolean;
  51. line_no : longint;
  52. line_count : longint; { second counter for unimportant tokens }
  53. next : pinputfile; { next input file in the stack of input files }
  54. ref_count : longint; { to handle the browser refs }
  55. constructor init(const p,n,e : string);
  56. procedure write_file_line(var t : text); { writes the file name and line number to t }
  57. function get_file_line : string;
  58. end;
  59. pfilemanager = ^tfilemanager;
  60. tfilemanager = object
  61. files : pextfile;
  62. last_ref_index : word;
  63. constructor init;
  64. destructor done;
  65. procedure close_all;
  66. procedure register_file(f : pextfile);
  67. function get_file(w : word) : pextfile;
  68. end;
  69. type
  70. tunitmap = array[0..maxunits-1] of pointer;
  71. punitmap = ^tunitmap;
  72. pmodule = ^tmodule;
  73. tmodule = object(tlinkedlist_item)
  74. {$ifdef NEWPPU}
  75. ppufile : pppufile; { the PPU file }
  76. {$else}
  77. ppufile : pextfile; { the PPU file }
  78. {$endif}
  79. crc,
  80. flags : longint; { the PPU flags }
  81. compiled, { unit is already compiled }
  82. do_assemble, { only assemble the object, don't recompile }
  83. do_compile, { need to compile the sources }
  84. sources_avail, { if all sources are reachable }
  85. is_unit,
  86. in_implementation, { processing the implementation part? }
  87. in_main : boolean; { global, after uses else false }
  88. map : punitmap; { mapping of all used units }
  89. unitcount : word; { local unit counter }
  90. unit_index : word; { global counter for browser }
  91. symtable : pointer; { pointer to the psymtable of this unit }
  92. uses_imports : boolean; { Set if the module imports from DLL's.}
  93. imports : plinkedlist;
  94. sourcefiles : tfilemanager;
  95. linksharedlibs,
  96. linkstaticlibs,
  97. linkofiles : tstringcontainer;
  98. used_units : tlinkedlist;
  99. current_inputfile : pinputfile;
  100. { used in firstpass for faster settings }
  101. current_index : word;
  102. modulename, { name of the module in uppercase }
  103. objfilename, { fullname of the objectfile }
  104. asmfilename, { fullname of the assemblerfile }
  105. ppufilename, { fullname of the ppufile }
  106. libfilename, { fullname of the libraryfile }
  107. mainsource : pstring; { name of the main sourcefile }
  108. constructor init(const s:string;_is_unit:boolean);
  109. destructor special_done;virtual; { this is to be called only when compiling again }
  110. procedure setfilename(const path,name:string);
  111. {$ifdef NEWPPU}
  112. function openppu(const unit_path:string):boolean;
  113. {$else}
  114. function load_ppu(const unit_path,n,ext:string):boolean;
  115. {$endif}
  116. procedure search_unit(const n : string);
  117. end;
  118. pused_unit = ^tused_unit;
  119. tused_unit = object(tlinkedlist_item)
  120. u : pmodule;
  121. in_uses,
  122. in_interface,
  123. is_stab_written : boolean;
  124. unitid : word;
  125. constructor init(_u : pmodule;f : byte);
  126. destructor done;virtual;
  127. end;
  128. {$ifndef NEWPPU}
  129. type
  130. tunitheader = array[0..19] of char;
  131. const
  132. { compiler version }
  133. { format | }
  134. { signature | | }
  135. { | | | }
  136. { /-------\ /-------\ /----\ }
  137. unitheader : tunitheader = ('P','P','U','0','1','4',#0,#99,
  138. #0,#0,#0,#0,#0,#0,#255,#255,
  139. { | | \---------/ \-------/ }
  140. { | | | | }
  141. { | | check sum | }
  142. { | \--flags unused }
  143. { target system }
  144. #0,#0,#0,#0);
  145. {\---------/ }
  146. { | }
  147. { start of machine language }
  148. ibloadunit = 1;
  149. iborddef = 2;
  150. ibpointerdef = 3;
  151. ibtypesym = 4;
  152. ibarraydef = 5;
  153. ibprocdef = 6;
  154. ibprocsym = 7;
  155. iblinkofile = 8;
  156. ibstringdef = 9;
  157. ibvarsym = 10;
  158. ibconstsym = 11;
  159. ibinitunit = 12;
  160. ibenumsym = 13;
  161. ibtypedconstsym = 14;
  162. ibrecorddef = 15;
  163. ibfiledef = 16;
  164. ibformaldef = 17;
  165. ibobjectdef = 18;
  166. ibenumdef = 19;
  167. ibsetdef = 20;
  168. ibprocvardef = 21;
  169. ibsourcefile = 22;
  170. ibdbxcount = 23;
  171. ibfloatdef = 24;
  172. ibref = 25;
  173. ibextsymref = 26;
  174. ibextdefref = 27;
  175. ibabsolutesym = 28;
  176. ibclassrefdef = 29;
  177. ibpropertysym = 30;
  178. ibsharedlibs = 31;
  179. iblongstringdef = 32;
  180. ibansistringdef = 33;
  181. ibunitname = 34;
  182. ibwidestringdef = 35;
  183. ibstaticlibs = 36;
  184. ibend = 255;
  185. { unit flags }
  186. uf_init = $1;
  187. uf_uses_dbx = $2;
  188. uf_uses_browser = $4;
  189. uf_in_library = $8;
  190. uf_shared_library = $10;
  191. uf_big_endian = $20;
  192. uf_smartlink = $40;
  193. {$endif}
  194. var
  195. main_module : pmodule;
  196. current_module : pmodule;
  197. global_unit_count : word;
  198. loaded_units : tlinkedlist;
  199. implementation
  200. uses
  201. dos,verbose,systems;
  202. {****************************************************************************
  203. TFILE
  204. ****************************************************************************}
  205. constructor textfile.init(const p,n,e : string);
  206. begin
  207. inherited init(p+n+e,extbufsize);
  208. path:=stringdup(p);
  209. name:=stringdup(n);
  210. ext:=stringdup(e);
  211. end;
  212. destructor textfile.done;
  213. begin
  214. inherited done;
  215. end;
  216. {****************************************************************************
  217. TINPUTFILE
  218. ****************************************************************************}
  219. constructor tinputfile.init(const p,n,e : string);
  220. begin
  221. inherited init(p,n,e);
  222. filenotatend:=true;
  223. line_no:=1;
  224. line_count:=0;
  225. next:=nil;
  226. end;
  227. procedure tinputfile.write_file_line(var t : text);
  228. begin
  229. write(t,get_file_line);
  230. end;
  231. function tinputfile.get_file_line : string;
  232. begin
  233. if Use_Rhide then
  234. get_file_line:=lower(bstoslash(path^)+name^+ext^)+':'+tostr(line_no)+':'
  235. else
  236. get_file_line:=name^+ext^+'('+tostr(line_no)+')'
  237. end;
  238. {****************************************************************************
  239. TFILEMANAGER
  240. ****************************************************************************}
  241. constructor tfilemanager.init;
  242. begin
  243. files:=nil;
  244. last_ref_index:=0;
  245. end;
  246. destructor tfilemanager.done;
  247. var
  248. hp : pextfile;
  249. begin
  250. hp:=files;
  251. while assigned(hp) do
  252. begin
  253. files:=files^._next;
  254. dispose(hp,done);
  255. hp:=files;
  256. end;
  257. last_ref_index:=0;
  258. end;
  259. procedure tfilemanager.close_all;
  260. var
  261. hp : pextfile;
  262. begin
  263. hp:=files;
  264. while assigned(hp) do
  265. begin
  266. hp^.close;
  267. hp:=hp^._next;
  268. end;
  269. end;
  270. procedure tfilemanager.register_file(f : pextfile);
  271. begin
  272. inc(last_ref_index);
  273. f^._next:=files;
  274. f^.ref_index:=last_ref_index;
  275. files:=f;
  276. end;
  277. function tfilemanager.get_file(w : word) : pextfile;
  278. var
  279. ff : pextfile;
  280. begin
  281. ff:=files;
  282. while assigned(ff) and (ff^.ref_index<>w) do
  283. ff:=ff^._next;
  284. get_file:=ff;
  285. end;
  286. {****************************************************************************
  287. TMODULE
  288. ****************************************************************************}
  289. procedure tmodule.setfilename(const path,name:string);
  290. var
  291. s : string;
  292. begin
  293. stringdispose(objfilename);
  294. stringdispose(asmfilename);
  295. stringdispose(ppufilename);
  296. stringdispose(libfilename);
  297. s:=FixFileName(FixPath(path)+name);
  298. objfilename:=stringdup(s+target_info.objext);
  299. asmfilename:=stringdup(s+target_info.asmext);
  300. ppufilename:=stringdup(s+target_info.unitext);
  301. libfilename:=stringdup(s+target_os.staticlibext);
  302. end;
  303. {$ifdef NEWPPU}
  304. function tmodule.openppu(const unit_path:string):boolean;
  305. var
  306. temp,hs : string;
  307. b : byte;
  308. incfile_found : boolean;
  309. objfiletime,
  310. ppufiletime,
  311. asmfiletime,
  312. source_time : longint;
  313. {$ifdef UseBrowser}
  314. hp : pextfile;
  315. _d : dirstr;
  316. _n : namestr;
  317. _e : extstr;
  318. {$endif UseBrowser}
  319. begin
  320. openppu:=false;
  321. { Get ppufile time (also check if the file exists) }
  322. ppufiletime:=getnamedfiletime(ppufilename^);
  323. if ppufiletime=-1 then
  324. exit;
  325. Message1(unit_u_ppu_loading,ppufilename^);
  326. ppufile:=new(pppufile,init(ppufilename^));
  327. if not ppufile^.open then
  328. begin
  329. dispose(ppufile,done);
  330. Message(unit_d_ppu_file_too_short);
  331. exit;
  332. end;
  333. { check for a valid PPU file }
  334. if not ppufile^.CheckPPUId then
  335. begin
  336. dispose(ppufile,done);
  337. Message(unit_d_ppu_invalid_header);
  338. exit;
  339. end;
  340. { check for allowed PPU versions }
  341. if not (ppufile^.GetPPUVersion in [15]) then
  342. begin
  343. dispose(ppufile,done);
  344. Message1(unit_d_ppu_invalid_version,tostr(ppufile^.GetPPUVersion));
  345. exit;
  346. end;
  347. flags:=ppufile^.header.flags;
  348. { Show Debug info }
  349. Message1(unit_d_ppu_time,filetimestring(ppufiletime));
  350. Message1(unit_d_ppu_flags,tostr(flags));
  351. Message1(unit_d_ppu_crc,tostr(ppufile^.header.checksum));
  352. { Unitname }
  353. b:=ppufile^.readentry;
  354. if b=ibmodulename then
  355. begin
  356. stringdispose(modulename);
  357. modulename:=stringdup(ppufile^.getstring);
  358. b:=ppufile^.readentry;
  359. end;
  360. { search source files there is at least one source file }
  361. do_compile:=false;
  362. sources_avail:=true;
  363. if b=ibsourcefiles then
  364. begin
  365. while not ppufile^.endofentry do
  366. begin
  367. hs:=ppufile^.getstring;
  368. if (flags and uf_in_library)<>0 then
  369. begin
  370. sources_avail:=false;
  371. temp:=' library';
  372. end
  373. else if pos('Macro ',hs)=1 then
  374. begin
  375. { we don't want to find this file }
  376. { but there is a problem with file indexing !! }
  377. temp:='';
  378. end
  379. else
  380. begin
  381. { check the date of the source files }
  382. Source_Time:=GetNamedFileTime(unit_path+hs);
  383. if Source_Time=-1 then
  384. begin
  385. { search for include files in the includepathlist }
  386. if b<>ibend then
  387. temp:=search(hs,includesearchpath,incfile_found);
  388. if incfile_found then
  389. begin
  390. hs:=temp+hs;
  391. Source_Time:=GetNamedFileTime(hs);
  392. end;
  393. end
  394. else
  395. hs:=unit_path+hs;
  396. if Source_Time=-1 then
  397. begin
  398. sources_avail:=false;
  399. temp:=' not found';
  400. end
  401. else
  402. begin
  403. temp:=' time '+filetimestring(source_time);
  404. if (source_time>ppufiletime) then
  405. begin
  406. do_compile:=true;
  407. temp:=temp+' *'
  408. end;
  409. end;
  410. end;
  411. Message1(unit_t_ppu_source,hs+temp);
  412. {$ifdef UseBrowser}
  413. fsplit(hs,_d,_n,_e);
  414. new(hp,init(_d,_n,_e));
  415. { the indexing should match what is done in writeasunit }
  416. sourcefiles.register_file(hp);
  417. {$endif UseBrowser}
  418. end;
  419. end;
  420. { main source is always the last }
  421. stringdispose(mainsource);
  422. mainsource:=stringdup(hs);
  423. { check the object and assembler file if not a library }
  424. if (flags and uf_in_library)=0 then
  425. begin
  426. if (flags and uf_smartlink)<>0 then
  427. begin
  428. objfiletime:=getnamedfiletime(libfilename^);
  429. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  430. do_compile:=true;
  431. end
  432. else
  433. begin
  434. { the objectfile should be newer than the ppu file }
  435. objfiletime:=getnamedfiletime(objfilename^);
  436. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  437. begin
  438. { check if assembler file is older than ppu file }
  439. asmfileTime:=GetNamedFileTime(asmfilename^);
  440. if (asmfiletime<0) or (ppufiletime>asmfiletime) then
  441. begin
  442. Message(unit_d_obj_and_asm_are_older_than_ppu);
  443. do_compile:=true;
  444. end
  445. else
  446. begin
  447. Message(unit_d_obj_is_older_than_asm);
  448. do_assemble:=true;
  449. end;
  450. end;
  451. end;
  452. end;
  453. openppu:=true;
  454. end;
  455. procedure tmodule.search_unit(const n : string);
  456. var
  457. ext : string[8];
  458. singlepathstring,
  459. Path,
  460. filename : string;
  461. found : boolean;
  462. start,i : longint;
  463. Function UnitExists(const ext:string):boolean;
  464. begin
  465. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  466. UnitExists:=FileExists(Singlepathstring+FileName+ext);
  467. end;
  468. begin
  469. start:=1;
  470. filename:=FixFileName(n);
  471. path:=UnitSearchPath;
  472. Found:=false;
  473. repeat
  474. { Create current path to check }
  475. i:=pos(';',path);
  476. if i=0 then
  477. i:=length(path)+1;
  478. singlepathstring:=FixPath(copy(path,start,i-start));
  479. delete(path,start,i-start+1);
  480. { Check for PPL file }
  481. if not (cs_link_static in aktswitches) then
  482. begin
  483. Found:=UnitExists(target_info.unitlibext);
  484. if Found then
  485. Begin
  486. SetFileName(SinglePathString,FileName);
  487. Found:=OpenPPU(singlepathstring);
  488. End;
  489. end;
  490. { Check for PPU file }
  491. if not (cs_link_dynamic in aktswitches) and not Found then
  492. begin
  493. Found:=UnitExists(target_info.unitext);
  494. if Found then
  495. Begin
  496. SetFileName(SinglePathString,FileName);
  497. Found:=OpenPPU(singlepathstring);
  498. End;
  499. end;
  500. { Check for Sources }
  501. if not Found then
  502. begin
  503. ppufile:=nil;
  504. do_compile:=true;
  505. {Check for .pp file}
  506. Found:=UnitExists(target_os.sourceext);
  507. if Found then
  508. Ext:=target_os.sourceext
  509. else
  510. begin
  511. {Check for .pas}
  512. Found:=UnitExists(target_os.pasext);
  513. if Found then
  514. Ext:=target_os.pasext;
  515. end;
  516. stringdispose(mainsource);
  517. if Found then
  518. begin
  519. sources_avail:=true;
  520. {Load Filenames when found}
  521. mainsource:=StringDup(SinglePathString+FileName+Ext);
  522. SetFileName(SinglePathString,FileName);
  523. end
  524. else
  525. sources_avail:=false;
  526. end;
  527. until Found or (path='');
  528. end;
  529. {$else NEWPPU}
  530. function tmodule.load_ppu(const unit_path,n,ext : string):boolean;
  531. var
  532. header : tunitheader;
  533. count : longint;
  534. temp,hs : string;
  535. b : byte;
  536. incfile_found : boolean;
  537. code : word;
  538. ppuversion,
  539. objfiletime,
  540. ppufiletime,
  541. asmfiletime,
  542. source_time : longint;
  543. {$ifdef UseBrowser}
  544. hp : pextfile;
  545. _d : dirstr;
  546. _n : namestr;
  547. _e : extstr;
  548. {$endif UseBrowser}
  549. begin
  550. load_ppu:=false;
  551. { Get ppufile time (also check if the file exists) }
  552. ppufiletime:=getnamedfiletime(ppufilename^);
  553. if ppufiletime=-1 then
  554. exit;
  555. Message1(unit_u_ppu_loading,ppufilename^);
  556. ppufile:=new(pextfile,init(unit_path,n,ext));
  557. ppufile^.reset;
  558. ppufile^.flush;
  559. { load the header }
  560. ppufile^.read_data(header,sizeof(header),count);
  561. if count<>sizeof(header) then
  562. begin
  563. ppufile^.done;
  564. Message(unit_d_ppu_file_too_short);
  565. exit;
  566. end;
  567. { check for a valid PPU file }
  568. if (header[0]<>'P') or (header[1]<>'P') or (header[2]<>'U') then
  569. begin
  570. ppufile^.done;
  571. Message(unit_d_ppu_invalid_header);
  572. exit;
  573. end;
  574. { load ppu version }
  575. val(header[3]+header[4]+header[5],ppuversion,code);
  576. if not(ppuversion in [13..14]) then
  577. begin
  578. ppufile^.done;
  579. Message1(unit_d_ppu_invalid_version,tostr(ppuversion));
  580. exit;
  581. end;
  582. flags:=byte(header[9]);
  583. crc:=plongint(@header[10])^;
  584. {Get ppufile time}
  585. ppufiletime:=getnamedfiletime(ppufilename^);
  586. {Show Debug info}
  587. Message1(unit_d_ppu_time,filetimestring(ppufiletime));
  588. Message1(unit_d_ppu_flags,tostr(flags));
  589. Message1(unit_d_ppu_crc,tostr(crc));
  590. { read name if its there }
  591. ppufile^.read_data(b,1,count);
  592. if b=ibunitname then
  593. begin
  594. ppufile^.read_data(hs[0],1,count);
  595. ppufile^.read_data(hs[1],ord(hs[0]),count);
  596. stringdispose(modulename);
  597. modulename:=stringdup(hs);
  598. ppufile^.read_data(b,1,count);
  599. end;
  600. { search source files there is at least one source file }
  601. do_compile:=false;
  602. sources_avail:=true;
  603. while b<>ibend do
  604. begin
  605. ppufile^.read_data(hs[0],1,count);
  606. ppufile^.read_data(hs[1],ord(hs[0]),count);
  607. ppufile^.read_data(b,1,count);
  608. if (flags and uf_in_library)<>0 then
  609. begin
  610. sources_avail:=false;
  611. temp:=' library';
  612. end
  613. else
  614. begin
  615. { check the date of the source files }
  616. Source_Time:=GetNamedFileTime(unit_path+hs);
  617. if Source_Time=-1 then
  618. begin
  619. { search for include files in the includepathlist }
  620. if b<>ibend then
  621. temp:=search(hs,includesearchpath,incfile_found);
  622. if incfile_found then
  623. begin
  624. hs:=temp+hs;
  625. Source_Time:=GetNamedFileTime(hs);
  626. end;
  627. end
  628. else
  629. hs:=unit_path+hs;
  630. if Source_Time=-1 then
  631. begin
  632. sources_avail:=false;
  633. temp:=' not found';
  634. end
  635. else
  636. begin
  637. temp:=' time '+filetimestring(source_time);
  638. if (source_time>ppufiletime) then
  639. begin
  640. do_compile:=true;
  641. temp:=temp+' *'
  642. end;
  643. end;
  644. end;
  645. Message1(unit_t_ppu_source,hs+temp);
  646. {$ifdef UseBrowser}
  647. fsplit(hs,_d,_n,_e);
  648. new(hp,init(_d,_n,_e));
  649. { the indexing should match what is done in writeasunit }
  650. sourcefiles.register_file(hp);
  651. {$endif UseBrowser}
  652. end;
  653. { main source is always the last }
  654. stringdispose(mainsource);
  655. mainsource:=stringdup(hs);
  656. { check the object and assembler file if not a library }
  657. if (flags and uf_smartlink)<>0 then
  658. begin
  659. objfiletime:=getnamedfiletime(libfilename^);
  660. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  661. do_compile:=true;
  662. end
  663. else
  664. begin
  665. if (flags and uf_in_library)=0 then
  666. begin
  667. { the objectfile should be newer than the ppu file }
  668. objfiletime:=getnamedfiletime(objfilename^);
  669. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  670. begin
  671. { check if assembler file is older than ppu file }
  672. asmfileTime:=GetNamedFileTime(asmfilename^);
  673. if (asmfiletime<0) or (ppufiletime>asmfiletime) then
  674. begin
  675. Message(unit_d_obj_and_asm_are_older_than_ppu);
  676. do_compile:=true;
  677. end
  678. else
  679. begin
  680. Message(unit_d_obj_is_older_than_asm);
  681. do_assemble:=true;
  682. end;
  683. end;
  684. end;
  685. end;
  686. load_ppu:=true;
  687. end;
  688. procedure tmodule.search_unit(const n : string);
  689. var
  690. ext : string[8];
  691. singlepathstring,
  692. Path,
  693. filename : string;
  694. found : boolean;
  695. start,i : longint;
  696. Function UnitExists(const ext:string):boolean;
  697. begin
  698. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  699. UnitExists:=FileExists(Singlepathstring+FileName+ext);
  700. end;
  701. begin
  702. start:=1;
  703. filename:=FixFileName(n);
  704. path:=UnitSearchPath;
  705. Found:=false;
  706. repeat
  707. {Create current path to check}
  708. i:=pos(';',path);
  709. if i=0 then
  710. i:=length(path)+1;
  711. singlepathstring:=FixPath(copy(path,start,i-start));
  712. delete(path,start,i-start+1);
  713. { Check for PPL file }
  714. if not (cs_link_static in aktswitches) then
  715. begin
  716. Found:=UnitExists(target_info.unitlibext);
  717. if Found then
  718. Begin
  719. SetFileName(SinglePathString,FileName);
  720. Found:=Load_PPU(singlepathstring,filename,target_info.unitlibext);
  721. End;
  722. end;
  723. { Check for PPU file }
  724. if not (cs_link_dynamic in aktswitches) and not Found then
  725. begin
  726. Found:=UnitExists(target_info.unitext);
  727. if Found then
  728. Begin
  729. SetFileName(SinglePathString,FileName);
  730. Found:=Load_PPU(singlepathstring,filename,target_info.unitext);
  731. End;
  732. end;
  733. { Check for Sources }
  734. if not Found then
  735. begin
  736. ppufile:=nil;
  737. do_compile:=true;
  738. {Check for .pp file}
  739. Found:=UnitExists(target_os.sourceext);
  740. if Found then
  741. Ext:=target_os.sourceext
  742. else
  743. begin
  744. {Check for .pas}
  745. Found:=UnitExists(target_os.pasext);
  746. if Found then
  747. Ext:=target_os.pasext;
  748. end;
  749. stringdispose(mainsource);
  750. if Found then
  751. begin
  752. sources_avail:=true;
  753. {Load Filenames when found}
  754. mainsource:=StringDup(SinglePathString+FileName+Ext);
  755. SetFileName(SinglePathString,FileName);
  756. end
  757. else
  758. sources_avail:=false;
  759. end;
  760. until Found or (path='');
  761. end;
  762. {$endif NEWPPU}
  763. constructor tmodule.init(const s:string;_is_unit:boolean);
  764. var
  765. p : dirstr;
  766. n : namestr;
  767. e : extstr;
  768. begin
  769. FSplit(s,p,n,e);
  770. { Programs have the name program to don't conflict with dup id's }
  771. if _is_unit then
  772. modulename:=stringdup(Upper(n))
  773. else
  774. modulename:=stringdup('PROGRAM');
  775. mainsource:=stringdup(s);
  776. objfilename:=nil;
  777. asmfilename:=nil;
  778. libfilename:=nil;
  779. ppufilename:=nil;
  780. setfilename(p,n);
  781. used_units.init;
  782. sourcefiles.init;
  783. linkofiles.init;
  784. linkstaticlibs.init;
  785. linksharedlibs.init;
  786. ppufile:=nil;
  787. current_inputfile:=nil;
  788. map:=nil;
  789. symtable:=nil;
  790. flags:=0;
  791. crc:=0;
  792. unitcount:=1;
  793. inc(global_unit_count);
  794. unit_index:=global_unit_count;
  795. do_assemble:=false;
  796. do_compile:=false;
  797. sources_avail:=true;
  798. compiled:=false;
  799. in_implementation:=false;
  800. in_main:=false;
  801. is_unit:=_is_unit;
  802. uses_imports:=false;
  803. imports:=new(plinkedlist,init);
  804. { set smartlink flag }
  805. if (cs_smartlink in aktswitches) then
  806. flags:=flags or uf_smartlink;
  807. { search the PPU file if it is an unit }
  808. if is_unit then
  809. search_unit(modulename^);
  810. end;
  811. destructor tmodule.special_done;
  812. begin
  813. if assigned(map) then
  814. dispose(map);
  815. { cannot remove that because it is linked
  816. in the global chain of used_objects
  817. used_units.done; }
  818. sourcefiles.done;
  819. linkofiles.done;
  820. linkstaticlibs.done;
  821. linksharedlibs.done;
  822. if assigned(ppufile) then
  823. dispose(ppufile,done);
  824. if assigned(imports) then
  825. dispose(imports,done);
  826. inherited done;
  827. end;
  828. {****************************************************************************
  829. TUSED_UNIT
  830. ****************************************************************************}
  831. constructor tused_unit.init(_u : pmodule;f : byte);
  832. begin
  833. u:=_u;
  834. in_interface:=false;
  835. in_uses:=false;
  836. is_stab_written:=false;
  837. unitid:=f;
  838. end;
  839. destructor tused_unit.done;
  840. begin
  841. inherited done;
  842. end;
  843. end.
  844. {
  845. $Log$
  846. Revision 1.15 1998-05-28 14:37:53 peter
  847. * default programname is PROGRAM (like TP7) to avoid dup id's
  848. Revision 1.14 1998/05/27 19:45:02 peter
  849. * symtable.pas splitted into includefiles
  850. * symtable adapted for $ifdef NEWPPU
  851. Revision 1.13 1998/05/23 01:21:05 peter
  852. + aktasmmode, aktoptprocessor, aktoutputformat
  853. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  854. + $LIBNAME to set the library name where the unit will be put in
  855. * splitted cgi386 a bit (codeseg to large for bp7)
  856. * nasm, tasm works again. nasm moved to ag386nsm.pas
  857. Revision 1.12 1998/05/20 09:42:33 pierre
  858. + UseTokenInfo now default
  859. * unit in interface uses and implementation uses gives error now
  860. * only one error for unknown symbol (uses lastsymknown boolean)
  861. the problem came from the label code !
  862. + first inlined procedures and function work
  863. (warning there might be allowed cases were the result is still wrong !!)
  864. * UseBrower updated gives a global list of all position of all used symbols
  865. with switch -gb
  866. Revision 1.11 1998/05/12 10:46:59 peter
  867. * moved printstatus to verb_def
  868. + V_Normal which is between V_Error and V_Warning and doesn't have a
  869. prefix like error: warning: and is included in V_Default
  870. * fixed some messages
  871. * first time parameter scan is only for -v and -T
  872. - removed old style messages
  873. Revision 1.10 1998/05/11 13:07:53 peter
  874. + $ifdef NEWPPU for the new ppuformat
  875. + $define GDB not longer required
  876. * removed all warnings and stripped some log comments
  877. * no findfirst/findnext anymore to remove smartlink *.o files
  878. Revision 1.9 1998/05/06 15:04:20 pierre
  879. + when trying to find source files of a ppufile
  880. check the includepathlist for included files
  881. the main file must still be in the same directory
  882. Revision 1.8 1998/05/04 17:54:25 peter
  883. + smartlinking works (only case jumptable left todo)
  884. * redesign of systems.pas to support assemblers and linkers
  885. + Unitname is now also in the PPU-file, increased version to 14
  886. Revision 1.7 1998/05/01 16:38:44 florian
  887. * handling of private and protected fixed
  888. + change_keywords_to_tp implemented to remove
  889. keywords which aren't supported by tp
  890. * break and continue are now symbols of the system unit
  891. + widestring, longstring and ansistring type released
  892. Revision 1.6 1998/05/01 07:43:53 florian
  893. + basics for rtti implemented
  894. + switch $m (generate rtti for published sections)
  895. Revision 1.5 1998/04/30 15:59:40 pierre
  896. * GDB works again better :
  897. correct type info in one pass
  898. + UseTokenInfo for better source position
  899. * fixed one remaining bug in scanner for line counts
  900. * several little fixes
  901. Revision 1.4 1998/04/29 10:33:52 pierre
  902. + added some code for ansistring (not complete nor working yet)
  903. * corrected operator overloading
  904. * corrected nasm output
  905. + started inline procedures
  906. + added starstarn : use ** for exponentiation (^ gave problems)
  907. + started UseTokenInfo cond to get accurate positions
  908. Revision 1.3 1998/04/27 23:10:28 peter
  909. + new scanner
  910. * $makelib -> if smartlink
  911. * small filename fixes pmodule.setfilename
  912. * moved import from files.pas -> import.pas
  913. Revision 1.2 1998/04/21 10:16:47 peter
  914. * patches from strasbourg
  915. * objects is not used anymore in the fpc compiled version
  916. }