pkgutil.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. (
  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 : 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. if not (target_info.system in systems_indirect_var_imports) then
  404. exit;
  405. for i:=0 to packagelist.count-1 do
  406. begin
  407. pkgentry:=ppackageentry(packagelist[i]);
  408. if pkgentry^.usedunits>0 then
  409. begin
  410. //writeln('package used: ',pkgentry^.realpkgname);
  411. pkgname:=pkgentry^.package.pplfilename;
  412. if copy(pkgname,1,length(target_info.sharedlibprefix))=target_info.sharedlibprefix then
  413. delete(pkgname,1,length(target_info.sharedlibprefix));
  414. if copy(pkgname,length(pkgname)-length(target_info.sharedlibext)+1,length(target_info.sharedlibext))=target_info.sharedlibext then
  415. delete(pkgname,length(pkgname)-length(target_info.sharedlibext)+1,length(target_info.sharedlibext));
  416. //writeln('adding library: ', pkgname);
  417. l.sharedlibfiles.concat(pkgname);
  418. end
  419. else
  420. {writeln('ignoring package: ',pkgentry^.realpkgname)};
  421. end;
  422. end;
  423. procedure createimportlibfromexternals;
  424. var
  425. alreadyloaded : tfpobjectlist;
  426. procedure import_proc_symbol(pd:tprocdef;pkg:tpackage);
  427. var
  428. item : TCmdStrListItem;
  429. begin
  430. item := TCmdStrListItem(pd.aliasnames.first);
  431. while assigned(item) do
  432. begin
  433. current_module.addexternalimport(pkg.pplfilename,item.str,item.str,0,false,false);
  434. item := TCmdStrListItem(item.next);
  435. end;
  436. end;
  437. procedure processimportedsyms(syms:tfpobjectlist);
  438. var
  439. i,j,k,l : longint;
  440. pkgentry : ppackageentry;
  441. sym : TSymEntry;
  442. srsymtable : tsymtable;
  443. module : tmodule;
  444. unitentry : pcontainedunit;
  445. name : tsymstr;
  446. pd : tprocdef;
  447. begin
  448. for i:=0 to syms.count-1 do
  449. begin
  450. sym:=tsymentry(syms[i]);
  451. if not (sym.typ in [staticvarsym,procsym]) then
  452. continue;
  453. if alreadyloaded.indexof(sym)>=0 then
  454. continue;
  455. { determine the unit of the symbol }
  456. srsymtable:=sym.owner;
  457. while not (srsymtable.symtabletype in [staticsymtable,globalsymtable]) do
  458. srsymtable:=srsymtable.defowner.owner;
  459. module:=tmodule(loaded_units.first);
  460. while assigned(module) do
  461. begin
  462. if (module.globalsymtable=srsymtable) or (module.localsymtable=srsymtable) then
  463. break;
  464. module:=tmodule(module.next);
  465. end;
  466. if not assigned(module) then
  467. internalerror(2014101001);
  468. if (uf_in_library and module.flags)=0 then
  469. { unit is not part of a package, so no need to handle it }
  470. continue;
  471. { loaded by a package? }
  472. for j:=0 to packagelist.count-1 do
  473. begin
  474. pkgentry:=ppackageentry(packagelist[j]);
  475. for k:=0 to pkgentry^.package.containedmodules.count-1 do
  476. begin
  477. unitentry:=pcontainedunit(pkgentry^.package.containedmodules[k]);
  478. if unitentry^.module=module then
  479. begin
  480. case sym.typ of
  481. staticvarsym:
  482. begin
  483. name:=tstaticvarsym(sym).mangledname;
  484. current_module.addexternalimport(pkgentry^.package.pplfilename,name,name+suffix_indirect,0,true,false);
  485. end;
  486. procsym:
  487. begin
  488. for l:=0 to tprocsym(sym).procdeflist.count-1 do
  489. begin
  490. pd:=tprocdef(tprocsym(sym).procdeflist[l]);
  491. import_proc_symbol(pd,pkgentry^.package);
  492. end;
  493. end;
  494. else
  495. internalerror(2014101001);
  496. end;
  497. alreadyloaded.add(sym);
  498. end;
  499. end;
  500. end;
  501. end;
  502. end;
  503. var
  504. unitentry : pcontainedunit;
  505. module : tmodule;
  506. begin
  507. { check each external asm symbol of each unit of the package whether it is
  508. contained in the unit of a loaded package (and thus an import entry
  509. is needed) }
  510. alreadyloaded:=tfpobjectlist.create(false);
  511. { first pass to find all symbols that were not loaded by asm name }
  512. module:=tmodule(loaded_units.first);
  513. while assigned(module) do
  514. begin
  515. //if not assigned(module.package) then
  516. if (uf_in_library and module.flags)=0 then
  517. processimportedsyms(module.unitimportsyms);
  518. module:=tmodule(module.next);
  519. end;
  520. alreadyloaded.free;
  521. end;
  522. end.