fppu.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the first loading and searching of the modules
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit fppu;
  18. {$i fpcdefs.inc}
  19. { close ppufiles on system that are
  20. short on file handles like DOS system PM }
  21. {$ifdef GO32V2}
  22. {$define SHORT_ON_FILE_HANDLES}
  23. {$endif GO32V2}
  24. {$ifdef WATCOM}
  25. {$define SHORT_ON_FILE_HANDLES}
  26. {$endif WATCOM}
  27. interface
  28. uses
  29. cutils,cclasses,
  30. globtype,globals,finput,fmodule,
  31. symbase,ppu,symtype;
  32. type
  33. tppumodule = class(tmodule)
  34. ppufile : tcompilerppufile; { the PPU file }
  35. sourcefn : pstring; { Source specified with "uses .. in '..'" }
  36. {$ifdef Test_Double_checksum}
  37. crc_array : pointer;
  38. crc_size : longint;
  39. crc_array2 : pointer;
  40. crc_size2 : longint;
  41. {$endif def Test_Double_checksum}
  42. constructor create(LoadedFrom:TModule;const s:string;const fn:string;_is_unit:boolean);
  43. destructor destroy;override;
  44. procedure reset;override;
  45. function openppu:boolean;
  46. procedure getppucrc;
  47. procedure writeppu;
  48. procedure loadppu;
  49. function needrecompile:boolean;
  50. private
  51. function search_unit(onlysource,shortname:boolean):boolean;
  52. procedure load_interface;
  53. procedure load_implementation;
  54. procedure load_symtable_refs;
  55. procedure load_usedunits;
  56. procedure writesourcefiles;
  57. procedure writeusedunit(intf:boolean);
  58. procedure writelinkcontainer(var p:tlinkcontainer;id:byte;strippath:boolean);
  59. procedure writederefmap;
  60. procedure writederefdata;
  61. procedure readsourcefiles;
  62. procedure readloadunit;
  63. procedure readlinkcontainer(var p:tlinkcontainer);
  64. procedure readderefmap;
  65. procedure readderefdata;
  66. {$IFDEF MACRO_DIFF_HINT}
  67. procedure writeusedmacro(p:TNamedIndexItem;arg:pointer);
  68. procedure writeusedmacros;
  69. procedure readusedmacros;
  70. {$ENDIF}
  71. end;
  72. procedure reload_flagged_units;
  73. function registerunit(callermodule:tmodule;const s : stringid;const fn:string) : tppumodule;
  74. implementation
  75. uses
  76. verbose,systems,version,
  77. symtable, symsym,
  78. scanner,
  79. aasmbase,
  80. parser;
  81. {****************************************************************************
  82. Helpers
  83. ****************************************************************************}
  84. procedure reload_flagged_units;
  85. var
  86. hp : tmodule;
  87. begin
  88. { now reload all dependent units }
  89. hp:=tmodule(loaded_units.first);
  90. while assigned(hp) do
  91. begin
  92. if hp.do_reload then
  93. tppumodule(hp).loadppu;
  94. hp:=tmodule(hp.next);
  95. end;
  96. end;
  97. {****************************************************************************
  98. TPPUMODULE
  99. ****************************************************************************}
  100. constructor tppumodule.create(LoadedFrom:TModule;const s:string;const fn:string;_is_unit:boolean);
  101. begin
  102. inherited create(LoadedFrom,s,_is_unit);
  103. ppufile:=nil;
  104. sourcefn:=stringdup(fn);
  105. end;
  106. destructor tppumodule.Destroy;
  107. begin
  108. if assigned(ppufile) then
  109. ppufile.free;
  110. ppufile:=nil;
  111. stringdispose(sourcefn);
  112. inherited Destroy;
  113. end;
  114. procedure tppumodule.reset;
  115. begin
  116. if assigned(ppufile) then
  117. begin
  118. ppufile.free;
  119. ppufile:=nil;
  120. end;
  121. inherited reset;
  122. end;
  123. function tppumodule.openppu:boolean;
  124. var
  125. ppufiletime : longint;
  126. begin
  127. openppu:=false;
  128. Message1(unit_t_ppu_loading,ppufilename^);
  129. { Get ppufile time (also check if the file exists) }
  130. ppufiletime:=getnamedfiletime(ppufilename^);
  131. if ppufiletime=-1 then
  132. exit;
  133. { Open the ppufile }
  134. Message1(unit_u_ppu_name,ppufilename^);
  135. ppufile:=tcompilerppufile.create(ppufilename^);
  136. if not ppufile.openfile then
  137. begin
  138. ppufile.free;
  139. ppufile:=nil;
  140. Message(unit_u_ppu_file_too_short);
  141. exit;
  142. end;
  143. { check for a valid PPU file }
  144. if not ppufile.CheckPPUId then
  145. begin
  146. ppufile.free;
  147. ppufile:=nil;
  148. Message(unit_u_ppu_invalid_header);
  149. exit;
  150. end;
  151. { check for allowed PPU versions }
  152. if not (ppufile.GetPPUVersion = CurrentPPUVersion) then
  153. begin
  154. Message1(unit_u_ppu_invalid_version,tostr(ppufile.GetPPUVersion));
  155. ppufile.free;
  156. ppufile:=nil;
  157. exit;
  158. end;
  159. { check the target processor }
  160. if tsystemcpu(ppufile.header.cpu)<>target_cpu then
  161. begin
  162. ppufile.free;
  163. ppufile:=nil;
  164. Message(unit_u_ppu_invalid_processor);
  165. exit;
  166. end;
  167. { check target }
  168. if tsystem(ppufile.header.target)<>target_info.system then
  169. begin
  170. ppufile.free;
  171. ppufile:=nil;
  172. Message(unit_u_ppu_invalid_target);
  173. exit;
  174. end;
  175. {$ifdef cpufpemu}
  176. { check if floating point emulation is on?}
  177. { fpu emulation isn't unit levelwise
  178. if ((ppufile.header.flags and uf_fpu_emulation)<>0) and
  179. (cs_fp_emulation in aktmoduleswitches) then
  180. begin
  181. ppufile.free;
  182. ppufile:=nil;
  183. Message(unit_u_ppu_invalid_fpumode);
  184. exit;
  185. end;
  186. }
  187. {$endif cpufpemu}
  188. { Load values to be access easier }
  189. flags:=ppufile.header.flags;
  190. crc:=ppufile.header.checksum;
  191. interface_crc:=ppufile.header.interface_checksum;
  192. { Show Debug info }
  193. Message1(unit_u_ppu_time,filetimestring(ppufiletime));
  194. Message1(unit_u_ppu_flags,tostr(flags));
  195. Message1(unit_u_ppu_crc,hexstr(ppufile.header.checksum,8));
  196. Message1(unit_u_ppu_crc,hexstr(ppufile.header.interface_checksum,8)+' (intfc)');
  197. do_compile:=false;
  198. openppu:=true;
  199. end;
  200. function tppumodule.search_unit(onlysource,shortname:boolean):boolean;
  201. var
  202. singlepathstring,
  203. filename : string;
  204. Function UnitExists(const ext:string;var foundfile:string):boolean;
  205. begin
  206. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  207. UnitExists:=FindFile(FileName+ext,Singlepathstring,foundfile);
  208. end;
  209. Function PPUSearchPath(const s:string):boolean;
  210. var
  211. found : boolean;
  212. hs : string;
  213. begin
  214. Found:=false;
  215. singlepathstring:=FixPath(s,false);
  216. { Check for PPU file }
  217. Found:=UnitExists(target_info.unitext,hs);
  218. if Found then
  219. Begin
  220. SetFileName(hs,false);
  221. Found:=OpenPPU;
  222. End;
  223. PPUSearchPath:=Found;
  224. end;
  225. Function SourceSearchPath(const s:string):boolean;
  226. var
  227. found : boolean;
  228. hs : string;
  229. begin
  230. Found:=false;
  231. singlepathstring:=FixPath(s,false);
  232. { Check for Sources }
  233. ppufile:=nil;
  234. do_compile:=true;
  235. recompile_reason:=rr_noppu;
  236. {Check for .pp file}
  237. Found:=UnitExists(sourceext,hs);
  238. if not Found then
  239. begin
  240. { Check for .pas }
  241. Found:=UnitExists(pasext,hs);
  242. end;
  243. if not Found and (m_mac in aktmodeswitches) then
  244. begin
  245. { Check for .p, if mode is macpas}
  246. Found:=UnitExists(pext,hs);
  247. end;
  248. stringdispose(mainsource);
  249. if Found then
  250. begin
  251. sources_avail:=true;
  252. { Load Filenames when found }
  253. mainsource:=StringDup(hs);
  254. SetFileName(hs,false);
  255. end
  256. else
  257. sources_avail:=false;
  258. SourceSearchPath:=Found;
  259. end;
  260. Function SearchPath(const s:string):boolean;
  261. var
  262. found : boolean;
  263. begin
  264. { First check for a ppu, then for the source }
  265. found:=false;
  266. if not onlysource then
  267. found:=PPUSearchPath(s);
  268. if not found then
  269. found:=SourceSearchPath(s);
  270. SearchPath:=found;
  271. end;
  272. Function SearchPathList(list:TSearchPathList):boolean;
  273. var
  274. hp : TStringListItem;
  275. found : boolean;
  276. begin
  277. found:=false;
  278. hp:=TStringListItem(list.First);
  279. while assigned(hp) do
  280. begin
  281. found:=SearchPath(hp.Str);
  282. if found then
  283. break;
  284. hp:=TStringListItem(hp.next);
  285. end;
  286. SearchPathList:=found;
  287. end;
  288. var
  289. fnd : boolean;
  290. hs : string;
  291. begin
  292. if shortname then
  293. filename:=FixFileName(Copy(realmodulename^,1,8))
  294. else
  295. filename:=FixFileName(realmodulename^);
  296. { try to find unit
  297. 1. look for ppu in cwd
  298. 2. look for ppu in outputpath if set, this is tp7 compatible (PFV)
  299. 3. look for the specified source file (from the uses line)
  300. 4. look for source in cwd
  301. 5. look in same path as local unit
  302. 6. local unit pathlist
  303. 7. global unit pathlist }
  304. fnd:=false;
  305. if not onlysource then
  306. begin
  307. fnd:=PPUSearchPath('.');
  308. if (not fnd) and (outputpath^<>'') then
  309. fnd:=PPUSearchPath(outputpath^);
  310. end;
  311. if (not fnd) and (sourcefn^<>'') then
  312. begin
  313. { the full filename is specified so we can't use here the
  314. searchpath (PFV) }
  315. Message1(unit_t_unitsearch,AddExtension(sourcefn^,sourceext));
  316. fnd:=FindFile(AddExtension(sourcefn^,sourceext),'',hs);
  317. if not fnd then
  318. begin
  319. Message1(unit_t_unitsearch,AddExtension(sourcefn^,pasext));
  320. fnd:=FindFile(AddExtension(sourcefn^,pasext),'',hs);
  321. end;
  322. if not fnd and ((m_mac in aktmodeswitches) or (tf_p_ext_support in target_info.flags)) then
  323. begin
  324. Message1(unit_t_unitsearch,AddExtension(sourcefn^,pext));
  325. fnd:=FindFile(AddExtension(sourcefn^,pext),'',hs);
  326. end;
  327. if fnd then
  328. begin
  329. sources_avail:=true;
  330. do_compile:=true;
  331. recompile_reason:=rr_noppu;
  332. stringdispose(mainsource);
  333. mainsource:=StringDup(hs);
  334. SetFileName(hs,false);
  335. end;
  336. end;
  337. if not fnd then
  338. fnd:=SourceSearchPath('.');
  339. if (not fnd) and Assigned(Loaded_From) then
  340. begin
  341. fnd:=PPUSearchPath(Loaded_From.Path^);
  342. if not fnd then
  343. fnd:=SourceSearchPath(Loaded_From.Path^);
  344. if not fnd then
  345. fnd:=SearchPathList(Loaded_From.LocalUnitSearchPath);
  346. end;
  347. if not fnd then
  348. fnd:=SearchPathList(UnitSearchPath);
  349. { try to find a file with the first 8 chars of the modulename, like
  350. dos }
  351. if (not fnd) and (length(filename)>8) then
  352. begin
  353. filename:=copy(filename,1,8);
  354. fnd:=SearchPath('.');
  355. if (not fnd) then
  356. fnd:=SearchPathList(LocalUnitSearchPath);
  357. if not fnd then
  358. fnd:=SearchPathList(UnitSearchPath);
  359. end;
  360. search_unit:=fnd;
  361. end;
  362. {**********************************
  363. PPU Reading/Writing Helpers
  364. ***********************************}
  365. {$IFDEF MACRO_DIFF_HINT}
  366. var
  367. is_initial: Boolean;
  368. procedure tppumodule.writeusedmacro(p:TNamedIndexItem;arg:pointer);
  369. begin
  370. if tmacro(p).is_used or is_initial then
  371. begin
  372. ppufile.putstring(p.name);
  373. ppufile.putbyte(byte(is_initial));
  374. ppufile.putbyte(byte(tmacro(p).is_used));
  375. end;
  376. end;
  377. procedure tppumodule.writeusedmacros;
  378. begin
  379. ppufile.do_crc:=false;
  380. is_initial:= true;
  381. initialmacrosymtable.foreach(@writeusedmacro,nil);
  382. is_initial:= false;
  383. if assigned(globalmacrosymtable) then
  384. globalmacrosymtable.foreach(@writeusedmacro,nil);
  385. localmacrosymtable.foreach(@writeusedmacro,nil);
  386. ppufile.writeentry(ibusedmacros);
  387. ppufile.do_crc:=true;
  388. end;
  389. {$ENDIF}
  390. procedure tppumodule.writesourcefiles;
  391. var
  392. hp : tinputfile;
  393. i,j : longint;
  394. begin
  395. { second write the used source files }
  396. ppufile.do_crc:=false;
  397. hp:=sourcefiles.files;
  398. { write source files directly in good order }
  399. j:=0;
  400. while assigned(hp) do
  401. begin
  402. inc(j);
  403. hp:=hp.ref_next;
  404. end;
  405. while j>0 do
  406. begin
  407. hp:=sourcefiles.files;
  408. for i:=1 to j-1 do
  409. hp:=hp.ref_next;
  410. ppufile.putstring(hp.name^);
  411. ppufile.putlongint(hp.getfiletime);
  412. dec(j);
  413. end;
  414. ppufile.writeentry(ibsourcefiles);
  415. ppufile.do_crc:=true;
  416. end;
  417. procedure tppumodule.writeusedunit(intf:boolean);
  418. var
  419. hp : tused_unit;
  420. oldcrc : boolean;
  421. begin
  422. { write a reference for each used unit }
  423. hp:=tused_unit(used_units.first);
  424. while assigned(hp) do
  425. begin
  426. if hp.in_interface=intf then
  427. begin
  428. ppufile.putstring(hp.u.realmodulename^);
  429. { the checksum should not affect the crc of this unit ! (PFV) }
  430. oldcrc:=ppufile.do_crc;
  431. ppufile.do_crc:=false;
  432. ppufile.putlongint(longint(hp.checksum));
  433. ppufile.putlongint(longint(hp.interface_checksum));
  434. ppufile.do_crc:=oldcrc;
  435. end;
  436. hp:=tused_unit(hp.next);
  437. end;
  438. ppufile.do_interface_crc:=true;
  439. ppufile.writeentry(ibloadunit);
  440. end;
  441. procedure tppumodule.writelinkcontainer(var p:tlinkcontainer;id:byte;strippath:boolean);
  442. var
  443. hcontainer : tlinkcontainer;
  444. s : string;
  445. mask : cardinal;
  446. begin
  447. hcontainer:=TLinkContainer.Create;
  448. while not p.empty do
  449. begin
  450. s:=p.get(mask);
  451. if strippath then
  452. ppufile.putstring(SplitFileName(s))
  453. else
  454. ppufile.putstring(s);
  455. ppufile.putlongint(mask);
  456. hcontainer.add(s,mask);
  457. end;
  458. ppufile.writeentry(id);
  459. p.Free;
  460. p:=hcontainer;
  461. end;
  462. procedure tppumodule.writederefmap;
  463. var
  464. i : longint;
  465. oldcrc : boolean;
  466. begin
  467. { This does not influence crc }
  468. oldcrc:=ppufile.do_crc;
  469. ppufile.do_crc:=false;
  470. { The unit map used for resolving }
  471. ppufile.putlongint(derefmapcnt);
  472. for i:=0 to derefmapcnt-1 do
  473. begin
  474. if not assigned(derefmap[i].u) then
  475. internalerror(2005011512);
  476. ppufile.putstring(derefmap[i].u.modulename^)
  477. end;
  478. ppufile.writeentry(ibderefmap);
  479. ppufile.do_crc:=oldcrc;
  480. end;
  481. procedure tppumodule.writederefdata;
  482. var
  483. oldcrc : boolean;
  484. len,hlen : longint;
  485. buf : array[0..1023] of byte;
  486. begin
  487. if derefdataintflen>derefdata.size then
  488. internalerror(200310223);
  489. derefdata.seek(0);
  490. { Write interface data }
  491. len:=derefdataintflen;
  492. while (len>0) do
  493. begin
  494. if len>1024 then
  495. hlen:=1024
  496. else
  497. hlen:=len;
  498. derefdata.read(buf,hlen);
  499. ppufile.putdata(buf,hlen);
  500. dec(len,hlen);
  501. end;
  502. { Write implementation data, this does not influence crc }
  503. oldcrc:=ppufile.do_crc;
  504. ppufile.do_crc:=false;
  505. len:=derefdata.size-derefdataintflen;
  506. while (len>0) do
  507. begin
  508. if len>1024 then
  509. hlen:=1024
  510. else
  511. hlen:=len;
  512. derefdata.read(buf,hlen);
  513. ppufile.putdata(buf,hlen);
  514. dec(len,hlen);
  515. end;
  516. if derefdata.pos<>derefdata.size then
  517. internalerror(200310224);
  518. ppufile.do_crc:=oldcrc;
  519. ppufile.writeentry(ibderefdata);
  520. end;
  521. {$IFDEF MACRO_DIFF_HINT}
  522. {
  523. Define MACRO_DIFF_HINT for the whole compiler (and ppudump)
  524. to turn this facility on. Also the hint messages defined
  525. below must be commented in in the msg/errore.msg file.
  526. There is some problems with this, thats why it is shut off:
  527. At the first compilation, consider a macro which is not initially
  528. defined, but it is used (e g the check that it is undefined is true).
  529. Since it do not exist, there is no macro object where the is_used
  530. flag can be set. Later on when the macro is defined, and the ppu
  531. is opened, the check cannot detect this.
  532. Also, in which macro object should this flag be set ? It cant be set
  533. for macros in the initialmacrosymboltable since this table is shared
  534. between different files.
  535. }
  536. procedure tppumodule.readusedmacros;
  537. var
  538. hs : string;
  539. mac : tmacro;
  540. was_initial,
  541. was_used : boolean;
  542. {Reads macros which was defined or used when the module was compiled.
  543. This is done when a ppu file is open, before it possibly is parsed.}
  544. begin
  545. while not ppufile.endofentry do
  546. begin
  547. hs:=ppufile.getstring;
  548. was_initial:=boolean(ppufile.getbyte);
  549. was_used:=boolean(ppufile.getbyte);
  550. mac:=tmacro(initialmacrosymtable.search(hs));
  551. if assigned(mac) then
  552. begin
  553. {$ifndef EXTDEBUG}
  554. { if we don't have the sources why tell }
  555. if sources_avail then
  556. {$endif ndef EXTDEBUG}
  557. if (not was_initial) and was_used then
  558. Message2(unit_h_cond_not_set_in_last_compile,hs,mainsource^);
  559. end
  560. else { not assigned }
  561. if was_initial and
  562. was_used then
  563. Message2(unit_h_cond_set_in_last_compile,hs,mainsource^);
  564. end;
  565. end;
  566. {$ENDIF}
  567. procedure tppumodule.readsourcefiles;
  568. var
  569. temp,hs : string;
  570. temp_dir : string;
  571. main_dir : string;
  572. found,
  573. is_main : boolean;
  574. orgfiletime,
  575. source_time : longint;
  576. hp : tinputfile;
  577. begin
  578. sources_avail:=true;
  579. is_main:=true;
  580. main_dir:='';
  581. while not ppufile.endofentry do
  582. begin
  583. hs:=ppufile.getstring;
  584. orgfiletime:=ppufile.getlongint;
  585. temp_dir:='';
  586. if (flags and uf_in_library)<>0 then
  587. begin
  588. sources_avail:=false;
  589. temp:=' library';
  590. end
  591. else if pos('Macro ',hs)=1 then
  592. begin
  593. { we don't want to find this file }
  594. { but there is a problem with file indexing !! }
  595. temp:='';
  596. end
  597. else
  598. begin
  599. { check the date of the source files:
  600. 1 path of ppu
  601. 2 path of main source
  602. 3 current dir
  603. 4 include/unit path }
  604. Source_Time:=GetNamedFileTime(path^+hs);
  605. found:=false;
  606. if Source_Time<>-1 then
  607. hs:=path^+hs
  608. else
  609. if not(is_main) then
  610. begin
  611. Source_Time:=GetNamedFileTime(main_dir+hs);
  612. if Source_Time<>-1 then
  613. hs:=main_dir+hs;
  614. end;
  615. if Source_Time=-1 then
  616. Source_Time:=GetNamedFileTime(hs);
  617. if (Source_Time=-1) then
  618. begin
  619. if is_main then
  620. found:=unitsearchpath.FindFile(hs,temp_dir)
  621. else
  622. found:=includesearchpath.FindFile(hs,temp_dir);
  623. if found then
  624. begin
  625. Source_Time:=GetNamedFileTime(temp_dir);
  626. if Source_Time<>-1 then
  627. hs:=temp_dir;
  628. end;
  629. end;
  630. if Source_Time<>-1 then
  631. begin
  632. if is_main then
  633. main_dir:=splitpath(hs);
  634. temp:=' time '+filetimestring(source_time);
  635. if (orgfiletime<>-1) and
  636. (source_time<>orgfiletime) then
  637. begin
  638. if ((flags and uf_release)=0) then
  639. begin
  640. do_compile:=true;
  641. recompile_reason:=rr_sourcenewer;
  642. end
  643. else
  644. Message2(unit_h_source_modified,hs,ppufilename^);
  645. temp:=temp+' *';
  646. end;
  647. end
  648. else
  649. begin
  650. sources_avail:=false;
  651. temp:=' not found';
  652. end;
  653. hp:=tinputfile.create(hs);
  654. { the indexing is wrong here PM }
  655. sourcefiles.register_file(hp);
  656. end;
  657. if is_main then
  658. begin
  659. stringdispose(mainsource);
  660. mainsource:=stringdup(hs);
  661. end;
  662. Message1(unit_u_ppu_source,hs+temp);
  663. is_main:=false;
  664. end;
  665. { check if we want to rebuild every unit, only if the sources are
  666. available }
  667. if do_build and sources_avail and
  668. ((flags and uf_release)=0) then
  669. begin
  670. do_compile:=true;
  671. recompile_reason:=rr_build;
  672. end;
  673. end;
  674. procedure tppumodule.readloadunit;
  675. var
  676. hs : string;
  677. pu : tused_unit;
  678. hp : tppumodule;
  679. intfchecksum,
  680. checksum : cardinal;
  681. begin
  682. while not ppufile.endofentry do
  683. begin
  684. hs:=ppufile.getstring;
  685. checksum:=cardinal(ppufile.getlongint);
  686. intfchecksum:=cardinal(ppufile.getlongint);
  687. { set the state of this unit before registering, this is
  688. needed for a correct circular dependency check }
  689. hp:=registerunit(self,hs,'');
  690. pu:=addusedunit(hp,false,nil);
  691. pu.checksum:=checksum;
  692. pu.interface_checksum:=intfchecksum;
  693. end;
  694. in_interface:=false;
  695. end;
  696. procedure tppumodule.readlinkcontainer(var p:tlinkcontainer);
  697. var
  698. s : string;
  699. m : longint;
  700. begin
  701. while not ppufile.endofentry do
  702. begin
  703. s:=ppufile.getstring;
  704. m:=ppufile.getlongint;
  705. p.add(s,m);
  706. end;
  707. end;
  708. procedure tppumodule.readderefmap;
  709. var
  710. i : longint;
  711. begin
  712. { Load unit map used for resolving }
  713. derefmapsize:=ppufile.getlongint;
  714. derefmapcnt:=derefmapsize;
  715. getmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  716. fillchar(derefmap^,derefmapsize*sizeof(tderefmaprec),0);
  717. for i:=0 to derefmapsize-1 do
  718. derefmap[i].modulename:=stringdup(ppufile.getstring);
  719. end;
  720. procedure tppumodule.readderefdata;
  721. var
  722. len,hlen : longint;
  723. buf : array[0..1023] of byte;
  724. begin
  725. len:=ppufile.entrysize;
  726. while (len>0) do
  727. begin
  728. if len>1024 then
  729. hlen:=1024
  730. else
  731. hlen:=len;
  732. ppufile.getdata(buf,hlen);
  733. derefdata.write(buf,hlen);
  734. dec(len,hlen);
  735. end;
  736. end;
  737. procedure tppumodule.load_interface;
  738. var
  739. b : byte;
  740. newmodulename : string;
  741. begin
  742. { read interface part }
  743. repeat
  744. b:=ppufile.readentry;
  745. case b of
  746. ibmodulename :
  747. begin
  748. newmodulename:=ppufile.getstring;
  749. if (cs_check_unit_name in aktglobalswitches) and
  750. (upper(newmodulename)<>modulename^) then
  751. Message2(unit_f_unit_name_error,realmodulename^,newmodulename);
  752. stringdispose(modulename);
  753. stringdispose(realmodulename);
  754. modulename:=stringdup(upper(newmodulename));
  755. realmodulename:=stringdup(newmodulename);
  756. end;
  757. ibsourcefiles :
  758. readsourcefiles;
  759. {$IFDEF MACRO_DIFF_HINT}
  760. ibusedmacros :
  761. readusedmacros;
  762. {$ENDIF}
  763. ibloadunit :
  764. readloadunit;
  765. iblinkunitofiles :
  766. readlinkcontainer(LinkUnitOFiles);
  767. iblinkunitstaticlibs :
  768. readlinkcontainer(LinkUnitStaticLibs);
  769. iblinkunitsharedlibs :
  770. readlinkcontainer(LinkUnitSharedLibs);
  771. iblinkotherofiles :
  772. readlinkcontainer(LinkotherOFiles);
  773. iblinkotherstaticlibs :
  774. readlinkcontainer(LinkotherStaticLibs);
  775. iblinkothersharedlibs :
  776. readlinkcontainer(LinkotherSharedLibs);
  777. iblinkdlls :
  778. readlinkcontainer(LinkDlls);
  779. ibderefmap :
  780. readderefmap;
  781. ibderefdata :
  782. readderefdata;
  783. ibendinterface :
  784. break;
  785. else
  786. Message1(unit_f_ppu_invalid_entry,tostr(b));
  787. end;
  788. { we can already stop when we know that we must recompile }
  789. if do_compile then
  790. exit;
  791. until false;
  792. end;
  793. procedure tppumodule.load_implementation;
  794. var
  795. b : byte;
  796. begin
  797. { read implementation part }
  798. repeat
  799. b:=ppufile.readentry;
  800. case b of
  801. ibloadunit :
  802. readloadunit;
  803. ibasmsymbols :
  804. {$warning TODO Remove ibasmsymbols}
  805. ;
  806. ibendimplementation :
  807. break;
  808. else
  809. Message1(unit_f_ppu_invalid_entry,tostr(b));
  810. end;
  811. until false;
  812. end;
  813. procedure tppumodule.load_symtable_refs;
  814. var
  815. b : byte;
  816. i : longint;
  817. begin
  818. if (flags and uf_has_browser)<>0 then
  819. begin
  820. tstoredsymtable(globalsymtable).load_references(ppufile,true);
  821. for i:=0 to unitmapsize-1 do
  822. tstoredsymtable(globalsymtable).load_references(ppufile,false);
  823. b:=ppufile.readentry;
  824. if b<>ibendbrowser then
  825. Message1(unit_f_ppu_invalid_entry,tostr(b));
  826. end;
  827. if ((flags and uf_local_browser)<>0) then
  828. tstaticsymtable(localsymtable).load_references(ppufile,true);
  829. end;
  830. procedure tppumodule.writeppu;
  831. var
  832. pu : tused_unit;
  833. begin
  834. Message1(unit_u_ppu_write,realmodulename^);
  835. { create unit flags }
  836. if cs_browser in aktmoduleswitches then
  837. flags:=flags or uf_has_browser;
  838. if cs_local_browser in aktmoduleswitches then
  839. flags:=flags or uf_local_browser;
  840. if do_release then
  841. flags:=flags or uf_release;
  842. if assigned(localsymtable) then
  843. flags:=flags or uf_local_symtable;
  844. {$ifdef cpufpemu}
  845. if (cs_fp_emulation in aktmoduleswitches) then
  846. flags:=flags or uf_fpu_emulation;
  847. {$endif cpufpemu}
  848. {$ifdef Test_Double_checksum_write}
  849. Assign(CRCFile,s+'.IMP');
  850. Rewrite(CRCFile);
  851. {$endif def Test_Double_checksum_write}
  852. { create new ppufile }
  853. ppufile:=tcompilerppufile.create(ppufilename^);
  854. if not ppufile.createfile then
  855. Message(unit_f_ppu_cannot_write);
  856. { first the unitname }
  857. ppufile.putstring(realmodulename^);
  858. ppufile.writeentry(ibmodulename);
  859. writesourcefiles;
  860. {$IFDEF MACRO_DIFF_HINT}
  861. writeusedmacros;
  862. {$ENDIF}
  863. { write interface uses }
  864. writeusedunit(true);
  865. { write the objectfiles and libraries that come for this unit,
  866. preserve the containers becuase they are still needed to load
  867. the link.res. All doesn't depend on the crc! It doesn't matter
  868. if a unit is in a .o or .a file }
  869. ppufile.do_crc:=false;
  870. writelinkcontainer(linkunitofiles,iblinkunitofiles,true);
  871. writelinkcontainer(linkunitstaticlibs,iblinkunitstaticlibs,true);
  872. writelinkcontainer(linkunitsharedlibs,iblinkunitsharedlibs,true);
  873. writelinkcontainer(linkotherofiles,iblinkotherofiles,false);
  874. writelinkcontainer(linkotherstaticlibs,iblinkotherstaticlibs,true);
  875. writelinkcontainer(linkothersharedlibs,iblinkothersharedlibs,true);
  876. writelinkcontainer(linkdlls,iblinkdlls,true);
  877. ppufile.do_crc:=true;
  878. { generate implementation deref data, the interface deref data is
  879. already generated when calculating the interface crc }
  880. if (cs_compilesystem in aktmoduleswitches) then
  881. begin
  882. tstoredsymtable(globalsymtable).buildderef;
  883. derefdataintflen:=derefdata.size;
  884. end;
  885. tstoredsymtable(globalsymtable).buildderefimpl;
  886. if (flags and uf_local_symtable)<>0 then
  887. begin
  888. tstoredsymtable(localsymtable).buildderef;
  889. tstoredsymtable(localsymtable).buildderefimpl;
  890. end;
  891. writederefmap;
  892. writederefdata;
  893. ppufile.writeentry(ibendinterface);
  894. { write the symtable entries }
  895. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  896. if assigned(globalmacrosymtable) and (globalmacrosymtable.symindex.count > 0) then
  897. begin
  898. ppufile.putbyte(byte(true));
  899. ppufile.writeentry(ibexportedmacros);
  900. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  901. end
  902. else
  903. begin
  904. ppufile.putbyte(byte(false));
  905. ppufile.writeentry(ibexportedmacros);
  906. end;
  907. { everything after this doesn't affect the crc }
  908. ppufile.do_crc:=false;
  909. { write implementation uses }
  910. writeusedunit(false);
  911. { end of implementation }
  912. ppufile.writeentry(ibendimplementation);
  913. { write static symtable
  914. needed for local debugging of unit functions }
  915. if (flags and uf_local_symtable)<>0 then
  916. tstoredsymtable(localsymtable).ppuwrite(ppufile);
  917. { write all browser section }
  918. if (flags and uf_has_browser)<>0 then
  919. begin
  920. tstoredsymtable(globalsymtable).write_references(ppufile,true);
  921. pu:=tused_unit(used_units.first);
  922. while assigned(pu) do
  923. begin
  924. tstoredsymtable(pu.u.globalsymtable).write_references(ppufile,false);
  925. pu:=tused_unit(pu.next);
  926. end;
  927. ppufile.writeentry(ibendbrowser);
  928. end;
  929. if ((flags and uf_local_browser)<>0) then
  930. begin
  931. if not assigned(localsymtable) then
  932. internalerror(200408271);
  933. tstaticsymtable(localsymtable).write_references(ppufile,true);
  934. end;
  935. { the last entry ibend is written automaticly }
  936. { flush to be sure }
  937. ppufile.flush;
  938. { create and write header }
  939. ppufile.header.size:=ppufile.size;
  940. ppufile.header.checksum:=ppufile.crc;
  941. ppufile.header.interface_checksum:=ppufile.interface_crc;
  942. ppufile.header.compiler:=wordversion;
  943. ppufile.header.cpu:=word(target_cpu);
  944. ppufile.header.target:=word(target_info.system);
  945. ppufile.header.flags:=flags;
  946. ppufile.writeheader;
  947. { save crc in current module also }
  948. crc:=ppufile.crc;
  949. interface_crc:=ppufile.interface_crc;
  950. {$ifdef Test_Double_checksum_write}
  951. close(CRCFile);
  952. {$endif Test_Double_checksum_write}
  953. ppufile.closefile;
  954. ppufile.free;
  955. ppufile:=nil;
  956. end;
  957. procedure tppumodule.getppucrc;
  958. begin
  959. {$ifdef Test_Double_checksum_write}
  960. Assign(CRCFile,s+'.INT')
  961. Rewrite(CRCFile);
  962. {$endif def Test_Double_checksum_write}
  963. { create new ppufile }
  964. ppufile:=tcompilerppufile.create(ppufilename^);
  965. ppufile.crc_only:=true;
  966. if not ppufile.createfile then
  967. Message(unit_f_ppu_cannot_write);
  968. { first the unitname }
  969. ppufile.putstring(realmodulename^);
  970. ppufile.writeentry(ibmodulename);
  971. { the interface units affect the crc }
  972. writeusedunit(true);
  973. { deref data of interface that affect the crc }
  974. derefdata.reset;
  975. tstoredsymtable(globalsymtable).buildderef;
  976. derefdataintflen:=derefdata.size;
  977. writederefmap;
  978. writederefdata;
  979. ppufile.writeentry(ibendinterface);
  980. { write the symtable entries }
  981. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  982. if assigned(globalmacrosymtable) and (globalmacrosymtable.symindex.count > 0) then
  983. begin
  984. ppufile.putbyte(byte(true));
  985. ppufile.writeentry(ibexportedmacros);
  986. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  987. end
  988. else
  989. begin
  990. ppufile.putbyte(byte(false));
  991. ppufile.writeentry(ibexportedmacros);
  992. end;
  993. { save crc }
  994. crc:=ppufile.crc;
  995. interface_crc:=ppufile.interface_crc;
  996. { end of implementation, to generate a correct ppufile
  997. for ppudump when using INTFPPU define }
  998. ppufile.writeentry(ibendimplementation);
  999. {$ifdef Test_Double_checksum}
  1000. crc_array:=ppufile.crc_test;
  1001. ppufile.crc_test:=nil;
  1002. crc_size:=ppufile.crc_index2;
  1003. crc_array2:=ppufile.crc_test2;
  1004. ppufile.crc_test2:=nil;
  1005. crc_size2:=ppufile.crc_index2;
  1006. {$endif Test_Double_checksum}
  1007. {$ifdef Test_Double_checksum_write}
  1008. close(CRCFile);
  1009. {$endif Test_Double_checksum_write}
  1010. { create and write header, this will only be used
  1011. for debugging purposes }
  1012. ppufile.header.size:=ppufile.size;
  1013. ppufile.header.checksum:=ppufile.crc;
  1014. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1015. ppufile.header.compiler:=wordversion;
  1016. ppufile.header.cpu:=word(target_cpu);
  1017. ppufile.header.target:=word(target_info.system);
  1018. ppufile.header.flags:=flags;
  1019. ppufile.writeheader;
  1020. ppufile.closefile;
  1021. ppufile.free;
  1022. ppufile:=nil;
  1023. end;
  1024. procedure tppumodule.load_usedunits;
  1025. var
  1026. pu : tused_unit;
  1027. load_refs : boolean;
  1028. begin
  1029. if current_module<>self then
  1030. internalerror(200212284);
  1031. load_refs:=true;
  1032. { load the used units from interface }
  1033. in_interface:=true;
  1034. pu:=tused_unit(used_units.first);
  1035. while assigned(pu) do
  1036. begin
  1037. if pu.in_interface then
  1038. begin
  1039. tppumodule(pu.u).loadppu;
  1040. { if this unit is compiled we can stop }
  1041. if state=ms_compiled then
  1042. exit;
  1043. { add this unit to the dependencies }
  1044. pu.u.adddependency(self);
  1045. { need to recompile the current unit, check the interface
  1046. crc. And when not compiled with -Ur then check the complete
  1047. crc }
  1048. if (pu.u.interface_crc<>pu.interface_checksum) or
  1049. (
  1050. ((ppufile.header.flags and uf_release)=0) and
  1051. (pu.u.crc<>pu.checksum)
  1052. ) then
  1053. begin
  1054. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^);
  1055. recompile_reason:=rr_crcchanged;
  1056. do_compile:=true;
  1057. exit;
  1058. end;
  1059. end;
  1060. pu:=tused_unit(pu.next);
  1061. end;
  1062. { ok, now load the interface of this unit }
  1063. if current_module<>self then
  1064. internalerror(200208187);
  1065. globalsymtable:=tglobalsymtable.create(modulename^,moduleid);
  1066. tstoredsymtable(globalsymtable).ppuload(ppufile);
  1067. if ppufile.readentry<>ibexportedmacros then
  1068. Message(unit_f_ppu_read_error);
  1069. if boolean(ppufile.getbyte) then
  1070. begin
  1071. globalmacrosymtable:=tmacrosymtable.Create(true);
  1072. tstoredsymtable(globalmacrosymtable).ppuload(ppufile)
  1073. end;
  1074. interface_compiled:=true;
  1075. { read the implementation part, containing
  1076. the implementation uses and ObjData }
  1077. in_interface:=false;
  1078. load_implementation;
  1079. { now only read the implementation uses }
  1080. pu:=tused_unit(used_units.first);
  1081. while assigned(pu) do
  1082. begin
  1083. if (not pu.in_interface) then
  1084. begin
  1085. tppumodule(pu.u).loadppu;
  1086. { if this unit is compiled we can stop }
  1087. if state=ms_compiled then
  1088. exit;
  1089. { add this unit to the dependencies }
  1090. pu.u.adddependency(self);
  1091. { need to recompile the current unit ? }
  1092. if (pu.u.interface_crc<>pu.interface_checksum) then
  1093. begin
  1094. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^+' {impl}');
  1095. recompile_reason:=rr_crcchanged;
  1096. do_compile:=true;
  1097. exit;
  1098. end;
  1099. end;
  1100. pu:=tused_unit(pu.next);
  1101. end;
  1102. { load implementation symtable }
  1103. if (flags and uf_local_symtable)<>0 then
  1104. begin
  1105. localsymtable:=tstaticsymtable.create(modulename^,moduleid);
  1106. tstaticsymtable(localsymtable).ppuload(ppufile);
  1107. end;
  1108. { we can now derefence all pointers to the implementation parts }
  1109. tstoredsymtable(globalsymtable).derefimpl;
  1110. if assigned(localsymtable) then
  1111. tstoredsymtable(localsymtable).derefimpl;
  1112. { load browser info if stored }
  1113. if ((flags and uf_has_browser)<>0) and load_refs then
  1114. begin
  1115. if current_module<>self then
  1116. internalerror(200208188);
  1117. load_symtable_refs;
  1118. end;
  1119. end;
  1120. function tppumodule.needrecompile:boolean;
  1121. var
  1122. pu : tused_unit;
  1123. begin
  1124. result:=false;
  1125. pu:=tused_unit(used_units.first);
  1126. while assigned(pu) do
  1127. begin
  1128. { need to recompile the current unit, check the interface
  1129. crc. And when not compiled with -Ur then check the complete
  1130. crc }
  1131. if (pu.u.interface_crc<>pu.interface_checksum) or
  1132. (
  1133. (pu.in_interface) and
  1134. (pu.u.crc<>pu.checksum)
  1135. ) then
  1136. begin
  1137. result:=true;
  1138. exit;
  1139. end;
  1140. pu:=tused_unit(pu.next);
  1141. end;
  1142. end;
  1143. procedure tppumodule.loadppu;
  1144. const
  1145. ImplIntf : array[boolean] of string[15]=('implementation','interface');
  1146. var
  1147. do_load,
  1148. second_time : boolean;
  1149. old_current_module : tmodule;
  1150. begin
  1151. old_current_module:=current_module;
  1152. Message3(unit_u_load_unit,old_current_module.modulename^,
  1153. ImplIntf[old_current_module.in_interface],
  1154. modulename^);
  1155. { Update loaded_from to detect cycles }
  1156. loaded_from:=old_current_module;
  1157. { check if the globalsymtable is already available, but
  1158. we must reload when the do_reload flag is set }
  1159. if (not do_reload) and
  1160. assigned(globalsymtable) then
  1161. exit;
  1162. { reset }
  1163. do_load:=true;
  1164. second_time:=false;
  1165. current_module:=self;
  1166. SetCompileModule(current_module);
  1167. Fillchar(aktfilepos,0,sizeof(aktfilepos));
  1168. { A force reload }
  1169. if do_reload then
  1170. begin
  1171. Message(unit_u_forced_reload);
  1172. do_reload:=false;
  1173. { When the unit is already loaded or being loaded
  1174. we can maybe skip a complete reload/recompile }
  1175. if assigned(globalsymtable) and
  1176. (not needrecompile) then
  1177. begin
  1178. { When we don't have any data stored yet there
  1179. is nothing to resolve }
  1180. if interface_compiled then
  1181. begin
  1182. Message1(unit_u_reresolving_unit,modulename^);
  1183. tstoredsymtable(globalsymtable).deref;
  1184. tstoredsymtable(globalsymtable).derefimpl;
  1185. if assigned(localsymtable) then
  1186. begin
  1187. tstoredsymtable(localsymtable).deref;
  1188. tstoredsymtable(localsymtable).derefimpl;
  1189. end;
  1190. end
  1191. else
  1192. Message1(unit_u_skipping_reresolving_unit,modulename^);
  1193. do_load:=false;
  1194. end;
  1195. end;
  1196. if do_load then
  1197. begin
  1198. { we are loading a new module, save the state of the scanner
  1199. and reset scanner+module }
  1200. if assigned(current_scanner) then
  1201. current_scanner.tempcloseinputfile;
  1202. current_scanner:=nil;
  1203. { loading the unit for a second time? }
  1204. if state=ms_registered then
  1205. state:=ms_load
  1206. else
  1207. begin
  1208. { try to load the unit a second time first }
  1209. Message1(unit_u_second_load_unit,modulename^);
  1210. Message2(unit_u_previous_state,modulename^,ModuleStateStr[state]);
  1211. { Flag modules to reload }
  1212. flagdependent(old_current_module);
  1213. { Reset the module }
  1214. reset;
  1215. if state in [ms_compile,ms_second_compile] then
  1216. begin
  1217. Message1(unit_u_second_compile_unit,modulename^);
  1218. state:=ms_second_compile;
  1219. do_compile:=true;
  1220. end
  1221. else
  1222. state:=ms_second_load;
  1223. second_time:=true;
  1224. end;
  1225. { close old_current_ppu on system that are
  1226. short on file handles like DOS PM }
  1227. {$ifdef SHORT_ON_FILE_HANDLES}
  1228. if old_current_module.is_unit and
  1229. assigned(tppumodule(old_current_module).ppufile) then
  1230. tppumodule(old_current_module).ppufile.tempclose;
  1231. {$endif SHORT_ON_FILE_HANDLES}
  1232. { try to opening ppu, skip this when we already
  1233. know that we need to compile the unit }
  1234. if not do_compile then
  1235. begin
  1236. Message1(unit_u_loading_unit,modulename^);
  1237. search_unit(false,false);
  1238. if not do_compile then
  1239. begin
  1240. load_interface;
  1241. if not do_compile then
  1242. begin
  1243. load_usedunits;
  1244. if not do_compile then
  1245. Message1(unit_u_finished_loading_unit,modulename^);
  1246. end;
  1247. end;
  1248. { PPU is not needed anymore }
  1249. if assigned(ppufile) then
  1250. begin
  1251. ppufile.closefile;
  1252. ppufile.free;
  1253. ppufile:=nil;
  1254. end;
  1255. end;
  1256. { Do we need to recompile the unit }
  1257. if do_compile then
  1258. begin
  1259. { recompile the unit or give a fatal error if sources not available }
  1260. if not(sources_avail) then
  1261. begin
  1262. if (not search_unit(true,false)) and
  1263. (length(modulename^)>8) then
  1264. search_unit(true,true);
  1265. if not(sources_avail) then
  1266. begin
  1267. if recompile_reason=rr_noppu then
  1268. Message1(unit_f_cant_find_ppu,realmodulename^)
  1269. else
  1270. Message1(unit_f_cant_compile_unit,realmodulename^);
  1271. end;
  1272. end;
  1273. { Flag modules to reload }
  1274. flagdependent(old_current_module);
  1275. { Reset the module }
  1276. reset;
  1277. { compile this module }
  1278. if not(state in [ms_compile,ms_second_compile]) then
  1279. state:=ms_compile;
  1280. compile(mainsource^);
  1281. end
  1282. else
  1283. state:=ms_compiled;
  1284. if current_module<>self then
  1285. internalerror(200212282);
  1286. if in_interface then
  1287. internalerror(200212283);
  1288. { for a second_time recompile reload all dependent units,
  1289. for a first time compile register the unit _once_ }
  1290. if second_time then
  1291. reload_flagged_units
  1292. else
  1293. usedunits.concat(tused_unit.create(self,true,false,nil));
  1294. { reopen the old module }
  1295. {$ifdef SHORT_ON_FILE_HANDLES}
  1296. if old_current_module.is_unit and
  1297. assigned(tppumodule(old_current_module).ppufile) then
  1298. tppumodule(old_current_module).ppufile.tempopen;
  1299. {$endif SHORT_ON_FILE_HANDLES}
  1300. { reload old scanner }
  1301. current_scanner:=tscannerfile(old_current_module.scanner);
  1302. if assigned(current_scanner) then
  1303. begin
  1304. current_scanner.tempopeninputfile;
  1305. current_scanner.gettokenpos
  1306. end
  1307. else
  1308. fillchar(aktfilepos,sizeof(aktfilepos),0);
  1309. end;
  1310. { we are back, restore current_module }
  1311. current_module:=old_current_module;
  1312. SetCompileModule(current_module);
  1313. end;
  1314. {*****************************************************************************
  1315. RegisterUnit
  1316. *****************************************************************************}
  1317. function registerunit(callermodule:tmodule;const s : stringid;const fn:string) : tppumodule;
  1318. var
  1319. ups : stringid;
  1320. hp : tppumodule;
  1321. hp2 : tmodule;
  1322. begin
  1323. { Info }
  1324. ups:=upper(s);
  1325. { search all loaded units }
  1326. hp:=tppumodule(loaded_units.first);
  1327. while assigned(hp) do
  1328. begin
  1329. if hp.modulename^=ups then
  1330. begin
  1331. { only check for units. The main program is also
  1332. as a unit in the loaded_units list. We simply need
  1333. to ignore this entry (PFV) }
  1334. if hp.is_unit then
  1335. begin
  1336. { both units in interface ? }
  1337. if callermodule.in_interface and
  1338. hp.in_interface then
  1339. begin
  1340. { check for a cycle }
  1341. hp2:=callermodule.loaded_from;
  1342. while assigned(hp2) and (hp2<>hp) do
  1343. begin
  1344. if hp2.in_interface then
  1345. hp2:=hp2.loaded_from
  1346. else
  1347. hp2:=nil;
  1348. end;
  1349. if assigned(hp2) then
  1350. Message2(unit_f_circular_unit_reference,callermodule.realmodulename^,hp.realmodulename^);
  1351. end;
  1352. break;
  1353. end;
  1354. end;
  1355. { the next unit }
  1356. hp:=tppumodule(hp.next);
  1357. end;
  1358. { the unit is not in the loaded units,
  1359. we create an entry and register the unit }
  1360. if not assigned(hp) then
  1361. begin
  1362. Message1(unit_u_registering_new_unit,Upper(s));
  1363. hp:=tppumodule.create(callermodule,s,fn,true);
  1364. hp.loaded_from:=callermodule;
  1365. addloadedunit(hp);
  1366. end;
  1367. { return }
  1368. registerunit:=hp;
  1369. end;
  1370. end.