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:
Brad King 2020-12-09 10:28:03 -05:00
parent 5f882f6ce5
commit b7f0327dcd
14 changed files with 120 additions and 0 deletions

View File

@ -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.

View File

@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.19)
project(${RunCMake_TEST} NONE)
include(${RunCMake_TEST}.cmake)

View 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} "")

View 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)

View File

@ -0,0 +1 @@
include(arm64-common.cmake)

View File

@ -0,0 +1 @@
include(arm64-common.cmake)

View 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)
{
}

View 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)
{
}

View 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})

View 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)

View File

@ -0,0 +1 @@
include(x86_64-common.cmake)

View File

@ -0,0 +1 @@
include(x86_64-common.cmake)

View 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)
{
}

View File

@ -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)