Supplying if() condition arguments via list variable

I know it’s not uncommon to do things like this:

set(_my_targets lib1 lib2 mainexe configexe)
install(
  TARGETS ${_my_targets}
  RUNTIME DESTINATION ...
)

Or this…

set(_qt_components Core Network Widgets)
find_package(Qt6 COMPONENTS ${_qt_components})

…But on a scale of 1 to “What is WRONG with you???”, how bad is it to do something like this?:

set(_dependency_checks
  ENABLE_DEP
  DepPkg_FOUND
  OtherPkg_FOUND
  AddonPkg_FOUND
)
add_feature_info(
  "Dependency"
  "${_dependency_checks}"
  "Use the dependency"
)
list(JOIN _dependency_checks ";AND;" _if_conditions)
if(${_if_conditions})
  # Configure the dependency; all of its conditions are met
endif()

(I’m specifically wondering about the if() statement there. The add_feature_info() second arg, I’m not worried about.)

I know it works, but it makes me feel kind of gross just writing it.

1 Like

I can sort of understand the motivation to deduplicate code between add_feature_info and your if statement, but I think this is more complex and error-prone than it needs to be. I might consider it if there were like 10+ conditions, but for only 4, I would just violate DRY in the name of easier-to-read code.

1 Like

Well, the real code does actually have… 8 conditions. So, it’s definitely on the line.

But it still feels icky.

(I mean, if it were just add_feature_info(), I could do this:

if(ENABLE_FOO AND Foo_FOUND AND Bar_FOUND AND
   Baz_FOUND AND Bat_FOUND AND Frob_FOUND AND
   Xyzzy_FOUND AND Zork_FOUND)
  add_feature_info("Dependency" TRUE "Use the dependency")
else()
  add_feature_info("Dependency" FALSE "Use the dependency")
endif()

But assume there’s more complexity involved in generating the initial _dependency_checks list to begin with, that precludes doing that.)