CheckLanguage does not take the C++ standard into account

Attila Krasznahorkay
Tue, 13 Aug 2024 11:48:53 +0000
Development

Hi,

I’ve run into an issue recently that I think CMake could be helping with in a better way. In one of our projects we tend to make use of the very latest version of certain compilers at the same time. Which does not always work. For instance right now one cannot use CUDA 12.6 with oneAPI 2024.2.1 as the host compiler.

/home/krasznaa/software/nvidia/cuda-12.6.0/x86_64/bin/nvcc
-forward-unknown-to-host-compiler
-ccbin=/home/krasznaa/software/intel/oneapi-2024.2.1/compiler/2024.2/bin/compiler/clang++
-allow-unsupported-compiler -std=c++20 -c main.cu -o main.cu.o
/usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/type_traits(3433):
error: type name is not allowed
       : bool_constant<__is_layout_compatible(_Tp, _Up)>
                                              ^

/usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/type_traits(3433):
error: type name is not allowed
       : bool_constant<__is_layout_compatible(_Tp, _Up)>
                                                   ^

/usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/type_traits(3433):
error: identifier "__is_layout_compatible" is undefined
       : bool_constant<__is_layout_compatible(_Tp, _Up)>
                       ^

/usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/type_traits(3440):
error: type name is not allowed
         = __is_layout_compatible(_Tp, _Up);
                                  ^

/usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/type_traits(3440):
error: type name is not allowed
         = __is_layout_compatible(_Tp, _Up);
                                       ^

5 errors detected in the compilation of "main.cu".

That part has nothing to do with CMake, the “interesting” part is just that this compiler combination happens to not have issues when using C++17. But it does with C++20. So now, when I write the following CMake code:

cmake_minimum_required(VERSION 3.26)
project(CheckLanguageCUDA VERSION 0.0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20 CACHE STRING "Host compiler C++ version")
set(CMAKE_CUDA_STANDARD 20 CACHE STRING "CUDA compiler C++ version")

include(CheckLanguage)
check_language(CUDA)

if(CMAKE_CUDA_COMPILER)
   enable_language(CUDA)
endif()

, I get the following output:

-- The CXX compiler identification is IntelLLVM 2024.2.1
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/krasznaa/software/intel/oneapi-2024.2.1/compiler/2024.2/bin/icpx - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for a CUDA compiler
-- Looking for a CUDA compiler - /home/krasznaa/software/nvidia/cuda-12.6.0/x86_64/bin/nvcc
-- Looking for a CUDA host compiler - /home/krasznaa/software/intel/oneapi-2024.2.1/compiler/2024.2/bin/icpx
-- The CUDA compiler identification is NVIDIA 12.6.20
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - failed
-- Check for working CUDA compiler: /home/krasznaa/software/nvidia/cuda-12.6.0/x86_64/bin/nvcc
-- Check for working CUDA compiler: /home/krasznaa/software/nvidia/cuda-12.6.0/x86_64/bin/nvcc - broken
CMake Error at /home/krasznaa/software/kitware/cmake-3.30.2/x86_64-ubuntu2204-gcc11-opt/share/cmake-3.30/Modules/CMakeTestCUDACompiler.cmake:59 (message):
  The CUDA compiler

    "/home/krasznaa/software/nvidia/cuda-12.6.0/x86_64/bin/nvcc"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: '/home/krasznaa/development/cmake/build/CMakeFiles/CMakeScratch/TryCompile-ym6qLI'

    Run Build Command(s): /home/krasznaa/software/kitware/cmake-3.30.2/x86_64-ubuntu2204-gcc11-opt/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_fac61/fast
    /usr/bin/gmake  -f CMakeFiles/cmTC_fac61.dir/build.make CMakeFiles/cmTC_fac61.dir/build
    gmake[1]: Entering directory '/home/krasznaa/development/cmake/build/CMakeFiles/CMakeScratch/TryCompile-ym6qLI'
    Building CUDA object CMakeFiles/cmTC_fac61.dir/main.cu.o
    /home/krasznaa/software/nvidia/cuda-12.6.0/x86_64/bin/nvcc -forward-unknown-to-host-compiler -ccbin=/home/krasznaa/software/intel/oneapi-2024.2.1/compiler/2024.2/bin/icpx   -allow-unsupported-compiler  -std=c++20 "--generate-code=arch=compute_52,code=[compute_52,sm_52]" -MD -MT CMakeFiles/cmTC_fac61.dir/main.cu.o -MF CMakeFiles/cmTC_fac61.dir/main.cu.o.d -x cu -c /home/krasznaa/development/cmake/build/CMakeFiles/CMakeScratch/TryCompile-ym6qLI/main.cu -o CMakeFiles/cmTC_fac61.dir/main.cu.o
    /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/type_traits(3433): error: type name is not allowed
          : bool_constant<__is_layout_compatible(_Tp, _Up)>
                                                 ^

    /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/type_traits(3433): error: type name is not allowed
          : bool_constant<__is_layout_compatible(_Tp, _Up)>
                                                      ^

    /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/type_traits(3433): error: identifier "__is_layout_compatible" is undefined
          : bool_constant<__is_layout_compatible(_Tp, _Up)>
                          ^

    /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/type_traits(3440): error: type name is not allowed
            = __is_layout_compatible(_Tp, _Up);
                                     ^

    /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/type_traits(3440): error: type name is not allowed
            = __is_layout_compatible(_Tp, _Up);
                                          ^

    5 errors detected in the compilation of "/home/krasznaa/development/cmake/build/CMakeFiles/CMakeScratch/TryCompile-ym6qLI/main.cu".
    gmake[1]: *** [CMakeFiles/cmTC_fac61.dir/build.make:79: CMakeFiles/cmTC_fac61.dir/main.cu.o] Error 2
    gmake[1]: Leaving directory '/home/krasznaa/development/cmake/build/CMakeFiles/CMakeScratch/TryCompile-ym6qLI'
    gmake: *** [Makefile:127: cmTC_fac61/fast] Error 2





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


-- Configuring incomplete, errors occurred!

This is because CheckLanguage doesn’t propagate (among other things) the CMAKE_<LANG>_STANDARD variables to the test project that it sets up. Which can result in an error like this.

Now… the setup that I’m describing here is broken. So it’s not the end of the world that CMake doesn’t handle it quite correctly at the moment. But still, I wonder if check_language(...) could potentially be taught to work correctly in such situations as well.

Cheers,
Attila

10 posts - 4 participants