pkgutil.pas 19 KB

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