How to prevent CMake from adding host system flags (-arch arm64) when cross-compiling with ExternalProject_Add?

I’m working on cross-compiling a library for RISC-V using CMake’s ExternalProject_Add. When CMake tries to test the compiler, it’s adding macOS-specific flags (-arch arm64 -isysroot) even though I’m using a RISC-V toolchain. Here’s the error:

[1/2] /path/to/riscv32-esp-elf-gcc -march=rv32imc_zicsr_zifencei -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX15.1.sdk ... riscv32-esp-elf-gcc: error: unrecognized command-line option '-arch'

I’ve tried:

  • Setting CMAKE_OSX_* variables to empty strings
  • Using a custom toolchain file
  • Setting CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR

But CMake still adds these macOS-specific flags when testing the compiler. How can I prevent CMake from adding host system flags when using ExternalProject_Add?"

I solved it by properly passing the toolchain that I was using to build my main project. For future reference this is just by passing

CMAKE_ARGS
    "-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}"
    # ... rest of the args`