summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2020-12-09 15:28:03 (GMT)
committerBrad King <brad.king@kitware.com>2020-12-10 17:08:15 (GMT)
commitb7f0327dcd4032c6a118825adcfdc8dab6295e7f (patch)
tree11acc0447f4c3e16e321f74de9a8fc2006f16dcd
parent5f882f6ce518a9f90600ae5c73f633ca5c15a7e5 (diff)
downloadCMake-b7f0327dcd4032c6a118825adcfdc8dab6295e7f.zip
CMake-b7f0327dcd4032c6a118825adcfdc8dab6295e7f.tar.gz
CMake-b7f0327dcd4032c6a118825adcfdc8dab6295e7f.tar.bz2
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
-rw-r--r--Tests/CMakeLists.txt16
-rw-r--r--Tests/RunCMake/AppleSilicon/CMakeLists.txt3
-rw-r--r--Tests/RunCMake/AppleSilicon/RunCMakeTest.cmake27
-rw-r--r--Tests/RunCMake/AppleSilicon/arm64-common.cmake10
-rw-r--r--Tests/RunCMake/AppleSilicon/arm64-env.cmake1
-rw-r--r--Tests/RunCMake/AppleSilicon/arm64-var.cmake1
-rw-r--r--Tests/RunCMake/AppleSilicon/arm64.c9
-rw-r--r--Tests/RunCMake/AppleSilicon/default.c14
-rw-r--r--Tests/RunCMake/AppleSilicon/default.cmake15
-rw-r--r--Tests/RunCMake/AppleSilicon/x86_64-common.cmake10
-rw-r--r--Tests/RunCMake/AppleSilicon/x86_64-env.cmake1
-rw-r--r--Tests/RunCMake/AppleSilicon/x86_64-var.cmake1
-rw-r--r--Tests/RunCMake/AppleSilicon/x86_64.c9
-rw-r--r--Tests/RunCMake/CMakeLists.txt3
14 files changed, 120 insertions, 0 deletions
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt
index 02759d1..1fb47cb 100644
--- a/Tests/CMakeLists.txt
+++ b/Tests/CMakeLists.txt
@@ -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.
diff --git a/Tests/RunCMake/AppleSilicon/CMakeLists.txt b/Tests/RunCMake/AppleSilicon/CMakeLists.txt
new file mode 100644
index 0000000..ab1a20c
--- /dev/null
+++ b/Tests/RunCMake/AppleSilicon/CMakeLists.txt
@@ -0,0 +1,3 @@
+cmake_minimum_required(VERSION 3.19)
+project(${RunCMake_TEST} NONE)
+include(${RunCMake_TEST}.cmake)
diff --git a/Tests/RunCMake/AppleSilicon/RunCMakeTest.cmake b/Tests/RunCMake/AppleSilicon/RunCMakeTest.cmake
new file mode 100644
index 0000000..39e462e
--- /dev/null
+++ b/Tests/RunCMake/AppleSilicon/RunCMakeTest.cmake
@@ -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} "")
diff --git a/Tests/RunCMake/AppleSilicon/arm64-common.cmake b/Tests/RunCMake/AppleSilicon/arm64-common.cmake
new file mode 100644
index 0000000..5a13728
--- /dev/null
+++ b/Tests/RunCMake/AppleSilicon/arm64-common.cmake
@@ -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)
diff --git a/Tests/RunCMake/AppleSilicon/arm64-env.cmake b/Tests/RunCMake/AppleSilicon/arm64-env.cmake
new file mode 100644
index 0000000..8cb9dbc
--- /dev/null
+++ b/Tests/RunCMake/AppleSilicon/arm64-env.cmake
@@ -0,0 +1 @@
+include(arm64-common.cmake)
diff --git a/Tests/RunCMake/AppleSilicon/arm64-var.cmake b/Tests/RunCMake/AppleSilicon/arm64-var.cmake
new file mode 100644
index 0000000..8cb9dbc
--- /dev/null
+++ b/Tests/RunCMake/AppleSilicon/arm64-var.cmake
@@ -0,0 +1 @@
+include(arm64-common.cmake)
diff --git a/Tests/RunCMake/AppleSilicon/arm64.c b/Tests/RunCMake/AppleSilicon/arm64.c
new file mode 100644
index 0000000..d5f65fe
--- /dev/null
+++ b/Tests/RunCMake/AppleSilicon/arm64.c
@@ -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)
+{
+}
diff --git a/Tests/RunCMake/AppleSilicon/default.c b/Tests/RunCMake/AppleSilicon/default.c
new file mode 100644
index 0000000..7b0535b
--- /dev/null
+++ b/Tests/RunCMake/AppleSilicon/default.c
@@ -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)
+{
+}
diff --git a/Tests/RunCMake/AppleSilicon/default.cmake b/Tests/RunCMake/AppleSilicon/default.cmake
new file mode 100644
index 0000000..f160e25
--- /dev/null
+++ b/Tests/RunCMake/AppleSilicon/default.cmake
@@ -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})
diff --git a/Tests/RunCMake/AppleSilicon/x86_64-common.cmake b/Tests/RunCMake/AppleSilicon/x86_64-common.cmake
new file mode 100644
index 0000000..602bf92
--- /dev/null
+++ b/Tests/RunCMake/AppleSilicon/x86_64-common.cmake
@@ -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)
diff --git a/Tests/RunCMake/AppleSilicon/x86_64-env.cmake b/Tests/RunCMake/AppleSilicon/x86_64-env.cmake
new file mode 100644
index 0000000..a44eaa4
--- /dev/null
+++ b/Tests/RunCMake/AppleSilicon/x86_64-env.cmake
@@ -0,0 +1 @@
+include(x86_64-common.cmake)
diff --git a/Tests/RunCMake/AppleSilicon/x86_64-var.cmake b/Tests/RunCMake/AppleSilicon/x86_64-var.cmake
new file mode 100644
index 0000000..a44eaa4
--- /dev/null
+++ b/Tests/RunCMake/AppleSilicon/x86_64-var.cmake
@@ -0,0 +1 @@
+include(x86_64-common.cmake)
diff --git a/Tests/RunCMake/AppleSilicon/x86_64.c b/Tests/RunCMake/AppleSilicon/x86_64.c
new file mode 100644
index 0000000..cd3cc9b
--- /dev/null
+++ b/Tests/RunCMake/AppleSilicon/x86_64.c
@@ -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)
+{
+}
diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt
index 615bf20..4f1a9f7 100644
--- a/Tests/RunCMake/CMakeLists.txt
+++ b/Tests/RunCMake/CMakeLists.txt
@@ -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)