# Race condition in CheckCXXSourceRuns ?

**URL:** https://discourse.cmake.org/t/race-condition-in-checkcxxsourceruns/1242
**Category:** Development
**Created:** [May 20, 2020, 6:31am UTC](https://discourse.cmake.org/t/race-condition-in-checkcxxsourceruns/1242 "2020-05-20T06:31:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Artem\_Dinaburg](https://discourse.cmake.org/user_avatar/discourse.cmake.org/artem_dinaburg/32/609_2.png) [@Artem\_Dinaburg](https://discourse.cmake.org/u/Artem_Dinaburg)
#### Post date: [May 20, 2020, 6:31am UTC](https://discourse.cmake.org/t/race-condition-in-checkcxxsourceruns/1242/1 "2020-05-20T06:31:13Z")

</div>

Hi,

I notice that [CheckCXXSourceRuns](https://github.com/Kitware/CMake/blob/master/Modules/CheckCXXSourceRuns.cmake) uses `file(WRITE ...)` to generate a source file, and then uses `try_compile` to build the freshly generated source.

It is my (possibly wrong?) understanding that there is no explicit dependency between the code generated by `file(WRITE ...)` and the `try_compile` command, so that the `try_compile` may occur _before_ the `file(WRITE ...` succeeds in a massively parallel build.

I ran into this exact error not in `CheckCXXSourceRuns`, but in nearly identical code modeled after the function, on a 40 core machine using Ninja as the generator.

Is there indeed a race condition here, or am I off the mark?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [May 20, 2020, 9:49am UTC](https://discourse.cmake.org/t/race-condition-in-checkcxxsourceruns/1242/2 "2020-05-20T09:49:22Z")

</div>

A CMake script runs sequentially. The call to `try_compile()` occurs later in the same script that calls `file(WRITE)`, so the write must be complete before `try_compile()`. If this script is executed as part of a build rule somehow (e.g. as part of an ExternalProject), then you may have multiple scripts running simultaneously, but the project is responsible for expressing dependencies, etc. correctly so that steps are executed in the right order.

---

<div class="post-metadata">

### Author: ![Artem\_Dinaburg](https://discourse.cmake.org/user_avatar/discourse.cmake.org/artem_dinaburg/32/609_2.png) [@Artem\_Dinaburg](https://discourse.cmake.org/u/Artem_Dinaburg)
#### Post date: [May 20, 2020, 3:51pm UTC](https://discourse.cmake.org/t/race-condition-in-checkcxxsourceruns/1242/3 "2020-05-20T15:51:13Z")

</div>

Thank you for the reply! I am at a loss as to what was happening then and will try to investigate a bit more.

The code in question is LLVM’s FindZ3.cmake ([https://github.com/llvm/llvm-project/blob/master/llvm/cmake/modules/FindZ3.cmake#L4-L27](https://github.com/llvm/llvm-project/blob/master/llvm/cmake/modules/FindZ3.cmake#L4-L27)) which has the same pattern, and should also be unaffected, but I was regularly having the `try_compile` fail because the input file was missing.

Maybe there was just an issue with the write failing in general. Is there a way to determine if `file(WRITE ...)` failed? My initial guess is to try reading the file back and compare content to what should be in there.
