From bbcdac4e5d53de12013c6af6395b28e4816f7823 Mon Sep 17 00:00:00 2001 From: Cristian Adam Date: Sat, 7 Aug 2021 08:07:55 -0700 Subject: PCH: Fix all-language precompile header support in Xcode Fixes: #22384 --- Source/cmGeneratorTarget.cxx | 4 ++-- Source/cmLocalGenerator.cxx | 15 +++++++++++++++ .../PrecompileHeaders/PchIncludedAllLanguages.cmake | 16 ++++++++++++++++ Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake | 1 + Tests/RunCMake/PrecompileHeaders/pch-included.c | 8 ++++++++ Tests/RunCMake/PrecompileHeaders/pch-included.cpp | 8 ++++++++ Tests/RunCMake/PrecompileHeaders/pch.h | 2 ++ 7 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 Tests/RunCMake/PrecompileHeaders/PchIncludedAllLanguages.cmake create mode 100644 Tests/RunCMake/PrecompileHeaders/pch-included.c create mode 100644 Tests/RunCMake/PrecompileHeaders/pch-included.cpp diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 17237bb..bb751d6 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -4100,7 +4100,7 @@ std::string cmGeneratorTarget::GetPchHeader(const std::string& config, if (this->GetGlobalGenerator()->IsXcode()) { file << "#ifndef CMAKE_SKIP_PRECOMPILE_HEADERS\n"; } - if (language == "CXX") { + if (language == "CXX" && !this->GetGlobalGenerator()->IsXcode()) { file << "#ifdef __cplusplus\n"; } for (auto const& header_bt : headers) { @@ -4118,7 +4118,7 @@ std::string cmGeneratorTarget::GetPchHeader(const std::string& config, firstHeaderOnDisk = header_bt.Value; } } - if (language == "CXX") { + if (language == "CXX" && !this->GetGlobalGenerator()->IsXcode()) { file << "#endif // __cplusplus\n"; } if (this->GetGlobalGenerator()->IsXcode()) { diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 028952a..016653b 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -2512,6 +2512,16 @@ void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target) static const std::array langs = { { "C", "CXX", "OBJC", "OBJCXX" } }; + bool haveAnyPch = false; + if (this->GetGlobalGenerator()->IsXcode()) { + for (const std::string& lang : langs) { + const std::string pchHeader = target->GetPchHeader(config, lang, ""); + if (!pchHeader.empty()) { + haveAnyPch = true; + } + } + } + for (const std::string& lang : langs) { auto langSources = std::count_if( sources.begin(), sources.end(), [lang](cmSourceFile* sf) { @@ -2552,6 +2562,11 @@ void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target) const std::string pchHeader = target->GetPchHeader(config, lang, arch); if (pchSource.empty() || pchHeader.empty()) { + if (this->GetGlobalGenerator()->IsXcode() && haveAnyPch) { + for (auto* sf : sources) { + sf->SetProperty("SKIP_PRECOMPILE_HEADERS", "ON"); + } + } continue; } diff --git a/Tests/RunCMake/PrecompileHeaders/PchIncludedAllLanguages.cmake b/Tests/RunCMake/PrecompileHeaders/PchIncludedAllLanguages.cmake new file mode 100644 index 0000000..a455410 --- /dev/null +++ b/Tests/RunCMake/PrecompileHeaders/PchIncludedAllLanguages.cmake @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.15) +project(PchIncludedAllLanguages C CXX) + +if(CMAKE_CXX_COMPILE_OPTIONS_USE_PCH) + add_definitions(-DHAVE_PCH_SUPPORT) +endif() + +add_executable(main + main.cpp + pch-included.c + pch-included.cpp +) +target_precompile_headers(main PRIVATE pch.h) + +enable_testing() +add_test(NAME main COMMAND main) diff --git a/Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake b/Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake index 8cc59d2..e723e03 100644 --- a/Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake +++ b/Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake @@ -28,3 +28,4 @@ if(RunCMake_GENERATOR MATCHES "Make|Ninja") endif() endif() run_test(PchReuseFromObjLib) +run_test(PchIncludedAllLanguages) diff --git a/Tests/RunCMake/PrecompileHeaders/pch-included.c b/Tests/RunCMake/PrecompileHeaders/pch-included.c new file mode 100644 index 0000000..96fce69 --- /dev/null +++ b/Tests/RunCMake/PrecompileHeaders/pch-included.c @@ -0,0 +1,8 @@ +#ifndef HAVE_PCH_SUPPORT +# include "pch.h" +#endif + +int testC(void) +{ + return PCH_INCLUDED; +} diff --git a/Tests/RunCMake/PrecompileHeaders/pch-included.cpp b/Tests/RunCMake/PrecompileHeaders/pch-included.cpp new file mode 100644 index 0000000..bf4d95d --- /dev/null +++ b/Tests/RunCMake/PrecompileHeaders/pch-included.cpp @@ -0,0 +1,8 @@ +#ifndef HAVE_PCH_SUPPORT +# include "pch.h" +#endif + +int testCpp() +{ + return PCH_INCLUDED; +} diff --git a/Tests/RunCMake/PrecompileHeaders/pch.h b/Tests/RunCMake/PrecompileHeaders/pch.h index 81b6d9e..20672d1 100644 --- a/Tests/RunCMake/PrecompileHeaders/pch.h +++ b/Tests/RunCMake/PrecompileHeaders/pch.h @@ -1,3 +1,5 @@ #pragma once #define PCH_INCLUDED 1 + +int testC(void); -- cgit v0.12