files.pas 38 KB

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