CMake: split out subzero and llvm-subzero into their own CMakeLists

Bug: b/145758253
Change-Id: Ife32c322cf87c9b1b98b4b50f591b75db6ea51e2
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/43148
Reviewed-by: Ben Clayton <bclayton@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Antonio Maiorano <amaiorano@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a0a8f37..9cd84a3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -360,8 +360,6 @@
 set(CPPDAP_DIR ${THIRD_PARTY_DIR}/cppdap)
 set(CPPDAP_INCLUDE_DIR ${CPPDAP_DIR}/include)
 set(JSON_INCLUDE_DIR ${THIRD_PARTY_DIR}/json/include)
-set(SUBZERO_DIR ${THIRD_PARTY_DIR}/subzero)
-set(SUBZERO_LLVM_DIR ${THIRD_PARTY_DIR}/llvm-subzero)
 set(TESTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests)
 set(HELLO2_DIR ${THIRD_PARTY_DIR}/PowerVR_SDK/Examples/Beginner/01_HelloAPI/OGLES2)
 
@@ -621,6 +619,11 @@
 ###########################################################
 add_subdirectory(third_party/llvm-${SWIFTSHADER_LLVM_VERSION})
 
+###########################################################
+# Subzero
+###########################################################
+add_subdirectory(third_party/llvm-subzero EXCLUDE_FROM_ALL)
+add_subdirectory(third_party/subzero EXCLUDE_FROM_ALL)
 
 ###########################################################
 # marl
@@ -639,84 +642,6 @@
     add_subdirectory(${CPPDAP_DIR})
 endif()
 
-
-###########################################################
-# Subzero
-###########################################################
-
-set(SUBZERO_LIST
-    ${SUBZERO_DIR}/src/IceAssembler.cpp
-    ${SUBZERO_DIR}/src/IceCfg.cpp
-    ${SUBZERO_DIR}/src/IceCfgNode.cpp
-    ${SUBZERO_DIR}/src/IceClFlags.cpp
-    ${SUBZERO_DIR}/src/IceELFObjectWriter.cpp
-    ${SUBZERO_DIR}/src/IceELFSection.cpp
-    ${SUBZERO_DIR}/src/IceFixups.cpp
-    ${SUBZERO_DIR}/src/IceGlobalContext.cpp
-    ${SUBZERO_DIR}/src/IceGlobalInits.cpp
-    ${SUBZERO_DIR}/src/IceInst.cpp
-    ${SUBZERO_DIR}/src/IceInstrumentation.cpp
-    ${SUBZERO_DIR}/src/IceIntrinsics.cpp
-    ${SUBZERO_DIR}/src/IceLiveness.cpp
-    ${SUBZERO_DIR}/src/IceLoopAnalyzer.cpp
-    ${SUBZERO_DIR}/src/IceMangling.cpp
-    ${SUBZERO_DIR}/src/IceMemory.cpp
-    ${SUBZERO_DIR}/src/IceOperand.cpp
-    ${SUBZERO_DIR}/src/IceRangeSpec.cpp
-    ${SUBZERO_DIR}/src/IceRegAlloc.cpp
-    ${SUBZERO_DIR}/src/IceRevision.cpp
-    ${SUBZERO_DIR}/src/IceRNG.cpp
-    ${SUBZERO_DIR}/src/IceSwitchLowering.cpp
-    ${SUBZERO_DIR}/src/IceTargetLowering.cpp
-    ${SUBZERO_DIR}/src/IceThreading.cpp
-    ${SUBZERO_DIR}/src/IceTimerTree.cpp
-    ${SUBZERO_DIR}/src/IceTypes.cpp
-    ${SUBZERO_DIR}/src/IceVariableSplitting.cpp
-)
-
-# FIXME: Shouldn't depend on external source files directly.
-list(APPEND SUBZERO_LIST
-    ${SOURCE_DIR}/Common/Memory.cpp
-)
-
-if(ARCH STREQUAL "x86_64")
-    list(APPEND SUBZERO_LIST
-        ${SUBZERO_DIR}/src/IceTargetLoweringX86.cpp
-        ${SUBZERO_DIR}/src/IceInstX8664.cpp
-        ${SUBZERO_DIR}/src/IceTargetLoweringX8664.cpp
-    )
-    set(SUBZERO_TARGET X8664)
-elseif(ARCH STREQUAL "x86")
-    list(APPEND SUBZERO_LIST
-        ${SUBZERO_DIR}/src/IceTargetLoweringX86.cpp
-        ${SUBZERO_DIR}/src/IceInstX8632.cpp
-        ${SUBZERO_DIR}/src/IceTargetLoweringX8632.cpp
-    )
-    set(SUBZERO_TARGET X8632)
-elseif(ARCH STREQUAL "arm")
-    list(APPEND SUBZERO_LIST
-        ${SUBZERO_DIR}/src/IceAssemblerARM32.cpp
-        ${SUBZERO_DIR}/src/IceInstARM32.cpp
-        ${SUBZERO_DIR}/src/IceTargetLoweringARM32.cpp
-    )
-    set(SUBZERO_TARGET ARM32)
-elseif(ARCH STREQUAL "mipsel")
-    list(APPEND SUBZERO_LIST
-        ${SUBZERO_DIR}/src/IceAssemblerMIPS32.cpp
-        ${SUBZERO_DIR}/src/IceInstMIPS32.cpp
-        ${SUBZERO_DIR}/src/IceTargetLoweringMIPS32.cpp
-    )
-    set(SUBZERO_TARGET MIPS32)
-else()
-    message(FATAL_ERROR "Architecture '${ARCH}' not supported by Subzero")
-endif()
-
-file(GLOB_RECURSE SUBZERO_DEPENDENCIES_LIST
-    ${SUBZERO_LLVM_DIR}/*.cpp
-    ${SUBZERO_LLVM_DIR}/*.c
-    ${SUBZERO_LLVM_DIR}/*.h
-)
-
 set(SUBZERO_REACTOR_LIST
     ${SOURCE_DIR}/Reactor/Debug.cpp
     ${SOURCE_DIR}/Reactor/Debug.hpp
@@ -734,61 +659,15 @@
     ${SOURCE_DIR}/Reactor/SubzeroReactor.cpp
 )
 
-set(SUBZERO_INCLUDE_DIR
-    ${SUBZERO_DIR}/
-    ${SUBZERO_LLVM_DIR}/include/
-    ${SUBZERO_DIR}/pnacl-llvm/include/
-)
-
-if(WIN32)
-    list(APPEND SUBZERO_INCLUDE_DIR ${SUBZERO_LLVM_DIR}/build/Windows/include/)
-elseif(LINUX)
-    list(APPEND SUBZERO_INCLUDE_DIR ${SUBZERO_LLVM_DIR}/build/Linux/include/)
-elseif(APPLE)
-    list(APPEND SUBZERO_INCLUDE_DIR ${SUBZERO_LLVM_DIR}/build/MacOS/include/)
-endif()
-
-if(WIN32)
-    list(APPEND SUBZERO_COMPILE_OPTIONS
-        "/wd4146" # unary minus operator applied to unsigned type, result still unsigned
-        "/wd4334" # ''operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
-        "/wd4996" # The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: new_name.
-    )
-    list(APPEND SUBZERO_DEPENDENCIES_COMPILE_OPTIONS
-        ${SUBZERO_COMPILE_OPTIONS}
-        "/wd4267"  # '=': conversion from 'size_t' to 'sopno', possible loss of data
-        "/wd4244"  # '=': conversion from '__int64' to 'llvm_regoff_t', possible loss of data
-        "/wd4141"  # 'inline': used more than once
-        "/wd4291"  # 'void *operator new(size_t,const `anonymous-namespace'::NamedBufferAlloc &)': no matching operator delete found; memory will not be freed if initialization throws an exception
-    )
-endif()
-
-add_library(SubzeroDependencies STATIC EXCLUDE_FROM_ALL
-    ${SUBZERO_DEPENDENCIES_LIST}
-)
-set_target_properties(SubzeroDependencies PROPERTIES
-    POSITION_INDEPENDENT_CODE 1
-    INCLUDE_DIRECTORIES "${SUBZERO_INCLUDE_DIR}"
-    COMPILE_OPTIONS "${SUBZERO_DEPENDENCIES_COMPILE_OPTIONS}"
-    FOLDER "Subzero"
-)
-
 add_library(ReactorSubzero STATIC EXCLUDE_FROM_ALL
-    ${SUBZERO_LIST}
     ${SUBZERO_REACTOR_LIST}
 )
 set_target_properties(ReactorSubzero PROPERTIES
     POSITION_INDEPENDENT_CODE 1
-    INCLUDE_DIRECTORIES "${SUBZERO_INCLUDE_DIR}"
-    COMPILE_OPTIONS "${SUBZERO_COMPILE_OPTIONS};${SWIFTSHADER_COMPILE_OPTIONS};${WARNINGS_AS_ERRORS}"
-    COMPILE_DEFINITIONS "SZTARGET=${SUBZERO_TARGET}; ALLOW_DUMP=0; ALLOW_TIMERS=0; ALLOW_LLVM_CL=0; ALLOW_LLVM_IR=0; ALLOW_LLVM_IR_AS_INPUT=0; ALLOW_MINIMAL_BUILD=0; ALLOW_WASM=0; ICE_THREAD_LOCAL_HACK=0;"
+    COMPILE_OPTIONS "${SWIFTSHADER_COMPILE_OPTIONS};${WARNINGS_AS_ERRORS}"
     FOLDER "Subzero"
 )
-target_link_libraries(ReactorSubzero SubzeroDependencies marl)
-
-if(WIN32)
-    target_compile_definitions(ReactorSubzero PRIVATE SUBZERO_USE_MICROSOFT_ABI)
-endif()
+target_link_libraries(ReactorSubzero subzero marl)
 
 ###########################################################
 # Include Directories
diff --git a/third_party/llvm-subzero/CMakeLists.txt b/third_party/llvm-subzero/CMakeLists.txt
new file mode 100644
index 0000000..f8a8ac7
--- /dev/null
+++ b/third_party/llvm-subzero/CMakeLists.txt
@@ -0,0 +1,250 @@
+# Copyright 2020 The SwiftShader Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+set(LLVM_SUBZERO_SRC_FILES
+    "build/Android/include/llvm/Config/abi-breaking.h"
+    "build/Android/include/llvm/Config/config.h"
+    "build/Android/include/llvm/Config/llvm-config.h"
+    "build/Android/include/llvm/Support/DataTypes.h"
+    "build/Fuchsia/include/llvm/Config/abi-breaking.h"
+    "build/Fuchsia/include/llvm/Config/config.h"
+    "build/Fuchsia/include/llvm/Config/llvm-config.h"
+    "build/Fuchsia/include/llvm/Support/DataTypes.h"
+    "build/Linux/include/llvm/Config/abi-breaking.h"
+    "build/Linux/include/llvm/Config/config.h"
+    "build/Linux/include/llvm/Config/llvm-config.h"
+    "build/Linux/include/llvm/Support/DataTypes.h"
+    "build/MacOS/include/llvm/Config/abi-breaking.h"
+    "build/MacOS/include/llvm/Config/config.h"
+    "build/MacOS/include/llvm/Config/llvm-config.h"
+    "build/MacOS/include/llvm/Support/DataTypes.h"
+    "build/Windows/include/llvm/Config/abi-breaking.h"
+    "build/Windows/include/llvm/Config/config.h"
+    "build/Windows/include/llvm/Config/llvm-config.h"
+    "build/Windows/include/llvm/Support/DataTypes.h"
+    "include/llvm-c/ErrorHandling.h"
+    "include/llvm-c/Support.h"
+    "include/llvm-c/Types.h"
+    "include/llvm/ADT/APFloat.h"
+    "include/llvm/ADT/APInt.h"
+    "include/llvm/ADT/ArrayRef.h"
+    "include/llvm/ADT/BitVector.h"
+    "include/llvm/ADT/DenseMap.h"
+    "include/llvm/ADT/DenseMapInfo.h"
+    "include/llvm/ADT/edit_distance.h"
+    "include/llvm/ADT/EpochTracker.h"
+    "include/llvm/ADT/FoldingSet.h"
+    "include/llvm/ADT/Hashing.h"
+    "include/llvm/ADT/ilist_base.h"
+    "include/llvm/ADT/ilist_iterator.h"
+    "include/llvm/ADT/ilist_node_base.h"
+    "include/llvm/ADT/ilist_node_options.h"
+    "include/llvm/ADT/ilist_node.h"
+    "include/llvm/ADT/ilist.h"
+    "include/llvm/ADT/IntrusiveRefCntPtr.h"
+    "include/llvm/ADT/iterator_range.h"
+    "include/llvm/ADT/iterator.h"
+    "include/llvm/ADT/None.h"
+    "include/llvm/ADT/Optional.h"
+    "include/llvm/ADT/PointerIntPair.h"
+    "include/llvm/ADT/PointerUnion.h"
+    "include/llvm/ADT/simple_ilist.h"
+    "include/llvm/ADT/SmallPtrSet.h"
+    "include/llvm/ADT/SmallSet.h"
+    "include/llvm/ADT/SmallString.h"
+    "include/llvm/ADT/SmallVector.h"
+    "include/llvm/ADT/Statistic.h"
+    "include/llvm/ADT/STLExtras.h"
+    "include/llvm/ADT/StringExtras.h"
+    "include/llvm/ADT/StringMap.h"
+    "include/llvm/ADT/StringRef.h"
+    "include/llvm/ADT/StringSwitch.h"
+    "include/llvm/ADT/Triple.h"
+    "include/llvm/ADT/Twine.h"
+    "include/llvm/Demangle/Demangle.h"
+    "include/llvm/IR/Argument.h"
+    "include/llvm/IR/Attributes.h"
+    "include/llvm/IR/BasicBlock.h"
+    "include/llvm/IR/CallingConv.h"
+    "include/llvm/IR/Constant.h"
+    "include/llvm/IR/DebugLoc.h"
+    "include/llvm/IR/DerivedTypes.h"
+    "include/llvm/IR/Function.h"
+    "include/llvm/IR/GlobalObject.h"
+    "include/llvm/IR/GlobalValue.h"
+    "include/llvm/IR/Instruction.h"
+    "include/llvm/IR/Intrinsics.h"
+    "include/llvm/IR/LLVMContext.h"
+    "include/llvm/IR/Metadata.h"
+    "include/llvm/IR/OperandTraits.h"
+    "include/llvm/IR/SymbolTableListTraits.h"
+    "include/llvm/IR/TrackingMDRef.h"
+    "include/llvm/IR/Type.h"
+    "include/llvm/IR/Use.h"
+    "include/llvm/IR/User.h"
+    "include/llvm/IR/Value.h"
+    "include/llvm/IRReader/IRReader.h"
+    "include/llvm/Support/AlignOf.h"
+    "include/llvm/Support/Allocator.h"
+    "include/llvm/Support/ARMBuildAttributes.h"
+    "include/llvm/Support/Atomic.h"
+    "include/llvm/Support/Casting.h"
+    "include/llvm/Support/CBindingWrapping.h"
+    "include/llvm/Support/Chrono.h"
+    "include/llvm/Support/circular_raw_ostream.h"
+    "include/llvm/Support/COFF.h"
+    "include/llvm/Support/CommandLine.h"
+    "include/llvm/Support/Compiler.h"
+    "include/llvm/Support/ConvertUTF.h"
+    "include/llvm/Support/DataStream.h"
+    "include/llvm/Support/Debug.h"
+    "include/llvm/Support/ELF.h"
+    "include/llvm/Support/Endian.h"
+    "include/llvm/Support/Errc.h"
+    "include/llvm/Support/Errno.h"
+    "include/llvm/Support/Error.h"
+    "include/llvm/Support/ErrorHandling.h"
+    "include/llvm/Support/ErrorOr.h"
+    "include/llvm/Support/FileSystem.h"
+    "include/llvm/Support/FileUtilities.h"
+    "include/llvm/Support/Format.h"
+    "include/llvm/Support/FormatCommon.h"
+    "include/llvm/Support/FormatProviders.h"
+    "include/llvm/Support/FormatVariadic.h"
+    "include/llvm/Support/FormatVariadicDetails.h"
+    "include/llvm/Support/Host.h"
+    "include/llvm/Support/MachO.h"
+    "include/llvm/Support/ManagedStatic.h"
+    "include/llvm/Support/MathExtras.h"
+    "include/llvm/Support/MD5.h"
+    "include/llvm/Support/Memory.h"
+    "include/llvm/Support/MemoryBuffer.h"
+    "include/llvm/Support/MemoryObject.h"
+    "include/llvm/Support/Mutex.h"
+    "include/llvm/Support/MutexGuard.h"
+    "include/llvm/Support/NativeFormatting.h"
+    "include/llvm/Support/Options.h"
+    "include/llvm/Support/Path.h"
+    "include/llvm/Support/PointerLikeTypeTraits.h"
+    "include/llvm/Support/Process.h"
+    "include/llvm/Support/Program.h"
+    "include/llvm/Support/raw_os_ostream.h"
+    "include/llvm/Support/raw_ostream.h"
+    "include/llvm/Support/Regex.h"
+    "include/llvm/Support/Signals.h"
+    "include/llvm/Support/SMLoc.h"
+    "include/llvm/Support/SourceMgr.h"
+    "include/llvm/Support/StreamingMemoryObject.h"
+    "include/llvm/Support/StringSaver.h"
+    "include/llvm/Support/SwapByteOrder.h"
+    "include/llvm/Support/TargetParser.h"
+    "include/llvm/Support/thread.h"
+    "include/llvm/Support/Threading.h"
+    "include/llvm/Support/Timer.h"
+    "include/llvm/Support/TimeValue.h"
+    "include/llvm/Support/type_traits.h"
+    "include/llvm/Support/UniqueLock.h"
+    "include/llvm/Support/Valgrind.h"
+    "include/llvm/Support/WindowsError.h"
+    "include/llvm/Support/YAMLParser.h"
+    "include/llvm/Support/YAMLTraits.h"
+    "lib/Demangle/ItaniumDemangle.cpp"
+    "lib/Support/APInt.cpp"
+    "lib/Support/Atomic.cpp"
+    "lib/Support/circular_raw_ostream.cpp"
+    "lib/Support/CommandLine.cpp"
+    "lib/Support/ConvertUTF.cpp"
+    "lib/Support/ConvertUTFWrapper.cpp"
+    "lib/Support/Debug.cpp"
+    "lib/Support/Errno.cpp"
+    "lib/Support/ErrorHandling.cpp"
+    "lib/Support/FoldingSet.cpp"
+    "lib/Support/Hashing.cpp"
+    "lib/Support/Host.cpp"
+    "lib/Support/ManagedStatic.cpp"
+    "lib/Support/MemoryBuffer.cpp"
+    "lib/Support/Mutex.cpp"
+    "lib/Support/NativeFormatting.cpp"
+    "lib/Support/Path.cpp"
+    "lib/Support/Process.cpp"
+    "lib/Support/Program.cpp"
+    "lib/Support/raw_os_ostream.cpp"
+    "lib/Support/raw_ostream.cpp"
+    "lib/Support/regcclass.h"
+    "lib/Support/regcname.h"
+    "lib/Support/regcomp.c"
+    "lib/Support/regerror.c"
+    "lib/Support/regex_impl.h"
+    "lib/Support/Regex.cpp"
+    "lib/Support/regex2.h"
+    "lib/Support/regexec.c"
+    "lib/Support/regfree.c"
+    "lib/Support/regstrlcpy.c"
+    "lib/Support/regutils.h"
+    "lib/Support/Signals.cpp"
+    "lib/Support/SmallPtrSet.cpp"
+    "lib/Support/SmallVector.cpp"
+    "lib/Support/StringExtras.cpp"
+    "lib/Support/StringMap.cpp"
+    "lib/Support/StringRef.cpp"
+    "lib/Support/StringSaver.cpp"
+    "lib/Support/TargetParser.cpp"
+    "lib/Support/Threading.cpp"
+    "lib/Support/Timer.cpp"
+    "lib/Support/Triple.cpp"
+    "lib/Support/Twine.cpp"
+    "lib/Support/Unix/Unix.h"
+    "lib/Support/Windows/WindowsSupport.h"
+)
+
+
+if(WIN32)
+    set(LLVM_SUBZERO_PLATFORM_INCLUDE_DIR "build/Windows/include")
+elseif(LINUX)
+    set(LLVM_SUBZERO_PLATFORM_INCLUDE_DIR "build/Linux/include")
+elseif(APPLE)
+    set(LLVM_SUBZERO_PLATFORM_INCLUDE_DIR "build/MacOS/include")
+endif()
+
+if(WIN32)
+    list(APPEND LLVM_SUBZERO_COMPILE_OPTIONS
+        "/wd4141"  # 'inline': used more than once
+        "/wd4146" # unary minus operator applied to unsigned type, result still unsigned
+        "/wd4244"  # '=': conversion from '__int64' to 'llvm_regoff_t', possible loss of data
+        "/wd4267"  # '=': conversion from 'size_t' to 'sopno', possible loss of data
+        "/wd4291"  # 'void *operator new(size_t,const `anonymous-namespace'::NamedBufferAlloc &)': no matching operator delete found; memory will not be freed if initialization throws an exception
+        "/wd4334" # ''operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
+        "/wd4996" # The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: new_name.
+    )
+endif()
+
+add_library(llvm-subzero STATIC EXCLUDE_FROM_ALL
+  ${LLVM_SUBZERO_SRC_FILES}
+)
+
+set_target_properties(llvm-subzero PROPERTIES
+    POSITION_INDEPENDENT_CODE 1
+    FOLDER "Subzero"
+)
+
+target_include_directories(llvm-subzero
+    PUBLIC
+        "include"
+        ${LLVM_SUBZERO_PLATFORM_INCLUDE_DIR}
+)
+
+target_compile_options(llvm-subzero
+    PUBLIC
+        ${LLVM_SUBZERO_COMPILE_OPTIONS}
+)
diff --git a/third_party/subzero/CMakeLists.txt b/third_party/subzero/CMakeLists.txt
index 3c7b3af..54d3d0f 100644
--- a/third_party/subzero/CMakeLists.txt
+++ b/third_party/subzero/CMakeLists.txt
@@ -1,65 +1,131 @@
-unset(PNACL_LLVM)
-# Define PNACL_LLVM for LLVM_VERSION <= 3.7
-if((NOT LLVM_VERSION_MAJOR GREATER 3) AND (NOT LLVM_VERSION_MINOR GREATER 7))
-  set(PNACL_LLVM 1)
-endif()
+# Copyright 2020 The SwiftShader Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
 
-if(PNACL_LLVM)
-  add_definitions(
-    -DPNACL_LLVM
+set(ROOT_PROJECT_COMPILE_OPTIONS
+    ${SWIFTSHADER_COMPILE_OPTIONS}
+    ${WARNINGS_AS_ERRORS}
+)
+
+set(SUBZERO_SRC_FILES
+    src/IceAssembler.cpp
+    src/IceCfg.cpp
+    src/IceCfgNode.cpp
+    src/IceClFlags.cpp
+    src/IceELFObjectWriter.cpp
+    src/IceELFSection.cpp
+    src/IceFixups.cpp
+    src/IceGlobalContext.cpp
+    src/IceGlobalInits.cpp
+    src/IceInst.cpp
+    src/IceInstrumentation.cpp
+    src/IceIntrinsics.cpp
+    src/IceLiveness.cpp
+    src/IceLoopAnalyzer.cpp
+    src/IceMangling.cpp
+    src/IceMemory.cpp
+    src/IceOperand.cpp
+    src/IceRangeSpec.cpp
+    src/IceRegAlloc.cpp
+    src/IceRevision.cpp
+    src/IceRNG.cpp
+    src/IceSwitchLowering.cpp
+    src/IceTargetLowering.cpp
+    src/IceThreading.cpp
+    src/IceTimerTree.cpp
+    src/IceTypes.cpp
+    src/IceVariableSplitting.cpp
+)
+
+if(ARCH STREQUAL "x86_64")
+    list(APPEND SUBZERO_SRC_FILES
+        src/IceTargetLoweringX86.cpp
+        src/IceInstX8664.cpp
+        src/IceTargetLoweringX8664.cpp
     )
-  set(PNACL_EXTRA_COMPONENTS NaClBitReader NaClBitTestUtils)
+    set(SUBZERO_TARGET_CPU X8664)
+elseif(ARCH STREQUAL "x86")
+    list(APPEND SUBZERO_SRC_FILES
+        src/IceTargetLoweringX86.cpp
+        src/IceInstX8632.cpp
+        src/IceTargetLoweringX8632.cpp
+    )
+    set(SUBZERO_TARGET_CPU X8632)
+elseif(ARCH STREQUAL "arm")
+    list(APPEND SUBZERO_SRC_FILES
+        src/IceAssemblerARM32.cpp
+        src/IceInstARM32.cpp
+        src/IceTargetLoweringARM32.cpp
+    )
+    set(SUBZERO_TARGET_CPU ARM32)
+elseif(ARCH STREQUAL "mipsel")
+    list(APPEND SUBZERO_SRC_FILES
+        src/IceAssemblerMIPS32.cpp
+        src/IceInstMIPS32.cpp
+        src/IceTargetLoweringMIPS32.cpp
+    )
+    set(SUBZERO_TARGET_CPU MIPS32)
 else()
-  include_directories(pnacl-llvm/include)
-  file(GLOB pnacl_llvm_SRCS "pnacl-llvm/*.cpp")
+    message(FATAL_ERROR "Architecture '${ARCH}' not supported by Subzero")
 endif()
 
-set(LLVM_LINK_COMPONENTS
-  ${LLVM_TARGETS_TO_BUILD}
-  Core
-  IRReader
-  Support
-  ${PNACL_EXTRA_COMPONENTS}
-  )
-
-file(GLOB pnacl_sz_SRCS "src/*.cpp")
-add_llvm_tool(pnacl-sz ${pnacl_sz_SRCS} ${pnacl_llvm_SRCS})
-
-if(PNACL_BROWSER_TRANSLATOR)
-  add_definitions(
-  -DALLOW_DUMP=0
-  -DALLOW_LLVM_CL=0
-  -DALLOW_LLVM_IR=0
-  -DALLOW_LLVM_IR_AS_INPUT=0
-  -DALLOW_TIMERS=0
-  -DALLOW_MINIMAL_BUILD=1
-  -DALLOW_WASM=0
-  -DPNACL_BROWSER_TRANSLATOR=1
-  )
-else()
-  add_definitions(
-  -DALLOW_DUMP=1
-  -DALLOW_LLVM_CL=1
-  -DALLOW_LLVM_IR=1
-  -DALLOW_LLVM_IR_AS_INPUT=1
-  -DALLOW_TIMERS=1
-  -DALLOW_MINIMAL_BUILD=0
-  -DALLOW_WASM=0
-  -DPNACL_BROWSER_TRANSLATOR=0
-  )
-  add_compile_options(-Wno-undefined-var-template)
+if(WIN32)
+    list(APPEND SUBZERO_COMPILE_OPTIONS
+        "/wd4146" # unary minus operator applied to unsigned type, result still unsigned
+        "/wd4334" # ''operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
+        "/wd4996" # The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: new_name.
+    )
 endif()
 
-execute_process(COMMAND git rev-parse HEAD
-  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-  OUTPUT_VARIABLE SZ_GIT_HASH
-  OUTPUT_STRIP_TRAILING_WHITESPACE
-  )
-execute_process(COMMAND git rev-list --count HEAD
-  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-  OUTPUT_VARIABLE SZ_COMMIT_COUNT
-  OUTPUT_STRIP_TRAILING_WHITESPACE
-  )
-add_definitions(-DSUBZERO_REVISION=${SZ_COMMIT_COUNT}_${SZ_GIT_HASH})
+add_library(subzero STATIC EXCLUDE_FROM_ALL
+    ${SUBZERO_SRC_FILES}
+)
 
-target_link_libraries(pnacl-sz pthread)
+set_target_properties(subzero PROPERTIES
+    POSITION_INDEPENDENT_CODE 1
+    FOLDER "Subzero"
+)
+
+target_include_directories(subzero
+    PUBLIC
+        # Add lib root as include dir, so client code can do #include "src/..."
+        # TODO: Split out headers into separate 'include' directory.
+        "."
+        "pnacl-llvm/include"
+)
+
+target_compile_options(subzero
+    PUBLIC
+        ${ROOT_PROJECT_COMPILE_OPTIONS}
+        ${SUBZERO_COMPILE_OPTIONS}
+)
+
+target_compile_definitions(subzero
+    PUBLIC
+        "SZTARGET=${SUBZERO_TARGET_CPU}"
+        "ALLOW_DUMP=0"
+        "ALLOW_TIMERS=0"
+        "ALLOW_LLVM_CL=0"
+        "ALLOW_LLVM_IR=0"
+        "ALLOW_LLVM_IR_AS_INPUT=0"
+        "ALLOW_MINIMAL_BUILD=0"
+        "ALLOW_WASM=0"
+        "ICE_THREAD_LOCAL_HACK=0"
+    PRIVATE
+        $<$<BOOL:${WIN32}>:"SUBZERO_USE_MICROSOFT_ABI">
+)
+
+target_link_libraries(subzero
+    PUBLIC
+        llvm-subzero
+)