CMake not recognizing compiler on MacOS with Xcode Generator

Hi,

I have installed CMake 3.17.1, gcc 9.3.0, openmpi 3.1.5 and other tools on my Mac (10.15.4) using spack. I am able to configure and build my code with the following options

cmake ../ \
 -DMPI_C_COMPILER=mpicc -DMPI_CXX_COMPILER=mpicxx \
 -DCMAKE_BUILD_TYPE=Release \
 -DCMAKE_C_COMPILER=gcc \
 -DCMAKE_CXX_COMPILER=g++ \
 -DCMAKE_Fortran_COMPILER=gfortran

However, when I add the Xcode generator option:

cmake -G Xcode ../ \
 -DMPI_C_COMPILER=mpicc -DMPI_CXX_COMPILER=mpicxx \
 -DCMAKE_BUILD_TYPE=Release \
 -DCMAKE_C_COMPILER=gcc \
 -DCMAKE_CXX_COMPILER=g++ \
 -DCMAKE_Fortran_COMPILER=gfortran

cmake is unable to identify the compiler and gives the following errors. Is there a way to resolve this and get cmake to generate the workspace for Xcode?

-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
<edited>
-- Configuring incomplete, errors occurred!

It looks like the build command that Cmake is using to compile the test code is adding options that are valid for clang, but not for gcc.

gcc -x c -target x86_64-apple-macos10.15 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -fpascal-strings -fasm-blocks --serialize-diagnostics <among other options>

The gcc compiler is complaining about these. Where/How does cmake decide which options to use? Is there a way to suppress these offending options?

The executables “gcc” and “g++” on macOS are actually symlinks to AppleClang by default. Have you tried setting absolute paths to your compiler, eg. /usr/local/bin/gcc-9 (if installed via Homebrew)?