Compile-time inclusion of binary packages data within library package

I have a library crate that assumes some config directory will be present at the cargo manifest level of a consuming binary package. I'd like to be able to include the contents of the files in that config directory in the binary, e.g. with include_str, so that they are accessible within the library crate.

Is that possible?

Why not? include_str returns a string that you can just pass to your library or how do you imagine your interface to look like?

Sorry, that’s not what I mean. I want the library to be able to load them from the consuming binary. The contract would be that the files exist at compile time. I don’t want to have to load them in the binary and pass them as arguments.

That sounds more like you want a build script that makes sure the files are in place during compile time?

Consider this:

  • Package A has a library which does what you want plus has 1 or more binaries which include it.
  • Package B has a library which depends on Package A's library plus has 1 or more binaries which depend on Package A's library.
  • Package C has a binary which depends on both of the other crates' libraries.

Cargo only builds Crate A's library once when building the entire set. Where should the file be pulled from when building Package A's library?

I’ve been looking at in the lib but it doesn’t seem possible to access the source of the binary application. Similarly with in the binary, I can’t inject the files into the out directory and include them in the lib.

When you say a build script, were you meaning or some custom setup?

I think the issue I have is that the library is really also the application. The binary is used to provide some customisation through traits and the main entry point.

Maybe I just need to rethink the architecture of this.

I was thinking a build script in the library rather than the binary yes. From a perspective of hermeticity I'd expect that to be the most common build workflow of a library, but I really never got into complex build patterns and haven't fully grasped what the contents of the file is you need when building your library. It does sound like the border between lib and binary is rather vague and cross-cutting and might be better abstracted in some way, maybe a macro or something?