Files
CMake/Modules/Compiler/Clang.cmake
Sergey Larin 5c6d6ec27c PCH: Clang: Update PCH usage flags to include original header
Add an additional include flag to PCH usage command line to fix programs
that rely on `compile_commands.json` file. Pass it to the preprocessor
directly to avoid compiler driver to change it to '-include-pch'.

When preprocessor is requested to preprocess a file, it tries to get
the original filename from '.pch' and uses that file for preprocessing.
CMake generates a '.pch' file from the '.hxx' file by passing an empty
'.cxx' source file to the compiler as a compilation unit and the header
file with the '-include' flag. After that, compiler puts compilation
unit filename in the '.pch' as the original filename.

However, CMake build system uses empty file as the source file and
passes the header file using '-include-pch' flag. As a result, Clang
uses the wrong file for preprocessing and produces the corrupted
preprocessed file.

Fixes: #20355
Signed-off-by: Sergey Larin <cerg2010cerg2010@mail.ru>
2020-02-24 10:53:39 -05:00

108 lines
3.7 KiB
CMake

# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
# This module is shared by multiple languages; use include blocker.
if(__COMPILER_CLANG)
return()
endif()
set(__COMPILER_CLANG 1)
include(Compiler/CMakeCommonCompilerMacros)
if("x${CMAKE_C_SIMULATE_ID}" STREQUAL "xMSVC"
OR "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC"
OR "x${CMAKE_Fortran_SIMULATE_ID}" STREQUAL "xMSVC")
macro(__compiler_clang lang)
endmacro()
else()
include(Compiler/GNU)
macro(__compiler_clang lang)
__compiler_gnu(${lang})
set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-fPIE")
# Link options for PIE are already set in 'Compiler/GNU.cmake'
# but clang may require alternate syntax on some platforms
if (APPLE)
set(CMAKE_${lang}_LINK_OPTIONS_PIE ${CMAKE_${lang}_COMPILE_OPTIONS_PIE} -Xlinker -pie)
set(CMAKE_${lang}_LINK_OPTIONS_NO_PIE -Xlinker -no_pie)
endif()
set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang} "-isystem ")
set(CMAKE_${lang}_COMPILE_OPTIONS_VISIBILITY "-fvisibility=")
if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.4.0)
set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "-target ")
set(CMAKE_${lang}_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN "-gcc-toolchain ")
else()
set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "--target=")
set(CMAKE_${lang}_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN "--gcc-toolchain=")
endif()
set(CMAKE_${lang}_LINKER_WRAPPER_FLAG "-Xlinker" " ")
set(CMAKE_${lang}_LINKER_WRAPPER_FLAG_SEP)
if(CMAKE_${lang}_COMPILER_TARGET)
if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.4.0)
list(APPEND CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND "-target" "${CMAKE_${lang}_COMPILER_TARGET}")
else()
list(APPEND CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND "--target=${CMAKE_${lang}_COMPILER_TARGET}")
endif()
endif()
set(_CMAKE_${lang}_IPO_SUPPORTED_BY_CMAKE YES)
set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES)
string(COMPARE EQUAL "${CMAKE_${lang}_COMPILER_ID}" "AppleClang" __is_apple_clang)
# '-flto=thin' available since Clang 3.9 and Xcode 8
# * http://clang.llvm.org/docs/ThinLTO.html#clang-llvm
# * https://trac.macports.org/wiki/XcodeVersionInfo
set(_CMAKE_LTO_THIN TRUE)
if(__is_apple_clang)
if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 8.0)
set(_CMAKE_LTO_THIN FALSE)
endif()
else()
if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.9)
set(_CMAKE_LTO_THIN FALSE)
endif()
endif()
if(_CMAKE_LTO_THIN)
set(CMAKE_${lang}_COMPILE_OPTIONS_IPO "-flto=thin")
else()
set(CMAKE_${lang}_COMPILE_OPTIONS_IPO "-flto")
endif()
if(ANDROID)
# https://github.com/android-ndk/ndk/issues/242
set(CMAKE_${lang}_LINK_OPTIONS_IPO "-fuse-ld=gold")
endif()
if(ANDROID OR __is_apple_clang)
set(__ar "${CMAKE_AR}")
set(__ranlib "${CMAKE_RANLIB}")
else()
set(__ar "${CMAKE_${lang}_COMPILER_AR}")
set(__ranlib "${CMAKE_${lang}_COMPILER_RANLIB}")
endif()
set(CMAKE_${lang}_ARCHIVE_CREATE_IPO
"\"${__ar}\" cr <TARGET> <LINK_FLAGS> <OBJECTS>"
)
set(CMAKE_${lang}_ARCHIVE_APPEND_IPO
"\"${__ar}\" r <TARGET> <LINK_FLAGS> <OBJECTS>"
)
set(CMAKE_${lang}_ARCHIVE_FINISH_IPO
"\"${__ranlib}\" <TARGET>"
)
set(CMAKE_PCH_EXTENSION .pch)
if (NOT CMAKE_GENERATOR MATCHES "Xcode")
set(CMAKE_PCH_PROLOGUE "#pragma clang system_header")
endif()
set(CMAKE_${lang}_COMPILE_OPTIONS_USE_PCH -Xclang -include-pch -Xclang <PCH_FILE> -Xclang -include -Xclang <PCH_HEADER>)
set(CMAKE_${lang}_COMPILE_OPTIONS_CREATE_PCH -Xclang -emit-pch -Xclang -include -Xclang <PCH_HEADER>)
endmacro()
endif()