pkgutil.pas 17 KB

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