# How to handle failure of multiple \`execute\_process\` calls

**URL:** https://discourse.cmake.org/t/how-to-handle-failure-of-multiple-execute-process-calls/4004
**Category:** Usage
**Created:** [August 31, 2021, 8:52pm UTC](https://discourse.cmake.org/t/how-to-handle-failure-of-multiple-execute-process-calls/4004 "2021-08-31T20:52:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![morrisonlevi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/morrisonlevi/32/1354_2.png) [@morrisonlevi](https://discourse.cmake.org/u/morrisonlevi)
#### Post date: [August 31, 2021, 8:52pm UTC](https://discourse.cmake.org/t/how-to-handle-failure-of-multiple-execute-process-calls/4004/1 "2021-08-31T20:52:24Z")

</div>

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

```auto
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.

```cmake
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?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [August 31, 2021, 9:52pm UTC](https://discourse.cmake.org/t/how-to-handle-failure-of-multiple-execute-process-calls/4004/2 "2021-08-31T21:52:14Z")

</div>

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

I would do something like this:

```cmake
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 ()

```
