files.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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.114 2000-02-09 13:22:52 peter
  1229. * log truncated
  1230. Revision 1.113 2000/01/11 09:52:06 peter
  1231. * fixed placing of .sl directories
  1232. * use -b again for base-file selection
  1233. * fixed group writing for linux with smartlinking
  1234. Revision 1.112 2000/01/07 01:14:27 peter
  1235. * updated copyright to 2000
  1236. Revision 1.111 1999/12/08 01:01:11 peter
  1237. * fixed circular unit reference checking. loaded_from was reset after
  1238. reseting a unit, so no loaded_from info was available anymore.
  1239. Revision 1.110 1999/11/16 23:39:04 peter
  1240. * use outputexedir for link.res location
  1241. Revision 1.109 1999/11/12 11:03:50 peter
  1242. * searchpaths changed to stringqueue object
  1243. Revision 1.108 1999/11/06 14:34:20 peter
  1244. * truncated log to 20 revs
  1245. Revision 1.107 1999/11/04 23:13:25 peter
  1246. * moved unit alias support into ifdef
  1247. Revision 1.106 1999/11/04 10:54:02 peter
  1248. + -Ua<oldname>=<newname> unit alias support
  1249. Revision 1.105 1999/10/28 13:14:00 pierre
  1250. * allow doubles in TLinkContainer needed for double libraries
  1251. Revision 1.104 1999/09/27 23:40:12 peter
  1252. * fixed macro within macro endless-loop
  1253. Revision 1.103 1999/09/16 08:00:50 pierre
  1254. + compiled_module to avoid wrong file info when load PPU files
  1255. Revision 1.102 1999/08/31 15:51:10 pierre
  1256. * in_second_compile cleaned up, in_compile and in_second_load added
  1257. Revision 1.101 1999/08/27 10:43:20 pierre
  1258. + interface CRC check with ifdef Test_double_checksum added
  1259. Revision 1.100 1999/08/24 13:14:01 peter
  1260. * MEMDEBUG to see the sizes of asmlist,asmsymbols,symtables
  1261. }