files.pas 41 KB

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