pkgutil.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. {
  2. Copyright (c) 2013-2016 by Free Pascal Development Team
  3. This unit implements basic parts of the package system
  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 pkgutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. fmodule,fpkg,link,cstreams,cclasses;
  22. procedure createimportlibfromexternals;
  23. Function RewritePPU(const PPUFn:String;OutStream:TCStream):Boolean;
  24. procedure export_unit(u:tmodule);
  25. procedure load_packages;
  26. procedure add_package(const name:string;ignoreduplicates:boolean;direct:boolean);
  27. procedure add_package_unit_ref(package:tpackage);
  28. procedure add_package_libs(l:tlinker);
  29. procedure check_for_indirect_package_usages(modules:tlinkedlist);
  30. implementation
  31. uses
  32. sysutils,
  33. globtype,systems,
  34. cutils,
  35. globals,verbose,
  36. symtype,symconst,symsym,symdef,symbase,symtable,
  37. psub,
  38. ppu,entfile,fpcp,
  39. export;
  40. procedure procexport(const s : string);
  41. var
  42. hp : texported_item;
  43. begin
  44. hp:=texported_item.create;
  45. hp.name:=stringdup(s);
  46. hp.options:=hp.options+[eo_name];
  47. exportlib.exportprocedure(hp);
  48. end;
  49. procedure varexport(const s : string);
  50. var
  51. hp : texported_item;
  52. begin
  53. hp:=texported_item.create;
  54. hp.name:=stringdup(s);
  55. hp.options:=hp.options+[eo_name];
  56. exportlib.exportvar(hp);
  57. end;
  58. procedure exportprocsym(sym:tprocsym;symtable:tsymtable);
  59. var
  60. i : longint;
  61. item : TCmdStrListItem;
  62. pd : tprocdef;
  63. begin
  64. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  65. begin
  66. pd:=tprocdef(tprocsym(sym).procdeflist[i]);
  67. if not(pd.proccalloption in [pocall_internproc]) and
  68. ((pd.procoptions*[po_external])=[]) and
  69. (
  70. (symtable.symtabletype in [globalsymtable,recordsymtable,objectsymtable]) or
  71. (
  72. (symtable.symtabletype=staticsymtable) and
  73. ([po_public,po_has_public_name]*pd.procoptions<>[])
  74. )
  75. ) then
  76. begin
  77. exportallprocdefnames(tprocsym(sym),pd,[eo_name,eo_no_sym_name]);
  78. end;
  79. end;
  80. end;
  81. procedure exportabstractrecorddef(def:tabstractrecorddef;symtable:tsymtable); forward;
  82. procedure exportabstractrecordsymproc(sym:tobject;arg:pointer);
  83. var
  84. def : tabstractrecorddef;
  85. begin
  86. case tsym(sym).typ of
  87. typesym:
  88. begin
  89. case ttypesym(sym).typedef.typ of
  90. objectdef,
  91. recorddef:
  92. exportabstractrecorddef(tabstractrecorddef(ttypesym(sym).typedef),tsymtable(arg));
  93. end;
  94. end;
  95. procsym:
  96. begin
  97. { don't export methods of interfaces }
  98. if is_interface(tdef(tabstractrecordsymtable(arg).defowner)) then
  99. exit;
  100. exportprocsym(tprocsym(sym),tsymtable(arg));
  101. end;
  102. staticvarsym:
  103. begin
  104. varexport(tsym(sym).mangledname);
  105. end;
  106. end;
  107. end;
  108. procedure exportabstractrecorddef(def:tabstractrecorddef;symtable:tsymtable);
  109. var
  110. hp : texported_item;
  111. begin
  112. { for cross unit type aliases this might happen }
  113. if def.owner<>symtable then
  114. exit;
  115. def.symtable.SymList.ForEachCall(@exportabstractrecordsymproc,def.symtable);
  116. { don't export generics or their nested types }
  117. if df_generic in def.defoptions then
  118. exit;
  119. if (def.typ=objectdef) and (oo_has_vmt in tobjectdef(def).objectoptions) then
  120. begin
  121. hp:=texported_item.create;
  122. hp.name:=stringdup(tobjectdef(def).vmt_mangledname);
  123. hp.options:=hp.options+[eo_name];
  124. exportlib.exportvar(hp);
  125. end;
  126. end;
  127. procedure insert_export(sym : TObject;arg:pointer);
  128. var
  129. i : longint;
  130. item : TCmdStrListItem;
  131. publiconly : boolean;
  132. begin
  133. publiconly:=tsymtable(arg).symtabletype=staticsymtable;
  134. case TSym(sym).typ of
  135. { ignore: }
  136. unitsym,
  137. syssym,
  138. constsym,
  139. namespacesym,
  140. propertysym,
  141. enumsym:
  142. ;
  143. typesym:
  144. begin
  145. case ttypesym(sym).typedef.typ of
  146. recorddef,
  147. objectdef:
  148. exportabstractrecorddef(tabstractrecorddef(ttypesym(sym).typedef),tsymtable(arg));
  149. end;
  150. end;
  151. procsym:
  152. begin
  153. exportprocsym(tprocsym(sym),tsymtable(arg));
  154. end;
  155. staticvarsym:
  156. begin
  157. if publiconly and not (vo_is_public in tstaticvarsym(sym).varoptions) then
  158. exit;
  159. if target_info.system in systems_indirect_var_imports then
  160. varexport(tsym(sym).mangledname)
  161. else
  162. varexport(tsym(sym).mangledname+suffix_indirect);
  163. end;
  164. else
  165. begin
  166. writeln('unknown: ',TSym(sym).typ);
  167. end;
  168. end;
  169. end;
  170. procedure export_unit(u: tmodule);
  171. begin
  172. u.globalsymtable.symlist.ForEachCall(@insert_export,u.globalsymtable);
  173. { check localsymtable for exports too to get public symbols }
  174. u.localsymtable.symlist.ForEachCall(@insert_export,u.localsymtable);
  175. { create special exports }
  176. if (u.flags and uf_init)<>0 then
  177. procexport(make_mangledname('INIT$',u.globalsymtable,''));
  178. if (u.flags and uf_finalize)<>0 then
  179. procexport(make_mangledname('FINALIZE$',u.globalsymtable,''));
  180. if (u.flags and uf_threadvars)=uf_threadvars then
  181. varexport(make_mangledname('THREADVARLIST',u.globalsymtable,''));
  182. end;
  183. Function RewritePPU(const PPUFn:String;OutStream:TCStream):Boolean;
  184. Var
  185. MakeStatic : Boolean;
  186. Var
  187. buffer : array[0..$1fff] of byte;
  188. inppu,
  189. outppu : tppufile;
  190. b,
  191. untilb : byte;
  192. l,m : longint;
  193. f : file;
  194. ext,
  195. s : string;
  196. ppuversion : dword;
  197. begin
  198. Result:=false;
  199. MakeStatic:=False;
  200. inppu:=tppufile.create(PPUFn);
  201. if not inppu.openfile then
  202. begin
  203. inppu.free;
  204. Comment(V_Error,'Could not open : '+PPUFn);
  205. Exit;
  206. end;
  207. { Check the ppufile }
  208. if not inppu.CheckPPUId then
  209. begin
  210. inppu.free;
  211. Comment(V_Error,'Not a PPU File : '+PPUFn);
  212. Exit;
  213. end;
  214. ppuversion:=inppu.getversion;
  215. if ppuversion<CurrentPPUVersion then
  216. begin
  217. inppu.free;
  218. Comment(V_Error,'Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn);
  219. Exit;
  220. end;
  221. { Already a lib? }
  222. if (inppu.header.common.flags and uf_in_library)<>0 then
  223. begin
  224. inppu.free;
  225. Comment(V_Error,'PPU is already in a library : '+PPUFn);
  226. Exit;
  227. end;
  228. { We need a static linked unit, but we also accept those without .o file }
  229. if (inppu.header.common.flags and (uf_static_linked or uf_no_link))=0 then
  230. begin
  231. inppu.free;
  232. Comment(V_Error,'PPU is not static linked : '+PPUFn);
  233. Exit;
  234. end;
  235. { Check if shared is allowed }
  236. if tsystem(inppu.header.common.target) in [system_i386_go32v2] then
  237. begin
  238. Comment(V_Error,'Shared library not supported for ppu target, switching to static library');
  239. MakeStatic:=true;
  240. end;
  241. { Create the new ppu }
  242. outppu:=tppufile.create(PPUFn);
  243. outppu.createstream(OutStream);
  244. { Create new header, with the new flags }
  245. outppu.header:=inppu.header;
  246. outppu.header.common.flags:=outppu.header.common.flags or uf_in_library;
  247. if MakeStatic then
  248. outppu.header.common.flags:=outppu.header.common.flags or uf_static_linked
  249. else
  250. outppu.header.common.flags:=outppu.header.common.flags or uf_shared_linked;
  251. { read until the object files are found }
  252. untilb:=iblinkunitofiles;
  253. repeat
  254. b:=inppu.readentry;
  255. if b in [ibendinterface,ibend] then
  256. begin
  257. inppu.free;
  258. outppu.free;
  259. Comment(V_Error,'No files to be linked found : '+PPUFn);
  260. Exit;
  261. end;
  262. if b<>untilb then
  263. begin
  264. repeat
  265. inppu.getdatabuf(buffer,sizeof(buffer),l);
  266. outppu.putdata(buffer,l);
  267. until l<sizeof(buffer);
  268. outppu.writeentry(b);
  269. end;
  270. until (b=untilb);
  271. { we have now reached the section for the files which need to be added,
  272. now add them to the list }
  273. case b of
  274. iblinkunitofiles :
  275. begin
  276. { add all o files, and save the entry when not creating a static
  277. library to keep staticlinking possible }
  278. while not inppu.endofentry do
  279. begin
  280. s:=inppu.getstring;
  281. m:=inppu.getlongint;
  282. if not MakeStatic then
  283. begin
  284. outppu.putstring(s);
  285. outppu.putlongint(m);
  286. end;
  287. current_module.linkotherofiles.add(s,link_always);;
  288. end;
  289. if not MakeStatic then
  290. outppu.writeentry(b);
  291. end;
  292. { iblinkunitstaticlibs :
  293. begin
  294. AddToLinkFiles(ExtractLib(inppu.getstring));
  295. if not inppu.endofentry then
  296. begin
  297. repeat
  298. inppu.getdatabuf(buffer^,bufsize,l);
  299. outppu.putdata(buffer^,l);
  300. until l<bufsize;
  301. outppu.writeentry(b);
  302. end;
  303. end; }
  304. end;
  305. { just add a new entry with the new lib }
  306. if MakeStatic then
  307. begin
  308. outppu.putstring('imp'+current_module.realmodulename^);
  309. outppu.putlongint(link_static);
  310. outppu.writeentry(iblinkunitstaticlibs)
  311. end
  312. else
  313. begin
  314. outppu.putstring('imp'+current_module.realmodulename^);
  315. outppu.putlongint(link_shared);
  316. outppu.writeentry(iblinkunitsharedlibs);
  317. end;
  318. { read all entries until the end and write them also to the new ppu }
  319. repeat
  320. b:=inppu.readentry;
  321. { don't write ibend, that's written automatically }
  322. if b<>ibend then
  323. begin
  324. if b=iblinkothersharedlibs then
  325. begin
  326. while not inppu.endofentry do
  327. begin
  328. s:=inppu.getstring;
  329. m:=inppu.getlongint;
  330. outppu.putstring(s);
  331. outppu.putlongint(m);
  332. { strip lib prefix }
  333. if copy(s,1,3)='lib' then
  334. delete(s,1,3);
  335. ext:=ExtractFileExt(s);
  336. if ext<>'' then
  337. delete(s,length(s)-length(ext)+1,length(ext));
  338. current_module.linkOtherSharedLibs.add(s,link_always);
  339. end;
  340. end
  341. else
  342. repeat
  343. inppu.getdatabuf(buffer,sizeof(buffer),l);
  344. outppu.putdata(buffer,l);
  345. until l<sizeof(buffer);
  346. outppu.writeentry(b);
  347. end;
  348. until b=ibend;
  349. { write the last stuff and close }
  350. outppu.flush;
  351. outppu.writeheader;
  352. outppu.free;
  353. inppu.free;
  354. Result:=True;
  355. end;
  356. procedure load_packages;
  357. var
  358. i,j : longint;
  359. pcp: tpcppackage;
  360. entry,
  361. entryreq : ppackageentry;
  362. name,
  363. uname : string;
  364. begin
  365. if not (tf_supports_packages in target_info.flags) then
  366. exit;
  367. i:=0;
  368. while i<packagelist.count do
  369. begin
  370. entry:=ppackageentry(packagelist[i]);
  371. if assigned(entry^.package) then
  372. internalerror(2013053104);
  373. Comment(V_Info,'Loading package: '+entry^.realpkgname);
  374. pcp:=tpcppackage.create(entry^.realpkgname);
  375. pcp.loadpcp;
  376. entry^.package:=pcp;
  377. { add all required packages that are not yet part of packagelist }
  378. for j:=0 to pcp.requiredpackages.count-1 do
  379. begin
  380. name:=pcp.requiredpackages.NameOfIndex(j);
  381. uname:=upper(name);
  382. if not assigned(packagelist.Find(uname)) then
  383. begin
  384. New(entryreq);
  385. entryreq^.realpkgname:=name;
  386. entryreq^.package:=nil;
  387. entryreq^.usedunits:=0;
  388. entryreq^.direct:=false;
  389. packagelist.add(uname,entryreq);
  390. end;
  391. end;
  392. Inc(i);
  393. end;
  394. { all packages are now loaded, so we can fill in the links of the required packages }
  395. for i:=0 to packagelist.count-1 do
  396. begin
  397. entry:=ppackageentry(packagelist[i]);
  398. if not assigned(entry^.package) then
  399. internalerror(2015111301);
  400. for j:=0 to entry^.package.requiredpackages.count-1 do
  401. begin
  402. if assigned(entry^.package.requiredpackages[j]) then
  403. internalerror(2015111303);
  404. entryreq:=packagelist.find(upper(entry^.package.requiredpackages.NameOfIndex(j)));
  405. if not assigned(entryreq) then
  406. internalerror(2015111302);
  407. entry^.package.requiredpackages[j]:=entryreq^.package;
  408. end;
  409. end;
  410. end;
  411. procedure add_package(const name:string;ignoreduplicates:boolean;direct:boolean);
  412. var
  413. entry : ppackageentry;
  414. i : longint;
  415. begin
  416. for i:=0 to packagelist.count-1 do
  417. begin
  418. if packagelist.nameofindex(i)=name then
  419. begin
  420. if not ignoreduplicates then
  421. Message1(package_e_duplicate_package,name);
  422. exit;
  423. end;
  424. end;
  425. new(entry);
  426. entry^.package:=nil;
  427. entry^.realpkgname:=name;
  428. entry^.usedunits:=0;
  429. entry^.direct:=direct;
  430. packagelist.add(upper(name),entry);
  431. end;
  432. procedure add_package_unit_ref(package: tpackage);
  433. var
  434. pkgentry : ppackageentry;
  435. begin
  436. pkgentry:=ppackageentry(packagelist.find(package.packagename^));
  437. if not assigned(pkgentry) then
  438. internalerror(2015100301);
  439. inc(pkgentry^.usedunits);
  440. end;
  441. procedure add_package_libs(l:tlinker);
  442. var
  443. pkgentry : ppackageentry;
  444. i : longint;
  445. pkgname : tpathstr;
  446. begin
  447. if target_info.system in systems_indirect_var_imports then
  448. { we're using import libraries anyway }
  449. exit;
  450. for i:=0 to packagelist.count-1 do
  451. begin
  452. pkgentry:=ppackageentry(packagelist[i]);
  453. if pkgentry^.usedunits>0 then
  454. begin
  455. //writeln('package used: ',pkgentry^.realpkgname);
  456. pkgname:=pkgentry^.package.pplfilename;
  457. if copy(pkgname,1,length(target_info.sharedlibprefix))=target_info.sharedlibprefix then
  458. delete(pkgname,1,length(target_info.sharedlibprefix));
  459. if copy(pkgname,length(pkgname)-length(target_info.sharedlibext)+1,length(target_info.sharedlibext))=target_info.sharedlibext then
  460. delete(pkgname,length(pkgname)-length(target_info.sharedlibext)+1,length(target_info.sharedlibext));
  461. //writeln('adding library: ', pkgname);
  462. l.sharedlibfiles.concat(pkgname);
  463. end
  464. else
  465. {writeln('ignoring package: ',pkgentry^.realpkgname)};
  466. end;
  467. end;
  468. procedure check_for_indirect_package_usages(modules:tlinkedlist);
  469. var
  470. uu : tused_unit;
  471. pentry : ppackageentry;
  472. begin
  473. uu:=tused_unit(modules.first);
  474. while assigned(uu) do
  475. begin
  476. if assigned(uu.u.package) then
  477. begin
  478. pentry:=ppackageentry(packagelist.find(uu.u.package.packagename^));
  479. if not assigned(pentry) then
  480. internalerror(2015112304);
  481. if not pentry^.direct then
  482. Message2(package_w_unit_from_indirect_package,uu.u.realmodulename^,uu.u.package.realpackagename^);
  483. end;
  484. uu:=tused_unit(uu.Next);
  485. end;
  486. end;
  487. procedure createimportlibfromexternals;
  488. var
  489. alreadyloaded : tfpobjectlist;
  490. procedure import_proc_symbol(pd:tprocdef;pkg:tpackage);
  491. var
  492. item : TCmdStrListItem;
  493. begin
  494. item := TCmdStrListItem(pd.aliasnames.first);
  495. if not assigned(item) then
  496. { at least import the mangled name }
  497. current_module.addexternalimport(pkg.pplfilename,pd.mangledname,pd.mangledname,0,false,false);
  498. while assigned(item) do
  499. begin
  500. current_module.addexternalimport(pkg.pplfilename,item.str,item.str,0,false,false);
  501. item := TCmdStrListItem(item.next);
  502. end;
  503. end;
  504. procedure processimportedsyms(syms:tfpobjectlist);
  505. var
  506. i,j,k,l : longint;
  507. pkgentry : ppackageentry;
  508. sym : TSymEntry;
  509. srsymtable : tsymtable;
  510. module : tmodule;
  511. unitentry : pcontainedunit;
  512. name : tsymstr;
  513. pd : tprocdef;
  514. begin
  515. for i:=0 to syms.count-1 do
  516. begin
  517. sym:=tsymentry(syms[i]);
  518. if not (sym.typ in [staticvarsym,procsym]) then
  519. continue;
  520. if alreadyloaded.indexof(sym)>=0 then
  521. continue;
  522. { determine the unit of the symbol }
  523. srsymtable:=sym.owner;
  524. while not (srsymtable.symtabletype in [staticsymtable,globalsymtable]) do
  525. srsymtable:=srsymtable.defowner.owner;
  526. module:=tmodule(loaded_units.first);
  527. while assigned(module) do
  528. begin
  529. if (module.globalsymtable=srsymtable) or (module.localsymtable=srsymtable) then
  530. break;
  531. module:=tmodule(module.next);
  532. end;
  533. if not assigned(module) then
  534. internalerror(2014101001);
  535. if (uf_in_library and module.flags)=0 then
  536. { unit is not part of a package, so no need to handle it }
  537. continue;
  538. { loaded by a package? }
  539. for j:=0 to packagelist.count-1 do
  540. begin
  541. pkgentry:=ppackageentry(packagelist[j]);
  542. for k:=0 to pkgentry^.package.containedmodules.count-1 do
  543. begin
  544. unitentry:=pcontainedunit(pkgentry^.package.containedmodules[k]);
  545. if unitentry^.module=module then
  546. begin
  547. case sym.typ of
  548. staticvarsym:
  549. begin
  550. name:=tstaticvarsym(sym).mangledname;
  551. current_module.addexternalimport(pkgentry^.package.pplfilename,name,name+suffix_indirect,0,true,false);
  552. end;
  553. procsym:
  554. begin
  555. for l:=0 to tprocsym(sym).procdeflist.count-1 do
  556. begin
  557. pd:=tprocdef(tprocsym(sym).procdeflist[l]);
  558. if [po_external,po_has_importdll]*pd.procoptions=[po_external,po_has_importdll] then
  559. { if we use an external procedure of another unit we
  560. need to import it ourselves from the correct library }
  561. import_external_proc(pd)
  562. else
  563. import_proc_symbol(pd,pkgentry^.package);
  564. end;
  565. end;
  566. else
  567. internalerror(2014101001);
  568. end;
  569. alreadyloaded.add(sym);
  570. end;
  571. end;
  572. end;
  573. end;
  574. end;
  575. var
  576. unitentry : pcontainedunit;
  577. module : tmodule;
  578. begin
  579. { check each external asm symbol of each unit of the package whether it is
  580. contained in the unit of a loaded package (and thus an import entry
  581. is needed) }
  582. alreadyloaded:=tfpobjectlist.create(false);
  583. { first pass to find all symbols that were not loaded by asm name }
  584. module:=tmodule(loaded_units.first);
  585. while assigned(module) do
  586. begin
  587. if not assigned(module.package) then
  588. processimportedsyms(module.unitimportsyms);
  589. module:=tmodule(module.next);
  590. end;
  591. alreadyloaded.free;
  592. end;
  593. end.