files.pas 38 KB

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