files.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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. getlinestr[0]:=chr(i);
  424. end;
  425. end;
  426. {****************************************************************************
  427. TFILEMANAGER
  428. ****************************************************************************}
  429. constructor tfilemanager.init;
  430. begin
  431. files:=nil;
  432. last_ref_index:=0;
  433. end;
  434. destructor tfilemanager.done;
  435. var
  436. hp : pinputfile;
  437. begin
  438. hp:=files;
  439. while assigned(hp) do
  440. begin
  441. files:=files^.ref_next;
  442. dispose(hp,done);
  443. hp:=files;
  444. end;
  445. last_ref_index:=0;
  446. end;
  447. procedure tfilemanager.register_file(f : pinputfile);
  448. begin
  449. inc(last_ref_index);
  450. f^.ref_next:=files;
  451. f^.ref_index:=last_ref_index;
  452. files:=f;
  453. {$ifdef FPC}
  454. {$ifdef heaptrc}
  455. writeln(stderr,f^.name^,' index ',current_module^.unit_index*100000+f^.ref_index);
  456. {$endif heaptrc}
  457. {$endif FPC}
  458. end;
  459. { this procedure is necessary after loading the
  460. sources files from a PPU file PM }
  461. procedure tfilemanager.inverse_register_indexes;
  462. var
  463. f : pinputfile;
  464. begin
  465. f:=files;
  466. while assigned(f) do
  467. begin
  468. f^.ref_index:=last_ref_index-f^.ref_index+1;
  469. f:=f^.ref_next;
  470. end;
  471. end;
  472. function tfilemanager.get_file(l :longint) : pinputfile;
  473. var
  474. ff : pinputfile;
  475. begin
  476. ff:=files;
  477. while assigned(ff) and (ff^.ref_index<>l) do
  478. ff:=ff^.ref_next;
  479. get_file:=ff;
  480. end;
  481. function tfilemanager.get_file_name(l :longint):string;
  482. var
  483. hp : pinputfile;
  484. begin
  485. hp:=get_file(l);
  486. if assigned(hp) then
  487. get_file_name:=hp^.name^
  488. else
  489. get_file_name:='';
  490. end;
  491. function tfilemanager.get_file_path(l :longint):string;
  492. var
  493. hp : pinputfile;
  494. begin
  495. hp:=get_file(l);
  496. if assigned(hp) then
  497. get_file_path:=hp^.path^
  498. else
  499. get_file_path:='';
  500. end;
  501. {****************************************************************************
  502. TMODULE
  503. ****************************************************************************}
  504. procedure tmodule.setfilename(const fn:string;allowoutput:boolean);
  505. var
  506. p : dirstr;
  507. n : NameStr;
  508. e : ExtStr;
  509. begin
  510. stringdispose(objfilename);
  511. stringdispose(asmfilename);
  512. stringdispose(ppufilename);
  513. stringdispose(staticlibfilename);
  514. stringdispose(sharedlibfilename);
  515. stringdispose(exefilename);
  516. stringdispose(path);
  517. { Create names }
  518. fsplit(fn,p,n,e);
  519. n:=FixFileName(n);
  520. { set path }
  521. path:=stringdup(FixPath(p,false));
  522. { obj,asm,ppu names }
  523. p:=path^;
  524. if AllowOutput then
  525. begin
  526. if (OutputUnitDir<>'') then
  527. p:=OutputUnitDir
  528. else
  529. if (OutputExeDir<>'') then
  530. p:=OutputExeDir;
  531. end;
  532. objfilename:=stringdup(p+n+target_info.objext);
  533. asmfilename:=stringdup(p+n+target_info.asmext);
  534. ppufilename:=stringdup(p+n+target_info.unitext);
  535. { lib and exe could be loaded with a file specified with -o }
  536. if AllowOutput and (OutputFile<>'') then
  537. n:=OutputFile;
  538. staticlibfilename:=stringdup(p+target_os.libprefix+n+target_os.staticlibext);
  539. if target_info.target=target_i386_WIN32 then
  540. sharedlibfilename:=stringdup(p+n+target_os.sharedlibext)
  541. else
  542. sharedlibfilename:=stringdup(p+target_os.libprefix+n+target_os.sharedlibext);
  543. { output dir of exe can be specified separatly }
  544. if AllowOutput and (OutputExeDir<>'') then
  545. p:=OutputExeDir
  546. else
  547. p:=path^;
  548. exefilename:=stringdup(p+n+target_os.exeext);
  549. end;
  550. function tmodule.openppu:boolean;
  551. var
  552. objfiletime,
  553. ppufiletime,
  554. asmfiletime : longint;
  555. begin
  556. openppu:=false;
  557. Message1(unit_t_ppu_loading,ppufilename^);
  558. { Get ppufile time (also check if the file exists) }
  559. ppufiletime:=getnamedfiletime(ppufilename^);
  560. if ppufiletime=-1 then
  561. exit;
  562. { Open the ppufile }
  563. Message1(unit_u_ppu_name,ppufilename^);
  564. ppufile:=new(pppufile,init(ppufilename^));
  565. ppufile^.change_endian:=source_os.endian<>target_os.endian;
  566. if not ppufile^.open then
  567. begin
  568. dispose(ppufile,done);
  569. Message(unit_u_ppu_file_too_short);
  570. exit;
  571. end;
  572. { check for a valid PPU file }
  573. if not ppufile^.CheckPPUId then
  574. begin
  575. dispose(ppufile,done);
  576. Message(unit_u_ppu_invalid_header);
  577. exit;
  578. end;
  579. { check for allowed PPU versions }
  580. if not (ppufile^.GetPPUVersion in [15]) then
  581. begin
  582. dispose(ppufile,done);
  583. Message1(unit_u_ppu_invalid_version,tostr(ppufile^.GetPPUVersion));
  584. exit;
  585. end;
  586. { check the target processor }
  587. if ttargetcpu(ppufile^.header.cpu)<>target_cpu then
  588. begin
  589. dispose(ppufile,done);
  590. Message(unit_u_ppu_invalid_processor);
  591. exit;
  592. end;
  593. { check target }
  594. if ttarget(ppufile^.header.target)<>target_info.target then
  595. begin
  596. dispose(ppufile,done);
  597. Message(unit_u_ppu_invalid_target);
  598. exit;
  599. end;
  600. { Load values to be access easier }
  601. flags:=ppufile^.header.flags;
  602. crc:=ppufile^.header.checksum;
  603. { Show Debug info }
  604. Message1(unit_u_ppu_time,filetimestring(ppufiletime));
  605. Message1(unit_u_ppu_flags,tostr(flags));
  606. Message1(unit_u_ppu_crc,tostr(ppufile^.header.checksum));
  607. { check the object and assembler file to see if we need only to
  608. assemble, only if it's not in a library }
  609. do_compile:=false;
  610. if (flags and uf_in_library)=0 then
  611. begin
  612. if ((flags and uf_static_linked)<>0) or
  613. ((flags and uf_smartlink)<>0) then
  614. begin
  615. objfiletime:=getnamedfiletime(staticlibfilename^);
  616. Message2(unit_u_check_time,staticlibfilename^,filetimestring(objfiletime));
  617. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  618. begin
  619. Message(unit_u_recompile_staticlib_is_older);
  620. do_compile:=true;
  621. exit;
  622. end;
  623. end
  624. else
  625. if (flags and uf_shared_linked)<>0 then
  626. begin
  627. objfiletime:=getnamedfiletime(sharedlibfilename^);
  628. Message2(unit_u_check_time,sharedlibfilename^,filetimestring(objfiletime));
  629. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  630. begin
  631. Message(unit_u_recompile_sharedlib_is_older);
  632. do_compile:=true;
  633. exit;
  634. end;
  635. end
  636. else
  637. begin
  638. { the objectfile should be newer than the ppu file }
  639. objfiletime:=getnamedfiletime(objfilename^);
  640. Message2(unit_u_check_time,objfilename^,filetimestring(objfiletime));
  641. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  642. begin
  643. { check if assembler file is older than ppu file }
  644. asmfileTime:=GetNamedFileTime(asmfilename^);
  645. Message2(unit_u_check_time,asmfilename^,filetimestring(asmfiletime));
  646. if (asmfiletime<0) or (ppufiletime>asmfiletime) then
  647. begin
  648. Message(unit_u_recompile_obj_and_asm_older);
  649. do_compile:=true;
  650. exit;
  651. end
  652. else
  653. begin
  654. Message(unit_u_recompile_obj_older_than_asm);
  655. if not(cs_asm_extern in aktglobalswitches) then
  656. begin
  657. do_compile:=true;
  658. exit;
  659. end;
  660. end;
  661. end;
  662. end;
  663. end;
  664. openppu:=true;
  665. end;
  666. function tmodule.search_unit(const n : string;onlysource:boolean):boolean;
  667. var
  668. ext : string[8];
  669. singlepathstring,
  670. unitPath,
  671. filename : string;
  672. found : boolean;
  673. start,i : longint;
  674. Function UnitExists(const ext:string):boolean;
  675. begin
  676. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  677. UnitExists:=FileExists(Singlepathstring+FileName+ext);
  678. end;
  679. begin
  680. start:=1;
  681. filename:=FixFileName(n);
  682. unitpath:=UnitSearchPath;
  683. Found:=false;
  684. repeat
  685. { Create current path to check }
  686. i:=pos(';',unitpath);
  687. if i=0 then
  688. i:=length(unitpath)+1;
  689. singlepathstring:=FixPath(copy(unitpath,start,i-start),false);
  690. delete(unitpath,start,i-start+1);
  691. if not onlysource then
  692. begin
  693. { Check for PPL file }
  694. if not Found then
  695. begin
  696. Found:=UnitExists(target_info.unitlibext);
  697. if Found then
  698. Begin
  699. SetFileName(SinglePathString+FileName,false);
  700. Found:=OpenPPU;
  701. End;
  702. end;
  703. { Check for PPU file }
  704. if not Found then
  705. begin
  706. Found:=UnitExists(target_info.unitext);
  707. if Found then
  708. Begin
  709. SetFileName(SinglePathString+FileName,false);
  710. Found:=OpenPPU;
  711. End;
  712. end;
  713. end;
  714. { Check for Sources }
  715. if not Found then
  716. begin
  717. ppufile:=nil;
  718. do_compile:=true;
  719. {Check for .pp file}
  720. Found:=UnitExists(target_os.sourceext);
  721. if Found then
  722. Ext:=target_os.sourceext
  723. else
  724. begin
  725. {Check for .pas}
  726. Found:=UnitExists(target_os.pasext);
  727. if Found then
  728. Ext:=target_os.pasext;
  729. end;
  730. stringdispose(mainsource);
  731. if Found then
  732. begin
  733. sources_avail:=true;
  734. {Load Filenames when found}
  735. mainsource:=StringDup(SinglePathString+FileName+Ext);
  736. SetFileName(SinglePathString+FileName,false);
  737. end
  738. else
  739. sources_avail:=false;
  740. end;
  741. until Found or (unitpath='');
  742. search_unit:=Found;
  743. end;
  744. procedure tmodule.reset;
  745. begin
  746. {$ifndef VER0_99_8}
  747. if assigned(scanner) then
  748. pscannerfile(scanner)^.invalid:=true;
  749. if assigned(globalsymtable) then
  750. begin
  751. dispose(punitsymtable(globalsymtable),done);
  752. globalsymtable:=nil;
  753. end;
  754. if assigned(localsymtable) then
  755. begin
  756. dispose(punitsymtable(localsymtable),done);
  757. localsymtable:=nil;
  758. end;
  759. {$endif}
  760. if assigned(map) then
  761. begin
  762. dispose(map);
  763. map:=nil;
  764. end;
  765. if assigned(ppufile) then
  766. begin
  767. dispose(ppufile,done);
  768. ppufile:=nil;
  769. end;
  770. sourcefiles^.done;
  771. sourcefiles^.init;
  772. imports^.done;
  773. imports^.init;
  774. used_units.done;
  775. used_units.init;
  776. linkofiles.done;
  777. linkofiles.init_no_double;
  778. linkstaticlibs.done;
  779. linkstaticlibs.init_no_double;
  780. linksharedlibs.done;
  781. linksharedlibs.init_no_double;
  782. uses_imports:=false;
  783. do_assemble:=false;
  784. do_compile:=false;
  785. { sources_avail:=true;
  786. should not be changed PM }
  787. compiled:=false;
  788. in_implementation:=false;
  789. in_global:=true;
  790. loaded_from:=nil;
  791. flags:=0;
  792. crc:=0;
  793. unitcount:=1;
  794. end;
  795. constructor tmodule.init(const s:string;_is_unit:boolean);
  796. var
  797. p : dirstr;
  798. n : namestr;
  799. e : extstr;
  800. begin
  801. FSplit(s,p,n,e);
  802. { Programs have the name program to don't conflict with dup id's }
  803. if _is_unit then
  804. modulename:=stringdup(Upper(n))
  805. else
  806. modulename:=stringdup('PROGRAM');
  807. mainsource:=stringdup(s);
  808. ppufilename:=nil;
  809. objfilename:=nil;
  810. asmfilename:=nil;
  811. staticlibfilename:=nil;
  812. sharedlibfilename:=nil;
  813. exefilename:=nil;
  814. { Dos has the famous 8.3 limit :( }
  815. {$ifdef tp}
  816. asmprefix:=stringdup(FixFileName('as'));
  817. {$else}
  818. {$ifdef go32v2}
  819. asmprefix:=stringdup(FixFileName('as'));
  820. {$else}
  821. {$ifdef OS2}
  822. {Allthough OS/2 supports long filenames I play it safe and
  823. use 8.3 filenames, because this allows the compiler to run
  824. on a FAT partition. (DM)}
  825. asmprefix:=stringdup(FixFileName('as'));
  826. {$else}
  827. asmprefix:=stringdup(FixFileName(n));
  828. {$endif}
  829. {$endif}
  830. {$endif tp}
  831. path:=nil;
  832. setfilename(p+n,true);
  833. used_units.init;
  834. new(sourcefiles,init);
  835. linkofiles.init_no_double;
  836. linkstaticlibs.init_no_double;
  837. linksharedlibs.init_no_double;
  838. ppufile:=nil;
  839. scanner:=nil;
  840. map:=nil;
  841. globalsymtable:=nil;
  842. localsymtable:=nil;
  843. loaded_from:=nil;
  844. flags:=0;
  845. crc:=0;
  846. unitcount:=1;
  847. inc(global_unit_count);
  848. unit_index:=global_unit_count;
  849. do_assemble:=false;
  850. do_compile:=false;
  851. sources_avail:=true;
  852. compiled:=false;
  853. in_second_compile:=false;
  854. in_implementation:=false;
  855. in_global:=true;
  856. is_unit:=_is_unit;
  857. islibrary:=false;
  858. uses_imports:=false;
  859. imports:=new(plinkedlist,init);
  860. { search the PPU file if it is an unit }
  861. if is_unit then
  862. begin
  863. if (not search_unit(modulename^,false)) and (length(modulename^)>8) then
  864. search_unit(copy(modulename^,1,8),false);
  865. end;
  866. end;
  867. destructor tmodule.done;
  868. begin
  869. if assigned(map) then
  870. dispose(map);
  871. if assigned(ppufile) then
  872. dispose(ppufile,done);
  873. ppufile:=nil;
  874. if assigned(imports) then
  875. dispose(imports,done);
  876. imports:=nil;
  877. {$ifndef VER0_99_8}
  878. if assigned(scanner) then
  879. pscannerfile(scanner)^.invalid:=true;
  880. {$endif}
  881. if assigned(sourcefiles) then
  882. dispose(sourcefiles,done);
  883. sourcefiles:=nil;
  884. used_units.done;
  885. linkofiles.done;
  886. linkstaticlibs.done;
  887. linksharedlibs.done;
  888. stringdispose(objfilename);
  889. stringdispose(asmfilename);
  890. stringdispose(ppufilename);
  891. stringdispose(staticlibfilename);
  892. stringdispose(sharedlibfilename);
  893. stringdispose(exefilename);
  894. stringdispose(path);
  895. stringdispose(modulename);
  896. stringdispose(mainsource);
  897. stringdispose(asmprefix);
  898. {$ifndef VER0_99_8}
  899. if assigned(globalsymtable) then
  900. dispose(punitsymtable(globalsymtable),done);
  901. globalsymtable:=nil;
  902. if assigned(localsymtable) then
  903. dispose(punitsymtable(localsymtable),done);
  904. localsymtable:=nil;
  905. {$endif}
  906. inherited done;
  907. end;
  908. {****************************************************************************
  909. TUSED_UNIT
  910. ****************************************************************************}
  911. constructor tused_unit.init(_u : pmodule;intface:boolean);
  912. begin
  913. u:=_u;
  914. in_interface:=intface;
  915. in_uses:=false;
  916. is_stab_written:=false;
  917. loaded:=true;
  918. name:=stringdup(_u^.modulename^);
  919. checksum:=_u^.crc;
  920. unitid:=0;
  921. end;
  922. constructor tused_unit.init_to_load(const n:string;c:longint;intface:boolean);
  923. begin
  924. u:=nil;
  925. in_interface:=intface;
  926. in_uses:=false;
  927. is_stab_written:=false;
  928. loaded:=false;
  929. name:=stringdup(n);
  930. checksum:=c;
  931. unitid:=0;
  932. end;
  933. destructor tused_unit.done;
  934. begin
  935. stringdispose(name);
  936. inherited done;
  937. end;
  938. end.
  939. {
  940. $Log$
  941. Revision 1.72 1998-11-15 16:32:35 florian
  942. * some stuff of Pavel implement (win32 dll creation)
  943. * bug with ansistring function results fixed
  944. Revision 1.71 1998/11/06 09:45:40 pierre
  945. * bug on errors (file used after dispose !) fixed
  946. Revision 1.70 1998/11/03 11:33:14 peter
  947. + search_unit arg to only search for sources
  948. Revision 1.69 1998/10/29 11:35:44 florian
  949. * some dll support for win32
  950. * fixed assembler writing for PalmOS
  951. Revision 1.68 1998/10/27 10:22:34 florian
  952. + First things for win32 export sections
  953. Revision 1.67 1998/10/26 22:23:29 peter
  954. + fixpath() has an extra option to allow a ./ as path
  955. Revision 1.66 1998/10/19 18:07:11 peter
  956. + external dll_name name func support for linux
  957. Revision 1.65 1998/10/15 12:22:25 pierre
  958. * close include files immediately after end reading
  959. instead of waiting until unit compilation ended !
  960. Revision 1.64 1998/10/14 13:38:19 peter
  961. * fixed path with staticlib/objects in ppufiles
  962. Revision 1.63 1998/10/14 11:02:49 daniel
  963. * Stupid typo fixed.
  964. Revision 1.62 1998/10/14 10:59:37 daniel
  965. * Staticlibfilename now doesn't include path.
  966. Revision 1.61 1998/10/14 10:57:25 daniel
  967. * Dirstr, namestr, extstr.
  968. * $V+ to prevent Peter from forgetting this.
  969. * OS/2 compiler uses 8.3 filenames to support running the compiler on an old
  970. DOS FAT partition.
  971. Revision 1.60 1998/10/14 10:45:07 pierre
  972. * ppu problems for m68k fixed (at least in cross compiling)
  973. * one last memory leak for sysamiga fixed
  974. * the amiga RTL compiles now completely !!
  975. Revision 1.59 1998/10/13 14:01:07 peter
  976. * fixed -al
  977. Revision 1.58 1998/10/12 11:59:00 peter
  978. + show name and date of .o and .s files which the compiler checks
  979. Revision 1.57 1998/10/09 16:36:03 pierre
  980. * some memory leaks specific to usebrowser define fixed
  981. * removed tmodule.implsymtable (was like tmodule.localsymtable)
  982. Revision 1.56 1998/10/09 08:56:26 pierre
  983. * several memory leaks fixed
  984. Revision 1.55 1998/10/08 23:28:54 peter
  985. * -vu shows unit info, -vt shows tried/used files
  986. Revision 1.54 1998/10/08 17:17:19 pierre
  987. * current_module old scanner tagged as invalid if unit is recompiled
  988. + added ppheap for better info on tracegetmem of heaptrc
  989. (adds line column and file index)
  990. * several memory leaks removed ith help of heaptrc !!
  991. Revision 1.53 1998/10/08 13:48:43 peter
  992. * fixed memory leaks for do nothing source
  993. * fixed unit interdependency
  994. Revision 1.52 1998/10/06 22:09:48 peter
  995. * fixed for compiling with 0.99.8 due circular units
  996. Revision 1.51 1998/10/06 17:16:47 pierre
  997. * some memory leaks fixed (thanks to Peter for heaptrc !)
  998. Revision 1.50 1998/09/30 16:43:34 peter
  999. * fixed unit interdependency with circular uses
  1000. Revision 1.49 1998/09/28 16:57:20 pierre
  1001. * changed all length(p^.value_str^) into str_length(p)
  1002. to get it work with and without ansistrings
  1003. * changed sourcefiles field of tmodule to a pointer
  1004. Revision 1.48 1998/09/24 23:46:34 peter
  1005. + outputdir support
  1006. Revision 1.47 1998/09/22 17:13:43 pierre
  1007. + browsing updated and developed
  1008. records and objects fields are also stored
  1009. Revision 1.46 1998/09/21 08:45:10 pierre
  1010. + added vmt_offset in tobjectdef.write for fututre use
  1011. (first steps to have objects without vmt if no virtual !!)
  1012. + added fpu_used field for tabstractprocdef :
  1013. sets this level to 2 if the functions return with value in FPU
  1014. (is then set to correct value at parsing of implementation)
  1015. THIS MIGHT refuse some code with FPU expression too complex
  1016. that were accepted before and even in some cases
  1017. that don't overflow in fact
  1018. ( like if f : float; is a forward that finally in implementation
  1019. only uses one fpu register !!)
  1020. Nevertheless I think that it will improve security on
  1021. FPU operations !!
  1022. * most other changes only for UseBrowser code
  1023. (added symtable references for record and objects)
  1024. local switch for refs to args and local of each function
  1025. (static symtable still missing)
  1026. UseBrowser still not stable and probably broken by
  1027. the definition hash array !!
  1028. Revision 1.45 1998/09/18 09:58:51 peter
  1029. * -s doesn't require the .o to be available, this allows compiling of
  1030. everything on other platforms (profiling the windows.pp loading ;)
  1031. Revision 1.44 1998/09/10 13:51:32 peter
  1032. * tp compiler also uses 'as' as asmprefix
  1033. Revision 1.43 1998/09/03 17:08:45 pierre
  1034. * better lines for stabs
  1035. (no scroll back to if before else part
  1036. no return to case line at jump outside case)
  1037. + source lines also if not in order
  1038. Revision 1.42 1998/09/03 11:24:00 peter
  1039. * moved more inputfile things from tscannerfile to tinputfile
  1040. * changed ifdef Sourceline to cs_asm_source
  1041. Revision 1.41 1998/08/26 15:35:30 peter
  1042. * fixed scannerfiles for macros
  1043. + $I %<environment>%
  1044. Revision 1.40 1998/08/26 10:08:48 peter
  1045. * fixed problem with libprefix at the wrong place
  1046. * fixed lib generation with smartlinking and no -CS used
  1047. Revision 1.39 1998/08/25 16:44:16 pierre
  1048. * openppu was true even if the object file is missing
  1049. this lead to trying to open a filename without extension
  1050. and prevented the 'make cycle' to work for win32
  1051. Revision 1.38 1998/08/19 10:06:12 peter
  1052. * fixed filenames and removedir which supports slash at the end
  1053. Revision 1.37 1998/08/18 20:52:19 peter
  1054. * renamed in_main to in_global which is more logical
  1055. Revision 1.36 1998/08/17 10:10:07 peter
  1056. - removed OLDPPU
  1057. Revision 1.35 1998/08/17 09:17:44 peter
  1058. * static/shared linking updates
  1059. Revision 1.34 1998/08/14 21:56:31 peter
  1060. * setting the outputfile using -o works now to create static libs
  1061. Revision 1.33 1998/08/11 14:09:08 peter
  1062. * fixed some messages and smaller msgtxt.inc
  1063. Revision 1.32 1998/08/10 14:49:58 peter
  1064. + localswitches, moduleswitches, globalswitches splitting
  1065. Revision 1.31 1998/07/14 14:46:48 peter
  1066. * released NEWINPUT
  1067. Revision 1.30 1998/07/07 11:19:55 peter
  1068. + NEWINPUT for a better inputfile and scanner object
  1069. Revision 1.29 1998/06/25 10:51:00 pierre
  1070. * removed a remaining ifndef NEWPPU
  1071. replaced by ifdef OLDPPU
  1072. * added uf_finalize to ppu unit
  1073. Revision 1.28 1998/06/25 08:48:12 florian
  1074. * first version of rtti support
  1075. Revision 1.27 1998/06/24 14:48:34 peter
  1076. * ifdef newppu -> ifndef oldppu
  1077. Revision 1.26 1998/06/17 14:36:19 peter
  1078. * forgot an $ifndef OLDPPU :(
  1079. Revision 1.25 1998/06/17 14:10:11 peter
  1080. * small os2 fixes
  1081. * fixed interdependent units with newppu (remake3 under linux works now)
  1082. Revision 1.24 1998/06/16 08:56:20 peter
  1083. + targetcpu
  1084. * cleaner pmodules for newppu
  1085. Revision 1.23 1998/06/15 14:44:36 daniel
  1086. * BP updates.
  1087. Revision 1.22 1998/06/14 18:25:41 peter
  1088. * small fix with crc in newppu
  1089. Revision 1.21 1998/06/13 00:10:05 peter
  1090. * working browser and newppu
  1091. * some small fixes against crashes which occured in bp7 (but not in
  1092. fpc?!)
  1093. Revision 1.20 1998/06/12 14:50:48 peter
  1094. * removed the tree dependency to types.pas
  1095. * long_fil.pas support (not fully tested yet)
  1096. Revision 1.19 1998/06/12 10:32:26 pierre
  1097. * column problem hopefully solved
  1098. + C vars declaration changed
  1099. Revision 1.18 1998/06/11 13:58:07 peter
  1100. * small fix to let newppu compile
  1101. Revision 1.17 1998/06/09 16:01:40 pierre
  1102. + added procedure directive parsing for procvars
  1103. (accepted are popstack cdecl and pascal)
  1104. + added C vars with the following syntax
  1105. var C calias 'true_c_name';(can be followed by external)
  1106. reason is that you must add the Cprefix
  1107. which is target dependent
  1108. Revision 1.16 1998/06/04 10:42:19 pierre
  1109. * small bug fix in load_ppu or openppu
  1110. Revision 1.15 1998/05/28 14:37:53 peter
  1111. * default programname is PROGRAM (like TP7) to avoid dup id's
  1112. Revision 1.14 1998/05/27 19:45:02 peter
  1113. * symtable.pas splitted into includefiles
  1114. * symtable adapted for $ifndef OLDPPU
  1115. Revision 1.13 1998/05/23 01:21:05 peter
  1116. + aktasmmode, aktoptprocessor, aktoutputformat
  1117. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1118. + $LIBNAME to set the library name where the unit will be put in
  1119. * splitted cgi386 a bit (codeseg to large for bp7)
  1120. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1121. Revision 1.12 1998/05/20 09:42:33 pierre
  1122. + UseTokenInfo now default
  1123. * unit in interface uses and implementation uses gives error now
  1124. * only one error for unknown symbol (uses lastsymknown boolean)
  1125. the problem came from the label code !
  1126. + first inlined procedures and function work
  1127. (warning there might be allowed cases were the result is still wrong !!)
  1128. * UseBrower updated gives a global list of all position of all used symbols
  1129. with switch -gb
  1130. Revision 1.11 1998/05/12 10:46:59 peter
  1131. * moved printstatus to verb_def
  1132. + V_Normal which is between V_Error and V_Warning and doesn't have a
  1133. prefix like error: warning: and is included in V_Default
  1134. * fixed some messages
  1135. * first time parameter scan is only for -v and -T
  1136. - removed old style messages
  1137. Revision 1.10 1998/05/11 13:07:53 peter
  1138. + $ifndef OLDPPU for the new ppuformat
  1139. + $define GDB not longer required
  1140. * removed all warnings and stripped some log comments
  1141. * no findfirst/findnext anymore to remove smartlink *.o files
  1142. Revision 1.9 1998/05/06 15:04:20 pierre
  1143. + when trying to find source files of a ppufile
  1144. check the includepathlist for included files
  1145. the main file must still be in the same directory
  1146. Revision 1.8 1998/05/04 17:54:25 peter
  1147. + smartlinking works (only case jumptable left todo)
  1148. * redesign of systems.pas to support assemblers and linkers
  1149. + Unitname is now also in the PPU-file, increased version to 14
  1150. Revision 1.7 1998/05/01 16:38:44 florian
  1151. * handling of private and protected fixed
  1152. + change_keywords_to_tp implemented to remove
  1153. keywords which aren't supported by tp
  1154. * break and continue are now symbols of the system unit
  1155. + widestring, longstring and ansistring type released
  1156. Revision 1.6 1998/05/01 07:43:53 florian
  1157. + basics for rtti implemented
  1158. + switch $m (generate rtti for published sections)
  1159. Revision 1.5 1998/04/30 15:59:40 pierre
  1160. * GDB works again better :
  1161. correct type info in one pass
  1162. + UseTokenInfo for better source position
  1163. * fixed one remaining bug in scanner for line counts
  1164. * several little fixes
  1165. Revision 1.4 1998/04/29 10:33:52 pierre
  1166. + added some code for ansistring (not complete nor working yet)
  1167. * corrected operator overloading
  1168. * corrected nasm output
  1169. + started inline procedures
  1170. + added starstarn : use ** for exponentiation (^ gave problems)
  1171. + started UseTokenInfo cond to get accurate positions
  1172. Revision 1.3 1998/04/27 23:10:28 peter
  1173. + new scanner
  1174. * $makelib -> if smartlink
  1175. * small filename fixes pmodule.setfilename
  1176. * moved import from files.pas -> import.pas
  1177. Revision 1.2 1998/04/21 10:16:47 peter
  1178. * patches from strasbourg
  1179. * objects is not used anymore in the fpc compiled version
  1180. }