PCH: Use per-arch .pch files only when building multiple Apple architectures

Since commit f593b354da (PCH: Add support for multi architecture iOS
projects, 2020-04-02, v3.18.0-rc1~414^2) we use per-arch .pch files
even when compiling for just the host architecture on macOS arm64.
This breaks with compilers that do not support `-Xarch_` flags, such
as GCC.  Avoid using per-arch .pch files in single-architecture builds.

Fixes: #25514
Issue: #20497
This commit is contained in:
Brad King 2024-05-01 12:34:38 -04:00
parent 99bfb430ee
commit ef006ebd9b
6 changed files with 34 additions and 35 deletions

View File

@ -1384,14 +1384,11 @@ CompileData Target::BuildCompileData(cmSourceFile* sf)
} }
// Add precompile headers compile options. // Add precompile headers compile options.
std::vector<std::string> architectures = std::vector<std::string> pchArchs =
this->GT->GetAppleArchs(this->Config, fd.Language); this->GT->GetPchArchs(this->Config, fd.Language);
if (architectures.empty()) {
architectures.emplace_back();
}
std::unordered_map<std::string, std::string> pchSources; std::unordered_map<std::string, std::string> pchSources;
for (const std::string& arch : architectures) { for (const std::string& arch : pchArchs) {
const std::string pchSource = const std::string pchSource =
this->GT->GetPchSource(this->Config, fd.Language, arch); this->GT->GetPchSource(this->Config, fd.Language, arch);
if (!pchSource.empty()) { if (!pchSource.empty()) {

View File

@ -4304,6 +4304,20 @@ std::vector<BT<std::string>> cmGeneratorTarget::GetPrecompileHeaders(
return list; return list;
} }
std::vector<std::string> cmGeneratorTarget::GetPchArchs(
std::string const& config, std::string const& lang) const
{
std::vector<std::string> pchArchs;
if (!this->GetGlobalGenerator()->IsXcode()) {
pchArchs = this->GetAppleArchs(config, lang);
}
if (pchArchs.size() < 2) {
// We do not need per-arch PCH files when building for one architecture.
pchArchs = { {} };
}
return pchArchs;
}
std::string cmGeneratorTarget::GetPchHeader(const std::string& config, std::string cmGeneratorTarget::GetPchHeader(const std::string& config,
const std::string& language, const std::string& language,
const std::string& arch) const const std::string& arch) const

View File

@ -598,6 +598,8 @@ public:
std::vector<BT<std::string>> GetPrecompileHeaders( std::vector<BT<std::string>> GetPrecompileHeaders(
const std::string& config, const std::string& language) const; const std::string& config, const std::string& language) const;
std::vector<std::string> GetPchArchs(std::string const& config,
std::string const& lang) const;
std::string GetPchHeader(const std::string& config, std::string GetPchHeader(const std::string& config,
const std::string& language, const std::string& language,
const std::string& arch = std::string()) const; const std::string& arch = std::string()) const;

View File

@ -2721,15 +2721,10 @@ void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target)
continue; continue;
} }
std::vector<std::string> architectures; std::vector<std::string> pchArchs = target->GetPchArchs(config, lang);
if (!this->GetGlobalGenerator()->IsXcode()) { if (pchArchs.size() > 1) {
architectures = target->GetAppleArchs(config, lang);
}
if (architectures.empty()) {
architectures.emplace_back();
} else {
std::string useMultiArchPch; std::string useMultiArchPch;
for (const std::string& arch : architectures) { for (const std::string& arch : pchArchs) {
const std::string pchHeader = const std::string pchHeader =
target->GetPchHeader(config, lang, arch); target->GetPchHeader(config, lang, arch);
if (!pchHeader.empty()) { if (!pchHeader.empty()) {
@ -2746,7 +2741,7 @@ void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target)
} }
} }
for (const std::string& arch : architectures) { for (const std::string& arch : pchArchs) {
const std::string pchSource = target->GetPchSource(config, lang, arch); const std::string pchSource = target->GetPchSource(config, lang, arch);
const std::string pchHeader = target->GetPchHeader(config, lang, arch); const std::string pchHeader = target->GetPchHeader(config, lang, arch);

View File

@ -672,15 +672,12 @@ void cmMakefileTargetGenerator::WriteObjectRuleFiles(
std::string const configUpper = cmSystemTools::UpperCase(config); std::string const configUpper = cmSystemTools::UpperCase(config);
// Add precompile headers dependencies // Add precompile headers dependencies
std::vector<std::string> architectures = std::vector<std::string> pchArchs =
this->GeneratorTarget->GetAppleArchs(config, lang); this->GeneratorTarget->GetPchArchs(config, lang);
if (architectures.empty()) {
architectures.emplace_back();
}
std::string filterArch; std::string filterArch;
std::unordered_map<std::string, std::string> pchSources; std::unordered_map<std::string, std::string> pchSources;
for (const std::string& arch : architectures) { for (const std::string& arch : pchArchs) {
const std::string pchSource = const std::string pchSource =
this->GeneratorTarget->GetPchSource(config, lang, arch); this->GeneratorTarget->GetPchSource(config, lang, arch);
if (pchSource == source.GetFullPath()) { if (pchSource == source.GetFullPath()) {
@ -692,7 +689,7 @@ void cmMakefileTargetGenerator::WriteObjectRuleFiles(
} }
if (!pchSources.empty() && !source.GetProperty("SKIP_PRECOMPILE_HEADERS")) { if (!pchSources.empty() && !source.GetProperty("SKIP_PRECOMPILE_HEADERS")) {
for (const std::string& arch : architectures) { for (const std::string& arch : pchArchs) {
std::string const& pchHeader = std::string const& pchHeader =
this->GeneratorTarget->GetPchHeader(config, lang, arch); this->GeneratorTarget->GetPchHeader(config, lang, arch);
depends.push_back(pchHeader); depends.push_back(pchHeader);

View File

@ -189,14 +189,11 @@ std::string cmNinjaTargetGenerator::ComputeFlagsForObject(
const std::string& config, const std::string& objectFileName) const std::string& config, const std::string& objectFileName)
{ {
std::unordered_map<std::string, std::string> pchSources; std::unordered_map<std::string, std::string> pchSources;
std::vector<std::string> architectures = std::vector<std::string> pchArchs =
this->GeneratorTarget->GetAppleArchs(config, language); this->GeneratorTarget->GetPchArchs(config, language);
if (architectures.empty()) {
architectures.emplace_back();
}
std::string filterArch; std::string filterArch;
for (const std::string& arch : architectures) { for (const std::string& arch : pchArchs) {
const std::string pchSource = const std::string pchSource =
this->GeneratorTarget->GetPchSource(config, language, arch); this->GeneratorTarget->GetPchSource(config, language, arch);
if (pchSource == source->GetFullPath()) { if (pchSource == source->GetFullPath()) {
@ -1500,14 +1497,11 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatement(
// Add precompile headers dependencies // Add precompile headers dependencies
std::vector<std::string> depList; std::vector<std::string> depList;
std::vector<std::string> architectures = std::vector<std::string> pchArchs =
this->GeneratorTarget->GetAppleArchs(config, language); this->GeneratorTarget->GetPchArchs(config, language);
if (architectures.empty()) {
architectures.emplace_back();
}
std::unordered_set<std::string> pchSources; std::unordered_set<std::string> pchSources;
for (const std::string& arch : architectures) { for (const std::string& arch : pchArchs) {
const std::string pchSource = const std::string pchSource =
this->GeneratorTarget->GetPchSource(config, language, arch); this->GeneratorTarget->GetPchSource(config, language, arch);
@ -1517,7 +1511,7 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatement(
} }
if (!pchSources.empty() && !source->GetProperty("SKIP_PRECOMPILE_HEADERS")) { if (!pchSources.empty() && !source->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
for (const std::string& arch : architectures) { for (const std::string& arch : pchArchs) {
depList.push_back( depList.push_back(
this->GeneratorTarget->GetPchHeader(config, language, arch)); this->GeneratorTarget->GetPchHeader(config, language, arch));
if (pchSources.find(source->GetFullPath()) == pchSources.end()) { if (pchSources.find(source->GetFullPath()) == pchSources.end()) {