files.pas 42 KB

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