How to handle failure of multiple `execute_process` calls

I’m writing a find module for php-config. I need to run a bunch of commands to gather what I need e.g.

php-config --includes
php-config --ldflags
php-config --libs
php-config --version

So, I have an execute_process for each of them. In the past I’ve used something like find_package_handle_standard_args to handle these, but in this case it’s fine if they are empty. For instance, on the PHP 8.1.0rc1 release I just built --ldflags is empty because all libs used were in standard locations.

For now I have a series of checks for the RESULT_VARIABLE e.g.

if(PhpConfig_INCLUDES_RESULT AND NOT PhpConfig_INCLUDES_RESULT EQUAL 0)
  return()
endif()

if(PhpConfig_LDFLAGS_RESULT AND NOT PhpConfig_LDFLAGS_RESULT EQUAL 0)
  return()
endif()

if(PhpConfig_LIBS_RESULT AND NOT PhpConfig_LIBS_RESULT EQUAL 0)
  return()
endif()

if(PhpConfig_VERSION_RESULT AND NOT PhpConfig_VERSION_RESULT EQUAL 0)
  return()
endif()

Is there a more elegant way to handle this?

All of your checks are redundant internally. Do you mean if (DEFINED … AND NOT … EQUAL "0")?

I would do something like this:

find_program(PHP_CONFIG_EXECUTABLE NAMES php-config DOC "php build configuration executable")
mark_as_advanced(PHP_CONFIG_EXECUTABLE)

foreach (flag IN ITEMS --includes --ldflags --libs --version)
  execute_process(…)
  if (result)
    # failure
    set(PHP_FOUND 0)
    set(PHP_NOT_FOUND_MESSAGE "…")
    return ()
  endif ()
endforeach ()

find_package_handle_standard_args(… REQUIRED_VARS PHP_CONFIG_EXECUTABLE)

if (PHP_FOUND)
  # make a target using the variables discovered above
endif ()