google / prefab

Prefab is a tool for generating build system integrations for prebuilt C/C++ libraries.
https://google.github.io/prefab/
Apache License 2.0
208 stars 32 forks source link

[FR] [QUESTION] How to model optional dependencies? #160

Open madebr opened 2 years ago

madebr commented 2 years ago

Is your feature request related to a problem? Please describe.

My application is able to dynamically load dependencies using dlopen + dladdr. I have modeled every library using prefab json files.

But I don't know of any way, other then explicitly do target_link_libraries to add a dynamic dependency to the .apk.

Describe the solution you'd like

I have 2 suggestions:

  1. add every dependency of the target to the .apk. Doing add_dependencies(myexecutable dynamic_dependency), should add dynamic_dependency to the .apk as well
  2. add some function to the android cmake toolchain script that marks a target to be added to the .apk: add_target_to_apk(dynamic_dependency)

Describe alternatives you've considered

Right now, I'm simply doing

target_link_libraries(mygame PRIVATE dynamic_dependency)

This type of dependency is honored by android's cmake support, but should not be necessary.

Additional context

I have encountered this issue while creating a prefab aar for SDL_image. SDL_image provides decoders for various image formats. Because not every applications needs every decoder, these are dynamically loaded. Dynamically loading also reduces the file size of an .apk.

DanAlbert commented 2 years ago

Depending on your answer to https://github.com/google/prefab/issues/159, I think these two are probably the same FR (modeling for runtime-only dependencies).

DanAlbert commented 2 years ago

Duplicate of #159

madebr commented 2 years ago

This is also a run-time dependency, but should only be satisfied when the application really needs it.

e.g. my game needs png support, so needs libpng. But my application does not need tiff support, so does not need libtiff. So I would like some way to make sure libpng is bundled in the .apk and not libtiff.

DanAlbert commented 2 years ago

Oh, and that choice is made at build time?

DanAlbert commented 2 years ago

Okay, I think not strictly a dup then, just closely related. Will reopen to track the optional aspect of runtime dependencies.

madebr commented 2 years ago

Oh, and that choice is made at build time?

Yes, the library must be included at build time.

159 is a dependency that can be modeled in json files to be processed by prefab.

The dependency here is made in the cmake script.

I'm not 100% sure this is a prefab issue though. If it isn't, can you please show me where to go instead or what alternatives there are?

DanAlbert commented 2 years ago

I haven't thought about it very deeply yet, but I suspect prefab needs to be at least somewhat involved here, if only to pass along metadata to another system like AGP.

How do you envision this being controlled by the consumer? I think this probably ends up being something system-specific in your build script. For CMake this is probably something like https://cmake.org/cmake/help/latest/command/install.html#installing-runtime-dependencies?

install(RUNTIME_DEPENDENCY_SET package::optional-dependency)

install seems to be how CMakes wants runtime dependencies declared. AGP doesn't currently use that model (it probably should have). We've considered adding support for that before, and this may require it.

ndk-build would need to grow a similar feature, but that's fairly easy.

Or did you have something else in mind? The other things I can think of would be something contained in the build.gradle file, which seems like it'd be cumbersome for the author to keep in sync with the real build script; or doubling up on packaging some modules so it would expose both package::module and package::module-with-optional-dependency, which works today but is prone to combinatoric explosion for something like ffmpeg...