<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/61432>61432</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Cross-compiling from macOS host fails CheckAtomics cmake test
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          eggyal
      </td>
    </tr>
</table>

<pre>
    When cross-compiling LLVM on macOS host (in my case, for Linux target) cmake fails:

```text
% cmake /path/to/llvm \
  -DCMAKE_SYSTEM_NAME=Linux \
  -DCMAKE_SYSTEM_PROCESSOR=x86_64 \
  -DCMAKE_C_COMPILER=x86_64-unknown-linux-gnu-gcc \
 -DCMAKE_CXX_COMPILER=x86_64-unknown-linux-gnu-g++ \
 -DCMAKE_BUILD_TYPE=Release

[snip]

-- Performing Test HAVE_CXX_ATOMICS_WITH_LIB - Failed
CMake Error at cmake/modules/CheckAtomic.cmake:56 (message):
  Host compiler must support std::atomic!
Call Stack (most recent call first):
 cmake/config-ix.cmake:411 (include)
  CMakeLists.txt:776 (include)
```

Relevant from `CMakeFiles/CMakeError.log`:

```text
Performing C++ SOURCE FILE Test HAVE_CXX_ATOMICS_WITH_LIB failed with the following output:
Change Dir: /path/to/build/CMakeFiles/CMakeScratch/TryCompile-lNXSZR

Run Build Command(s):/usr/bin/make -f Makefile cmTC_b0c0b/fast && /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/cmTC_b0c0b.dir/build.make CMakeFiles/cmTC_b0c0b.dir/build
Building CXX object CMakeFiles/cmTC_b0c0b.dir/src.cxx.o
/usr/local/bin/x86_64-unknown-linux-gnu-g++ -DHAVE_CXX_ATOMICS_WITH_LIB  -std=c++11 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk -std=c++14 -o CMakeFiles/cmTC_b0c0b.dir/src.cxx.o -c /path/to/build/CMakeFiles/CMakeScratch/TryCompile-lNXSZR/src.cxx
In file included from /usr/local/Cellar/x86_64-unknown-linux-gnu/7.2.0/toolchain/x86_64-unknown-linux-gnu/include/c++/7.2.0/x86_64-unknown-linux-gnu/bits/c++config.h:533:0,
 from /usr/local/Cellar/x86_64-unknown-linux-gnu/7.2.0/toolchain/x86_64-unknown-linux-gnu/include/c++/7.2.0/bits/atomic_base.h:35,
 from /usr/local/Cellar/x86_64-unknown-linux-gnu/7.2.0/toolchain/x86_64-unknown-linux-gnu/include/c++/7.2.0/atomic:41,
 from /path/to/build/CMakeFiles/CMakeScratch/TryCompile-lNXSZR/src.cxx:2:
/usr/local/Cellar/x86_64-unknown-linux-gnu/7.2.0/toolchain/x86_64-unknown-linux-gnu/include/c++/7.2.0/x86_64-unknown-linux-gnu/bits/os_defines.h:39:10: fatal error: features.h: No such file or directory
 #include <features.h>
          ^~~~~~~~~~~~
compilation terminated.
make[1]: *** [CMakeFiles/cmTC_b0c0b.dir/src.cxx.o] Error 1
make: *** [cmTC_b0c0b/fast] Error 2


Source file was:

#include <atomic>
std::atomic<int> x;
std::atomic<short> y;
std::atomic<char> z;
int main() {
  ++z;
  ++y;
  return ++x;
}
```

I note in particular that the C++ compiler was invoked with `-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk`.  However, I don't think this option should be present when compiling for a non-Darwin target? Indeed if it is removed, the compilation succeeds:

```
% cat src.cxx 
#include <atomic>
std::atomic<int> x;
std::atomic<short> y;
std::atomic<char> z;
int main() {
  ++z;
  ++y;
  return ++x;
}


% x86_64-unknown-linux-gnu-g++ -DHAVE_CXX_ATOMICS_WITH_LIB  -std=c++11 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk -std=c++14 -c src.cxx 
In file included from /usr/local/Cellar/x86_64-unknown-linux-gnu/7.2.0/toolchain/x86_64-unknown-linux-gnu/include/c++/7.2.0/x86_64-unknown-linux-gnu/bits/c++config.h:533:0,
 from /usr/local/Cellar/x86_64-unknown-linux-gnu/7.2.0/toolchain/x86_64-unknown-linux-gnu/include/c++/7.2.0/bits/atomic_base.h:35,
 from /usr/local/Cellar/x86_64-unknown-linux-gnu/7.2.0/toolchain/x86_64-unknown-linux-gnu/include/c++/7.2.0/atomic:41,
 from src.cxx:1:
/usr/local/Cellar/x86_64-unknown-linux-gnu/7.2.0/toolchain/x86_64-unknown-linux-gnu/include/c++/7.2.0/x86_64-unknown-linux-gnu/bits/os_defines.h:39:10: fatal error: features.h: No such file or directory
 #include <features.h>
          ^~~~~~~~~~~~
compilation terminated.


% x86_64-unknown-linux-gnu-g++ -DHAVE_CXX_ATOMICS_WITH_LIB  -std=c++11 -std=c++14 -c src.cxx && echo success
success
```

In a fresh CMake project using LLVM's `CheckAtomics` module, the `-isysroot` option isn't present... so it seems that LLVM's CMake project is responsible here.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWF9v4roS_zTmZZQoOA3QBx4gEC06dLsqPef03BdknAnxrWMj2ynlPtzPfuUk_Gm32-2V9kh7dRchUOKZ8fzzzM_DrBVbhTgmyZQksx6rXanNGLfbA5O9jc4P4z9LVMCNtjbgutoJKdQWlss_bkArqBi_XUGprQNCR0JBdQDOLBKaQqENLIWqn8Exs0VH6DXwij0iFExIS-IJiWYkOv4Oovbr8Nl1r2jSMRCa7ZgrCc2cJjST8qkCkqQtGUAwS28mv83Xq79W9_Ob9efJzZzEs3bvb5N9ubtN56vV7R2JZ8-jwXpw9RZxuk5vb74slvMzWVCrR6X3KpB-h2Cr6mDL-QXziffh4WPchE4Jnb4hYfr7Yjlb3__1xRt0hxK9by-9lkytEjuSzC7fBgF8QVNoU_lY3aN18GnyR6vP5P72ZpGu1n8u7j-tl4spBJAxITFvWdMb7--5MdoAc637Cc0qndcSLaFZWiJ_nDhdCR62q_EkGfjoV2gt2yKh16fQAnzyqdHmDRqoauvA1rudNg6syz1hPGGNNEL7nQpMSlg5xh8bqV6AQY7KAfcrhTDWvdjkqC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