Zig module dependency injection

I’ve been thinking about making my own package containing media helper functions using SDL.

Originally I planned to package SDL directly into this project, and then the user of my package would get access to SDL by proxy. But it got me thinking, is there a way I can specify that my package depends on SDL without having to manage that dependency myself? In other words, is there a way that I can require the user of my package to pass in a module containing the SDL library?

There would be some nice benefits to this. For one, it would simplify my utilities package, which is always neat. But mainly, it would give the user of the package more control over how SDL is set up. They could easily use some sort of SDL wrapper library alongside my utilities library, just by passing in that SDL dependency that they would get from whatever wrapper they are using. I imagine this would make it a lot more plug-and-play with other packages, which is ideal for a utilities library.

You can take a look at how DVUI does it with b.systemIntegrationOption. DVUI has sdl as a dependency but can be overridden by passing -fsys.

Other than using the system’s sdl, you could change how the dependency is used. Instead of calling b.dependency, you ask the user to import the build script, then call public functions to get the module. There you can ask for the sdl artifact/module and link against it.

It wasn’t clear in my mind until recently that what @abdullah-b-al suggests was a thing. One exemple of this approach I happen to discover recently is zemscripten , in case it gives you some inspiration.