Skip to content

`check_language(lang)` does not set `CMAKE_${lang}_COMPILER_ARG1`

Consider a minimal CMake project

cmake_minimum_required (VERSION 3.16)
project (Proton LANGUAGES)
include (CheckLanguage)
check_language(C)
enable_language(C)

On my system, I have a zig cc installed, so my C compiler is the binary zig with an argument cc.

What happens is that check_language(C) sets CMAKE_C_COMPILER to zig but it drops the cc argument. When enable_language(C) runs afterwards, it goes with zig as the C compiler, which fails.

$ CC='zig cc' cmake .
-- Looking for a C compiler
-- Looking for a C compiler - /home/jdanek/.local/bin/zig
-- The C compiler identification is unknown
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /home/jdanek/.local/bin/zig
-- Check for working C compiler: /home/jdanek/.local/bin/zig - broken
CMake Error at /usr/share/cmake/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "/home/jdanek/.local/bin/zig"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: '/home/jdanek/minimal/CMakeFiles/CMakeScratch/TryCompile-zh4j6p'
    
    Run Build Command(s): /usr/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_d5805/fast
    /usr/bin/gmake  -f CMakeFiles/cmTC_d5805.dir/build.make CMakeFiles/cmTC_d5805.dir/build
    gmake[1]: Entering directory '/home/jdanek/minimal/CMakeFiles/CMakeScratch/TryCompile-zh4j6p'
    Building C object CMakeFiles/cmTC_d5805.dir/testCCompiler.c.o
    /home/jdanek/.local/bin/zig    -o CMakeFiles/cmTC_d5805.dir/testCCompiler.c.o -c /home/jdanek/minimal/CMakeFiles/CMakeScratch/TryCompile-zh4j6p/testCCompiler.c
    info: Usage: zig [command] [options]
    
    Commands:
    
      build            Build project from build.zig
      init-exe         Initialize a `zig build` application in the cwd
      init-lib         Initialize a `zig build` library in the cwd
    
      ast-check        Look for simple compile errors in any set of files
      build-exe        Create executable from source or object files
      build-lib        Create library from source or object files
      build-obj        Create object from source or object files
      fmt              Reformat Zig source into canonical form
      run              Create executable and run immediately
      test             Create and run a test build
      translate-c      Convert C code to Zig code
    
      ar               Use Zig as a drop-in archiver
      cc               Use Zig as a drop-in C compiler
      c++              Use Zig as a drop-in C++ compiler
      dlltool          Use Zig as a drop-in dlltool.exe
      lib              Use Zig as a drop-in lib.exe
      ranlib           Use Zig as a drop-in ranlib
      objcopy          Use Zig as a drop-in objcopy
    
      env              Print lib path, std path, cache directory, and version
      help             Print this help and exit
      libc             Display native libc paths file or validate one
      targets          List available compilation targets
      version          Print version number and exit
      zen              Print Zen of Zig and exit
    
    General Options:
    
      -h, --help       Print command-specific usage
    
    error: unknown command: -o
    gmake[1]: *** [CMakeFiles/cmTC_d5805.dir/build.make:78: CMakeFiles/cmTC_d5805.dir/testCCompiler.c.o] Error 1
    gmake[1]: Leaving directory '/home/jdanek/minimal/CMakeFiles/CMakeScratch/TryCompile-zh4j6p'
    gmake: *** [Makefile:127: cmTC_d5805/fast] Error 2
    
    

  

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:5 (enable_language)


-- Configuring incomplete, errors occurred!

Removing the call to check_language(C) allows the project generation to succeed.

I want check_language(C) to stop breaking my project cache.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information