files.pas 42 KB

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