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