CMake not passing -arch command during macOS cross-compile

I have been cross-compiling LLVM on a macOS (studio) arm machine for x86_64 and arm64 separately, because you need to specify a ‘default’ target triple for the given platform. There is a variable called LLVM_HOST_TRIPLE and it might have x86_64-apple-darwin for example.

When only a single architecture is set in CMAKE_OSX_ARCHITECTURES no -arch flag is passed, you only get -arch when two architectures are specified. This means the compiler generates arm (instead of x86) binaries when a single architecture is specified and the default triple of the compiler is arm64-apple-darwin, because no -arch flag was specified. This can result in a confused compiler that outputs x86_64 binaries by default but only runs on arm64.

In summary, when cross-compiling using LLVM you should always pass the -arch flag to guarantee the binary generated matches CMAKE_OSX_ARCHITECTURES. Alternatively, cmake could check the default target triple of the compiler and make sure it matches the single architecture specified in CMAKE_OSX_ARCHITECTURES.

CACHE INTERNAL was missing on CMAKE_OSX_ARCHITECTURES