Fix macOS arm64 cross compiling.

This fixes the following issues when cross compiling SwiftShader
for arm64 macOS using CMake:

- Sets architecture based on CMAKE_OSX_ARCHITECTURES settings.
- Add missing dependency to LLVM.
- Add a workaround for assembly file compilation on macOS in marl.

Bug: fix build
Test: cmake .. -DCMAKE_OSX_ARCHITECTURES=arm64

Change-Id: I8abba9e39907d8f4d59f51c57f09cd55d4edc148
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/51890
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Tested-by: Yilong Li <liyl@google.com>
Commit-Queue: Yilong Li <liyl@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ec8544c..ea63657 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -68,6 +68,22 @@
     endif()
 endif()
 
+# Cross compiling on macOS. The cross compiling architecture should override
+# auto-detected system architecture settings.
+if(CMAKE_OSX_ARCHITECTURES)
+    if(CMAKE_OSX_ARCHITECTURES MATCHES "arm64")
+        set(ARCH "aarch64")
+    elseif(CMAKE_OSX_ARCHITECTURES MATCHES "x86_64")
+        set(ARCH "x86_64")
+    elseif(CMAKE_OSX_ARCHITECTURES MATCHES "i386")
+        set(ARCH "x86")
+    else()
+        message(FATAL_ERROR "Architecture ${CMAKE_OSX_ARCHITECTURES} is not "
+                            "supported. Only one architecture (arm64, x86_64 "
+                            "or i386) could be specified at build time.")
+    endif()
+endif()
+
 set(CMAKE_MACOSX_RPATH TRUE)
 
 if ((CMAKE_GENERATOR MATCHES "Visual Studio") AND (CMAKE_GENERATOR_TOOLSET STREQUAL ""))
diff --git a/third_party/llvm-10.0/CMakeLists.txt b/third_party/llvm-10.0/CMakeLists.txt
index 6018a12..0124ffe 100644
--- a/third_party/llvm-10.0/CMakeLists.txt
+++ b/third_party/llvm-10.0/CMakeLists.txt
@@ -160,7 +160,10 @@
     ${LLVM_DIR}/lib/CodeGen/GCMetadataPrinter.cpp
     ${LLVM_DIR}/lib/CodeGen/GCRootLowering.cpp
     ${LLVM_DIR}/lib/CodeGen/GCStrategy.cpp
+    ${LLVM_DIR}/lib/CodeGen/GlobalMerge.cpp
     ${LLVM_DIR}/lib/CodeGen/GlobalISel/CallLowering.cpp
+    ${LLVM_DIR}/lib/CodeGen/GlobalISel/Combiner.cpp
+    ${LLVM_DIR}/lib/CodeGen/GlobalISel/CombinerHelper.cpp
     ${LLVM_DIR}/lib/CodeGen/GlobalISel/CSEInfo.cpp
     ${LLVM_DIR}/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp
     ${LLVM_DIR}/lib/CodeGen/GlobalISel/GISelChangeObserver.cpp
@@ -268,6 +271,7 @@
     ${LLVM_DIR}/lib/CodeGen/RegAllocGreedy.cpp
     ${LLVM_DIR}/lib/CodeGen/RegisterClassInfo.cpp
     ${LLVM_DIR}/lib/CodeGen/RegisterCoalescer.cpp
+    ${LLVM_DIR}/lib/CodeGen/RegAllocPBQP.cpp
     ${LLVM_DIR}/lib/CodeGen/RegisterPressure.cpp
     ${LLVM_DIR}/lib/CodeGen/RegisterScavenging.cpp
     ${LLVM_DIR}/lib/CodeGen/RegisterUsageInfo.cpp
@@ -456,6 +460,7 @@
     ${LLVM_DIR}/lib/IR/Value.cpp
     ${LLVM_DIR}/lib/IR/ValueSymbolTable.cpp
     ${LLVM_DIR}/lib/IR/Verifier.cpp
+    ${LLVM_DIR}/lib/MC/ConstantPools.cpp
     ${LLVM_DIR}/lib/MC/ELFObjectWriter.cpp
     ${LLVM_DIR}/lib/MC/MachObjectWriter.cpp
     ${LLVM_DIR}/lib/MC/MCAsmBackend.cpp
@@ -732,6 +737,7 @@
     ${LLVM_DIR}/lib/Transforms/Scalar/InstSimplifyPass.cpp
     ${LLVM_DIR}/lib/Transforms/Scalar/JumpThreading.cpp
     ${LLVM_DIR}/lib/Transforms/Scalar/LICM.cpp
+    ${LLVM_DIR}/lib/Transforms/Scalar/LoopDataPrefetch.cpp
     ${LLVM_DIR}/lib/Transforms/Scalar/LoopDeletion.cpp
     ${LLVM_DIR}/lib/Transforms/Scalar/LoopDistribute.cpp
     ${LLVM_DIR}/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
@@ -757,6 +763,7 @@
     ${LLVM_DIR}/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp
     ${LLVM_DIR}/lib/Transforms/Scalar/Reassociate.cpp
     ${LLVM_DIR}/lib/Transforms/Scalar/SCCP.cpp
+    ${LLVM_DIR}/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
     ${LLVM_DIR}/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
     ${LLVM_DIR}/lib/Transforms/Scalar/SimplifyCFGPass.cpp
     ${LLVM_DIR}/lib/Transforms/Scalar/SpeculativeExecution.cpp
@@ -943,6 +950,7 @@
     )
 elseif(ARCH STREQUAL "aarch64")
     list(APPEND LLVM_LIST
+        ${LLVM_DIR}/lib/Support/AArch64TargetParser.cpp
         ${LLVM_DIR}/lib/Target/AArch64/AArch64A53Fix835769.cpp
         ${LLVM_DIR}/lib/Target/AArch64/AArch64A57FPLoadBalancing.cpp
         ${LLVM_DIR}/lib/Target/AArch64/AArch64AdvSIMDScalarPass.cpp
diff --git a/third_party/marl/CMakeLists.txt b/third_party/marl/CMakeLists.txt
index c8bf95b..015732d 100644
--- a/third_party/marl/CMakeLists.txt
+++ b/third_party/marl/CMakeLists.txt
@@ -167,6 +167,18 @@
         ${MARL_SRC_DIR}/osfiber_x64.c
         ${MARL_SRC_DIR}/osfiber_x86.c
     )
+    # CMAKE_OSX_ARCHITECTURES settings aren't propagated to assembly files when
+    # building for Apple platforms (https://gitlab.kitware.com/cmake/cmake/-/issues/20771),
+    # we treat assembly files as C files to work around this bug.
+    set_source_files_properties(
+        ${MARL_SRC_DIR}/osfiber_asm_aarch64.S
+        ${MARL_SRC_DIR}/osfiber_asm_arm.S
+        ${MARL_SRC_DIR}/osfiber_asm_mips64.S
+        ${MARL_SRC_DIR}/osfiber_asm_ppc64.S
+        ${MARL_SRC_DIR}/osfiber_asm_x64.S
+        ${MARL_SRC_DIR}/osfiber_asm_x86.S
+        PROPERTIES LANGUAGE C
+    )
 endif(NOT MSVC)
 
 ###########################################################