files.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  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. interface
  24. uses
  25. cobjects,globals,ppu;
  26. const
  27. {$ifdef FPC}
  28. maxunits = 1024;
  29. InputFileBufSize=32*1024;
  30. linebufincrease=512;
  31. {$else}
  32. maxunits = 128;
  33. InputFileBufSize=1024;
  34. linebufincrease=64;
  35. {$endif}
  36. type
  37. {$ifdef FPC}
  38. tlongintarr = array[0..1000000] of longint;
  39. {$else}
  40. tlongintarr = array[0..16000] of longint;
  41. {$endif}
  42. plongintarr = ^tlongintarr;
  43. pinputfile = ^tinputfile;
  44. tinputfile = object
  45. path,name : pstring; { path and filename }
  46. next : pinputfile; { next file for reading }
  47. f : file; { current file handle }
  48. is_macro,
  49. endoffile, { still bytes left to read }
  50. closed : boolean; { is the file closed }
  51. buf : pchar; { buffer }
  52. bufstart, { buffer start position in the file }
  53. bufsize, { amount of bytes in the buffer }
  54. maxbufsize : longint; { size in memory for the buffer }
  55. saveinputpointer : pchar; { save fields for scanner variables }
  56. savelastlinepos,
  57. saveline_no : longint;
  58. linebuf : plongintarr; { line buffer to retrieve lines }
  59. maxlinebuf : longint;
  60. ref_count : longint; { to handle the browser refs }
  61. ref_index : longint;
  62. ref_next : pinputfile;
  63. constructor init(const fn:string);
  64. destructor done;
  65. procedure setpos(l:longint);
  66. procedure seekbuf(fpos:longint);
  67. procedure readbuf;
  68. function open:boolean;
  69. procedure close;
  70. procedure tempclose;
  71. function tempopen:boolean;
  72. procedure setmacro(p:pchar;len:longint);
  73. procedure setline(line,linepos:longint);
  74. function getlinestr(l:longint):string;
  75. end;
  76. pfilemanager = ^tfilemanager;
  77. tfilemanager = object
  78. files : pinputfile;
  79. last_ref_index : longint;
  80. constructor init;
  81. destructor done;
  82. procedure register_file(f : pinputfile);
  83. procedure inverse_register_indexes;
  84. function get_file(l:longint) : pinputfile;
  85. function get_file_name(l :longint):string;
  86. function get_file_path(l :longint):string;
  87. end;
  88. type
  89. {$ifndef NEWMAP}
  90. tunitmap = array[0..maxunits-1] of pointer;
  91. punitmap = ^tunitmap;
  92. pmodule = ^tmodule;
  93. {$else NEWMAP}
  94. pmodule = ^tmodule;
  95. tunitmap = array[0..maxunits-1] of pmodule;
  96. punitmap = ^tunitmap;
  97. {$endif NEWMAP}
  98. tmodule = object(tlinkedlist_item)
  99. ppufile : pppufile; { the PPU file }
  100. crc,
  101. flags : longint; { the PPU flags }
  102. compiled, { unit is already compiled }
  103. do_assemble, { only assemble the object, don't recompile }
  104. do_compile, { need to compile the sources }
  105. sources_avail, { if all sources are reachable }
  106. is_unit,
  107. in_second_compile, { is this unit being compiled for the 2nd time? }
  108. in_implementation, { processing the implementation part? }
  109. in_global : boolean; { allow global settings }
  110. islibrary : boolean; { if it is a library (win32 dll) }
  111. map : punitmap; { mapping of all used units }
  112. unitcount : word; { local unit counter }
  113. unit_index : word; { global counter for browser }
  114. globalsymtable, { pointer to the local/static symtable of this unit }
  115. localsymtable : pointer; { pointer to the psymtable of this unit }
  116. scanner : pointer; { scanner object used }
  117. loaded_from : pmodule;
  118. uses_imports : boolean; { Set if the module imports from DLL's.}
  119. imports : plinkedlist;
  120. _exports : plinkedlist;
  121. sourcefiles : pfilemanager;
  122. linksharedlibs,
  123. linkstaticlibs,
  124. linkofiles : tstringcontainer;
  125. used_units : tlinkedlist;
  126. path, { path where the module is find/created }
  127. modulename, { name of the module in uppercase }
  128. objfilename, { fullname of the objectfile }
  129. asmfilename, { fullname of the assemblerfile }
  130. ppufilename, { fullname of the ppufile }
  131. staticlibfilename, { fullname of the static libraryfile }
  132. sharedlibfilename, { fullname of the shared libraryfile }
  133. exefilename, { fullname of the exefile }
  134. asmprefix, { prefix for the smartlink asmfiles }
  135. mainsource : pstring; { name of the main sourcefile }
  136. constructor init(const s:string;_is_unit:boolean);
  137. destructor done;virtual;
  138. procedure reset;
  139. procedure setfilename(const fn:string;allowoutput:boolean);
  140. function openppu:boolean;
  141. function search_unit(const n : string;onlysource:boolean):boolean;
  142. end;
  143. pused_unit = ^tused_unit;
  144. tused_unit = object(tlinkedlist_item)
  145. unitid : word;
  146. name : pstring;
  147. checksum : longint;
  148. loaded : boolean;
  149. in_uses,
  150. in_interface,
  151. is_stab_written : boolean;
  152. u : pmodule;
  153. constructor init(_u : pmodule;intface:boolean);
  154. constructor init_to_load(const n:string;c:longint;intface:boolean);
  155. destructor done;virtual;
  156. end;
  157. var
  158. main_module : pmodule; { Main module of the program }
  159. current_module : pmodule; { Current module which is compiled }
  160. current_ppu : pppufile; { Current ppufile which is read }
  161. global_unit_count : word;
  162. usedunits : tlinkedlist; { Used units for this program }
  163. loaded_units : tlinkedlist; { All loaded units }
  164. implementation
  165. uses
  166. dos,verbose,systems
  167. {$ifndef VER0_99_8}
  168. ,symtable,scanner
  169. {$endif}
  170. ;
  171. {****************************************************************************
  172. TINPUTFILE
  173. ****************************************************************************}
  174. constructor tinputfile.init(const fn:string);
  175. var
  176. p:dirstr;
  177. n:namestr;
  178. e:extstr;
  179. begin
  180. FSplit(fn,p,n,e);
  181. name:=stringdup(n+e);
  182. path:=stringdup(p);
  183. next:=nil;
  184. { file info }
  185. is_macro:=false;
  186. endoffile:=false;
  187. closed:=true;
  188. buf:=nil;
  189. bufstart:=0;
  190. bufsize:=0;
  191. maxbufsize:=InputFileBufSize;
  192. { save fields }
  193. saveinputpointer:=nil;
  194. saveline_no:=0;
  195. savelastlinepos:=0;
  196. { indexing refs }
  197. ref_next:=nil;
  198. ref_count:=0;
  199. ref_index:=0;
  200. { line buffer }
  201. linebuf:=nil;
  202. maxlinebuf:=0;
  203. end;
  204. destructor tinputfile.done;
  205. begin
  206. if not closed then
  207. close;
  208. stringdispose(path);
  209. stringdispose(name);
  210. { free memory }
  211. if assigned(linebuf) then
  212. freemem(linebuf,maxlinebuf shl 2);
  213. end;
  214. procedure tinputfile.setpos(l:longint);
  215. begin
  216. bufstart:=l;
  217. end;
  218. procedure tinputfile.seekbuf(fpos:longint);
  219. begin
  220. if closed then
  221. exit;
  222. seek(f,fpos);
  223. bufstart:=fpos;
  224. bufsize:=0;
  225. end;
  226. procedure tinputfile.readbuf;
  227. {$ifdef TP}
  228. var
  229. w : word;
  230. {$endif}
  231. begin
  232. if is_macro then
  233. endoffile:=true;
  234. if closed then
  235. exit;
  236. inc(bufstart,bufsize);
  237. {$ifdef TP}
  238. blockread(f,buf^,maxbufsize-1,w);
  239. bufsize:=w;
  240. {$else}
  241. blockread(f,buf^,maxbufsize-1,bufsize);
  242. {$endif}
  243. buf[bufsize]:=#0;
  244. endoffile:=not(bufsize=maxbufsize-1);
  245. end;
  246. function tinputfile.open:boolean;
  247. var
  248. ofm : byte;
  249. begin
  250. open:=false;
  251. if not closed then
  252. Close;
  253. ofm:=filemode;
  254. filemode:=0;
  255. Assign(f,path^+name^);
  256. {$I-}
  257. reset(f,1);
  258. {$I+}
  259. filemode:=ofm;
  260. if ioresult<>0 then
  261. exit;
  262. { file }
  263. endoffile:=false;
  264. closed:=false;
  265. Getmem(buf,MaxBufsize);
  266. bufstart:=0;
  267. bufsize:=0;
  268. open:=true;
  269. end;
  270. procedure tinputfile.close;
  271. var
  272. i : word;
  273. begin
  274. if is_macro then
  275. begin
  276. if assigned(buf) then
  277. Freemem(buf,maxbufsize);
  278. buf:=nil;
  279. {is_macro:=false;
  280. still needed for dispose in scanner PM }
  281. closed:=true;
  282. exit;
  283. end;
  284. if not closed then
  285. begin
  286. {$I-}
  287. system.close(f);
  288. {$I+}
  289. i:=ioresult;
  290. closed:=true;
  291. end;
  292. if assigned(buf) then
  293. begin
  294. Freemem(buf,maxbufsize);
  295. buf:=nil;
  296. end;
  297. bufstart:=0;
  298. end;
  299. procedure tinputfile.tempclose;
  300. var
  301. i : word;
  302. begin
  303. if is_macro then
  304. exit;
  305. if not closed then
  306. begin
  307. {$I-}
  308. system.close(f);
  309. {$I+}
  310. i:=ioresult;
  311. Freemem(buf,maxbufsize);
  312. buf:=nil;
  313. closed:=true;
  314. end;
  315. end;
  316. function tinputfile.tempopen:boolean;
  317. var
  318. ofm : byte;
  319. begin
  320. tempopen:=false;
  321. if is_macro then
  322. begin
  323. tempopen:=true;
  324. exit;
  325. end;
  326. if not closed then
  327. exit;
  328. ofm:=filemode;
  329. filemode:=0;
  330. Assign(f,path^+name^);
  331. {$I-}
  332. reset(f,1);
  333. {$I+}
  334. filemode:=ofm;
  335. if ioresult<>0 then
  336. exit;
  337. closed:=false;
  338. { get new mem }
  339. Getmem(buf,maxbufsize);
  340. { restore state }
  341. seek(f,BufStart);
  342. bufsize:=0;
  343. readbuf;
  344. tempopen:=true;
  345. end;
  346. procedure tinputfile.setmacro(p:pchar;len:longint);
  347. begin
  348. { create new buffer }
  349. getmem(buf,len+1);
  350. move(p^,buf^,len);
  351. buf[len]:=#0;
  352. { reset }
  353. bufstart:=0;
  354. bufsize:=len;
  355. maxbufsize:=len+1;
  356. is_macro:=true;
  357. endoffile:=true;
  358. closed:=true;
  359. end;
  360. procedure tinputfile.setline(line,linepos:longint);
  361. var
  362. oldlinebuf : plongintarr;
  363. begin
  364. if line<1 then
  365. exit;
  366. while (line>=maxlinebuf) do
  367. begin
  368. oldlinebuf:=linebuf;
  369. { create new linebuf and move old info }
  370. getmem(linebuf,(maxlinebuf+linebufincrease) shl 2);
  371. if assigned(oldlinebuf) then
  372. begin
  373. move(oldlinebuf^,linebuf^,maxlinebuf shl 2);
  374. freemem(oldlinebuf,maxlinebuf shl 2);
  375. end;
  376. fillchar(linebuf^[maxlinebuf],linebufincrease shl 2,0);
  377. inc(maxlinebuf,linebufincrease);
  378. end;
  379. linebuf^[line]:=linepos;
  380. end;
  381. function tinputfile.getlinestr(l:longint):string;
  382. var
  383. c : char;
  384. i,
  385. fpos : longint;
  386. p : pchar;
  387. begin
  388. getlinestr:='';
  389. if l<maxlinebuf then
  390. begin
  391. fpos:=linebuf^[l];
  392. { fpos is set negativ if the line was already written }
  393. { but we still know the correct value }
  394. if fpos<0 then
  395. fpos:=-fpos+1;
  396. if closed then
  397. open;
  398. { in current buf ? }
  399. if (fpos<bufstart) or (fpos>bufstart+bufsize) then
  400. begin
  401. seekbuf(fpos);
  402. readbuf;
  403. end;
  404. { the begin is in the buf now simply read until #13,#10 }
  405. i:=0;
  406. p:=@buf[fpos-bufstart];
  407. repeat
  408. c:=p^;
  409. if c=#0 then
  410. begin
  411. if endoffile then
  412. break;
  413. readbuf;
  414. p:=buf;
  415. c:=p^;
  416. end;
  417. if c in [#10,#13] then
  418. break;
  419. inc(i);
  420. getlinestr[i]:=c;
  421. inc(longint(p));
  422. until (i=255);
  423. {$ifopt H+}
  424. setlength(getlinestr,i);
  425. {$else}
  426. getlinestr[0]:=chr(i);
  427. {$endif}
  428. end;
  429. end;
  430. {****************************************************************************
  431. TFILEMANAGER
  432. ****************************************************************************}
  433. constructor tfilemanager.init;
  434. begin
  435. files:=nil;
  436. last_ref_index:=0;
  437. end;
  438. destructor tfilemanager.done;
  439. var
  440. hp : pinputfile;
  441. begin
  442. hp:=files;
  443. while assigned(hp) do
  444. begin
  445. files:=files^.ref_next;
  446. dispose(hp,done);
  447. hp:=files;
  448. end;
  449. last_ref_index:=0;
  450. end;
  451. procedure tfilemanager.register_file(f : pinputfile);
  452. begin
  453. inc(last_ref_index);
  454. f^.ref_next:=files;
  455. f^.ref_index:=last_ref_index;
  456. files:=f;
  457. {$ifdef FPC}
  458. {$ifdef heaptrc}
  459. writeln(stderr,f^.name^,' index ',current_module^.unit_index*100000+f^.ref_index);
  460. {$endif heaptrc}
  461. {$endif FPC}
  462. end;
  463. { this procedure is necessary after loading the
  464. sources files from a PPU file PM }
  465. procedure tfilemanager.inverse_register_indexes;
  466. var
  467. f : pinputfile;
  468. begin
  469. f:=files;
  470. while assigned(f) do
  471. begin
  472. f^.ref_index:=last_ref_index-f^.ref_index+1;
  473. f:=f^.ref_next;
  474. end;
  475. end;
  476. function tfilemanager.get_file(l :longint) : pinputfile;
  477. var
  478. ff : pinputfile;
  479. begin
  480. ff:=files;
  481. while assigned(ff) and (ff^.ref_index<>l) do
  482. ff:=ff^.ref_next;
  483. get_file:=ff;
  484. end;
  485. function tfilemanager.get_file_name(l :longint):string;
  486. var
  487. hp : pinputfile;
  488. begin
  489. hp:=get_file(l);
  490. if assigned(hp) then
  491. get_file_name:=hp^.name^
  492. else
  493. get_file_name:='';
  494. end;
  495. function tfilemanager.get_file_path(l :longint):string;
  496. var
  497. hp : pinputfile;
  498. begin
  499. hp:=get_file(l);
  500. if assigned(hp) then
  501. get_file_path:=hp^.path^
  502. else
  503. get_file_path:='';
  504. end;
  505. {****************************************************************************
  506. TMODULE
  507. ****************************************************************************}
  508. procedure tmodule.setfilename(const fn:string;allowoutput:boolean);
  509. var
  510. p : dirstr;
  511. n : NameStr;
  512. e : ExtStr;
  513. begin
  514. stringdispose(objfilename);
  515. stringdispose(asmfilename);
  516. stringdispose(ppufilename);
  517. stringdispose(staticlibfilename);
  518. stringdispose(sharedlibfilename);
  519. stringdispose(exefilename);
  520. stringdispose(path);
  521. { Create names }
  522. fsplit(fn,p,n,e);
  523. n:=FixFileName(n);
  524. { set path }
  525. path:=stringdup(FixPath(p,false));
  526. { obj,asm,ppu names }
  527. p:=path^;
  528. if AllowOutput then
  529. begin
  530. if (OutputUnitDir<>'') then
  531. p:=OutputUnitDir
  532. else
  533. if (OutputExeDir<>'') then
  534. p:=OutputExeDir;
  535. end;
  536. objfilename:=stringdup(p+n+target_info.objext);
  537. asmfilename:=stringdup(p+n+target_info.asmext);
  538. ppufilename:=stringdup(p+n+target_info.unitext);
  539. { lib and exe could be loaded with a file specified with -o }
  540. if AllowOutput and (OutputFile<>'') then
  541. n:=OutputFile;
  542. staticlibfilename:=stringdup(p+target_os.libprefix+n+target_os.staticlibext);
  543. if target_info.target=target_i386_WIN32 then
  544. sharedlibfilename:=stringdup(p+n+target_os.sharedlibext)
  545. else
  546. sharedlibfilename:=stringdup(p+target_os.libprefix+n+target_os.sharedlibext);
  547. { output dir of exe can be specified separatly }
  548. if AllowOutput and (OutputExeDir<>'') then
  549. p:=OutputExeDir
  550. else
  551. p:=path^;
  552. exefilename:=stringdup(p+n+target_os.exeext);
  553. end;
  554. function tmodule.openppu:boolean;
  555. var
  556. objfiletime,
  557. ppufiletime,
  558. asmfiletime : longint;
  559. begin
  560. openppu:=false;
  561. Message1(unit_t_ppu_loading,ppufilename^);
  562. { Get ppufile time (also check if the file exists) }
  563. ppufiletime:=getnamedfiletime(ppufilename^);
  564. if ppufiletime=-1 then
  565. exit;
  566. { Open the ppufile }
  567. Message1(unit_u_ppu_name,ppufilename^);
  568. ppufile:=new(pppufile,init(ppufilename^));
  569. ppufile^.change_endian:=source_os.endian<>target_os.endian;
  570. if not ppufile^.open then
  571. begin
  572. dispose(ppufile,done);
  573. Message(unit_u_ppu_file_too_short);
  574. exit;
  575. end;
  576. { check for a valid PPU file }
  577. if not ppufile^.CheckPPUId then
  578. begin
  579. dispose(ppufile,done);
  580. Message(unit_u_ppu_invalid_header);
  581. exit;
  582. end;
  583. { check for allowed PPU versions }
  584. if not (ppufile^.GetPPUVersion in [15]) then
  585. begin
  586. dispose(ppufile,done);
  587. Message1(unit_u_ppu_invalid_version,tostr(ppufile^.GetPPUVersion));
  588. exit;
  589. end;
  590. { check the target processor }
  591. if ttargetcpu(ppufile^.header.cpu)<>target_cpu then
  592. begin
  593. dispose(ppufile,done);
  594. Message(unit_u_ppu_invalid_processor);
  595. exit;
  596. end;
  597. { check target }
  598. if ttarget(ppufile^.header.target)<>target_info.target then
  599. begin
  600. dispose(ppufile,done);
  601. Message(unit_u_ppu_invalid_target);
  602. exit;
  603. end;
  604. { Load values to be access easier }
  605. flags:=ppufile^.header.flags;
  606. crc:=ppufile^.header.checksum;
  607. { Show Debug info }
  608. Message1(unit_u_ppu_time,filetimestring(ppufiletime));
  609. Message1(unit_u_ppu_flags,tostr(flags));
  610. Message1(unit_u_ppu_crc,tostr(ppufile^.header.checksum));
  611. { check the object and assembler file to see if we need only to
  612. assemble, only if it's not in a library }
  613. do_compile:=false;
  614. if (flags and uf_in_library)=0 then
  615. begin
  616. if ((flags and uf_static_linked)<>0) or
  617. ((flags and uf_smartlink)<>0) then
  618. begin
  619. objfiletime:=getnamedfiletime(staticlibfilename^);
  620. Message2(unit_u_check_time,staticlibfilename^,filetimestring(objfiletime));
  621. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  622. begin
  623. Message(unit_u_recompile_staticlib_is_older);
  624. do_compile:=true;
  625. exit;
  626. end;
  627. end
  628. else
  629. if (flags and uf_shared_linked)<>0 then
  630. begin
  631. objfiletime:=getnamedfiletime(sharedlibfilename^);
  632. Message2(unit_u_check_time,sharedlibfilename^,filetimestring(objfiletime));
  633. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  634. begin
  635. Message(unit_u_recompile_sharedlib_is_older);
  636. do_compile:=true;
  637. exit;
  638. end;
  639. end
  640. else
  641. begin
  642. { the objectfile should be newer than the ppu file }
  643. objfiletime:=getnamedfiletime(objfilename^);
  644. Message2(unit_u_check_time,objfilename^,filetimestring(objfiletime));
  645. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  646. begin
  647. { check if assembler file is older than ppu file }
  648. asmfileTime:=GetNamedFileTime(asmfilename^);
  649. Message2(unit_u_check_time,asmfilename^,filetimestring(asmfiletime));
  650. if (asmfiletime<0) or (ppufiletime>asmfiletime) then
  651. begin
  652. Message(unit_u_recompile_obj_and_asm_older);
  653. do_compile:=true;
  654. exit;
  655. end
  656. else
  657. begin
  658. Message(unit_u_recompile_obj_older_than_asm);
  659. if not(cs_asm_extern in aktglobalswitches) then
  660. begin
  661. do_compile:=true;
  662. exit;
  663. end;
  664. end;
  665. end;
  666. end;
  667. end;
  668. openppu:=true;
  669. end;
  670. function tmodule.search_unit(const n : string;onlysource:boolean):boolean;
  671. var
  672. ext : string[8];
  673. singlepathstring,
  674. unitPath,
  675. filename : string;
  676. found : boolean;
  677. start,i : longint;
  678. Function UnitExists(const ext:string):boolean;
  679. begin
  680. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  681. UnitExists:=FileExists(Singlepathstring+FileName+ext);
  682. end;
  683. begin
  684. start:=1;
  685. filename:=FixFileName(n);
  686. unitpath:=UnitSearchPath;
  687. Found:=false;
  688. repeat
  689. { Create current path to check }
  690. i:=pos(';',unitpath);
  691. if i=0 then
  692. i:=length(unitpath)+1;
  693. singlepathstring:=FixPath(copy(unitpath,start,i-start),false);
  694. delete(unitpath,start,i-start+1);
  695. if not onlysource then
  696. begin
  697. { Check for PPL file }
  698. if not Found then
  699. begin
  700. Found:=UnitExists(target_info.unitlibext);
  701. if Found then
  702. Begin
  703. SetFileName(SinglePathString+FileName,false);
  704. Found:=OpenPPU;
  705. End;
  706. end;
  707. { Check for PPU file }
  708. if not Found then
  709. begin
  710. Found:=UnitExists(target_info.unitext);
  711. if Found then
  712. Begin
  713. SetFileName(SinglePathString+FileName,false);
  714. Found:=OpenPPU;
  715. End;
  716. end;
  717. end;
  718. { Check for Sources }
  719. if not Found then
  720. begin
  721. ppufile:=nil;
  722. do_compile:=true;
  723. {Check for .pp file}
  724. Found:=UnitExists(target_os.sourceext);
  725. if Found then
  726. Ext:=target_os.sourceext
  727. else
  728. begin
  729. {Check for .pas}
  730. Found:=UnitExists(target_os.pasext);
  731. if Found then
  732. Ext:=target_os.pasext;
  733. end;
  734. stringdispose(mainsource);
  735. if Found then
  736. begin
  737. sources_avail:=true;
  738. {Load Filenames when found}
  739. mainsource:=StringDup(SinglePathString+FileName+Ext);
  740. SetFileName(SinglePathString+FileName,false);
  741. end
  742. else
  743. sources_avail:=false;
  744. end;
  745. until Found or (unitpath='');
  746. search_unit:=Found;
  747. end;
  748. procedure tmodule.reset;
  749. begin
  750. {$ifndef VER0_99_8}
  751. if assigned(scanner) then
  752. pscannerfile(scanner)^.invalid:=true;
  753. if assigned(globalsymtable) then
  754. begin
  755. dispose(punitsymtable(globalsymtable),done);
  756. globalsymtable:=nil;
  757. end;
  758. if assigned(localsymtable) then
  759. begin
  760. dispose(punitsymtable(localsymtable),done);
  761. localsymtable:=nil;
  762. end;
  763. {$endif}
  764. if assigned(map) then
  765. begin
  766. dispose(map);
  767. map:=nil;
  768. end;
  769. if assigned(ppufile) then
  770. begin
  771. dispose(ppufile,done);
  772. ppufile:=nil;
  773. end;
  774. sourcefiles^.done;
  775. sourcefiles^.init;
  776. imports^.done;
  777. imports^.init;
  778. _exports^.done;
  779. _exports^.init;
  780. used_units.done;
  781. used_units.init;
  782. linkofiles.done;
  783. linkofiles.init_no_double;
  784. linkstaticlibs.done;
  785. linkstaticlibs.init_no_double;
  786. linksharedlibs.done;
  787. linksharedlibs.init_no_double;
  788. uses_imports:=false;
  789. do_assemble:=false;
  790. do_compile:=false;
  791. { sources_avail:=true;
  792. should not be changed PM }
  793. compiled:=false;
  794. in_implementation:=false;
  795. in_global:=true;
  796. loaded_from:=nil;
  797. flags:=0;
  798. crc:=0;
  799. unitcount:=1;
  800. end;
  801. constructor tmodule.init(const s:string;_is_unit:boolean);
  802. var
  803. p : dirstr;
  804. n : namestr;
  805. e : extstr;
  806. begin
  807. FSplit(s,p,n,e);
  808. { Programs have the name program to don't conflict with dup id's }
  809. if _is_unit then
  810. modulename:=stringdup(Upper(n))
  811. else
  812. modulename:=stringdup('PROGRAM');
  813. mainsource:=stringdup(s);
  814. ppufilename:=nil;
  815. objfilename:=nil;
  816. asmfilename:=nil;
  817. staticlibfilename:=nil;
  818. sharedlibfilename:=nil;
  819. exefilename:=nil;
  820. { Dos has the famous 8.3 limit :( }
  821. {$ifdef tp}
  822. asmprefix:=stringdup(FixFileName('as'));
  823. {$else}
  824. {$ifdef go32v2}
  825. asmprefix:=stringdup(FixFileName('as'));
  826. {$else}
  827. {$ifdef OS2}
  828. {Allthough OS/2 supports long filenames I play it safe and
  829. use 8.3 filenames, because this allows the compiler to run
  830. on a FAT partition. (DM)}
  831. asmprefix:=stringdup(FixFileName('as'));
  832. {$else}
  833. asmprefix:=stringdup(FixFileName(n));
  834. {$endif}
  835. {$endif}
  836. {$endif tp}
  837. path:=nil;
  838. setfilename(p+n,true);
  839. used_units.init;
  840. new(sourcefiles,init);
  841. linkofiles.init_no_double;
  842. linkstaticlibs.init_no_double;
  843. linksharedlibs.init_no_double;
  844. ppufile:=nil;
  845. scanner:=nil;
  846. map:=nil;
  847. globalsymtable:=nil;
  848. localsymtable:=nil;
  849. loaded_from:=nil;
  850. flags:=0;
  851. crc:=0;
  852. unitcount:=1;
  853. inc(global_unit_count);
  854. unit_index:=global_unit_count;
  855. do_assemble:=false;
  856. do_compile:=false;
  857. sources_avail:=true;
  858. compiled:=false;
  859. in_second_compile:=false;
  860. in_implementation:=false;
  861. in_global:=true;
  862. is_unit:=_is_unit;
  863. islibrary:=false;
  864. uses_imports:=false;
  865. imports:=new(plinkedlist,init);
  866. _exports:=new(plinkedlist,init);
  867. { search the PPU file if it is an unit }
  868. if is_unit then
  869. begin
  870. if (not search_unit(modulename^,false)) and (length(modulename^)>8) then
  871. search_unit(copy(modulename^,1,8),false);
  872. end;
  873. end;
  874. destructor tmodule.done;
  875. begin
  876. if assigned(map) then
  877. dispose(map);
  878. if assigned(ppufile) then
  879. dispose(ppufile,done);
  880. ppufile:=nil;
  881. if assigned(imports) then
  882. dispose(imports,done);
  883. imports:=nil;
  884. if assigned(_exports) then
  885. dispose(_exports,done);
  886. _exports:=nil;
  887. {$ifndef VER0_99_8}
  888. if assigned(scanner) then
  889. pscannerfile(scanner)^.invalid:=true;
  890. {$endif}
  891. if assigned(sourcefiles) then
  892. dispose(sourcefiles,done);
  893. sourcefiles:=nil;
  894. used_units.done;
  895. linkofiles.done;
  896. linkstaticlibs.done;
  897. linksharedlibs.done;
  898. stringdispose(objfilename);
  899. stringdispose(asmfilename);
  900. stringdispose(ppufilename);
  901. stringdispose(staticlibfilename);
  902. stringdispose(sharedlibfilename);
  903. stringdispose(exefilename);
  904. stringdispose(path);
  905. stringdispose(modulename);
  906. stringdispose(mainsource);
  907. stringdispose(asmprefix);
  908. {$ifndef VER0_99_8}
  909. if assigned(globalsymtable) then
  910. dispose(punitsymtable(globalsymtable),done);
  911. globalsymtable:=nil;
  912. if assigned(localsymtable) then
  913. dispose(punitsymtable(localsymtable),done);
  914. localsymtable:=nil;
  915. {$endif}
  916. inherited done;
  917. end;
  918. {****************************************************************************
  919. TUSED_UNIT
  920. ****************************************************************************}
  921. constructor tused_unit.init(_u : pmodule;intface:boolean);
  922. begin
  923. u:=_u;
  924. in_interface:=intface;
  925. in_uses:=false;
  926. is_stab_written:=false;
  927. loaded:=true;
  928. name:=stringdup(_u^.modulename^);
  929. checksum:=_u^.crc;
  930. unitid:=0;
  931. end;
  932. constructor tused_unit.init_to_load(const n:string;c:longint;intface:boolean);
  933. begin
  934. u:=nil;
  935. in_interface:=intface;
  936. in_uses:=false;
  937. is_stab_written:=false;
  938. loaded:=false;
  939. name:=stringdup(n);
  940. checksum:=c;
  941. unitid:=0;
  942. end;
  943. destructor tused_unit.done;
  944. begin
  945. stringdispose(name);
  946. inherited done;
  947. end;
  948. end.
  949. {
  950. $Log$
  951. Revision 1.74 1998-11-16 12:18:01 peter
  952. * H+ fixes
  953. Revision 1.73 1998/11/16 11:28:58 pierre
  954. * stackcheck removed for i386_win32
  955. * exportlist does not crash at least !!
  956. (was need for tests dir !)z
  957. Revision 1.72 1998/11/15 16:32:35 florian
  958. * some stuff of Pavel implement (win32 dll creation)
  959. * bug with ansistring function results fixed
  960. Revision 1.71 1998/11/06 09:45:40 pierre
  961. * bug on errors (file used after dispose !) fixed
  962. Revision 1.70 1998/11/03 11:33:14 peter
  963. + search_unit arg to only search for sources
  964. Revision 1.69 1998/10/29 11:35:44 florian
  965. * some dll support for win32
  966. * fixed assembler writing for PalmOS
  967. Revision 1.68 1998/10/27 10:22:34 florian
  968. + First things for win32 export sections
  969. Revision 1.67 1998/10/26 22:23:29 peter
  970. + fixpath() has an extra option to allow a ./ as path
  971. Revision 1.66 1998/10/19 18:07:11 peter
  972. + external dll_name name func support for linux
  973. Revision 1.65 1998/10/15 12:22:25 pierre
  974. * close include files immediately after end reading
  975. instead of waiting until unit compilation ended !
  976. Revision 1.64 1998/10/14 13:38:19 peter
  977. * fixed path with staticlib/objects in ppufiles
  978. Revision 1.63 1998/10/14 11:02:49 daniel
  979. * Stupid typo fixed.
  980. Revision 1.62 1998/10/14 10:59:37 daniel
  981. * Staticlibfilename now doesn't include path.
  982. Revision 1.61 1998/10/14 10:57:25 daniel
  983. * Dirstr, namestr, extstr.
  984. * $V+ to prevent Peter from forgetting this.
  985. * OS/2 compiler uses 8.3 filenames to support running the compiler on an old
  986. DOS FAT partition.
  987. Revision 1.60 1998/10/14 10:45:07 pierre
  988. * ppu problems for m68k fixed (at least in cross compiling)
  989. * one last memory leak for sysamiga fixed
  990. * the amiga RTL compiles now completely !!
  991. Revision 1.59 1998/10/13 14:01:07 peter
  992. * fixed -al
  993. Revision 1.58 1998/10/12 11:59:00 peter
  994. + show name and date of .o and .s files which the compiler checks
  995. Revision 1.57 1998/10/09 16:36:03 pierre
  996. * some memory leaks specific to usebrowser define fixed
  997. * removed tmodule.implsymtable (was like tmodule.localsymtable)
  998. Revision 1.56 1998/10/09 08:56:26 pierre
  999. * several memory leaks fixed
  1000. Revision 1.55 1998/10/08 23:28:54 peter
  1001. * -vu shows unit info, -vt shows tried/used files
  1002. Revision 1.54 1998/10/08 17:17:19 pierre
  1003. * current_module old scanner tagged as invalid if unit is recompiled
  1004. + added ppheap for better info on tracegetmem of heaptrc
  1005. (adds line column and file index)
  1006. * several memory leaks removed ith help of heaptrc !!
  1007. Revision 1.53 1998/10/08 13:48:43 peter
  1008. * fixed memory leaks for do nothing source
  1009. * fixed unit interdependency
  1010. Revision 1.52 1998/10/06 22:09:48 peter
  1011. * fixed for compiling with 0.99.8 due circular units
  1012. Revision 1.51 1998/10/06 17:16:47 pierre
  1013. * some memory leaks fixed (thanks to Peter for heaptrc !)
  1014. Revision 1.50 1998/09/30 16:43:34 peter
  1015. * fixed unit interdependency with circular uses
  1016. Revision 1.49 1998/09/28 16:57:20 pierre
  1017. * changed all length(p^.value_str^) into str_length(p)
  1018. to get it work with and without ansistrings
  1019. * changed sourcefiles field of tmodule to a pointer
  1020. Revision 1.48 1998/09/24 23:46:34 peter
  1021. + outputdir support
  1022. Revision 1.47 1998/09/22 17:13:43 pierre
  1023. + browsing updated and developed
  1024. records and objects fields are also stored
  1025. Revision 1.46 1998/09/21 08:45:10 pierre
  1026. + added vmt_offset in tobjectdef.write for fututre use
  1027. (first steps to have objects without vmt if no virtual !!)
  1028. + added fpu_used field for tabstractprocdef :
  1029. sets this level to 2 if the functions return with value in FPU
  1030. (is then set to correct value at parsing of implementation)
  1031. THIS MIGHT refuse some code with FPU expression too complex
  1032. that were accepted before and even in some cases
  1033. that don't overflow in fact
  1034. ( like if f : float; is a forward that finally in implementation
  1035. only uses one fpu register !!)
  1036. Nevertheless I think that it will improve security on
  1037. FPU operations !!
  1038. * most other changes only for UseBrowser code
  1039. (added symtable references for record and objects)
  1040. local switch for refs to args and local of each function
  1041. (static symtable still missing)
  1042. UseBrowser still not stable and probably broken by
  1043. the definition hash array !!
  1044. Revision 1.45 1998/09/18 09:58:51 peter
  1045. * -s doesn't require the .o to be available, this allows compiling of
  1046. everything on other platforms (profiling the windows.pp loading ;)
  1047. Revision 1.44 1998/09/10 13:51:32 peter
  1048. * tp compiler also uses 'as' as asmprefix
  1049. Revision 1.43 1998/09/03 17:08:45 pierre
  1050. * better lines for stabs
  1051. (no scroll back to if before else part
  1052. no return to case line at jump outside case)
  1053. + source lines also if not in order
  1054. Revision 1.42 1998/09/03 11:24:00 peter
  1055. * moved more inputfile things from tscannerfile to tinputfile
  1056. * changed ifdef Sourceline to cs_asm_source
  1057. Revision 1.41 1998/08/26 15:35:30 peter
  1058. * fixed scannerfiles for macros
  1059. + $I %<environment>%
  1060. Revision 1.40 1998/08/26 10:08:48 peter
  1061. * fixed problem with libprefix at the wrong place
  1062. * fixed lib generation with smartlinking and no -CS used
  1063. Revision 1.39 1998/08/25 16:44:16 pierre
  1064. * openppu was true even if the object file is missing
  1065. this lead to trying to open a filename without extension
  1066. and prevented the 'make cycle' to work for win32
  1067. Revision 1.38 1998/08/19 10:06:12 peter
  1068. * fixed filenames and removedir which supports slash at the end
  1069. Revision 1.37 1998/08/18 20:52:19 peter
  1070. * renamed in_main to in_global which is more logical
  1071. Revision 1.36 1998/08/17 10:10:07 peter
  1072. - removed OLDPPU
  1073. Revision 1.35 1998/08/17 09:17:44 peter
  1074. * static/shared linking updates
  1075. Revision 1.34 1998/08/14 21:56:31 peter
  1076. * setting the outputfile using -o works now to create static libs
  1077. Revision 1.33 1998/08/11 14:09:08 peter
  1078. * fixed some messages and smaller msgtxt.inc
  1079. Revision 1.32 1998/08/10 14:49:58 peter
  1080. + localswitches, moduleswitches, globalswitches splitting
  1081. Revision 1.31 1998/07/14 14:46:48 peter
  1082. * released NEWINPUT
  1083. Revision 1.30 1998/07/07 11:19:55 peter
  1084. + NEWINPUT for a better inputfile and scanner object
  1085. Revision 1.29 1998/06/25 10:51:00 pierre
  1086. * removed a remaining ifndef NEWPPU
  1087. replaced by ifdef OLDPPU
  1088. * added uf_finalize to ppu unit
  1089. Revision 1.28 1998/06/25 08:48:12 florian
  1090. * first version of rtti support
  1091. Revision 1.27 1998/06/24 14:48:34 peter
  1092. * ifdef newppu -> ifndef oldppu
  1093. Revision 1.26 1998/06/17 14:36:19 peter
  1094. * forgot an $ifndef OLDPPU :(
  1095. Revision 1.25 1998/06/17 14:10:11 peter
  1096. * small os2 fixes
  1097. * fixed interdependent units with newppu (remake3 under linux works now)
  1098. Revision 1.24 1998/06/16 08:56:20 peter
  1099. + targetcpu
  1100. * cleaner pmodules for newppu
  1101. Revision 1.23 1998/06/15 14:44:36 daniel
  1102. * BP updates.
  1103. Revision 1.22 1998/06/14 18:25:41 peter
  1104. * small fix with crc in newppu
  1105. Revision 1.21 1998/06/13 00:10:05 peter
  1106. * working browser and newppu
  1107. * some small fixes against crashes which occured in bp7 (but not in
  1108. fpc?!)
  1109. Revision 1.20 1998/06/12 14:50:48 peter
  1110. * removed the tree dependency to types.pas
  1111. * long_fil.pas support (not fully tested yet)
  1112. Revision 1.19 1998/06/12 10:32:26 pierre
  1113. * column problem hopefully solved
  1114. + C vars declaration changed
  1115. Revision 1.18 1998/06/11 13:58:07 peter
  1116. * small fix to let newppu compile
  1117. Revision 1.17 1998/06/09 16:01:40 pierre
  1118. + added procedure directive parsing for procvars
  1119. (accepted are popstack cdecl and pascal)
  1120. + added C vars with the following syntax
  1121. var C calias 'true_c_name';(can be followed by external)
  1122. reason is that you must add the Cprefix
  1123. which is target dependent
  1124. Revision 1.16 1998/06/04 10:42:19 pierre
  1125. * small bug fix in load_ppu or openppu
  1126. Revision 1.15 1998/05/28 14:37:53 peter
  1127. * default programname is PROGRAM (like TP7) to avoid dup id's
  1128. Revision 1.14 1998/05/27 19:45:02 peter
  1129. * symtable.pas splitted into includefiles
  1130. * symtable adapted for $ifndef OLDPPU
  1131. Revision 1.13 1998/05/23 01:21:05 peter
  1132. + aktasmmode, aktoptprocessor, aktoutputformat
  1133. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1134. + $LIBNAME to set the library name where the unit will be put in
  1135. * splitted cgi386 a bit (codeseg to large for bp7)
  1136. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1137. Revision 1.12 1998/05/20 09:42:33 pierre
  1138. + UseTokenInfo now default
  1139. * unit in interface uses and implementation uses gives error now
  1140. * only one error for unknown symbol (uses lastsymknown boolean)
  1141. the problem came from the label code !
  1142. + first inlined procedures and function work
  1143. (warning there might be allowed cases were the result is still wrong !!)
  1144. * UseBrower updated gives a global list of all position of all used symbols
  1145. with switch -gb
  1146. Revision 1.11 1998/05/12 10:46:59 peter
  1147. * moved printstatus to verb_def
  1148. + V_Normal which is between V_Error and V_Warning and doesn't have a
  1149. prefix like error: warning: and is included in V_Default
  1150. * fixed some messages
  1151. * first time parameter scan is only for -v and -T
  1152. - removed old style messages
  1153. Revision 1.10 1998/05/11 13:07:53 peter
  1154. + $ifndef OLDPPU for the new ppuformat
  1155. + $define GDB not longer required
  1156. * removed all warnings and stripped some log comments
  1157. * no findfirst/findnext anymore to remove smartlink *.o files
  1158. Revision 1.9 1998/05/06 15:04:20 pierre
  1159. + when trying to find source files of a ppufile
  1160. check the includepathlist for included files
  1161. the main file must still be in the same directory
  1162. Revision 1.8 1998/05/04 17:54:25 peter
  1163. + smartlinking works (only case jumptable left todo)
  1164. * redesign of systems.pas to support assemblers and linkers
  1165. + Unitname is now also in the PPU-file, increased version to 14
  1166. Revision 1.7 1998/05/01 16:38:44 florian
  1167. * handling of private and protected fixed
  1168. + change_keywords_to_tp implemented to remove
  1169. keywords which aren't supported by tp
  1170. * break and continue are now symbols of the system unit
  1171. + widestring, longstring and ansistring type released
  1172. Revision 1.6 1998/05/01 07:43:53 florian
  1173. + basics for rtti implemented
  1174. + switch $m (generate rtti for published sections)
  1175. Revision 1.5 1998/04/30 15:59:40 pierre
  1176. * GDB works again better :
  1177. correct type info in one pass
  1178. + UseTokenInfo for better source position
  1179. * fixed one remaining bug in scanner for line counts
  1180. * several little fixes
  1181. Revision 1.4 1998/04/29 10:33:52 pierre
  1182. + added some code for ansistring (not complete nor working yet)
  1183. * corrected operator overloading
  1184. * corrected nasm output
  1185. + started inline procedures
  1186. + added starstarn : use ** for exponentiation (^ gave problems)
  1187. + started UseTokenInfo cond to get accurate positions
  1188. Revision 1.3 1998/04/27 23:10:28 peter
  1189. + new scanner
  1190. * $makelib -> if smartlink
  1191. * small filename fixes pmodule.setfilename
  1192. * moved import from files.pas -> import.pas
  1193. Revision 1.2 1998/04/21 10:16:47 peter
  1194. * patches from strasbourg
  1195. * objects is not used anymore in the fpc compiled version
  1196. }