files.pas 38 KB

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