I have this interface for plugins to register plugins in my program:
let ht : (string, (string * (_ -> _ -> bool))) Hashtbl.t = Hashtbl.create 0
let assets = ref ""
let register ~ns m fn = let fn = fn !assets in Hashtbl.add ht m (ns, fn)
let get = Hashtbl.find_opt ht
Then I have the main executable loading plugins this way:
let register_plugin dir =
let assets = Filename.concat dir "assets" in
GwdPlugin.assets := assets ;
Secure.add_lang_path assets ;
Dynlink.loadfile (Filename.concat dir "plugin.cmxs") ;
GwdPlugin.assets := ""
But I have a lot of problems when trying to use libraries which are not included in my main program.
I tried to use embed_in_plugin_libraries and -linkall. Each time I get un undefined symbol, I add the corresponding lib in embed_in_plugin_libraries.
aaa si the library of my main program (plugin registration + the rest of its world), bbb ccc ddd libraries not used in the main program.
But now, I am stuck because the undefined symbol is actually
undefined symbol: camlStdlib__lexing__engine_113
Is it possible for my main program to link the whole stdlib without the compiler removing ? It is ok for me to state that the main program to embed unused code if it is only the stdlib (maybe this would be sufficent to solve the issue)
Is it possible for my plugin to embed stdlib as I did it with other libs ? Can’t find a package for this.
The last time Idid this sort of thing (which was a good long while ago), it was with bytecode. And I remember that there was something I had to do, to ensure that modules were linked that were needed by any libraries I wanted to Dynload. Maybe that’s what you’ve done with your hack. [I realize that you’re doing this with native code.] Does it work when you try all this with bytecode?
typically you would use, either Dynlink, orFindlib. Findlib is a an extra layer that takes care of loading plugin dependencies recursively.
for your “stdlib” problem, you should add -linkall to the link_flags of the main program.
If you want to use Findlib, then you can use it to load your main plugin as well, by putting the Findlib.load_packages call in the main program (in lieu of Dynlink.loadprivate).
I will try with the -linkall option for my main program.
And I think that I can not use findlib in the final product since my use case is to distribute binary (main program) and plugins without the whole ocaml environnement. Also the reason why I can not use bytecode and need native plugins.
So, I’ll try to linkall my main program and to use embed_in_plugin_libraries for my plugins (which leads me to an error of already loaded module, if I rember correctly, but I need to double check this)
This field is meant for libraries that need to be statically linked into your plugin (ie which are not linked into the main program). In other words, libraries that are used simultaneously from several plugins should be linked into the main program, and not put in this field.
In other words, libraries that are used simultaneously from several plugins should be linked into the main program, and not put in this field.
This restriction may make building independent plugins very difficult. I am trying to make mutliple simple plugins in order to export libs one by one. Is it completely insane or should it work?
And it works, but as I said, I feel like it is completely insane to do so.
The dependencies hell and the extra work required by this workflow is ok for me, as I planned to have a package manager to handle it for me anyway (or as I do not really care about having to add all that options).
The (flags -linkall) is a bit fishy. If anything, it should be (link_flags -linkall), but my guess is that you don’t want to put this flag in at all in your “plugin” stanzas.
There is an alternative approach to building plugin systems in dune with automatic dependency handling, which does not depend on findlib, called “sites”. It may or may not be suitable for your use-case: you can read more about it here: dune/doc/sites.rst at main · ocaml/dune · GitHub
I will try without the flag (and actually did not know about link_flags) but I thought that compiler might eliminate library’s code if not using it (since my plugins do absolutly nothing).
Nerver heard about this! I will have a look, thanks.
The sites are new in the futur 2.8, there is also a dune install --relocatable --prefix=<dir> mode for installing the executable, the librairies and the plugins without everything else inside a relocatable directory <dir>. However all the libraries that are used by the plugins but not used by the binaries should be in the dune workspace (or you can copy by hand the installed libraries inside <dir> ).