files.pas 37 KB

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