files.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  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 : 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. flags:=0;
  1037. crc:=0;
  1038. interface_crc:=0;
  1039. unitcount:=1;
  1040. recompile_reason:=rr_unknown;
  1041. end;
  1042. constructor tmodule.init(const s:string;_is_unit:boolean);
  1043. var
  1044. p : dirstr;
  1045. n : namestr;
  1046. e : extstr;
  1047. begin
  1048. FSplit(s,p,n,e);
  1049. { Programs have the name program to don't conflict with dup id's }
  1050. if _is_unit then
  1051. {$ifdef UNITALIASES}
  1052. modulename:=stringdup(GetUnitAlias(Upper(n)))
  1053. {$else}
  1054. modulename:=stringdup(Upper(n))
  1055. {$endif}
  1056. else
  1057. modulename:=stringdup('PROGRAM');
  1058. mainsource:=stringdup(s);
  1059. ppufilename:=nil;
  1060. objfilename:=nil;
  1061. asmfilename:=nil;
  1062. staticlibfilename:=nil;
  1063. sharedlibfilename:=nil;
  1064. exefilename:=nil;
  1065. { Dos has the famous 8.3 limit :( }
  1066. {$ifdef SHORTASMPREFIX}
  1067. asmprefix:=stringdup(FixFileName('as'));
  1068. {$else}
  1069. asmprefix:=stringdup(FixFileName(n));
  1070. {$endif}
  1071. path:=nil;
  1072. setfilename(p+n,true);
  1073. localunitsearchpath.init;
  1074. localobjectsearchpath.init;
  1075. localincludesearchpath.init;
  1076. locallibrarysearchpath.init;
  1077. used_units.init;
  1078. dependent_units.init;
  1079. new(sourcefiles,init);
  1080. resourcefiles.init;
  1081. linkunitofiles.init;
  1082. linkunitstaticlibs.init;
  1083. linkunitsharedlibs.init;
  1084. linkotherofiles.init;
  1085. linkotherstaticlibs.init;
  1086. linkothersharedlibs.init;
  1087. ppufile:=nil;
  1088. scanner:=nil;
  1089. map:=nil;
  1090. globalsymtable:=nil;
  1091. localsymtable:=nil;
  1092. loaded_from:=nil;
  1093. flags:=0;
  1094. crc:=0;
  1095. interface_crc:=0;
  1096. do_reload:=false;
  1097. unitcount:=1;
  1098. inc(global_unit_count);
  1099. unit_index:=global_unit_count;
  1100. do_assemble:=false;
  1101. do_compile:=false;
  1102. sources_avail:=true;
  1103. compiled:=false;
  1104. recompile_reason:=rr_unknown;
  1105. in_second_load:=false;
  1106. in_compile:=false;
  1107. in_second_compile:=false;
  1108. in_implementation:=false;
  1109. in_global:=true;
  1110. is_unit:=_is_unit;
  1111. islibrary:=false;
  1112. uses_imports:=false;
  1113. imports:=new(plinkedlist,init);
  1114. _exports:=new(plinkedlist,init);
  1115. { search the PPU file if it is an unit }
  1116. if is_unit then
  1117. search_unit(modulename^,false);
  1118. end;
  1119. destructor tmodule.done;
  1120. {$ifdef MEMDEBUG}
  1121. var
  1122. d : tmemdebug;
  1123. {$endif}
  1124. begin
  1125. if assigned(map) then
  1126. dispose(map);
  1127. if assigned(ppufile) then
  1128. dispose(ppufile,done);
  1129. ppufile:=nil;
  1130. if assigned(imports) then
  1131. dispose(imports,done);
  1132. imports:=nil;
  1133. if assigned(_exports) then
  1134. dispose(_exports,done);
  1135. _exports:=nil;
  1136. if assigned(scanner) then
  1137. pscannerfile(scanner)^.invalid:=true;
  1138. if assigned(sourcefiles) then
  1139. dispose(sourcefiles,done);
  1140. sourcefiles:=nil;
  1141. used_units.done;
  1142. dependent_units.done;
  1143. resourcefiles.done;
  1144. linkunitofiles.done;
  1145. linkunitstaticlibs.done;
  1146. linkunitsharedlibs.done;
  1147. linkotherofiles.done;
  1148. linkotherstaticlibs.done;
  1149. linkothersharedlibs.done;
  1150. stringdispose(objfilename);
  1151. stringdispose(asmfilename);
  1152. stringdispose(ppufilename);
  1153. stringdispose(staticlibfilename);
  1154. stringdispose(sharedlibfilename);
  1155. stringdispose(exefilename);
  1156. stringdispose(path);
  1157. stringdispose(modulename);
  1158. stringdispose(mainsource);
  1159. stringdispose(asmprefix);
  1160. localunitsearchpath.done;
  1161. localobjectsearchpath.done;
  1162. localincludesearchpath.done;
  1163. locallibrarysearchpath.done;
  1164. {$ifdef MEMDEBUG}
  1165. d.init('symtable');
  1166. {$endif}
  1167. if assigned(globalsymtable) then
  1168. dispose(punitsymtable(globalsymtable),done);
  1169. globalsymtable:=nil;
  1170. if assigned(localsymtable) then
  1171. dispose(punitsymtable(localsymtable),done);
  1172. localsymtable:=nil;
  1173. {$ifdef MEMDEBUG}
  1174. d.done;
  1175. {$endif}
  1176. inherited done;
  1177. end;
  1178. {****************************************************************************
  1179. TUSED_UNIT
  1180. ****************************************************************************}
  1181. constructor tused_unit.init(_u : pmodule;intface:boolean);
  1182. begin
  1183. u:=_u;
  1184. in_interface:=intface;
  1185. in_uses:=false;
  1186. is_stab_written:=false;
  1187. loaded:=true;
  1188. name:=stringdup(_u^.modulename^);
  1189. checksum:=_u^.crc;
  1190. interface_checksum:=_u^.interface_crc;
  1191. unitid:=0;
  1192. end;
  1193. constructor tused_unit.init_to_load(const n:string;c,intfc:longint;intface:boolean);
  1194. begin
  1195. u:=nil;
  1196. in_interface:=intface;
  1197. in_uses:=false;
  1198. is_stab_written:=false;
  1199. loaded:=false;
  1200. name:=stringdup(n);
  1201. checksum:=c;
  1202. interface_checksum:=intfc;
  1203. unitid:=0;
  1204. end;
  1205. destructor tused_unit.done;
  1206. begin
  1207. stringdispose(name);
  1208. inherited done;
  1209. end;
  1210. {****************************************************************************
  1211. TDENPENDENT_UNIT
  1212. ****************************************************************************}
  1213. constructor tdependent_unit.init(_u : pmodule);
  1214. begin
  1215. u:=_u;
  1216. end;
  1217. end.
  1218. {
  1219. $Log$
  1220. Revision 1.110 1999-11-16 23:39:04 peter
  1221. * use outputexedir for link.res location
  1222. Revision 1.109 1999/11/12 11:03:50 peter
  1223. * searchpaths changed to stringqueue object
  1224. Revision 1.108 1999/11/06 14:34:20 peter
  1225. * truncated log to 20 revs
  1226. Revision 1.107 1999/11/04 23:13:25 peter
  1227. * moved unit alias support into ifdef
  1228. Revision 1.106 1999/11/04 10:54:02 peter
  1229. + -Ua<oldname>=<newname> unit alias support
  1230. Revision 1.105 1999/10/28 13:14:00 pierre
  1231. * allow doubles in TLinkContainer needed for double libraries
  1232. Revision 1.104 1999/09/27 23:40:12 peter
  1233. * fixed macro within macro endless-loop
  1234. Revision 1.103 1999/09/16 08:00:50 pierre
  1235. + compiled_module to avoid wrong file info when load PPU files
  1236. Revision 1.102 1999/08/31 15:51:10 pierre
  1237. * in_second_compile cleaned up, in_compile and in_second_load added
  1238. Revision 1.101 1999/08/27 10:43:20 pierre
  1239. + interface CRC check with ifdef Test_double_checksum added
  1240. Revision 1.100 1999/08/24 13:14:01 peter
  1241. * MEMDEBUG to see the sizes of asmlist,asmsymbols,symtables
  1242. Revision 1.99 1999/07/18 14:47:26 florian
  1243. * bug 487 fixed, (inc(<property>) isn't allowed)
  1244. * more fixes to compile with Delphi
  1245. Revision 1.98 1999/07/18 10:19:51 florian
  1246. * made it compilable with Dlephi 4 again
  1247. + fixed problem with large stack allocations on win32
  1248. Revision 1.97 1999/07/14 21:19:03 florian
  1249. + implemented a better error message if a PPU file isn't found as suggested
  1250. by Lee John
  1251. Revision 1.96 1999/07/03 00:29:47 peter
  1252. * new link writing to the ppu, one .ppu is needed for all link types,
  1253. static (.o) is now always created also when smartlinking is used
  1254. Revision 1.95 1999/05/13 21:59:25 peter
  1255. * removed oldppu code
  1256. * warning if objpas is loaded from uses
  1257. * first things for new deref writing
  1258. Revision 1.94 1999/05/04 21:44:42 florian
  1259. * changes to compile it with Delphi 4.0
  1260. Revision 1.93 1999/04/26 13:31:29 peter
  1261. * release storenumber,double_checksum
  1262. Revision 1.92 1999/04/25 15:08:36 peter
  1263. * small fixes for double_checksum
  1264. Revision 1.91 1999/04/21 09:43:36 peter
  1265. * storenumber works
  1266. * fixed some typos in double_checksum
  1267. + incompatible types type1 and type2 message (with storenumber)
  1268. Revision 1.90 1999/04/14 09:14:48 peter
  1269. * first things to store the symbol/def number in the ppu
  1270. Revision 1.89 1999/04/07 15:39:29 pierre
  1271. + double_checksum code added
  1272. Revision 1.88 1999/03/25 16:55:29 peter
  1273. + unitpath,librarypath,includepath,objectpath directives
  1274. }