files.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. {$ifdef TP}
  21. {$V+}
  22. {$endif}
  23. {$ifdef TP}
  24. {$define SHORTASMPREFIX}
  25. {$endif}
  26. {$ifdef go32v1}
  27. {$define SHORTASMPREFIX}
  28. {$endif}
  29. {$ifdef go32v2}
  30. {$define SHORTASMPREFIX}
  31. {$endif}
  32. {$ifdef OS2}
  33. { Allthough OS/2 supports long filenames I play it safe and
  34. use 8.3 filenames, because this allows the compiler to run
  35. on a FAT partition. (DM) }
  36. {$define SHORTASMPREFIX}
  37. {$endif}
  38. interface
  39. uses
  40. globtype,
  41. cobjects,globals,ppu;
  42. const
  43. {$ifdef FPC}
  44. maxunits = 1024;
  45. InputFileBufSize=32*1024;
  46. linebufincrease=512;
  47. {$else}
  48. maxunits = 128;
  49. InputFileBufSize=1024;
  50. linebufincrease=64;
  51. {$endif}
  52. type
  53. trecompile_reason = (rr_unknown,rr_noppu,rr_sourcenewer,
  54. rr_build,rr_libolder,rr_objolder,rr_asmolder,rr_crcchanged);
  55. {$ifdef FPC}
  56. tlongintarr = array[0..1000000] of longint;
  57. {$else}
  58. tlongintarr = array[0..16000] of longint;
  59. {$endif}
  60. plongintarr = ^tlongintarr;
  61. pinputfile = ^tinputfile;
  62. tinputfile = object
  63. path,name : pstring; { path and filename }
  64. next : pinputfile; { next file for reading }
  65. f : file; { current file handle }
  66. is_macro,
  67. endoffile, { still bytes left to read }
  68. closed : boolean; { is the file closed }
  69. buf : pchar; { buffer }
  70. bufstart, { buffer start position in the file }
  71. bufsize, { amount of bytes in the buffer }
  72. maxbufsize : longint; { size in memory for the buffer }
  73. saveinputpointer : pchar; { save fields for scanner variables }
  74. savelastlinepos,
  75. saveline_no : longint;
  76. linebuf : plongintarr; { line buffer to retrieve lines }
  77. maxlinebuf : longint;
  78. ref_count : longint; { to handle the browser refs }
  79. ref_index : longint;
  80. ref_next : pinputfile;
  81. constructor init(const fn:string);
  82. destructor done;
  83. procedure setpos(l:longint);
  84. procedure seekbuf(fpos:longint);
  85. procedure readbuf;
  86. function open:boolean;
  87. procedure close;
  88. procedure tempclose;
  89. function tempopen:boolean;
  90. procedure setmacro(p:pchar;len:longint);
  91. procedure setline(line,linepos:longint);
  92. function getlinestr(l:longint):string;
  93. end;
  94. pfilemanager = ^tfilemanager;
  95. tfilemanager = object
  96. files : pinputfile;
  97. last_ref_index : longint;
  98. cacheindex : longint;
  99. cacheinputfile : pinputfile;
  100. constructor init;
  101. destructor done;
  102. procedure register_file(f : pinputfile);
  103. procedure inverse_register_indexes;
  104. function get_file(l:longint) : pinputfile;
  105. function get_file_name(l :longint):string;
  106. function get_file_path(l :longint):string;
  107. end;
  108. plinkcontaineritem=^tlinkcontaineritem;
  109. tlinkcontaineritem=object(tcontaineritem)
  110. data : pstring;
  111. needlink : longint;
  112. constructor init(const s:string;m:longint);
  113. destructor done;virtual;
  114. end;
  115. plinkcontainer=^tlinkcontainer;
  116. tlinkcontainer=object(tcontainer)
  117. constructor Init;
  118. procedure insert(const s : string;m:longint);
  119. function get(var m:longint) : string;
  120. function getusemask(mask:longint) : string;
  121. function find(const s:string):boolean;
  122. end;
  123. {$ifndef NEWMAP}
  124. tunitmap = array[0..maxunits-1] of pointer;
  125. punitmap = ^tunitmap;
  126. pmodule = ^tmodule;
  127. {$else NEWMAP}
  128. pmodule = ^tmodule;
  129. tunitmap = array[0..maxunits-1] of pmodule;
  130. punitmap = ^tunitmap;
  131. {$endif NEWMAP}
  132. tmodule = object(tlinkedlist_item)
  133. ppufile : pppufile; { the PPU file }
  134. crc,
  135. interface_crc,
  136. flags : longint; { the PPU flags }
  137. compiled, { unit is already compiled }
  138. do_reload, { force reloading of the unit }
  139. do_assemble, { only assemble the object, don't recompile }
  140. do_compile, { need to compile the sources }
  141. sources_avail, { if all sources are reachable }
  142. is_unit,
  143. in_compile, { is it being compiled ?? }
  144. in_second_compile, { is this unit being compiled for the 2nd time? }
  145. in_second_load, { is this unit PPU loaded a 2nd time? }
  146. in_implementation, { processing the implementation part? }
  147. in_global : boolean; { allow global settings }
  148. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  149. islibrary : boolean; { if it is a library (win32 dll) }
  150. map : punitmap; { mapping of all used units }
  151. unitcount : word; { local unit counter }
  152. unit_index : word; { global counter for browser }
  153. globalsymtable, { pointer to the local/static symtable of this unit }
  154. localsymtable : pointer; { pointer to the psymtable of this unit }
  155. scanner : pointer; { scanner object used }
  156. loaded_from : pmodule;
  157. uses_imports : boolean; { Set if the module imports from DLL's.}
  158. imports : plinkedlist;
  159. _exports : plinkedlist;
  160. sourcefiles : pfilemanager;
  161. resourcefiles : tstringcontainer;
  162. linkunitofiles,
  163. linkunitstaticlibs,
  164. linkunitsharedlibs,
  165. linkotherofiles, { objects,libs loaded from the source }
  166. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  167. linkotherstaticlibs : tlinkcontainer;
  168. used_units : tlinkedlist;
  169. dependent_units : tlinkedlist;
  170. localunitsearchpath, { local searchpaths }
  171. localobjectsearchpath,
  172. localincludesearchpath,
  173. locallibrarysearchpath : TSearchPathList;
  174. path, { path where the module is find/created }
  175. modulename, { name of the module in uppercase }
  176. objfilename, { fullname of the objectfile }
  177. asmfilename, { fullname of the assemblerfile }
  178. ppufilename, { fullname of the ppufile }
  179. staticlibfilename, { fullname of the static libraryfile }
  180. sharedlibfilename, { fullname of the shared libraryfile }
  181. exefilename, { fullname of the exefile }
  182. asmprefix, { prefix for the smartlink asmfiles }
  183. mainsource : pstring; { name of the main sourcefile }
  184. {$ifdef Test_Double_checksum}
  185. crc_array : pointer;
  186. crc_size : longint;
  187. crc_array2 : pointer;
  188. crc_size2 : longint;
  189. {$endif def Test_Double_checksum}
  190. constructor init(const s:string;_is_unit:boolean);
  191. destructor done;virtual;
  192. procedure reset;
  193. procedure setfilename(const fn:string;allowoutput:boolean);
  194. function openppu:boolean;
  195. function search_unit(const n : string;onlysource:boolean):boolean;
  196. end;
  197. pused_unit = ^tused_unit;
  198. tused_unit = object(tlinkedlist_item)
  199. unitid : word;
  200. name : pstring;
  201. checksum,
  202. interface_checksum : longint;
  203. loaded : boolean;
  204. in_uses,
  205. in_interface,
  206. is_stab_written : boolean;
  207. u : pmodule;
  208. constructor init(_u : pmodule;intface:boolean);
  209. constructor init_to_load(const n:string;c,intfc:longint;intface:boolean);
  210. destructor done;virtual;
  211. end;
  212. pdependent_unit = ^tdependent_unit;
  213. tdependent_unit = object(tlinkedlist_item)
  214. u : pmodule;
  215. constructor init(_u : pmodule);
  216. end;
  217. var
  218. main_module : pmodule; { Main module of the program }
  219. current_module : pmodule; { Current module which is compiled or loaded }
  220. compiled_module : pmodule; { Current module which is compiled }
  221. current_ppu : pppufile; { Current ppufile which is read }
  222. global_unit_count : word;
  223. usedunits : tlinkedlist; { Used units for this program }
  224. loaded_units : tlinkedlist; { All loaded units }
  225. function get_source_file(moduleindex,fileindex : word) : pinputfile;
  226. implementation
  227. uses
  228. {$ifdef Delphi}
  229. dmisc,
  230. {$else Delphi}
  231. dos,
  232. {$endif Delphi}
  233. verbose,systems,
  234. symtable,scanner;
  235. {****************************************************************************
  236. TINPUTFILE
  237. ****************************************************************************}
  238. constructor tinputfile.init(const fn:string);
  239. var
  240. p:dirstr;
  241. n:namestr;
  242. e:extstr;
  243. begin
  244. FSplit(fn,p,n,e);
  245. name:=stringdup(n+e);
  246. path:=stringdup(p);
  247. next:=nil;
  248. { file info }
  249. is_macro:=false;
  250. endoffile:=false;
  251. closed:=true;
  252. buf:=nil;
  253. bufstart:=0;
  254. bufsize:=0;
  255. maxbufsize:=InputFileBufSize;
  256. { save fields }
  257. saveinputpointer:=nil;
  258. saveline_no:=0;
  259. savelastlinepos:=0;
  260. { indexing refs }
  261. ref_next:=nil;
  262. ref_count:=0;
  263. ref_index:=0;
  264. { line buffer }
  265. linebuf:=nil;
  266. maxlinebuf:=0;
  267. end;
  268. destructor tinputfile.done;
  269. begin
  270. if not closed then
  271. close;
  272. stringdispose(path);
  273. stringdispose(name);
  274. { free memory }
  275. if assigned(linebuf) then
  276. freemem(linebuf,maxlinebuf shl 2);
  277. end;
  278. procedure tinputfile.setpos(l:longint);
  279. begin
  280. bufstart:=l;
  281. end;
  282. procedure tinputfile.seekbuf(fpos:longint);
  283. begin
  284. if closed then
  285. exit;
  286. seek(f,fpos);
  287. bufstart:=fpos;
  288. bufsize:=0;
  289. end;
  290. procedure tinputfile.readbuf;
  291. {$ifdef TP}
  292. var
  293. w : word;
  294. {$endif}
  295. begin
  296. if is_macro then
  297. endoffile:=true;
  298. if closed then
  299. exit;
  300. inc(bufstart,bufsize);
  301. {$ifdef VER70}
  302. blockread(f,buf^,maxbufsize-1,w);
  303. bufsize:=w;
  304. {$else}
  305. blockread(f,buf^,maxbufsize-1,bufsize);
  306. {$endif}
  307. buf[bufsize]:=#0;
  308. endoffile:=eof(f);
  309. end;
  310. function tinputfile.open:boolean;
  311. var
  312. ofm : byte;
  313. begin
  314. open:=false;
  315. if not closed then
  316. Close;
  317. ofm:=filemode;
  318. filemode:=0;
  319. Assign(f,path^+name^);
  320. {$I-}
  321. reset(f,1);
  322. {$I+}
  323. filemode:=ofm;
  324. if ioresult<>0 then
  325. exit;
  326. { file }
  327. endoffile:=false;
  328. closed:=false;
  329. Getmem(buf,MaxBufsize);
  330. bufstart:=0;
  331. bufsize:=0;
  332. open:=true;
  333. end;
  334. procedure tinputfile.close;
  335. var
  336. i : word;
  337. begin
  338. if is_macro then
  339. begin
  340. if assigned(buf) then
  341. Freemem(buf,maxbufsize);
  342. buf:=nil;
  343. {is_macro:=false;
  344. still needed for dispose in scanner PM }
  345. closed:=true;
  346. exit;
  347. end;
  348. if not closed then
  349. begin
  350. {$I-}
  351. system.close(f);
  352. {$I+}
  353. i:=ioresult;
  354. closed:=true;
  355. end;
  356. if assigned(buf) then
  357. begin
  358. Freemem(buf,maxbufsize);
  359. buf:=nil;
  360. end;
  361. bufstart:=0;
  362. end;
  363. procedure tinputfile.tempclose;
  364. var
  365. i : word;
  366. begin
  367. if is_macro then
  368. exit;
  369. if not closed then
  370. begin
  371. {$I-}
  372. system.close(f);
  373. {$I+}
  374. i:=ioresult;
  375. Freemem(buf,maxbufsize);
  376. buf:=nil;
  377. closed:=true;
  378. end;
  379. end;
  380. function tinputfile.tempopen:boolean;
  381. var
  382. ofm : byte;
  383. begin
  384. tempopen:=false;
  385. if is_macro then
  386. begin
  387. { seek buffer postion to bufstart }
  388. if bufstart>0 then
  389. begin
  390. move(buf[bufstart],buf[0],bufsize-bufstart+1);
  391. bufstart:=0;
  392. end;
  393. tempopen:=true;
  394. exit;
  395. end;
  396. if not closed then
  397. exit;
  398. ofm:=filemode;
  399. filemode:=0;
  400. Assign(f,path^+name^);
  401. {$I-}
  402. reset(f,1);
  403. {$I+}
  404. filemode:=ofm;
  405. if ioresult<>0 then
  406. exit;
  407. closed:=false;
  408. { get new mem }
  409. Getmem(buf,maxbufsize);
  410. { restore state }
  411. seek(f,BufStart);
  412. bufsize:=0;
  413. readbuf;
  414. tempopen:=true;
  415. end;
  416. procedure tinputfile.setmacro(p:pchar;len:longint);
  417. begin
  418. { create new buffer }
  419. getmem(buf,len+1);
  420. move(p^,buf^,len);
  421. buf[len]:=#0;
  422. { reset }
  423. bufstart:=0;
  424. bufsize:=len;
  425. maxbufsize:=len+1;
  426. is_macro:=true;
  427. endoffile:=true;
  428. closed:=true;
  429. end;
  430. procedure tinputfile.setline(line,linepos:longint);
  431. var
  432. oldlinebuf : plongintarr;
  433. begin
  434. if line<1 then
  435. exit;
  436. while (line>=maxlinebuf) do
  437. begin
  438. oldlinebuf:=linebuf;
  439. { create new linebuf and move old info }
  440. getmem(linebuf,(maxlinebuf+linebufincrease) shl 2);
  441. if assigned(oldlinebuf) then
  442. begin
  443. move(oldlinebuf^,linebuf^,maxlinebuf shl 2);
  444. freemem(oldlinebuf,maxlinebuf shl 2);
  445. end;
  446. fillchar(linebuf^[maxlinebuf],linebufincrease shl 2,0);
  447. inc(maxlinebuf,linebufincrease);
  448. end;
  449. linebuf^[line]:=linepos;
  450. end;
  451. function tinputfile.getlinestr(l:longint):string;
  452. var
  453. c : char;
  454. i,
  455. fpos : longint;
  456. p : pchar;
  457. begin
  458. getlinestr:='';
  459. if l<maxlinebuf then
  460. begin
  461. fpos:=linebuf^[l];
  462. { fpos is set negativ if the line was already written }
  463. { but we still know the correct value }
  464. if fpos<0 then
  465. fpos:=-fpos+1;
  466. if closed then
  467. open;
  468. { in current buf ? }
  469. if (fpos<bufstart) or (fpos>bufstart+bufsize) then
  470. begin
  471. seekbuf(fpos);
  472. readbuf;
  473. end;
  474. { the begin is in the buf now simply read until #13,#10 }
  475. i:=0;
  476. p:=@buf[fpos-bufstart];
  477. repeat
  478. c:=p^;
  479. if c=#0 then
  480. begin
  481. if endoffile then
  482. break;
  483. readbuf;
  484. p:=buf;
  485. c:=p^;
  486. end;
  487. if c in [#10,#13] then
  488. break;
  489. inc(i);
  490. getlinestr[i]:=c;
  491. inc(longint(p));
  492. until (i=255);
  493. {$ifndef TP}
  494. {$ifopt H+}
  495. setlength(getlinestr,i);
  496. {$else}
  497. getlinestr[0]:=chr(i);
  498. {$endif}
  499. {$else}
  500. getlinestr[0]:=chr(i);
  501. {$endif}
  502. end;
  503. end;
  504. {****************************************************************************
  505. TFILEMANAGER
  506. ****************************************************************************}
  507. constructor tfilemanager.init;
  508. begin
  509. files:=nil;
  510. last_ref_index:=0;
  511. cacheindex:=0;
  512. cacheinputfile:=nil;
  513. end;
  514. destructor tfilemanager.done;
  515. var
  516. hp : pinputfile;
  517. begin
  518. hp:=files;
  519. while assigned(hp) do
  520. begin
  521. files:=files^.ref_next;
  522. dispose(hp,done);
  523. hp:=files;
  524. end;
  525. last_ref_index:=0;
  526. end;
  527. procedure tfilemanager.register_file(f : pinputfile);
  528. begin
  529. { don't register macro's }
  530. if f^.is_macro then
  531. exit;
  532. inc(last_ref_index);
  533. f^.ref_next:=files;
  534. f^.ref_index:=last_ref_index;
  535. files:=f;
  536. { update cache }
  537. cacheindex:=last_ref_index;
  538. cacheinputfile:=f;
  539. {$ifdef FPC}
  540. {$ifdef heaptrc}
  541. writeln(stderr,f^.name^,' index ',current_module^.unit_index*100000+f^.ref_index);
  542. {$endif heaptrc}
  543. {$endif FPC}
  544. end;
  545. { this procedure is necessary after loading the
  546. sources files from a PPU file PM }
  547. procedure tfilemanager.inverse_register_indexes;
  548. var
  549. f : pinputfile;
  550. begin
  551. f:=files;
  552. while assigned(f) do
  553. begin
  554. f^.ref_index:=last_ref_index-f^.ref_index+1;
  555. f:=f^.ref_next;
  556. end;
  557. { reset cache }
  558. cacheindex:=0;
  559. cacheinputfile:=nil;
  560. end;
  561. function tfilemanager.get_file(l :longint) : pinputfile;
  562. var
  563. ff : pinputfile;
  564. begin
  565. { check cache }
  566. if (l=cacheindex) and assigned(cacheinputfile) then
  567. begin
  568. get_file:=cacheinputfile;
  569. exit;
  570. end;
  571. ff:=files;
  572. while assigned(ff) and (ff^.ref_index<>l) do
  573. ff:=ff^.ref_next;
  574. get_file:=ff;
  575. end;
  576. function tfilemanager.get_file_name(l :longint):string;
  577. var
  578. hp : pinputfile;
  579. begin
  580. hp:=get_file(l);
  581. if assigned(hp) then
  582. get_file_name:=hp^.name^
  583. else
  584. get_file_name:='';
  585. end;
  586. function tfilemanager.get_file_path(l :longint):string;
  587. var
  588. hp : pinputfile;
  589. begin
  590. hp:=get_file(l);
  591. if assigned(hp) then
  592. get_file_path:=hp^.path^
  593. else
  594. get_file_path:='';
  595. end;
  596. function get_source_file(moduleindex,fileindex : word) : pinputfile;
  597. var
  598. hp : pmodule;
  599. f : pinputfile;
  600. begin
  601. hp:=pmodule(loaded_units.first);
  602. while assigned(hp) and (hp^.unit_index<>moduleindex) do
  603. hp:=pmodule(hp^.next);
  604. get_source_file:=nil;
  605. if not assigned(hp) then
  606. exit;
  607. f:=pinputfile(hp^.sourcefiles^.files);
  608. while assigned(f) do
  609. begin
  610. if f^.ref_index=fileindex then
  611. begin
  612. get_source_file:=f;
  613. exit;
  614. end;
  615. f:=pinputfile(f^.ref_next);
  616. end;
  617. end;
  618. {****************************************************************************
  619. TLinkContainerItem
  620. ****************************************************************************}
  621. constructor TLinkContainerItem.Init(const s:string;m:longint);
  622. begin
  623. inherited Init;
  624. data:=stringdup(s);
  625. needlink:=m;
  626. end;
  627. destructor TLinkContainerItem.Done;
  628. begin
  629. stringdispose(data);
  630. end;
  631. {****************************************************************************
  632. TLinkContainer
  633. ****************************************************************************}
  634. constructor TLinkContainer.Init;
  635. begin
  636. inherited init;
  637. end;
  638. procedure TLinkContainer.insert(const s : string;m:longint);
  639. var
  640. newnode : plinkcontaineritem;
  641. begin
  642. {if find(s) then
  643. exit; }
  644. new(newnode,init(s,m));
  645. inherited insert(newnode);
  646. end;
  647. function TLinkContainer.get(var m:longint) : string;
  648. var
  649. p : plinkcontaineritem;
  650. begin
  651. p:=plinkcontaineritem(inherited get);
  652. if p=nil then
  653. begin
  654. get:='';
  655. m:=0;
  656. exit;
  657. end;
  658. get:=p^.data^;
  659. m:=p^.needlink;
  660. dispose(p,done);
  661. end;
  662. function TLinkContainer.getusemask(mask:longint) : string;
  663. var
  664. p : plinkcontaineritem;
  665. found : boolean;
  666. begin
  667. found:=false;
  668. repeat
  669. p:=plinkcontaineritem(inherited get);
  670. if p=nil then
  671. begin
  672. getusemask:='';
  673. exit;
  674. end;
  675. getusemask:=p^.data^;
  676. found:=(p^.needlink and mask)<>0;
  677. dispose(p,done);
  678. until found;
  679. end;
  680. function TLinkContainer.find(const s:string):boolean;
  681. var
  682. newnode : plinkcontaineritem;
  683. begin
  684. find:=false;
  685. newnode:=plinkcontaineritem(root);
  686. while assigned(newnode) do
  687. begin
  688. if newnode^.data^=s then
  689. begin
  690. find:=true;
  691. exit;
  692. end;
  693. newnode:=plinkcontaineritem(newnode^.next);
  694. end;
  695. end;
  696. {****************************************************************************
  697. TMODULE
  698. ****************************************************************************}
  699. procedure tmodule.setfilename(const fn:string;allowoutput:boolean);
  700. var
  701. p : dirstr;
  702. n : NameStr;
  703. e : ExtStr;
  704. begin
  705. stringdispose(objfilename);
  706. stringdispose(asmfilename);
  707. stringdispose(ppufilename);
  708. stringdispose(staticlibfilename);
  709. stringdispose(sharedlibfilename);
  710. stringdispose(exefilename);
  711. stringdispose(path);
  712. { Create names }
  713. fsplit(fn,p,n,e);
  714. n:=FixFileName(n);
  715. { set path }
  716. path:=stringdup(FixPath(p,false));
  717. { obj,asm,ppu names }
  718. p:=path^;
  719. if AllowOutput then
  720. begin
  721. if (OutputUnitDir<>'') then
  722. p:=OutputUnitDir
  723. else
  724. if (OutputExeDir<>'') then
  725. p:=OutputExeDir;
  726. end;
  727. objfilename:=stringdup(p+n+target_info.objext);
  728. asmfilename:=stringdup(p+n+target_info.asmext);
  729. ppufilename:=stringdup(p+n+target_info.unitext);
  730. { lib and exe could be loaded with a file specified with -o }
  731. if AllowOutput and (OutputFile<>'') then
  732. n:=OutputFile;
  733. staticlibfilename:=stringdup(p+target_os.libprefix+n+target_os.staticlibext);
  734. if target_info.target=target_i386_WIN32 then
  735. sharedlibfilename:=stringdup(p+n+target_os.sharedlibext)
  736. else
  737. sharedlibfilename:=stringdup(p+target_os.libprefix+n+target_os.sharedlibext);
  738. { output dir of exe can be specified separatly }
  739. if AllowOutput and (OutputExeDir<>'') then
  740. p:=OutputExeDir
  741. else
  742. p:=path^;
  743. exefilename:=stringdup(p+n+target_info.exeext);
  744. end;
  745. function tmodule.openppu:boolean;
  746. var
  747. objfiletime,
  748. ppufiletime,
  749. asmfiletime : longint;
  750. begin
  751. openppu:=false;
  752. Message1(unit_t_ppu_loading,ppufilename^);
  753. { Get ppufile time (also check if the file exists) }
  754. ppufiletime:=getnamedfiletime(ppufilename^);
  755. if ppufiletime=-1 then
  756. exit;
  757. { Open the ppufile }
  758. Message1(unit_u_ppu_name,ppufilename^);
  759. ppufile:=new(pppufile,init(ppufilename^));
  760. ppufile^.change_endian:=source_os.endian<>target_os.endian;
  761. if not ppufile^.open then
  762. begin
  763. dispose(ppufile,done);
  764. Message(unit_u_ppu_file_too_short);
  765. exit;
  766. end;
  767. { check for a valid PPU file }
  768. if not ppufile^.CheckPPUId then
  769. begin
  770. dispose(ppufile,done);
  771. Message(unit_u_ppu_invalid_header);
  772. exit;
  773. end;
  774. { check for allowed PPU versions }
  775. if not (ppufile^.GetPPUVersion = CurrentPPUVersion) then
  776. begin
  777. dispose(ppufile,done);
  778. Message1(unit_u_ppu_invalid_version,tostr(ppufile^.GetPPUVersion));
  779. exit;
  780. end;
  781. { check the target processor }
  782. if ttargetcpu(ppufile^.header.cpu)<>target_cpu then
  783. begin
  784. dispose(ppufile,done);
  785. Message(unit_u_ppu_invalid_processor);
  786. exit;
  787. end;
  788. { check target }
  789. if ttarget(ppufile^.header.target)<>target_info.target then
  790. begin
  791. dispose(ppufile,done);
  792. Message(unit_u_ppu_invalid_target);
  793. exit;
  794. end;
  795. { Load values to be access easier }
  796. flags:=ppufile^.header.flags;
  797. crc:=ppufile^.header.checksum;
  798. interface_crc:=ppufile^.header.interface_checksum;
  799. { Show Debug info }
  800. Message1(unit_u_ppu_time,filetimestring(ppufiletime));
  801. Message1(unit_u_ppu_flags,tostr(flags));
  802. Message1(unit_u_ppu_crc,tostr(ppufile^.header.checksum));
  803. Message1(unit_u_ppu_crc,tostr(ppufile^.header.interface_checksum)+' (intfc)');
  804. { check the object and assembler file to see if we need only to
  805. assemble, only if it's not in a library }
  806. do_compile:=false;
  807. if (flags and uf_in_library)=0 then
  808. begin
  809. if (flags and uf_smart_linked)<>0 then
  810. begin
  811. objfiletime:=getnamedfiletime(staticlibfilename^);
  812. Message2(unit_u_check_time,staticlibfilename^,filetimestring(objfiletime));
  813. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  814. begin
  815. recompile_reason:=rr_libolder;
  816. Message(unit_u_recompile_staticlib_is_older);
  817. do_compile:=true;
  818. exit;
  819. end;
  820. end;
  821. if (flags and uf_static_linked)<>0 then
  822. begin
  823. { the objectfile should be newer than the ppu file }
  824. objfiletime:=getnamedfiletime(objfilename^);
  825. Message2(unit_u_check_time,objfilename^,filetimestring(objfiletime));
  826. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  827. begin
  828. { check if assembler file is older than ppu file }
  829. asmfileTime:=GetNamedFileTime(asmfilename^);
  830. Message2(unit_u_check_time,asmfilename^,filetimestring(asmfiletime));
  831. if (asmfiletime<0) or (ppufiletime>asmfiletime) then
  832. begin
  833. Message(unit_u_recompile_obj_and_asm_older);
  834. recompile_reason:=rr_objolder;
  835. do_compile:=true;
  836. exit;
  837. end
  838. else
  839. begin
  840. Message(unit_u_recompile_obj_older_than_asm);
  841. if not(cs_asm_extern in aktglobalswitches) then
  842. begin
  843. do_compile:=true;
  844. recompile_reason:=rr_asmolder;
  845. exit;
  846. end;
  847. end;
  848. end;
  849. end;
  850. end;
  851. openppu:=true;
  852. end;
  853. function tmodule.search_unit(const n : string;onlysource:boolean):boolean;
  854. var
  855. singlepathstring,
  856. filename : string;
  857. Function UnitExists(const ext:string):boolean;
  858. begin
  859. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  860. UnitExists:=FileExists(Singlepathstring+FileName+ext);
  861. end;
  862. Function SearchPath(const s:string):boolean;
  863. var
  864. found : boolean;
  865. ext : string[8];
  866. begin
  867. Found:=false;
  868. singlepathstring:=FixPath(s,false);
  869. if not onlysource then
  870. begin
  871. { Check for PPL file }
  872. if not Found then
  873. begin
  874. Found:=UnitExists(target_info.unitlibext);
  875. if Found then
  876. Begin
  877. SetFileName(SinglePathString+FileName,false);
  878. Found:=OpenPPU;
  879. End;
  880. end;
  881. { Check for PPU file }
  882. if not Found then
  883. begin
  884. Found:=UnitExists(target_info.unitext);
  885. if Found then
  886. Begin
  887. SetFileName(SinglePathString+FileName,false);
  888. Found:=OpenPPU;
  889. End;
  890. end;
  891. end;
  892. { Check for Sources }
  893. if not Found then
  894. begin
  895. ppufile:=nil;
  896. do_compile:=true;
  897. recompile_reason:=rr_noppu;
  898. {Check for .pp file}
  899. Found:=UnitExists(target_os.sourceext);
  900. if Found then
  901. Ext:=target_os.sourceext
  902. else
  903. begin
  904. {Check for .pas}
  905. Found:=UnitExists(target_os.pasext);
  906. if Found then
  907. Ext:=target_os.pasext;
  908. end;
  909. stringdispose(mainsource);
  910. if Found then
  911. begin
  912. sources_avail:=true;
  913. {Load Filenames when found}
  914. mainsource:=StringDup(SinglePathString+FileName+Ext);
  915. SetFileName(SinglePathString+FileName,false);
  916. end
  917. else
  918. sources_avail:=false;
  919. end;
  920. SearchPath:=Found;
  921. end;
  922. Function SearchPathList(list:TSearchPathList):boolean;
  923. var
  924. hp : PStringQueueItem;
  925. found : boolean;
  926. begin
  927. found:=false;
  928. hp:=list.First;
  929. while assigned(hp) do
  930. begin
  931. found:=SearchPath(hp^.data^);
  932. if found then
  933. break;
  934. hp:=hp^.next;
  935. end;
  936. SearchPathList:=found;
  937. end;
  938. var
  939. fnd : boolean;
  940. begin
  941. filename:=FixFileName(n);
  942. { try to find unit
  943. 1. cwd
  944. 2. local unit path
  945. 3. global unit path }
  946. fnd:=SearchPath('.');
  947. if (not fnd) then
  948. fnd:=SearchPathList(current_module^.LocalUnitSearchPath);
  949. if (not fnd) then
  950. fnd:=SearchPathList(UnitSearchPath);
  951. { try to find a file with the first 8 chars of the modulename, like
  952. dos }
  953. if (not fnd) and (length(filename)>8) then
  954. begin
  955. filename:=copy(filename,1,8);
  956. fnd:=SearchPath('.');
  957. if (not fnd) then
  958. fnd:=SearchPathList(current_module^.LocalUnitSearchPath);
  959. if not fnd then
  960. fnd:=SearchPathList(UnitSearchPath);
  961. end;
  962. search_unit:=fnd;
  963. end;
  964. procedure tmodule.reset;
  965. var
  966. pm : pdependent_unit;
  967. begin
  968. if assigned(scanner) then
  969. pscannerfile(scanner)^.invalid:=true;
  970. if assigned(globalsymtable) then
  971. begin
  972. dispose(punitsymtable(globalsymtable),done);
  973. globalsymtable:=nil;
  974. end;
  975. if assigned(localsymtable) then
  976. begin
  977. dispose(punitsymtable(localsymtable),done);
  978. localsymtable:=nil;
  979. end;
  980. if assigned(map) then
  981. begin
  982. dispose(map);
  983. map:=nil;
  984. end;
  985. if assigned(ppufile) then
  986. begin
  987. dispose(ppufile,done);
  988. ppufile:=nil;
  989. end;
  990. sourcefiles^.done;
  991. sourcefiles^.init;
  992. imports^.done;
  993. imports^.init;
  994. _exports^.done;
  995. _exports^.init;
  996. used_units.done;
  997. used_units.init;
  998. { all units that depend on this one must be recompiled ! }
  999. pm:=pdependent_unit(dependent_units.first);
  1000. while assigned(pm) do
  1001. begin
  1002. if pm^.u^.in_second_compile then
  1003. Comment(v_debug,'No reload already in second compile: '+pm^.u^.modulename^)
  1004. else
  1005. begin
  1006. pm^.u^.do_reload:=true;
  1007. Comment(v_debug,'Reloading '+pm^.u^.modulename^+' needed because '+modulename^+' is reloaded');
  1008. end;
  1009. pm:=pdependent_unit(pm^.next);
  1010. end;
  1011. dependent_units.done;
  1012. dependent_units.init;
  1013. resourcefiles.done;
  1014. resourcefiles.init;
  1015. linkunitofiles.done;
  1016. linkunitofiles.init;
  1017. linkunitstaticlibs.done;
  1018. linkunitstaticlibs.init;
  1019. linkunitsharedlibs.done;
  1020. linkunitsharedlibs.init;
  1021. linkotherofiles.done;
  1022. linkotherofiles.init;
  1023. linkotherstaticlibs.done;
  1024. linkotherstaticlibs.init;
  1025. linkothersharedlibs.done;
  1026. linkothersharedlibs.init;
  1027. uses_imports:=false;
  1028. do_assemble:=false;
  1029. do_compile:=false;
  1030. { sources_avail:=true;
  1031. should not be changed PM }
  1032. compiled:=false;
  1033. in_implementation:=false;
  1034. in_global:=true;
  1035. {loaded_from:=nil;
  1036. should not be changed PFV }
  1037. flags:=0;
  1038. crc:=0;
  1039. interface_crc:=0;
  1040. unitcount:=1;
  1041. recompile_reason:=rr_unknown;
  1042. end;
  1043. constructor tmodule.init(const s:string;_is_unit:boolean);
  1044. var
  1045. p : dirstr;
  1046. n : namestr;
  1047. e : extstr;
  1048. begin
  1049. FSplit(s,p,n,e);
  1050. { Programs have the name program to don't conflict with dup id's }
  1051. if _is_unit then
  1052. {$ifdef UNITALIASES}
  1053. modulename:=stringdup(GetUnitAlias(Upper(n)))
  1054. {$else}
  1055. modulename:=stringdup(Upper(n))
  1056. {$endif}
  1057. else
  1058. modulename:=stringdup('PROGRAM');
  1059. mainsource:=stringdup(s);
  1060. ppufilename:=nil;
  1061. objfilename:=nil;
  1062. asmfilename:=nil;
  1063. staticlibfilename:=nil;
  1064. sharedlibfilename:=nil;
  1065. exefilename:=nil;
  1066. { Dos has the famous 8.3 limit :( }
  1067. {$ifdef SHORTASMPREFIX}
  1068. asmprefix:=stringdup(FixFileName('as'));
  1069. {$else}
  1070. asmprefix:=stringdup(FixFileName(n));
  1071. {$endif}
  1072. path:=nil;
  1073. setfilename(p+n,true);
  1074. localunitsearchpath.init;
  1075. localobjectsearchpath.init;
  1076. localincludesearchpath.init;
  1077. locallibrarysearchpath.init;
  1078. used_units.init;
  1079. dependent_units.init;
  1080. new(sourcefiles,init);
  1081. resourcefiles.init;
  1082. linkunitofiles.init;
  1083. linkunitstaticlibs.init;
  1084. linkunitsharedlibs.init;
  1085. linkotherofiles.init;
  1086. linkotherstaticlibs.init;
  1087. linkothersharedlibs.init;
  1088. ppufile:=nil;
  1089. scanner:=nil;
  1090. map:=nil;
  1091. globalsymtable:=nil;
  1092. localsymtable:=nil;
  1093. loaded_from:=nil;
  1094. flags:=0;
  1095. crc:=0;
  1096. interface_crc:=0;
  1097. do_reload:=false;
  1098. unitcount:=1;
  1099. inc(global_unit_count);
  1100. unit_index:=global_unit_count;
  1101. do_assemble:=false;
  1102. do_compile:=false;
  1103. sources_avail:=true;
  1104. compiled:=false;
  1105. recompile_reason:=rr_unknown;
  1106. in_second_load:=false;
  1107. in_compile:=false;
  1108. in_second_compile:=false;
  1109. in_implementation:=false;
  1110. in_global:=true;
  1111. is_unit:=_is_unit;
  1112. islibrary:=false;
  1113. uses_imports:=false;
  1114. imports:=new(plinkedlist,init);
  1115. _exports:=new(plinkedlist,init);
  1116. { search the PPU file if it is an unit }
  1117. if is_unit then
  1118. search_unit(modulename^,false);
  1119. end;
  1120. destructor tmodule.done;
  1121. {$ifdef MEMDEBUG}
  1122. var
  1123. d : tmemdebug;
  1124. {$endif}
  1125. begin
  1126. if assigned(map) then
  1127. dispose(map);
  1128. if assigned(ppufile) then
  1129. dispose(ppufile,done);
  1130. ppufile:=nil;
  1131. if assigned(imports) then
  1132. dispose(imports,done);
  1133. imports:=nil;
  1134. if assigned(_exports) then
  1135. dispose(_exports,done);
  1136. _exports:=nil;
  1137. if assigned(scanner) then
  1138. pscannerfile(scanner)^.invalid:=true;
  1139. if assigned(sourcefiles) then
  1140. dispose(sourcefiles,done);
  1141. sourcefiles:=nil;
  1142. used_units.done;
  1143. dependent_units.done;
  1144. resourcefiles.done;
  1145. linkunitofiles.done;
  1146. linkunitstaticlibs.done;
  1147. linkunitsharedlibs.done;
  1148. linkotherofiles.done;
  1149. linkotherstaticlibs.done;
  1150. linkothersharedlibs.done;
  1151. stringdispose(objfilename);
  1152. stringdispose(asmfilename);
  1153. stringdispose(ppufilename);
  1154. stringdispose(staticlibfilename);
  1155. stringdispose(sharedlibfilename);
  1156. stringdispose(exefilename);
  1157. stringdispose(path);
  1158. stringdispose(modulename);
  1159. stringdispose(mainsource);
  1160. stringdispose(asmprefix);
  1161. localunitsearchpath.done;
  1162. localobjectsearchpath.done;
  1163. localincludesearchpath.done;
  1164. locallibrarysearchpath.done;
  1165. {$ifdef MEMDEBUG}
  1166. d.init('symtable');
  1167. {$endif}
  1168. if assigned(globalsymtable) then
  1169. dispose(punitsymtable(globalsymtable),done);
  1170. globalsymtable:=nil;
  1171. if assigned(localsymtable) then
  1172. dispose(punitsymtable(localsymtable),done);
  1173. localsymtable:=nil;
  1174. {$ifdef MEMDEBUG}
  1175. d.done;
  1176. {$endif}
  1177. inherited done;
  1178. end;
  1179. {****************************************************************************
  1180. TUSED_UNIT
  1181. ****************************************************************************}
  1182. constructor tused_unit.init(_u : pmodule;intface:boolean);
  1183. begin
  1184. u:=_u;
  1185. in_interface:=intface;
  1186. in_uses:=false;
  1187. is_stab_written:=false;
  1188. loaded:=true;
  1189. name:=stringdup(_u^.modulename^);
  1190. checksum:=_u^.crc;
  1191. interface_checksum:=_u^.interface_crc;
  1192. unitid:=0;
  1193. end;
  1194. constructor tused_unit.init_to_load(const n:string;c,intfc:longint;intface:boolean);
  1195. begin
  1196. u:=nil;
  1197. in_interface:=intface;
  1198. in_uses:=false;
  1199. is_stab_written:=false;
  1200. loaded:=false;
  1201. name:=stringdup(n);
  1202. checksum:=c;
  1203. interface_checksum:=intfc;
  1204. unitid:=0;
  1205. end;
  1206. destructor tused_unit.done;
  1207. begin
  1208. stringdispose(name);
  1209. inherited done;
  1210. end;
  1211. {****************************************************************************
  1212. TDENPENDENT_UNIT
  1213. ****************************************************************************}
  1214. constructor tdependent_unit.init(_u : pmodule);
  1215. begin
  1216. u:=_u;
  1217. end;
  1218. end.
  1219. {
  1220. $Log$
  1221. Revision 1.112 2000-01-07 01:14:27 peter
  1222. * updated copyright to 2000
  1223. Revision 1.111 1999/12/08 01:01:11 peter
  1224. * fixed circular unit reference checking. loaded_from was reset after
  1225. reseting a unit, so no loaded_from info was available anymore.
  1226. Revision 1.110 1999/11/16 23:39:04 peter
  1227. * use outputexedir for link.res location
  1228. Revision 1.109 1999/11/12 11:03:50 peter
  1229. * searchpaths changed to stringqueue object
  1230. Revision 1.108 1999/11/06 14:34:20 peter
  1231. * truncated log to 20 revs
  1232. Revision 1.107 1999/11/04 23:13:25 peter
  1233. * moved unit alias support into ifdef
  1234. Revision 1.106 1999/11/04 10:54:02 peter
  1235. + -Ua<oldname>=<newname> unit alias support
  1236. Revision 1.105 1999/10/28 13:14:00 pierre
  1237. * allow doubles in TLinkContainer needed for double libraries
  1238. Revision 1.104 1999/09/27 23:40:12 peter
  1239. * fixed macro within macro endless-loop
  1240. Revision 1.103 1999/09/16 08:00:50 pierre
  1241. + compiled_module to avoid wrong file info when load PPU files
  1242. Revision 1.102 1999/08/31 15:51:10 pierre
  1243. * in_second_compile cleaned up, in_compile and in_second_load added
  1244. Revision 1.101 1999/08/27 10:43:20 pierre
  1245. + interface CRC check with ifdef Test_double_checksum added
  1246. Revision 1.100 1999/08/24 13:14:01 peter
  1247. * MEMDEBUG to see the sizes of asmlist,asmsymbols,symtables
  1248. Revision 1.99 1999/07/18 14:47:26 florian
  1249. * bug 487 fixed, (inc(<property>) isn't allowed)
  1250. * more fixes to compile with Delphi
  1251. Revision 1.98 1999/07/18 10:19:51 florian
  1252. * made it compilable with Dlephi 4 again
  1253. + fixed problem with large stack allocations on win32
  1254. Revision 1.97 1999/07/14 21:19:03 florian
  1255. + implemented a better error message if a PPU file isn't found as suggested
  1256. by Lee John
  1257. Revision 1.96 1999/07/03 00:29:47 peter
  1258. * new link writing to the ppu, one .ppu is needed for all link types,
  1259. static (.o) is now always created also when smartlinking is used
  1260. Revision 1.95 1999/05/13 21:59:25 peter
  1261. * removed oldppu code
  1262. * warning if objpas is loaded from uses
  1263. * first things for new deref writing
  1264. Revision 1.94 1999/05/04 21:44:42 florian
  1265. * changes to compile it with Delphi 4.0
  1266. Revision 1.93 1999/04/26 13:31:29 peter
  1267. * release storenumber,double_checksum
  1268. Revision 1.92 1999/04/25 15:08:36 peter
  1269. * small fixes for double_checksum
  1270. Revision 1.91 1999/04/21 09:43:36 peter
  1271. * storenumber works
  1272. * fixed some typos in double_checksum
  1273. + incompatible types type1 and type2 message (with storenumber)
  1274. Revision 1.90 1999/04/14 09:14:48 peter
  1275. * first things to store the symbol/def number in the ppu
  1276. Revision 1.89 1999/04/07 15:39:29 pierre
  1277. + double_checksum code added
  1278. Revision 1.88 1999/03/25 16:55:29 peter
  1279. + unitpath,librarypath,includepath,objectpath directives
  1280. }