mirror-llvm-project/cmake/Modules/CMakePolicy.cmake
Matt Arsenault 885ef9daac
[runtimes] Don't create shared library targets when unsupported (#205517)
On platforms that don't support shared libraries (e.g. CMAKE_SYSTEM_NAME
of "Generic", used for GPU and other baremetal targets), CMake's
Platform/Generic.cmake sets the global TARGET_SUPPORTS_SHARED_LIBS
property to FALSE. Under CMP0164's OLD behavior (the default, since the runtimes 
set cmake_minimum_required(3.20)), CMake silently demotes SHARED library
targets to STATIC archives. libcxx, libcxxabi and libunwind always create their
shared target, so after demotion both the shared and static targets emit e.g.
"libc++abi.a" and Ninja fails with "multiple rules generate ...".

Rather than papering over the collision with a distinct output name,
skip creating the shared library targets entirely when the platform does not
support them, gating on the TARGET_SUPPORTS_SHARED_LIBS property (left 
undefined on platforms that do support shared libraries). The few consumers of 
the shared targets are guarded with TARGET checks so they fall back to the static
library or are skipped.

Also set policy CMP0164 to NEW so that any future unguarded
add_library(... SHARED ...) on an unsupported platform fails at
configure time instead of silently producing a colliding static archive.

See https://gitlab.kitware.com/cmake/cmake/-/issues/25759.

The bodies of the newly-conditional blocks are left at their original
indentation to keep this diff minimal. A follow-up commit will fix the
indentation. This was mostly AI generated with some comment fixups.

Co-authored-by: Claude (Opus 4.8) <noreply@anthropic.com>
2026-06-29 17:53:09 +02:00

55 lines
2.1 KiB
CMake

# CMake policy settings shared between LLVM projects
# MSVC debug information format flags are selected via
# CMAKE_MSVC_DEBUG_INFORMATION_FORMAT, instead of
# embedding flags in e.g. CMAKE_CXX_FLAGS_RELEASE.
# New in CMake 3.25.
#
# Supports debug info with SCCache
# (https://github.com/mozilla/sccache?tab=readme-ov-file#usage)
# avoiding “fatal error C1041: cannot open program database; if
# multiple CL.EXE write to the same .PDB file, please use /FS"
if(POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
endif()
# CMP0144: find_package() uses uppercase <PackageName>_ROOT variables.
# New in CMake 3.27: https://cmake.org/cmake/help/latest/policy/CMP0144.html
if(POLICY CMP0144)
cmake_policy(SET CMP0144 NEW)
endif()
# CMP0147: Visual Studio Generators build custom commands in parallel.
# New in CMake 3.27: https://cmake.org/cmake/help/latest/policy/CMP0147.html
if(POLICY CMP0147)
cmake_policy(SET CMP0147 NEW)
endif()
# CMP0156: De-duplicate libraries on link lines based on linker capabilities.
# New in CMake 3.29: https://cmake.org/cmake/help/latest/policy/CMP0156.html
# Avoids the deluge of 'ld: warning: ignoring duplicate libraries' warnings when
# building with the Apple linker.
if(POLICY CMP0156)
cmake_policy(SET CMP0156 NEW)
# CMP0179: De-duplication of static libraries on link lines keeps first occurrence.
# Dependent on CMP0156=NEW. Unifies the behaviour across platforms.
# Works around a LLD bug ELF backend bug (#116669) and required for CMP0156
# to have an effect for affected versions. Also fixes building with CMake 3.31.0,
# which lacked the workaround of ignoring CMP0156 unless this is enabled.
if(POLICY CMP0179)
cmake_policy(SET CMP0179 NEW)
endif()
endif()
# CMP0182: Create shared library archives by default on AIX.
# New in CMake 4.0: https://cmake.org/cmake/help/latest/policy/CMP0182.html
if(POLICY CMP0182)
cmake_policy(SET CMP0182 NEW)
endif()
# CMP0164: add_library(... SHARED ...) fails on platforms that do not support
# shared libraries, instead of silently building a static library.
if(POLICY CMP0164)
cmake_policy(SET CMP0164 NEW)
endif()