CMake: split out Reactor into its own CMakeLists

Bug: b/145758253
Change-Id: I36f1bbf599389c27e084fea9af3d732225567076
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/43230
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 d1c0c13..2fe9c34 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -577,9 +577,17 @@
 string(REPLACE ";" " " SWIFTSHADER_LINK_FLAGS "${SWIFTSHADER_LINK_FLAGS}")
 
 ###########################################################
+# libbacktrace and boost
+###########################################################
+if(REACTOR_EMIT_DEBUG_INFO)
+    add_subdirectory(third_party/libbacktrace EXCLUDE_FROM_ALL)
+    add_subdirectory(third_party/boost EXCLUDE_FROM_ALL)
+endif()
+
+###########################################################
 # LLVM
 ###########################################################
-add_subdirectory(third_party/llvm-${SWIFTSHADER_LLVM_VERSION})
+add_subdirectory(third_party/llvm-${SWIFTSHADER_LLVM_VERSION} EXCLUDE_FROM_ALL)
 
 ###########################################################
 # Subzero
@@ -604,32 +612,6 @@
     add_subdirectory(${CPPDAP_DIR})
 endif()
 
-set(SUBZERO_REACTOR_LIST
-    ${SOURCE_DIR}/Reactor/Debug.cpp
-    ${SOURCE_DIR}/Reactor/Debug.hpp
-    ${SOURCE_DIR}/Reactor/EmulatedReactor.cpp
-    ${SOURCE_DIR}/Reactor/ExecutableMemory.cpp
-    ${SOURCE_DIR}/Reactor/ExecutableMemory.hpp
-    ${SOURCE_DIR}/Reactor/Nucleus.hpp
-    ${SOURCE_DIR}/Reactor/Optimizer.cpp
-    ${SOURCE_DIR}/Reactor/Print.hpp
-    ${SOURCE_DIR}/Reactor/Reactor.cpp
-    ${SOURCE_DIR}/Reactor/Reactor.hpp
-    ${SOURCE_DIR}/Reactor/ReactorDebugInfo.cpp
-    ${SOURCE_DIR}/Reactor/ReactorDebugInfo.hpp
-    ${SOURCE_DIR}/Reactor/Routine.hpp
-    ${SOURCE_DIR}/Reactor/SubzeroReactor.cpp
-)
-
-add_library(ReactorSubzero STATIC EXCLUDE_FROM_ALL
-    ${SUBZERO_REACTOR_LIST}
-)
-set_target_properties(ReactorSubzero PROPERTIES
-    POSITION_INDEPENDENT_CODE 1
-    COMPILE_OPTIONS "${SWIFTSHADER_COMPILE_OPTIONS};${WARNINGS_AS_ERRORS}"
-    FOLDER "Subzero"
-)
-target_link_libraries(ReactorSubzero subzero marl)
 
 ###########################################################
 # Include Directories
@@ -679,28 +661,6 @@
     ${SOURCE_DIR}/Common/GrallocAndroid.hpp
 )
 
-set(REACTOR_LLVM_LIST
-    ${SOURCE_DIR}/Reactor/CPUID.cpp
-    ${SOURCE_DIR}/Reactor/CPUID.hpp
-    ${SOURCE_DIR}/Reactor/Debug.cpp
-    ${SOURCE_DIR}/Reactor/Debug.hpp
-    ${SOURCE_DIR}/Reactor/EmulatedReactor.cpp
-    ${SOURCE_DIR}/Reactor/ExecutableMemory.cpp
-    ${SOURCE_DIR}/Reactor/ExecutableMemory.hpp
-    ${SOURCE_DIR}/Reactor/LLVMJIT.cpp
-    ${SOURCE_DIR}/Reactor/LLVMReactor.cpp
-    ${SOURCE_DIR}/Reactor/LLVMReactor.hpp
-    ${SOURCE_DIR}/Reactor/LLVMReactorDebugInfo.cpp
-    ${SOURCE_DIR}/Reactor/LLVMReactorDebugInfo.hpp
-    ${SOURCE_DIR}/Reactor/Nucleus.hpp
-    ${SOURCE_DIR}/Reactor/Print.hpp
-    ${SOURCE_DIR}/Reactor/Reactor.cpp
-    ${SOURCE_DIR}/Reactor/Reactor.hpp
-    ${SOURCE_DIR}/Reactor/ReactorDebugInfo.cpp
-    ${SOURCE_DIR}/Reactor/ReactorDebugInfo.hpp
-    ${SOURCE_DIR}/Reactor/Routine.hpp
-)
-
 file(GLOB_RECURSE EGL_LIST
     ${OPENGL_DIR}/libEGL/*.cpp
     ${OPENGL_DIR}/libEGL/*.h
@@ -922,15 +882,7 @@
 )
 target_link_libraries(SwiftShader ${OS_LIBS} ${SWIFTSHADER_LIBS})
 
-add_library(ReactorLLVM STATIC EXCLUDE_FROM_ALL ${REACTOR_LLVM_LIST})
-set_target_properties(ReactorLLVM PROPERTIES
-    INCLUDE_DIRECTORIES "${COMMON_INCLUDE_DIR}"
-    POSITION_INDEPENDENT_CODE 1
-    FOLDER "Core"
-    COMPILE_OPTIONS "${SWIFTSHADER_COMPILE_OPTIONS};${WARNINGS_AS_ERRORS}"
-    LINK_FLAGS "${SWIFTSHADER_LINK_FLAGS}"
-)
-target_link_libraries(ReactorLLVM llvm ${OS_LIBS})
+add_subdirectory(src/Reactor) # Add ReactorSubzero and ReactorLLVM targets
 
 if(${REACTOR_BACKEND} STREQUAL "LLVM")
     set(Reactor ReactorLLVM)
@@ -940,22 +892,6 @@
     message(FATAL_ERROR "REACTOR_BACKEND must be 'LLVM' or 'Subzero'")
 endif()
 
-if(REACTOR_EMIT_DEBUG_INFO)
-    if(WIN32)
-        # Boost stacktrace uses COM on Windows.
-        # On Windows, cache COM instances in TLS for performance.
-        target_compile_definitions(${Reactor} PRIVATE BOOST_STACKTRACE_USE_WINDBG_CACHED)
-    else()
-        # Boost stacktrace uses libbacktrace
-        add_subdirectory(third_party/libbacktrace)
-        target_compile_definitions(${Reactor} PRIVATE BOOST_STACKTRACE_USE_BACKTRACE)
-        target_link_libraries(${Reactor} libbacktrace)
-    endif()
-
-    add_subdirectory(third_party/boost)
-    target_link_libraries(${Reactor} Boost::boost)
-endif(REACTOR_EMIT_DEBUG_INFO)
-
 add_library(GLCompiler STATIC EXCLUDE_FROM_ALL ${OPENGL_COMPILER_LIST})
 set_target_properties(GLCompiler PROPERTIES
     INCLUDE_DIRECTORIES "${OPENGL_INCLUDE_DIR}"
diff --git a/src/Reactor/CMakeLists.txt b/src/Reactor/CMakeLists.txt
new file mode 100644
index 0000000..965cbcc
--- /dev/null
+++ b/src/Reactor/CMakeLists.txt
@@ -0,0 +1,131 @@
+# 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(ROOT_PROJECT_COMPILE_OPTIONS
+    ${SWIFTSHADER_COMPILE_OPTIONS}
+    ${WARNINGS_AS_ERRORS}
+)
+
+set(REACTOR_SRC_FILES
+    Debug.cpp
+    Debug.hpp
+    EmulatedReactor.cpp
+    ExecutableMemory.cpp
+    ExecutableMemory.hpp
+    Nucleus.hpp
+    Print.hpp
+    Reactor.cpp
+    Reactor.hpp
+    ReactorDebugInfo.cpp
+    ReactorDebugInfo.hpp
+    Routine.hpp
+)
+
+set(SUBZERO_SRC_FILES
+    Optimizer.cpp
+    SubzeroReactor.cpp
+)
+
+set(LLVM_SRC_FILES
+    CPUID.cpp
+    CPUID.hpp
+    LLVMJIT.cpp
+    LLVMReactor.cpp
+    LLVMReactor.hpp
+    LLVMReactorDebugInfo.cpp
+    LLVMReactorDebugInfo.hpp
+)
+
+if(REACTOR_EMIT_DEBUG_INFO)
+    if(WIN32)
+        # Boost stacktrace uses COM on Windows.
+        # On Windows, cache COM instances in TLS for performance.
+        list(APPEND REACTOR_PRIVATE_COMPILE_DEFINITIONS "BOOST_STACKTRACE_USE_WINDBG_CACHED")
+    else()
+        # Boost stacktrace uses libbacktrace
+        list(APPEND REACTOR_PRIVATE_COMPILE_DEFINITIONS "BOOST_STACKTRACE_USE_BACKTRACE")
+        list(APPEND REACTOR_PRIVATE_LINK_LIBRARIES libbacktrace)
+    endif()
+
+    list(APPEND REACTOR_PRIVATE_LINK_LIBRARIES Boost::boost)
+endif(REACTOR_EMIT_DEBUG_INFO)
+
+
+# SubzeroReactor library
+
+add_library(ReactorSubzero STATIC EXCLUDE_FROM_ALL
+    ${REACTOR_SRC_FILES}
+    ${SUBZERO_SRC_FILES}
+)
+
+set_target_properties(ReactorSubzero PROPERTIES
+    POSITION_INDEPENDENT_CODE 1
+    FOLDER "Reactor"
+)
+
+target_include_directories(ReactorSubzero
+    PUBLIC
+        .
+)
+
+target_compile_definitions(ReactorSubzero
+    PRIVATE
+        ${REACTOR_PRIVATE_COMPILE_DEFINITIONS}
+)
+
+target_compile_options(ReactorSubzero
+    PUBLIC
+        ${ROOT_PROJECT_COMPILE_OPTIONS}
+)
+
+target_link_libraries(ReactorSubzero
+    PRIVATE
+        subzero
+        marl
+        ${REACTOR_PRIVATE_LINK_LIBRARIES}
+)
+
+
+# ReactorLLVM library
+
+add_library(ReactorLLVM STATIC EXCLUDE_FROM_ALL
+    ${REACTOR_SRC_FILES}
+    ${LLVM_SRC_FILES}
+)
+
+set_target_properties(ReactorLLVM PROPERTIES
+    POSITION_INDEPENDENT_CODE 1
+    FOLDER "Reactor"
+)
+
+target_include_directories(ReactorLLVM
+    PUBLIC
+        .
+)
+
+target_compile_definitions(ReactorLLVM
+    PRIVATE
+        ${REACTOR_PRIVATE_COMPILE_DEFINITIONS}
+)
+
+target_compile_options(ReactorLLVM
+    PUBLIC
+        ${ROOT_PROJECT_COMPILE_OPTIONS}
+)
+
+target_link_libraries(ReactorLLVM
+    PRIVATE
+        llvm
+        ${REACTOR_PRIVATE_LINK_LIBRARIES}
+)