From 93afe804ce4949823dc4521d555811720afd7783 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 14 Mar 2023 17:01:45 -0400 Subject: cmGeneratorTarget: Convert GetAppleArchs output argument to return value --- Source/cmFileAPICodemodel.cxx | 4 ++-- Source/cmGeneratorTarget.cxx | 8 +++++--- Source/cmGeneratorTarget.h | 3 +-- Source/cmGlobalXCodeGenerator.cxx | 3 +-- Source/cmLocalGenerator.cxx | 5 ++--- Source/cmMakefileTargetGenerator.cxx | 8 ++++---- Source/cmNinjaTargetGenerator.cxx | 8 ++++---- 7 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Source/cmFileAPICodemodel.cxx b/Source/cmFileAPICodemodel.cxx index d3683d4..6d77f85 100644 --- a/Source/cmFileAPICodemodel.cxx +++ b/Source/cmFileAPICodemodel.cxx @@ -1355,8 +1355,8 @@ CompileData Target::BuildCompileData(cmSourceFile* sf) } // Add precompile headers compile options. - std::vector architectures; - this->GT->GetAppleArchs(this->Config, architectures); + std::vector architectures = + this->GT->GetAppleArchs(this->Config); if (architectures.empty()) { architectures.emplace_back(); } diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 4cfa1d7..541c2a6 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -3448,11 +3448,12 @@ std::string cmGeneratorTarget::GetCompilePDBDirectory( return ""; } -void cmGeneratorTarget::GetAppleArchs(const std::string& config, - std::vector& archVec) const +std::vector cmGeneratorTarget::GetAppleArchs( + std::string const& config) const { + std::vector archVec; if (!this->IsApple()) { - return; + return archVec; } cmValue archs = nullptr; if (!config.empty()) { @@ -3469,6 +3470,7 @@ void cmGeneratorTarget::GetAppleArchs(const std::string& config, if (archVec.empty()) { this->Makefile->GetDefExpandList("_CMAKE_APPLE_ARCHS_DEFAULT", archVec); } + return archVec; } void cmGeneratorTarget::AddExplicitLanguageFlags(std::string& flags, diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h index 5d26191..7c1ff72 100644 --- a/Source/cmGeneratorTarget.h +++ b/Source/cmGeneratorTarget.h @@ -484,8 +484,7 @@ public: holding object files for the given configuration. */ std::string GetObjectDirectory(std::string const& config) const; - void GetAppleArchs(const std::string& config, - std::vector& archVec) const; + std::vector GetAppleArchs(std::string const& config) const; void AddExplicitLanguageFlags(std::string& flags, cmSourceFile const& sf) const; diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index 5b3ac60..6fca5f4 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -2513,8 +2513,7 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmGeneratorTarget* gtgt, } // Set target-specific architectures. - std::vector archs; - gtgt->GetAppleArchs(configName, archs); + std::vector archs = gtgt->GetAppleArchs(configName); if (!archs.empty()) { // Enable ARCHS attribute. diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index bd7eb3f..6fc4597 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -1873,8 +1873,7 @@ void cmLocalGenerator::AddArchitectureFlags(std::string& flags, { // Only add Apple specific flags on Apple platforms if (target->IsApple() && this->EmitUniversalBinaryFlags) { - std::vector archs; - target->GetAppleArchs(config, archs); + std::vector archs = target->GetAppleArchs(config); if (!archs.empty() && (lang == "C" || lang == "CXX" || lang == "OBJC" || lang == "OBJCXX" || lang == "ASM")) { @@ -2594,7 +2593,7 @@ void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target) std::vector architectures; if (!this->GetGlobalGenerator()->IsXcode()) { - target->GetAppleArchs(config, architectures); + architectures = target->GetAppleArchs(config); } if (architectures.empty()) { architectures.emplace_back(); diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index 2ead739..a7e5eca 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -541,8 +541,8 @@ void cmMakefileTargetGenerator::WriteTargetLanguageFlags() *this->FlagFileStream << language << "_DEFINES = " << defines << "\n\n"; *this->FlagFileStream << language << "_INCLUDES = " << includes << "\n\n"; - std::vector architectures; - this->GeneratorTarget->GetAppleArchs(this->GetConfigName(), architectures); + std::vector architectures = + this->GeneratorTarget->GetAppleArchs(this->GetConfigName()); architectures.emplace_back(); for (const std::string& arch : architectures) { @@ -671,8 +671,8 @@ void cmMakefileTargetGenerator::WriteObjectRuleFiles( std::string configUpper = cmSystemTools::UpperCase(config); // Add precompile headers dependencies - std::vector architectures; - this->GeneratorTarget->GetAppleArchs(config, architectures); + std::vector architectures = + this->GeneratorTarget->GetAppleArchs(config); if (architectures.empty()) { architectures.emplace_back(); } diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx index dc56142..ce5e501 100644 --- a/Source/cmNinjaTargetGenerator.cxx +++ b/Source/cmNinjaTargetGenerator.cxx @@ -169,9 +169,9 @@ std::string cmNinjaTargetGenerator::ComputeFlagsForObject( cmSourceFile const* source, const std::string& language, const std::string& config) { - std::vector architectures; std::unordered_map pchSources; - this->GeneratorTarget->GetAppleArchs(config, architectures); + std::vector architectures = + this->GeneratorTarget->GetAppleArchs(config); if (architectures.empty()) { architectures.emplace_back(); } @@ -1390,8 +1390,8 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatement( // Add precompile headers dependencies std::vector depList; - std::vector architectures; - this->GeneratorTarget->GetAppleArchs(config, architectures); + std::vector architectures = + this->GeneratorTarget->GetAppleArchs(config); if (architectures.empty()) { architectures.emplace_back(); } -- cgit v0.12 From 43e973eba2681788c7acf7a4fcdf261bcf6a622a Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 14 Mar 2023 17:11:27 -0400 Subject: cmGeneratorTarget: Pass language to GetAppleArchs when possible --- Source/cmFileAPICodemodel.cxx | 2 +- Source/cmGeneratorTarget.cxx | 3 ++- Source/cmGeneratorTarget.h | 3 ++- Source/cmGlobalXCodeGenerator.cxx | 3 ++- Source/cmLocalGenerator.cxx | 4 ++-- Source/cmMakefileTargetGenerator.cxx | 4 ++-- Source/cmNinjaTargetGenerator.cxx | 4 ++-- 7 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Source/cmFileAPICodemodel.cxx b/Source/cmFileAPICodemodel.cxx index 6d77f85..4a8716f 100644 --- a/Source/cmFileAPICodemodel.cxx +++ b/Source/cmFileAPICodemodel.cxx @@ -1356,7 +1356,7 @@ CompileData Target::BuildCompileData(cmSourceFile* sf) // Add precompile headers compile options. std::vector architectures = - this->GT->GetAppleArchs(this->Config); + this->GT->GetAppleArchs(this->Config, fd.Language); if (architectures.empty()) { architectures.emplace_back(); } diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 541c2a6..0b376c1 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -3449,8 +3449,9 @@ std::string cmGeneratorTarget::GetCompilePDBDirectory( } std::vector cmGeneratorTarget::GetAppleArchs( - std::string const& config) const + std::string const& config, cm::optional lang) const { + static_cast(lang); std::vector archVec; if (!this->IsApple()) { return archVec; diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h index 7c1ff72..256ca3b 100644 --- a/Source/cmGeneratorTarget.h +++ b/Source/cmGeneratorTarget.h @@ -484,7 +484,8 @@ public: holding object files for the given configuration. */ std::string GetObjectDirectory(std::string const& config) const; - std::vector GetAppleArchs(std::string const& config) const; + std::vector GetAppleArchs(std::string const& config, + cm::optional lang) const; void AddExplicitLanguageFlags(std::string& flags, cmSourceFile const& sf) const; diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index 6fca5f4..1396357 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -2513,7 +2513,8 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmGeneratorTarget* gtgt, } // Set target-specific architectures. - std::vector archs = gtgt->GetAppleArchs(configName); + std::vector archs = + gtgt->GetAppleArchs(configName, cm::nullopt); if (!archs.empty()) { // Enable ARCHS attribute. diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 6fc4597..01e4241 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -1873,7 +1873,7 @@ void cmLocalGenerator::AddArchitectureFlags(std::string& flags, { // Only add Apple specific flags on Apple platforms if (target->IsApple() && this->EmitUniversalBinaryFlags) { - std::vector archs = target->GetAppleArchs(config); + std::vector archs = target->GetAppleArchs(config, lang); if (!archs.empty() && (lang == "C" || lang == "CXX" || lang == "OBJC" || lang == "OBJCXX" || lang == "ASM")) { @@ -2593,7 +2593,7 @@ void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target) std::vector architectures; if (!this->GetGlobalGenerator()->IsXcode()) { - architectures = target->GetAppleArchs(config); + architectures = target->GetAppleArchs(config, lang); } if (architectures.empty()) { architectures.emplace_back(); diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index a7e5eca..e6f8cdd 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -542,7 +542,7 @@ void cmMakefileTargetGenerator::WriteTargetLanguageFlags() *this->FlagFileStream << language << "_INCLUDES = " << includes << "\n\n"; std::vector architectures = - this->GeneratorTarget->GetAppleArchs(this->GetConfigName()); + this->GeneratorTarget->GetAppleArchs(this->GetConfigName(), language); architectures.emplace_back(); for (const std::string& arch : architectures) { @@ -672,7 +672,7 @@ void cmMakefileTargetGenerator::WriteObjectRuleFiles( // Add precompile headers dependencies std::vector architectures = - this->GeneratorTarget->GetAppleArchs(config); + this->GeneratorTarget->GetAppleArchs(config, lang); if (architectures.empty()) { architectures.emplace_back(); } diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx index ce5e501..8719364 100644 --- a/Source/cmNinjaTargetGenerator.cxx +++ b/Source/cmNinjaTargetGenerator.cxx @@ -171,7 +171,7 @@ std::string cmNinjaTargetGenerator::ComputeFlagsForObject( { std::unordered_map pchSources; std::vector architectures = - this->GeneratorTarget->GetAppleArchs(config); + this->GeneratorTarget->GetAppleArchs(config, language); if (architectures.empty()) { architectures.emplace_back(); } @@ -1391,7 +1391,7 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatement( std::vector depList; std::vector architectures = - this->GeneratorTarget->GetAppleArchs(config); + this->GeneratorTarget->GetAppleArchs(config, language); if (architectures.empty()) { architectures.emplace_back(); } -- cgit v0.12 From 98e319a88f0be4fdc5d374cdf5a3d5c0e295699d Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 15 Mar 2023 10:20:27 -0400 Subject: macOS: Suppress default '-arch arm64' with CMAKE__COMPILER_TARGET Since commit b6c60f14b6 (macOS: Default to arm64 architecture on Apple Silicon hosts, 2020-09-28, v3.19.0-rc1~63^2) we add `-arch arm64` by default on Apple Silicon hosts if `CMAKE_OSX_ARCHITECTURES` is not set. This is necessary to prevent the toolchain from selecting its own default architecture based on that of the build tool (e.g., `x86_64`). If `CMAKE__COMPILER_TARGET` is set, its `-target` flag tells the compiler what architecture to use, so do not add `-arch arm64`. Fixes: #24599 --- Source/cmGeneratorTarget.cxx | 8 ++++++-- Tests/RunCMake/AppleSilicon/RunCMakeTest.cmake | 8 +++++++- .../RunCMake/AppleSilicon/default-target-arm64-build-check.cmake | 5 +++++ Tests/RunCMake/AppleSilicon/default-target-arm64.cmake | 5 +++++ .../RunCMake/AppleSilicon/default-target-x86_64-build-check.cmake | 5 +++++ Tests/RunCMake/AppleSilicon/default-target-x86_64.cmake | 5 +++++ 6 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 Tests/RunCMake/AppleSilicon/default-target-arm64-build-check.cmake create mode 100644 Tests/RunCMake/AppleSilicon/default-target-arm64.cmake create mode 100644 Tests/RunCMake/AppleSilicon/default-target-x86_64-build-check.cmake create mode 100644 Tests/RunCMake/AppleSilicon/default-target-x86_64.cmake diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 0b376c1..8a14eaf 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -3451,7 +3451,6 @@ std::string cmGeneratorTarget::GetCompilePDBDirectory( std::vector cmGeneratorTarget::GetAppleArchs( std::string const& config, cm::optional lang) const { - static_cast(lang); std::vector archVec; if (!this->IsApple()) { return archVec; @@ -3468,7 +3467,12 @@ std::vector cmGeneratorTarget::GetAppleArchs( if (archs) { cmExpandList(*archs, archVec); } - if (archVec.empty()) { + if (archVec.empty() && + // Fall back to a default architecture if no compiler target is set. + (!lang || + this->Makefile + ->GetDefinition(cmStrCat("CMAKE_", *lang, "_COMPILER_TARGET")) + .IsEmpty())) { this->Makefile->GetDefExpandList("_CMAKE_APPLE_ARCHS_DEFAULT", archVec); } return archVec; diff --git a/Tests/RunCMake/AppleSilicon/RunCMakeTest.cmake b/Tests/RunCMake/AppleSilicon/RunCMakeTest.cmake index 39e462e..58c50e0 100644 --- a/Tests/RunCMake/AppleSilicon/RunCMakeTest.cmake +++ b/Tests/RunCMake/AppleSilicon/RunCMakeTest.cmake @@ -10,11 +10,17 @@ function(run_arch case) run_cmake(${case}) unset(RunCMake_TEST_OPTIONS) set(RunCMake_TEST_NO_CLEAN 1) - run_cmake_command(${case}-build ${CMAKE_COMMAND} --build . --config Debug) + set(RunCMake_TEST_OUTPUT_MERGE 1) + run_cmake_command(${case}-build ${CMAKE_COMMAND} --build . --config Debug --verbose) endfunction() run_arch(default) +if(RunCMake_GENERATOR MATCHES "Makefiles|Ninja") + run_arch(default-target-arm64 -DCMAKE_C_COMPILER_TARGET=arm64-apple-macosx) + run_arch(default-target-x86_64 -DCMAKE_C_COMPILER_TARGET=x86_64-apple-macosx) +endif() + run_arch(arm64-var -DCMAKE_APPLE_SILICON_PROCESSOR=arm64) run_arch(x86_64-var -DCMAKE_APPLE_SILICON_PROCESSOR=x86_64) diff --git a/Tests/RunCMake/AppleSilicon/default-target-arm64-build-check.cmake b/Tests/RunCMake/AppleSilicon/default-target-arm64-build-check.cmake new file mode 100644 index 0000000..33ad931 --- /dev/null +++ b/Tests/RunCMake/AppleSilicon/default-target-arm64-build-check.cmake @@ -0,0 +1,5 @@ +if(NOT actual_stdout MATCHES "[ -]-target=arm64-apple-macosx ") + set(RunCMake_TEST_FAILED "No -target=arm64-apple-macosx flag found!") +elseif(actual_stdout MATCHES " (-arch +[^ ]*)") + set(RunCMake_TEST_FAILED "'${CMAKE_MATCH_1}' flag incorrectly found!") +endif() diff --git a/Tests/RunCMake/AppleSilicon/default-target-arm64.cmake b/Tests/RunCMake/AppleSilicon/default-target-arm64.cmake new file mode 100644 index 0000000..8c94020 --- /dev/null +++ b/Tests/RunCMake/AppleSilicon/default-target-arm64.cmake @@ -0,0 +1,5 @@ +enable_language(C) +if(NOT CMAKE_OSX_ARCHITECTURES STREQUAL "") + message(FATAL_ERROR "CMAKE_OSX_ARCHITECTURES is '${CMAKE_OSX_ARCHITECTURES}', not empty ''") +endif() +add_library(arm64 arm64.c) diff --git a/Tests/RunCMake/AppleSilicon/default-target-x86_64-build-check.cmake b/Tests/RunCMake/AppleSilicon/default-target-x86_64-build-check.cmake new file mode 100644 index 0000000..9116ef4 --- /dev/null +++ b/Tests/RunCMake/AppleSilicon/default-target-x86_64-build-check.cmake @@ -0,0 +1,5 @@ +if(NOT actual_stdout MATCHES "[ -]-target=x86_64-apple-macosx ") + set(RunCMake_TEST_FAILED "No -target=x86_64-apple-macosx flag found!") +elseif(actual_stdout MATCHES " (-arch +[^ ]*)") + set(RunCMake_TEST_FAILED "'${CMAKE_MATCH_1}' flag incorrectly found!") +endif() diff --git a/Tests/RunCMake/AppleSilicon/default-target-x86_64.cmake b/Tests/RunCMake/AppleSilicon/default-target-x86_64.cmake new file mode 100644 index 0000000..ded46b7 --- /dev/null +++ b/Tests/RunCMake/AppleSilicon/default-target-x86_64.cmake @@ -0,0 +1,5 @@ +enable_language(C) +if(NOT CMAKE_OSX_ARCHITECTURES STREQUAL "") + message(FATAL_ERROR "CMAKE_OSX_ARCHITECTURES is '${CMAKE_OSX_ARCHITECTURES}', not empty ''") +endif() +add_library(x86_64 x86_64.c) -- cgit v0.12