Search code examples
ddub

In DUB, how do I conditionally compile code based on optional dependencies?


If I have a DUB-based project with an optional dependency, how to I make it so that some code (be it modules or a version(...){...} block) only gets compiled if the dependency is chosen? Is there a way I can make a version identifier defined based on whether the dependency is present or absent?

I have already read about the "optional" and "default" attributes of the "dependency" tag as documented here. This allows me to define a dependency as optional, but it lacks teeth if I can't version my code to reflect the dependencies chosen.


Solution

  • Maybe you can, I am not sure but it seems you can use something like this:

    version(Have_name_of_dependency)
    

    So if your optional dependency would be sdlang-d you could use

    Have_sdlang_d
    

    Same as here

    This is based on this part of dub code