CheckIPOSupported not regarded CMAKE_LINKER_TYPE

At the present time, GCC LTO cannot link with LLVM LLD, but CheckIPOSupported may give different answer depending on how cmake command line invoked.

For

CC=gcc CXX=g++ LDFLAGS=-fuse-ld=lld cmake

CheckIPOSupported returns failure, then the project can safely set IPO to OFF.

For

cmake -D CMAKE_C_COMPILER=gcc -D CMAKE_CXX_COMPILER=g++ -D CMAKE_LINKER_TYPE=LLD 

CheckIPOSupported returns success, then the project may set IPO to ON, then get link-time errors.

I failed to reproduce your problem. Can you provide a snippet reproducing the problem?
By the way, can you specify your environment (platform, compiler type and version, CMake version)?

Environment

Arch Linux
cmake version 3.31.1
ninja 1.12.1
gcc (GCC) 14.2.1 20240910
LLD 18.1.8 (compatible with GNU linkers)

Project to Reproduce

CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(test_proj)
include(CheckIPOSupported)
check_ipo_supported(RESULT C_COMPILER_HAS_IPO_SUPPORT LANGUAGES C)
message(STATUS "LTO: ${C_COMPILER_HAS_IPO_SUPPORT}")
add_executable(main main.c)
set_property(TARGET main PROPERTY INTERPROCEDURAL_OPTIMIZATION ${C_COMPILER_HAS_IPO_SUPPORT})

main.c:

#include <stdio.h>
int main() {
  printf("Hello world!\n");
  return 0;
}

Command Line Outputs

$ cmake -B build -G Ninja -D CMAKE_C_COMPILER=gcc -D CMAKE_CXX_COMPILER=g++ -D CMAKE_LINKER_TYPE=LLD        
-- The C compiler identification is GNU 14.2.1
-- The CXX compiler identification is GNU 14.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- LTO: YES
-- Configuring done (1.0s)
-- Generating done (0.0s)
-- Build files have been written to: /tmp/tmp.J0m6gKPUMX/build
$ ninja -C build -v                                                                                 
ninja: Entering directory `build'
[1/2] /usr/bin/gcc   -flto=auto -fno-fat-lto-objects -MD -MT CMakeFiles/main.dir/main.c.o -MF CMakeFiles/main.dir/main.c.o.d -o CMakeFiles/main.dir/main.c.o -c /tmp/tmp.J0m6gKPUMX/main.c
[2/2] : && /usr/bin/gcc -flto=auto -fno-fat-lto-objects -fuse-ld=lld -Wl,--dependency-file,CMakeFiles/main.dir/link.d CMakeFiles/main.dir/main.c.o -o main   && :
FAILED: main 
: && /usr/bin/gcc -flto=auto -fno-fat-lto-objects -fuse-ld=lld -Wl,--dependency-file,CMakeFiles/main.dir/link.d CMakeFiles/main.dir/main.c.o -o main   && :
ld.lld: error: undefined symbol: main
>>> referenced by /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../lib/Scrt1.o:(_start)
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

CMakeConfigureLog.yaml


---
-
    (Redacted)
-
    kind: "try_compile-v1"
    backtrace:
      - "/usr/share/cmake/Modules/CheckIPOSupported.cmake:138 (try_compile)"
      - "/usr/share/cmake/Modules/CheckIPOSupported.cmake:261 (_ipo_run_language_check)"
      - "CMakeLists.txt:4 (check_ipo_supported)"
    directories:
      source: "/tmp/tmp.J0m6gKPUMX/build/CMakeFiles/_CMakeLTOTest-C/src"
      binary: "/tmp/tmp.J0m6gKPUMX/build/CMakeFiles/_CMakeLTOTest-C/bin"
    buildResult:
      variable: "_IPO_LANGUAGE_CHECK_RESULT"
      cached: true
      stdout: |
        Change Dir: '/tmp/tmp.J0m6gKPUMX/build/CMakeFiles/_CMakeLTOTest-C/bin'
        
        Run Build Command(s): /usr/bin/ninja -v
        [1/4] /usr/bin/gcc   -flto=auto -fno-fat-lto-objects -o CMakeFiles/boo.dir/main.c.o -c /tmp/tmp.J0m6gKPUMX/build/CMakeFiles/_CMakeLTOTest-C/src/main.c
        [2/4] /usr/bin/gcc   -flto=auto -fno-fat-lto-objects -o CMakeFiles/foo.dir/foo.c.o -c /tmp/tmp.J0m6gKPUMX/build/CMakeFiles/_CMakeLTOTest-C/src/foo.c
        [3/4] : && /usr/bin/cmake -E rm -f libfoo.a && "/usr/bin/gcc-ar" qc libfoo.a  CMakeFiles/foo.dir/foo.c.o && "/usr/bin/gcc-ranlib" libfoo.a && :
        [4/4] : && /usr/bin/gcc -flto=auto -fno-fat-lto-objects  CMakeFiles/boo.dir/main.c.o -o boo  libfoo.a && :
        
      exitCode: 0
...

The problem comes from your CMake version specified through cmake_minumum_required() command. The policy CMP0138 is not defined, so flags are propagated to the IPO check.

Update cmake_minumum_required() to version to 3.24 or set explicitly CMP0138 to NEW with cmake_policy() command.