2
0

files.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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 in [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.76 1998-12-01 12:51:19 peter
  960. * fixed placing of ppas.sh and link.res when using -FE
  961. Revision 1.75 1998/11/16 15:41:40 peter
  962. * tp7 didn't like my ifopt H+ :(
  963. Revision 1.74 1998/11/16 12:18:01 peter
  964. * H+ fixes
  965. Revision 1.73 1998/11/16 11:28:58 pierre
  966. * stackcheck removed for i386_win32
  967. * exportlist does not crash at least !!
  968. (was need for tests dir !)z
  969. Revision 1.72 1998/11/15 16:32:35 florian
  970. * some stuff of Pavel implement (win32 dll creation)
  971. * bug with ansistring function results fixed
  972. Revision 1.71 1998/11/06 09:45:40 pierre
  973. * bug on errors (file used after dispose !) fixed
  974. Revision 1.70 1998/11/03 11:33:14 peter
  975. + search_unit arg to only search for sources
  976. Revision 1.69 1998/10/29 11:35:44 florian
  977. * some dll support for win32
  978. * fixed assembler writing for PalmOS
  979. Revision 1.68 1998/10/27 10:22:34 florian
  980. + First things for win32 export sections
  981. Revision 1.67 1998/10/26 22:23:29 peter
  982. + fixpath() has an extra option to allow a ./ as path
  983. Revision 1.66 1998/10/19 18:07:11 peter
  984. + external dll_name name func support for linux
  985. Revision 1.65 1998/10/15 12:22:25 pierre
  986. * close include files immediately after end reading
  987. instead of waiting until unit compilation ended !
  988. Revision 1.64 1998/10/14 13:38:19 peter
  989. * fixed path with staticlib/objects in ppufiles
  990. Revision 1.63 1998/10/14 11:02:49 daniel
  991. * Stupid typo fixed.
  992. Revision 1.62 1998/10/14 10:59:37 daniel
  993. * Staticlibfilename now doesn't include path.
  994. Revision 1.61 1998/10/14 10:57:25 daniel
  995. * Dirstr, namestr, extstr.
  996. * $V+ to prevent Peter from forgetting this.
  997. * OS/2 compiler uses 8.3 filenames to support running the compiler on an old
  998. DOS FAT partition.
  999. Revision 1.60 1998/10/14 10:45:07 pierre
  1000. * ppu problems for m68k fixed (at least in cross compiling)
  1001. * one last memory leak for sysamiga fixed
  1002. * the amiga RTL compiles now completely !!
  1003. Revision 1.59 1998/10/13 14:01:07 peter
  1004. * fixed -al
  1005. Revision 1.58 1998/10/12 11:59:00 peter
  1006. + show name and date of .o and .s files which the compiler checks
  1007. Revision 1.57 1998/10/09 16:36:03 pierre
  1008. * some memory leaks specific to usebrowser define fixed
  1009. * removed tmodule.implsymtable (was like tmodule.localsymtable)
  1010. Revision 1.56 1998/10/09 08:56:26 pierre
  1011. * several memory leaks fixed
  1012. Revision 1.55 1998/10/08 23:28:54 peter
  1013. * -vu shows unit info, -vt shows tried/used files
  1014. Revision 1.54 1998/10/08 17:17:19 pierre
  1015. * current_module old scanner tagged as invalid if unit is recompiled
  1016. + added ppheap for better info on tracegetmem of heaptrc
  1017. (adds line column and file index)
  1018. * several memory leaks removed ith help of heaptrc !!
  1019. Revision 1.53 1998/10/08 13:48:43 peter
  1020. * fixed memory leaks for do nothing source
  1021. * fixed unit interdependency
  1022. Revision 1.52 1998/10/06 22:09:48 peter
  1023. * fixed for compiling with 0.99.8 due circular units
  1024. Revision 1.51 1998/10/06 17:16:47 pierre
  1025. * some memory leaks fixed (thanks to Peter for heaptrc !)
  1026. Revision 1.50 1998/09/30 16:43:34 peter
  1027. * fixed unit interdependency with circular uses
  1028. Revision 1.49 1998/09/28 16:57:20 pierre
  1029. * changed all length(p^.value_str^) into str_length(p)
  1030. to get it work with and without ansistrings
  1031. * changed sourcefiles field of tmodule to a pointer
  1032. Revision 1.48 1998/09/24 23:46:34 peter
  1033. + outputdir support
  1034. Revision 1.47 1998/09/22 17:13:43 pierre
  1035. + browsing updated and developed
  1036. records and objects fields are also stored
  1037. Revision 1.46 1998/09/21 08:45:10 pierre
  1038. + added vmt_offset in tobjectdef.write for fututre use
  1039. (first steps to have objects without vmt if no virtual !!)
  1040. + added fpu_used field for tabstractprocdef :
  1041. sets this level to 2 if the functions return with value in FPU
  1042. (is then set to correct value at parsing of implementation)
  1043. THIS MIGHT refuse some code with FPU expression too complex
  1044. that were accepted before and even in some cases
  1045. that don't overflow in fact
  1046. ( like if f : float; is a forward that finally in implementation
  1047. only uses one fpu register !!)
  1048. Nevertheless I think that it will improve security on
  1049. FPU operations !!
  1050. * most other changes only for UseBrowser code
  1051. (added symtable references for record and objects)
  1052. local switch for refs to args and local of each function
  1053. (static symtable still missing)
  1054. UseBrowser still not stable and probably broken by
  1055. the definition hash array !!
  1056. Revision 1.45 1998/09/18 09:58:51 peter
  1057. * -s doesn't require the .o to be available, this allows compiling of
  1058. everything on other platforms (profiling the windows.pp loading ;)
  1059. Revision 1.44 1998/09/10 13:51:32 peter
  1060. * tp compiler also uses 'as' as asmprefix
  1061. Revision 1.43 1998/09/03 17:08:45 pierre
  1062. * better lines for stabs
  1063. (no scroll back to if before else part
  1064. no return to case line at jump outside case)
  1065. + source lines also if not in order
  1066. Revision 1.42 1998/09/03 11:24:00 peter
  1067. * moved more inputfile things from tscannerfile to tinputfile
  1068. * changed ifdef Sourceline to cs_asm_source
  1069. Revision 1.41 1998/08/26 15:35:30 peter
  1070. * fixed scannerfiles for macros
  1071. + $I %<environment>%
  1072. Revision 1.40 1998/08/26 10:08:48 peter
  1073. * fixed problem with libprefix at the wrong place
  1074. * fixed lib generation with smartlinking and no -CS used
  1075. Revision 1.39 1998/08/25 16:44:16 pierre
  1076. * openppu was true even if the object file is missing
  1077. this lead to trying to open a filename without extension
  1078. and prevented the 'make cycle' to work for win32
  1079. Revision 1.38 1998/08/19 10:06:12 peter
  1080. * fixed filenames and removedir which supports slash at the end
  1081. Revision 1.37 1998/08/18 20:52:19 peter
  1082. * renamed in_main to in_global which is more logical
  1083. Revision 1.36 1998/08/17 10:10:07 peter
  1084. - removed OLDPPU
  1085. Revision 1.35 1998/08/17 09:17:44 peter
  1086. * static/shared linking updates
  1087. Revision 1.34 1998/08/14 21:56:31 peter
  1088. * setting the outputfile using -o works now to create static libs
  1089. Revision 1.33 1998/08/11 14:09:08 peter
  1090. * fixed some messages and smaller msgtxt.inc
  1091. Revision 1.32 1998/08/10 14:49:58 peter
  1092. + localswitches, moduleswitches, globalswitches splitting
  1093. Revision 1.31 1998/07/14 14:46:48 peter
  1094. * released NEWINPUT
  1095. Revision 1.30 1998/07/07 11:19:55 peter
  1096. + NEWINPUT for a better inputfile and scanner object
  1097. Revision 1.29 1998/06/25 10:51:00 pierre
  1098. * removed a remaining ifndef NEWPPU
  1099. replaced by ifdef OLDPPU
  1100. * added uf_finalize to ppu unit
  1101. Revision 1.28 1998/06/25 08:48:12 florian
  1102. * first version of rtti support
  1103. Revision 1.27 1998/06/24 14:48:34 peter
  1104. * ifdef newppu -> ifndef oldppu
  1105. Revision 1.26 1998/06/17 14:36:19 peter
  1106. * forgot an $ifndef OLDPPU :(
  1107. Revision 1.25 1998/06/17 14:10:11 peter
  1108. * small os2 fixes
  1109. * fixed interdependent units with newppu (remake3 under linux works now)
  1110. Revision 1.24 1998/06/16 08:56:20 peter
  1111. + targetcpu
  1112. * cleaner pmodules for newppu
  1113. Revision 1.23 1998/06/15 14:44:36 daniel
  1114. * BP updates.
  1115. Revision 1.22 1998/06/14 18:25:41 peter
  1116. * small fix with crc in newppu
  1117. Revision 1.21 1998/06/13 00:10:05 peter
  1118. * working browser and newppu
  1119. * some small fixes against crashes which occured in bp7 (but not in
  1120. fpc?!)
  1121. Revision 1.20 1998/06/12 14:50:48 peter
  1122. * removed the tree dependency to types.pas
  1123. * long_fil.pas support (not fully tested yet)
  1124. Revision 1.19 1998/06/12 10:32:26 pierre
  1125. * column problem hopefully solved
  1126. + C vars declaration changed
  1127. Revision 1.18 1998/06/11 13:58:07 peter
  1128. * small fix to let newppu compile
  1129. Revision 1.17 1998/06/09 16:01:40 pierre
  1130. + added procedure directive parsing for procvars
  1131. (accepted are popstack cdecl and pascal)
  1132. + added C vars with the following syntax
  1133. var C calias 'true_c_name';(can be followed by external)
  1134. reason is that you must add the Cprefix
  1135. which is target dependent
  1136. Revision 1.16 1998/06/04 10:42:19 pierre
  1137. * small bug fix in load_ppu or openppu
  1138. Revision 1.15 1998/05/28 14:37:53 peter
  1139. * default programname is PROGRAM (like TP7) to avoid dup id's
  1140. Revision 1.14 1998/05/27 19:45:02 peter
  1141. * symtable.pas splitted into includefiles
  1142. * symtable adapted for $ifndef OLDPPU
  1143. Revision 1.13 1998/05/23 01:21:05 peter
  1144. + aktasmmode, aktoptprocessor, aktoutputformat
  1145. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1146. + $LIBNAME to set the library name where the unit will be put in
  1147. * splitted cgi386 a bit (codeseg to large for bp7)
  1148. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1149. Revision 1.12 1998/05/20 09:42:33 pierre
  1150. + UseTokenInfo now default
  1151. * unit in interface uses and implementation uses gives error now
  1152. * only one error for unknown symbol (uses lastsymknown boolean)
  1153. the problem came from the label code !
  1154. + first inlined procedures and function work
  1155. (warning there might be allowed cases were the result is still wrong !!)
  1156. * UseBrower updated gives a global list of all position of all used symbols
  1157. with switch -gb
  1158. Revision 1.11 1998/05/12 10:46:59 peter
  1159. * moved printstatus to verb_def
  1160. + V_Normal which is between V_Error and V_Warning and doesn't have a
  1161. prefix like error: warning: and is included in V_Default
  1162. * fixed some messages
  1163. * first time parameter scan is only for -v and -T
  1164. - removed old style messages
  1165. Revision 1.10 1998/05/11 13:07:53 peter
  1166. + $ifndef OLDPPU for the new ppuformat
  1167. + $define GDB not longer required
  1168. * removed all warnings and stripped some log comments
  1169. * no findfirst/findnext anymore to remove smartlink *.o files
  1170. Revision 1.9 1998/05/06 15:04:20 pierre
  1171. + when trying to find source files of a ppufile
  1172. check the includepathlist for included files
  1173. the main file must still be in the same directory
  1174. Revision 1.8 1998/05/04 17:54:25 peter
  1175. + smartlinking works (only case jumptable left todo)
  1176. * redesign of systems.pas to support assemblers and linkers
  1177. + Unitname is now also in the PPU-file, increased version to 14
  1178. Revision 1.7 1998/05/01 16:38:44 florian
  1179. * handling of private and protected fixed
  1180. + change_keywords_to_tp implemented to remove
  1181. keywords which aren't supported by tp
  1182. * break and continue are now symbols of the system unit
  1183. + widestring, longstring and ansistring type released
  1184. Revision 1.6 1998/05/01 07:43:53 florian
  1185. + basics for rtti implemented
  1186. + switch $m (generate rtti for published sections)
  1187. Revision 1.5 1998/04/30 15:59:40 pierre
  1188. * GDB works again better :
  1189. correct type info in one pass
  1190. + UseTokenInfo for better source position
  1191. * fixed one remaining bug in scanner for line counts
  1192. * several little fixes
  1193. Revision 1.4 1998/04/29 10:33:52 pierre
  1194. + added some code for ansistring (not complete nor working yet)
  1195. * corrected operator overloading
  1196. * corrected nasm output
  1197. + started inline procedures
  1198. + added starstarn : use ** for exponentiation (^ gave problems)
  1199. + started UseTokenInfo cond to get accurate positions
  1200. Revision 1.3 1998/04/27 23:10:28 peter
  1201. + new scanner
  1202. * $makelib -> if smartlink
  1203. * small filename fixes pmodule.setfilename
  1204. * moved import from files.pas -> import.pas
  1205. Revision 1.2 1998/04/21 10:16:47 peter
  1206. * patches from strasbourg
  1207. * objects is not used anymore in the fpc compiled version
  1208. }