pkgutil.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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);
  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. ((symtable.symtabletype in [globalsymtable,recordsymtable,objectsymtable]) or
  66. ((symtable.symtabletype=staticsymtable) and (po_public in tprocdef(tprocsym(sym).ProcdefList[i]).procoptions))
  67. ) then
  68. begin
  69. exportallprocdefnames(tprocsym(sym),tprocdef(tprocsym(sym).ProcdefList[i]),[]);
  70. end;
  71. end;
  72. end;
  73. procedure exportabstractrecorddef(def:tabstractrecorddef); forward;
  74. procedure exportabstractrecordsymproc(sym:tobject;arg:pointer);
  75. var
  76. def : tabstractrecorddef;
  77. begin
  78. case tsym(sym).typ of
  79. typesym:
  80. begin
  81. case ttypesym(sym).typedef.typ of
  82. objectdef,
  83. recorddef:
  84. exportabstractrecorddef(tabstractrecorddef(ttypesym(sym).typedef));
  85. end;
  86. end;
  87. procsym:
  88. begin
  89. { don't export methods of interfaces }
  90. if is_interface(tdef(tabstractrecordsymtable(arg).defowner)) then
  91. exit;
  92. exportprocsym(tprocsym(sym),tsymtable(arg));
  93. end;
  94. staticvarsym:
  95. begin
  96. varexport(tsym(sym).mangledname);
  97. end;
  98. end;
  99. end;
  100. procedure exportabstractrecorddef(def:tabstractrecorddef);
  101. var
  102. hp : texported_item;
  103. begin
  104. def.symtable.SymList.ForEachCall(@exportabstractrecordsymproc,def.symtable);
  105. { don't export generics or their nested types }
  106. if df_generic in def.defoptions then
  107. exit;
  108. if (def.typ=objectdef) and (oo_has_vmt in tobjectdef(def).objectoptions) then
  109. begin
  110. hp:=texported_item.create;
  111. hp.name:=stringdup(tobjectdef(def).vmt_mangledname);
  112. hp.options:=hp.options+[eo_name];
  113. exportlib.exportvar(hp);
  114. end;
  115. end;
  116. procedure insert_export(sym : TObject;arg:pointer);
  117. var
  118. i : longint;
  119. item : TCmdStrListItem;
  120. publiconly : boolean;
  121. begin
  122. publiconly:=tsymtable(arg).symtabletype=staticsymtable;
  123. case TSym(sym).typ of
  124. { ignore: }
  125. unitsym,
  126. syssym,
  127. constsym,
  128. namespacesym,
  129. propertysym,
  130. enumsym:
  131. ;
  132. typesym:
  133. begin
  134. case ttypesym(sym).typedef.typ of
  135. recorddef,
  136. objectdef:
  137. exportabstractrecorddef(tabstractrecorddef(ttypesym(sym).typedef));
  138. end;
  139. end;
  140. procsym:
  141. begin
  142. exportprocsym(tprocsym(sym),tsymtable(arg));
  143. end;
  144. staticvarsym:
  145. begin
  146. if publiconly and not (vo_is_public in tstaticvarsym(sym).varoptions) then
  147. exit;
  148. varexport(tsym(sym).mangledname);
  149. end;
  150. else
  151. begin
  152. writeln('unknown: ',ord(TSym(sym).typ));
  153. end;
  154. end;
  155. end;
  156. procedure export_unit(u: tmodule);
  157. begin
  158. u.globalsymtable.symlist.ForEachCall(@insert_export,u.globalsymtable);
  159. { check localsymtable for exports too to get public symbols }
  160. u.localsymtable.symlist.ForEachCall(@insert_export,u.localsymtable);
  161. { create special exports }
  162. if (u.flags and uf_init)<>0 then
  163. procexport(make_mangledname('INIT$',u.globalsymtable,''));
  164. if (u.flags and uf_finalize)<>0 then
  165. procexport(make_mangledname('FINALIZE$',u.globalsymtable,''));
  166. if (u.flags and uf_threadvars)=uf_threadvars then
  167. varexport(make_mangledname('THREADVARLIST',u.globalsymtable,''));
  168. end;
  169. Function RewritePPU(const PPUFn:String;OutStream:TCStream):Boolean;
  170. Var
  171. MakeStatic : Boolean;
  172. Var
  173. buffer : array[0..$1fff] of byte;
  174. inppu,
  175. outppu : tppufile;
  176. b,
  177. untilb : byte;
  178. l,m : longint;
  179. f : file;
  180. ext,
  181. s : string;
  182. ppuversion : dword;
  183. begin
  184. Result:=false;
  185. MakeStatic:=False;
  186. inppu:=tppufile.create(PPUFn);
  187. if not inppu.openfile then
  188. begin
  189. inppu.free;
  190. Comment(V_Error,'Could not open : '+PPUFn);
  191. Exit;
  192. end;
  193. { Check the ppufile }
  194. if not inppu.CheckPPUId then
  195. begin
  196. inppu.free;
  197. Comment(V_Error,'Not a PPU File : '+PPUFn);
  198. Exit;
  199. end;
  200. ppuversion:=inppu.getversion;
  201. if ppuversion<CurrentPPUVersion then
  202. begin
  203. inppu.free;
  204. Comment(V_Error,'Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn);
  205. Exit;
  206. end;
  207. { No .o file generated for this ppu, just skip }
  208. if (inppu.header.common.flags and uf_no_link)<>0 then
  209. begin
  210. inppu.free;
  211. Result:=true;
  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 }
  222. if (inppu.header.common.flags and uf_static_linked)=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 : longint;
  352. pcp: tpcppackage;
  353. entry : ppackageentry;
  354. begin
  355. if not (tf_supports_packages in target_info.flags) then
  356. exit;
  357. for i:=0 to packagelist.count-1 do
  358. begin
  359. entry:=ppackageentry(packagelist[i]);
  360. if assigned(entry^.package) then
  361. internalerror(2013053104);
  362. Comment(V_Info,'Loading package: '+entry^.realpkgname);
  363. pcp:=tpcppackage.create(entry^.realpkgname);
  364. pcp.loadpcp;
  365. entry^.package:=pcp;
  366. end;
  367. end;
  368. procedure add_package(const name:string;ignoreduplicates:boolean);
  369. var
  370. entry : ppackageentry;
  371. i : longint;
  372. begin
  373. for i:=0 to packagelist.count-1 do
  374. begin
  375. if packagelist.nameofindex(i)=name then
  376. begin
  377. if not ignoreduplicates then
  378. Message1(package_e_duplicate_package,name);
  379. exit;
  380. end;
  381. end;
  382. new(entry);
  383. entry^.package:=nil;
  384. entry^.realpkgname:=name;
  385. entry^.usedunits:=0;
  386. packagelist.add(upper(name),entry);
  387. end;
  388. procedure add_package_unit_ref(package: tpackage);
  389. var
  390. pkgentry : ppackageentry;
  391. begin
  392. pkgentry:=ppackageentry(packagelist.find(package.packagename^));
  393. if not assigned(pkgentry) then
  394. internalerror(2015100301);
  395. inc(pkgentry^.usedunits);
  396. end;
  397. procedure add_package_libs(l:tlinker);
  398. var
  399. pkgentry : ppackageentry;
  400. i : longint;
  401. pkgname : tpathstr;
  402. begin
  403. for i:=0 to packagelist.count-1 do
  404. begin
  405. pkgentry:=ppackageentry(packagelist[i]);
  406. if pkgentry^.usedunits>0 then
  407. begin
  408. //writeln('package used: ',pkgentry^.realpkgname);
  409. pkgname:=pkgentry^.package.pplfilename;
  410. if copy(pkgname,1,length(target_info.sharedlibprefix))=target_info.sharedlibprefix then
  411. delete(pkgname,1,length(target_info.sharedlibprefix));
  412. if copy(pkgname,length(pkgname)-length(target_info.sharedlibext)+1,length(target_info.sharedlibext))=target_info.sharedlibext then
  413. delete(pkgname,length(pkgname)-length(target_info.sharedlibext)+1,length(target_info.sharedlibext));
  414. //writeln('adding library: ', pkgname);
  415. l.sharedlibfiles.concat(pkgname);
  416. end
  417. else
  418. {writeln('ignoring package: ',pkgentry^.realpkgname)};
  419. end;
  420. end;
  421. procedure createimportlibfromexternals;
  422. var
  423. alreadyloaded : tfpobjectlist;
  424. procedure import_proc_symbol(pd:tprocdef;pkg:tpackage);
  425. var
  426. item : TCmdStrListItem;
  427. begin
  428. item := TCmdStrListItem(pd.aliasnames.first);
  429. while assigned(item) do
  430. begin
  431. current_module.addexternalimport(pkg.pplfilename,item.str,item.str,0,false,false);
  432. item := TCmdStrListItem(item.next);
  433. end;
  434. end;
  435. procedure processimportedsyms(syms:tfpobjectlist);
  436. var
  437. i,j,k,l : longint;
  438. pkgentry : ppackageentry;
  439. sym : TSymEntry;
  440. srsymtable : tsymtable;
  441. module : tmodule;
  442. unitentry : pcontainedunit;
  443. name : tsymstr;
  444. pd : tprocdef;
  445. begin
  446. for i:=0 to syms.count-1 do
  447. begin
  448. sym:=tsymentry(syms[i]);
  449. if not (sym.typ in [staticvarsym,procsym]) then
  450. continue;
  451. if alreadyloaded.indexof(sym)>=0 then
  452. continue;
  453. { determine the unit of the symbol }
  454. srsymtable:=sym.owner;
  455. while not (srsymtable.symtabletype in [staticsymtable,globalsymtable]) do
  456. srsymtable:=srsymtable.defowner.owner;
  457. module:=tmodule(loaded_units.first);
  458. while assigned(module) do
  459. begin
  460. if (module.globalsymtable=srsymtable) or (module.localsymtable=srsymtable) then
  461. break;
  462. module:=tmodule(module.next);
  463. end;
  464. if not assigned(module) then
  465. internalerror(2014101001);
  466. if (uf_in_library and module.flags)=0 then
  467. { unit is not part of a package, so no need to handle it }
  468. continue;
  469. { loaded by a package? }
  470. for j:=0 to packagelist.count-1 do
  471. begin
  472. pkgentry:=ppackageentry(packagelist[j]);
  473. for k:=0 to pkgentry^.package.containedmodules.count-1 do
  474. begin
  475. unitentry:=pcontainedunit(pkgentry^.package.containedmodules[k]);
  476. if unitentry^.module=module then
  477. begin
  478. case sym.typ of
  479. staticvarsym:
  480. begin
  481. name:=tstaticvarsym(sym).mangledname;
  482. current_module.addexternalimport(pkgentry^.package.pplfilename,name,name+suffix_indirect,0,true,false);
  483. end;
  484. procsym:
  485. begin
  486. for l:=0 to tprocsym(sym).procdeflist.count-1 do
  487. begin
  488. pd:=tprocdef(tprocsym(sym).procdeflist[l]);
  489. import_proc_symbol(pd,pkgentry^.package);
  490. end;
  491. end;
  492. else
  493. internalerror(2014101001);
  494. end;
  495. alreadyloaded.add(sym);
  496. end;
  497. end;
  498. end;
  499. end;
  500. end;
  501. var
  502. unitentry : pcontainedunit;
  503. module : tmodule;
  504. begin
  505. { check each external asm symbol of each unit of the package whether it is
  506. contained in the unit of a loaded package (and thus an import entry
  507. is needed) }
  508. alreadyloaded:=tfpobjectlist.create(false);
  509. { first pass to find all symbols that were not loaded by asm name }
  510. module:=tmodule(loaded_units.first);
  511. while assigned(module) do
  512. begin
  513. //if not assigned(module.package) then
  514. if (uf_in_library and module.flags)=0 then
  515. processimportedsyms(module.unitimportsyms);
  516. module:=tmodule(module.next);
  517. end;
  518. alreadyloaded.free;
  519. end;
  520. end.