Tests: Cover macOS host architecture selection on Apple Silicon hosts
Add test cases verifying that `CMAKE_APPLE_SILICON_PROCESSOR` set as either a cache or environment variable causes that to be selected as the host architecture. Also verify that sources compile using whatever is selected as the host architecture, even when the explicit setting is not used. Issue: #21554
This commit is contained in:
parent
5f882f6ce5
commit
b7f0327dcd
@ -228,6 +228,22 @@ if(BUILD_TESTING)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND NOT DEFINED CMake_TEST_APPLE_SILICON)
|
||||
execute_process(COMMAND sysctl -q hw.optional.arm64
|
||||
OUTPUT_VARIABLE _sysctl_stdout
|
||||
ERROR_VARIABLE _sysctl_stderr
|
||||
RESULT_VARIABLE _sysctl_result
|
||||
)
|
||||
if(_sysctl_result EQUAL 0 AND _sysctl_stdout MATCHES "hw.optional.arm64: 1")
|
||||
set(CMake_TEST_APPLE_SILICON 1)
|
||||
else()
|
||||
set(CMake_TEST_APPLE_SILICON 0)
|
||||
endif()
|
||||
unset(_sysctl_result)
|
||||
unset(_sysctl_stderr)
|
||||
unset(_sysctl_stdout)
|
||||
endif()
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# Add tests below here.
|
||||
|
||||
|
3
Tests/RunCMake/AppleSilicon/CMakeLists.txt
Normal file
3
Tests/RunCMake/AppleSilicon/CMakeLists.txt
Normal file
@ -0,0 +1,3 @@
|
||||
cmake_minimum_required(VERSION 3.19)
|
||||
project(${RunCMake_TEST} NONE)
|
||||
include(${RunCMake_TEST}.cmake)
|
27
Tests/RunCMake/AppleSilicon/RunCMakeTest.cmake
Normal file
27
Tests/RunCMake/AppleSilicon/RunCMakeTest.cmake
Normal file
@ -0,0 +1,27 @@
|
||||
include(RunCMake)
|
||||
|
||||
# Isolate from caller's environment.
|
||||
set(ENV{CMAKE_APPLE_SILICON_PROCESSOR} "")
|
||||
set(ENV{CMAKE_OSX_ARCHITECTURES} "")
|
||||
|
||||
function(run_arch case)
|
||||
set(RunCMake_TEST_OPTIONS ${ARGN})
|
||||
set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/${case}-build")
|
||||
run_cmake(${case})
|
||||
unset(RunCMake_TEST_OPTIONS)
|
||||
set(RunCMake_TEST_NO_CLEAN 1)
|
||||
run_cmake_command(${case}-build ${CMAKE_COMMAND} --build . --config Debug)
|
||||
endfunction()
|
||||
|
||||
run_arch(default)
|
||||
|
||||
run_arch(arm64-var -DCMAKE_APPLE_SILICON_PROCESSOR=arm64)
|
||||
run_arch(x86_64-var -DCMAKE_APPLE_SILICON_PROCESSOR=x86_64)
|
||||
|
||||
set(ENV{CMAKE_APPLE_SILICON_PROCESSOR} "arm64")
|
||||
run_arch(arm64-env)
|
||||
|
||||
set(ENV{CMAKE_APPLE_SILICON_PROCESSOR} "x86_64")
|
||||
run_arch(x86_64-env)
|
||||
|
||||
set(ENV{CMAKE_APPLE_SILICON_PROCESSOR} "")
|
10
Tests/RunCMake/AppleSilicon/arm64-common.cmake
Normal file
10
Tests/RunCMake/AppleSilicon/arm64-common.cmake
Normal file
@ -0,0 +1,10 @@
|
||||
enable_language(C)
|
||||
|
||||
if(NOT CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64")
|
||||
message(FATAL_ERROR "CMAKE_HOST_SYSTEM_PROCESSOR is '${CMAKE_HOST_SYSTEM_PROCESSOR}', not 'arm64'")
|
||||
endif()
|
||||
if(NOT CMAKE_OSX_ARCHITECTURES STREQUAL "")
|
||||
message(FATAL_ERROR "CMAKE_OSX_ARCHITECTURES is '${CMAKE_OSX_ARCHITECTURES}', not empty ''")
|
||||
endif()
|
||||
|
||||
add_library(arm64 arm64.c)
|
1
Tests/RunCMake/AppleSilicon/arm64-env.cmake
Normal file
1
Tests/RunCMake/AppleSilicon/arm64-env.cmake
Normal file
@ -0,0 +1 @@
|
||||
include(arm64-common.cmake)
|
1
Tests/RunCMake/AppleSilicon/arm64-var.cmake
Normal file
1
Tests/RunCMake/AppleSilicon/arm64-var.cmake
Normal file
@ -0,0 +1 @@
|
||||
include(arm64-common.cmake)
|
9
Tests/RunCMake/AppleSilicon/arm64.c
Normal file
9
Tests/RunCMake/AppleSilicon/arm64.c
Normal file
@ -0,0 +1,9 @@
|
||||
#ifndef __aarch64__
|
||||
# error "Not compiling as arm64"
|
||||
#endif
|
||||
#ifdef __x86_64__
|
||||
# error "Incorrectly compiling as x86_64"
|
||||
#endif
|
||||
void arm64_arch(void)
|
||||
{
|
||||
}
|
14
Tests/RunCMake/AppleSilicon/default.c
Normal file
14
Tests/RunCMake/AppleSilicon/default.c
Normal file
@ -0,0 +1,14 @@
|
||||
#if defined(HOST_ARM64)
|
||||
# if !defined(__aarch64__)
|
||||
# error "Not compiling as host arm64"
|
||||
# endif
|
||||
#elif defined(HOST_X86_64)
|
||||
# if !defined(__x86_64__)
|
||||
# error "Not compiling as host x86_64"
|
||||
# endif
|
||||
#else
|
||||
# error "One of HOST_ARM64 or HOST_X86_64 must be defined."
|
||||
#endif
|
||||
void default_arch(void)
|
||||
{
|
||||
}
|
15
Tests/RunCMake/AppleSilicon/default.cmake
Normal file
15
Tests/RunCMake/AppleSilicon/default.cmake
Normal file
@ -0,0 +1,15 @@
|
||||
enable_language(C)
|
||||
|
||||
if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64")
|
||||
set(host_def HOST_ARM64)
|
||||
elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
|
||||
set(host_def HOST_X86_64)
|
||||
else()
|
||||
message(FATAL_ERROR "CMAKE_HOST_SYSTEM_PROCESSOR is '${CMAKE_HOST_SYSTEM_PROCESSOR}', not 'arm64' or 'x86_64'")
|
||||
endif()
|
||||
if(NOT CMAKE_OSX_ARCHITECTURES STREQUAL "")
|
||||
message(FATAL_ERROR "CMAKE_OSX_ARCHITECTURES is '${CMAKE_OSX_ARCHITECTURES}', not empty ''")
|
||||
endif()
|
||||
|
||||
add_library(default default.c)
|
||||
target_compile_definitions(default PRIVATE ${host_def})
|
10
Tests/RunCMake/AppleSilicon/x86_64-common.cmake
Normal file
10
Tests/RunCMake/AppleSilicon/x86_64-common.cmake
Normal file
@ -0,0 +1,10 @@
|
||||
enable_language(C)
|
||||
|
||||
if(NOT CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
|
||||
message(FATAL_ERROR "CMAKE_HOST_SYSTEM_PROCESSOR is '${CMAKE_HOST_SYSTEM_PROCESSOR}', not 'x86_64'")
|
||||
endif()
|
||||
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)
|
1
Tests/RunCMake/AppleSilicon/x86_64-env.cmake
Normal file
1
Tests/RunCMake/AppleSilicon/x86_64-env.cmake
Normal file
@ -0,0 +1 @@
|
||||
include(x86_64-common.cmake)
|
1
Tests/RunCMake/AppleSilicon/x86_64-var.cmake
Normal file
1
Tests/RunCMake/AppleSilicon/x86_64-var.cmake
Normal file
@ -0,0 +1 @@
|
||||
include(x86_64-common.cmake)
|
9
Tests/RunCMake/AppleSilicon/x86_64.c
Normal file
9
Tests/RunCMake/AppleSilicon/x86_64.c
Normal file
@ -0,0 +1,9 @@
|
||||
#ifndef __x86_64__
|
||||
# error "Not compiling as x86_64"
|
||||
#endif
|
||||
#ifdef __aarch64__
|
||||
# error "Incorrectly compiling as arm64"
|
||||
#endif
|
||||
void x86_64_arch(void)
|
||||
{
|
||||
}
|
@ -176,6 +176,9 @@ if(NOT CMake_TEST_EXTERNAL_CMAKE)
|
||||
endif()
|
||||
|
||||
add_RunCMake_test(AndroidTestUtilities)
|
||||
if(CMake_TEST_APPLE_SILICON)
|
||||
add_RunCMake_test(AppleSilicon)
|
||||
endif()
|
||||
set(autogen_with_qt5 FALSE)
|
||||
if(CMake_TEST_Qt5)
|
||||
find_package(Qt5Widgets QUIET NO_MODULE)
|
||||
|
Loading…
Reference in New Issue
Block a user