pkgutil.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. { don't export generics or their nested types }
  116. if df_generic in def.defoptions then
  117. exit;
  118. def.symtable.SymList.ForEachCall(@exportabstractrecordsymproc,def.symtable);
  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. varexport(tsym(sym).mangledname);
  160. end;
  161. else
  162. begin
  163. writeln('unknown: ',TSym(sym).typ);
  164. end;
  165. end;
  166. end;
  167. procedure export_unit(u: tmodule);
  168. begin
  169. u.globalsymtable.symlist.ForEachCall(@insert_export,u.globalsymtable);
  170. { check localsymtable for exports too to get public symbols }
  171. u.localsymtable.symlist.ForEachCall(@insert_export,u.localsymtable);
  172. { create special exports }
  173. if (u.flags and uf_init)<>0 then
  174. procexport(make_mangledname('INIT$',u.globalsymtable,''));
  175. if (u.flags and uf_finalize)<>0 then
  176. procexport(make_mangledname('FINALIZE$',u.globalsymtable,''));
  177. if (u.flags and uf_threadvars)=uf_threadvars then
  178. varexport(make_mangledname('THREADVARLIST',u.globalsymtable,''));
  179. end;
  180. Function RewritePPU(const PPUFn:String;OutStream:TCStream):Boolean;
  181. Var
  182. MakeStatic : Boolean;
  183. Var
  184. buffer : array[0..$1fff] of byte;
  185. inppu,
  186. outppu : tppufile;
  187. b,
  188. untilb : byte;
  189. l,m : longint;
  190. f : file;
  191. ext,
  192. s : string;
  193. ppuversion : dword;
  194. begin
  195. Result:=false;
  196. MakeStatic:=False;
  197. inppu:=tppufile.create(PPUFn);
  198. if not inppu.openfile then
  199. begin
  200. inppu.free;
  201. Comment(V_Error,'Could not open : '+PPUFn);
  202. Exit;
  203. end;
  204. { Check the ppufile }
  205. if not inppu.CheckPPUId then
  206. begin
  207. inppu.free;
  208. Comment(V_Error,'Not a PPU File : '+PPUFn);
  209. Exit;
  210. end;
  211. ppuversion:=inppu.getversion;
  212. if ppuversion<CurrentPPUVersion then
  213. begin
  214. inppu.free;
  215. Comment(V_Error,'Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn);
  216. Exit;
  217. end;
  218. { Already a lib? }
  219. if (inppu.header.common.flags and uf_in_library)<>0 then
  220. begin
  221. inppu.free;
  222. Comment(V_Error,'PPU is already in a library : '+PPUFn);
  223. Exit;
  224. end;
  225. { We need a static linked unit, but we also accept those without .o file }
  226. if (inppu.header.common.flags and (uf_static_linked or uf_no_link))=0 then
  227. begin
  228. inppu.free;
  229. Comment(V_Error,'PPU is not static linked : '+PPUFn);
  230. Exit;
  231. end;
  232. { Check if shared is allowed }
  233. if tsystem(inppu.header.common.target) in [system_i386_go32v2] then
  234. begin
  235. Comment(V_Error,'Shared library not supported for ppu target, switching to static library');
  236. MakeStatic:=true;
  237. end;
  238. { Create the new ppu }
  239. outppu:=tppufile.create(PPUFn);
  240. outppu.createstream(OutStream);
  241. { Create new header, with the new flags }
  242. outppu.header:=inppu.header;
  243. outppu.header.common.flags:=outppu.header.common.flags or uf_in_library;
  244. if MakeStatic then
  245. outppu.header.common.flags:=outppu.header.common.flags or uf_static_linked
  246. else
  247. outppu.header.common.flags:=outppu.header.common.flags or uf_shared_linked;
  248. { read until the object files are found }
  249. untilb:=iblinkunitofiles;
  250. repeat
  251. b:=inppu.readentry;
  252. if b in [ibendinterface,ibend] then
  253. begin
  254. inppu.free;
  255. outppu.free;
  256. Comment(V_Error,'No files to be linked found : '+PPUFn);
  257. Exit;
  258. end;
  259. if b<>untilb then
  260. begin
  261. repeat
  262. inppu.getdatabuf(buffer,sizeof(buffer),l);
  263. outppu.putdata(buffer,l);
  264. until l<sizeof(buffer);
  265. outppu.writeentry(b);
  266. end;
  267. until (b=untilb);
  268. { we have now reached the section for the files which need to be added,
  269. now add them to the list }
  270. case b of
  271. iblinkunitofiles :
  272. begin
  273. { add all o files, and save the entry when not creating a static
  274. library to keep staticlinking possible }
  275. while not inppu.endofentry do
  276. begin
  277. s:=inppu.getstring;
  278. m:=inppu.getlongint;
  279. if not MakeStatic then
  280. begin
  281. outppu.putstring(s);
  282. outppu.putlongint(m);
  283. end;
  284. current_module.linkotherofiles.add(s,link_always);;
  285. end;
  286. if not MakeStatic then
  287. outppu.writeentry(b);
  288. end;
  289. { iblinkunitstaticlibs :
  290. begin
  291. AddToLinkFiles(ExtractLib(inppu.getstring));
  292. if not inppu.endofentry then
  293. begin
  294. repeat
  295. inppu.getdatabuf(buffer^,bufsize,l);
  296. outppu.putdata(buffer^,l);
  297. until l<bufsize;
  298. outppu.writeentry(b);
  299. end;
  300. end; }
  301. end;
  302. { just add a new entry with the new lib }
  303. if MakeStatic then
  304. begin
  305. outppu.putstring('imp'+current_module.realmodulename^);
  306. outppu.putlongint(link_static);
  307. outppu.writeentry(iblinkunitstaticlibs)
  308. end
  309. else
  310. begin
  311. outppu.putstring('imp'+current_module.realmodulename^);
  312. outppu.putlongint(link_shared);
  313. outppu.writeentry(iblinkunitsharedlibs);
  314. end;
  315. { read all entries until the end and write them also to the new ppu }
  316. repeat
  317. b:=inppu.readentry;
  318. { don't write ibend, that's written automatically }
  319. if b<>ibend then
  320. begin
  321. if b=iblinkothersharedlibs then
  322. begin
  323. while not inppu.endofentry do
  324. begin
  325. s:=inppu.getstring;
  326. m:=inppu.getlongint;
  327. outppu.putstring(s);
  328. outppu.putlongint(m);
  329. { strip lib prefix }
  330. if copy(s,1,3)='lib' then
  331. delete(s,1,3);
  332. ext:=ExtractFileExt(s);
  333. if ext<>'' then
  334. delete(s,length(s)-length(ext)+1,length(ext));
  335. current_module.linkOtherSharedLibs.add(s,link_always);
  336. end;
  337. end
  338. else
  339. repeat
  340. inppu.getdatabuf(buffer,sizeof(buffer),l);
  341. outppu.putdata(buffer,l);
  342. until l<sizeof(buffer);
  343. outppu.writeentry(b);
  344. end;
  345. until b=ibend;
  346. { write the last stuff and close }
  347. outppu.flush;
  348. outppu.writeheader;
  349. outppu.free;
  350. inppu.free;
  351. Result:=True;
  352. end;
  353. procedure load_packages;
  354. var
  355. i,j : longint;
  356. pcp: tpcppackage;
  357. entry,
  358. entryreq : ppackageentry;
  359. name,
  360. uname : string;
  361. begin
  362. if not (tf_supports_packages in target_info.flags) then
  363. exit;
  364. i:=0;
  365. while i<packagelist.count do
  366. begin
  367. entry:=ppackageentry(packagelist[i]);
  368. if assigned(entry^.package) then
  369. internalerror(2013053104);
  370. Comment(V_Info,'Loading package: '+entry^.realpkgname);
  371. pcp:=tpcppackage.create(entry^.realpkgname);
  372. pcp.loadpcp;
  373. entry^.package:=pcp;
  374. { add all required packages that are not yet part of packagelist }
  375. for j:=0 to pcp.requiredpackages.count-1 do
  376. begin
  377. name:=pcp.requiredpackages.NameOfIndex(j);
  378. uname:=upper(name);
  379. if not assigned(packagelist.Find(uname)) then
  380. begin
  381. New(entryreq);
  382. entryreq^.realpkgname:=name;
  383. entryreq^.package:=nil;
  384. entryreq^.usedunits:=0;
  385. entryreq^.direct:=false;
  386. packagelist.add(uname,entryreq);
  387. end;
  388. end;
  389. Inc(i);
  390. end;
  391. { all packages are now loaded, so we can fill in the links of the required packages }
  392. for i:=0 to packagelist.count-1 do
  393. begin
  394. entry:=ppackageentry(packagelist[i]);
  395. if not assigned(entry^.package) then
  396. internalerror(2015111301);
  397. for j:=0 to entry^.package.requiredpackages.count-1 do
  398. begin
  399. if assigned(entry^.package.requiredpackages[j]) then
  400. internalerror(2015111303);
  401. entryreq:=packagelist.find(upper(entry^.package.requiredpackages.NameOfIndex(j)));
  402. if not assigned(entryreq) then
  403. internalerror(2015111302);
  404. entry^.package.requiredpackages[j]:=entryreq^.package;
  405. end;
  406. end;
  407. end;
  408. procedure add_package(const name:string;ignoreduplicates:boolean;direct:boolean);
  409. var
  410. entry : ppackageentry;
  411. i : longint;
  412. begin
  413. for i:=0 to packagelist.count-1 do
  414. begin
  415. if packagelist.nameofindex(i)=name then
  416. begin
  417. if not ignoreduplicates then
  418. Message1(package_e_duplicate_package,name);
  419. exit;
  420. end;
  421. end;
  422. new(entry);
  423. entry^.package:=nil;
  424. entry^.realpkgname:=name;
  425. entry^.usedunits:=0;
  426. entry^.direct:=direct;
  427. packagelist.add(upper(name),entry);
  428. end;
  429. procedure add_package_unit_ref(package: tpackage);
  430. var
  431. pkgentry : ppackageentry;
  432. begin
  433. pkgentry:=ppackageentry(packagelist.find(package.packagename^));
  434. if not assigned(pkgentry) then
  435. internalerror(2015100301);
  436. inc(pkgentry^.usedunits);
  437. end;
  438. procedure add_package_libs(l:tlinker);
  439. var
  440. pkgentry : ppackageentry;
  441. i : longint;
  442. pkgname : tpathstr;
  443. begin
  444. if target_info.system in systems_indirect_var_imports then
  445. { we're using import libraries anyway }
  446. exit;
  447. for i:=0 to packagelist.count-1 do
  448. begin
  449. pkgentry:=ppackageentry(packagelist[i]);
  450. if pkgentry^.usedunits>0 then
  451. begin
  452. //writeln('package used: ',pkgentry^.realpkgname);
  453. pkgname:=pkgentry^.package.pplfilename;
  454. if copy(pkgname,1,length(target_info.sharedlibprefix))=target_info.sharedlibprefix then
  455. delete(pkgname,1,length(target_info.sharedlibprefix));
  456. if copy(pkgname,length(pkgname)-length(target_info.sharedlibext)+1,length(target_info.sharedlibext))=target_info.sharedlibext then
  457. delete(pkgname,length(pkgname)-length(target_info.sharedlibext)+1,length(target_info.sharedlibext));
  458. //writeln('adding library: ', pkgname);
  459. l.sharedlibfiles.concat(pkgname);
  460. end
  461. else
  462. {writeln('ignoring package: ',pkgentry^.realpkgname)};
  463. end;
  464. end;
  465. procedure check_for_indirect_package_usages(modules:tlinkedlist);
  466. var
  467. uu : tused_unit;
  468. pentry : ppackageentry;
  469. begin
  470. uu:=tused_unit(modules.first);
  471. while assigned(uu) do
  472. begin
  473. if assigned(uu.u.package) then
  474. begin
  475. pentry:=ppackageentry(packagelist.find(uu.u.package.packagename^));
  476. if not assigned(pentry) then
  477. internalerror(2015112304);
  478. if not pentry^.direct then
  479. Message2(package_w_unit_from_indirect_package,uu.u.realmodulename^,uu.u.package.realpackagename^);
  480. end;
  481. uu:=tused_unit(uu.Next);
  482. end;
  483. end;
  484. procedure createimportlibfromexternals;
  485. var
  486. alreadyloaded : tfpobjectlist;
  487. procedure import_proc_symbol(pd:tprocdef;pkg:tpackage);
  488. var
  489. item : TCmdStrListItem;
  490. begin
  491. item := TCmdStrListItem(pd.aliasnames.first);
  492. if not assigned(item) then
  493. { at least import the mangled name }
  494. current_module.addexternalimport(pkg.pplfilename,pd.mangledname,pd.mangledname,0,false,false);
  495. while assigned(item) do
  496. begin
  497. current_module.addexternalimport(pkg.pplfilename,item.str,item.str,0,false,false);
  498. item := TCmdStrListItem(item.next);
  499. end;
  500. end;
  501. procedure processimportedsyms(syms:tfpobjectlist);
  502. var
  503. i,j,k,l : longint;
  504. pkgentry : ppackageentry;
  505. sym : TSymEntry;
  506. srsymtable : tsymtable;
  507. module : tmodule;
  508. unitentry : pcontainedunit;
  509. name : tsymstr;
  510. pd : tprocdef;
  511. begin
  512. for i:=0 to syms.count-1 do
  513. begin
  514. sym:=tsymentry(syms[i]);
  515. if not (sym.typ in [staticvarsym,procsym]) then
  516. continue;
  517. if alreadyloaded.indexof(sym)>=0 then
  518. continue;
  519. { determine the unit of the symbol }
  520. srsymtable:=sym.owner;
  521. while not (srsymtable.symtabletype in [staticsymtable,globalsymtable]) do
  522. srsymtable:=srsymtable.defowner.owner;
  523. module:=tmodule(loaded_units.first);
  524. while assigned(module) do
  525. begin
  526. if (module.globalsymtable=srsymtable) or (module.localsymtable=srsymtable) then
  527. break;
  528. module:=tmodule(module.next);
  529. end;
  530. if not assigned(module) then
  531. internalerror(2014101001);
  532. if (uf_in_library and module.flags)=0 then
  533. { unit is not part of a package, so no need to handle it }
  534. continue;
  535. { loaded by a package? }
  536. for j:=0 to packagelist.count-1 do
  537. begin
  538. pkgentry:=ppackageentry(packagelist[j]);
  539. for k:=0 to pkgentry^.package.containedmodules.count-1 do
  540. begin
  541. unitentry:=pcontainedunit(pkgentry^.package.containedmodules[k]);
  542. if unitentry^.module=module then
  543. begin
  544. case sym.typ of
  545. staticvarsym:
  546. begin
  547. name:=tstaticvarsym(sym).mangledname;
  548. current_module.addexternalimport(pkgentry^.package.pplfilename,name,name+suffix_indirect,0,true,false);
  549. end;
  550. procsym:
  551. begin
  552. for l:=0 to tprocsym(sym).procdeflist.count-1 do
  553. begin
  554. pd:=tprocdef(tprocsym(sym).procdeflist[l]);
  555. if [po_external,po_has_importdll]*pd.procoptions=[po_external,po_has_importdll] then
  556. { if we use an external procedure of another unit we
  557. need to import it ourselves from the correct library }
  558. import_external_proc(pd)
  559. else
  560. import_proc_symbol(pd,pkgentry^.package);
  561. end;
  562. end;
  563. else
  564. internalerror(2014101001);
  565. end;
  566. alreadyloaded.add(sym);
  567. end;
  568. end;
  569. end;
  570. end;
  571. end;
  572. var
  573. unitentry : pcontainedunit;
  574. module : tmodule;
  575. begin
  576. { check each external asm symbol of each unit of the package whether it is
  577. contained in the unit of a loaded package (and thus an import entry
  578. is needed) }
  579. alreadyloaded:=tfpobjectlist.create(false);
  580. { first pass to find all symbols that were not loaded by asm name }
  581. module:=tmodule(loaded_units.first);
  582. while assigned(module) do
  583. begin
  584. if not assigned(module.package) then
  585. processimportedsyms(module.unitimportsyms);
  586. module:=tmodule(module.next);
  587. end;
  588. alreadyloaded.free;
  589. end;
  590. end.