Search code examples
meson-build

if exists, run a custom target from another target in meson


I have two custom targets and a parent one:

child1_tgt = custom_target('child1-target', ...)

Sometimes child2_tgt won't be created:

# pseudo code:
if windows
child2_tgt = custom_target('child2-target', ...)
endif

I need to detect if child2_tgt is created and add it as parent dependency:

parent_tgt = custom_target('parent-target', ...)

if child2_tgt.exists()
# code here to add as dependency to parent_tgt
endif

My questions are:

  1. how can I add a target run before/after my parent_tgt is build?
  2. how can I add child2_tgt as dependecy target for parent_tgt?

Solution

  • Since the parent_tgt target (possibly) depends on the child2_tgt target, it must be defined before child2_tgt. meson does not support attaching dependencies after a target has been defined.

    Since you are conditionally defining child2_tgt based off the host platform, you might encounter issues with meson complaining that child2_tgt is not defined. You can get around this by simply setting child2_tgt to an empty array, like...

    if windows:
      child2_tgt = custom_target(...)
    else
      child2_tgt = []
    endif
    
    parent_tgt = custom_target(..., depends: [child2_tgt])
    

    Even if child2_tgt is [], since meson auto-flattens most arguments, it will end up resolving to just depends: [].