Add GN builds for Reactor unit tests

This will enable running Reactor tests both with the LLVM and Subzero
backend for maximum coverage.

Bug: b/173404759
Change-Id: I43ff938924ccf864583fbf455141d7937998f754
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/50388
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Kokoro-Result: kokoro <noreply+kokoro@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 03c30e9..a527912 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -15,6 +15,7 @@
 import("//build/config/c++/c++.gni")
 import("//build/config/compiler/compiler.gni")
 import("//build/config/mips.gni")
+import("src/Reactor/reactor.gni")
 
 config("swiftshader_config") {
   defines = []
@@ -201,5 +202,13 @@
         "tests/GLESUnitTests:swiftshader_unittests",
         "tests/SystemUnitTests:swiftshader_system_unittests",
       ]
+
+      if (supports_llvm) {
+        data_deps += [ "tests/ReactorUnitTests:swiftshader_reactor_llvm_unittests" ]
+      }
+
+      if (supports_subzero) {
+        data_deps += [ "tests/ReactorUnitTests:swiftshader_reactor_subzero_unittests" ]
+      }
     }
 }
diff --git a/src/Reactor/BUILD.gn b/src/Reactor/BUILD.gn
index 86bfabf..5dd8cff 100644
--- a/src/Reactor/BUILD.gn
+++ b/src/Reactor/BUILD.gn
@@ -12,16 +12,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import("reactor.gni")
 import("../swiftshader.gni")
 
-declare_args() {
-  # Subzero produces smaller binaries, but doesn't support ARM64, MIPS64, and
-  # PPC64. It also doesn't support MemorySanitizer instrumentation.
-  use_swiftshader_with_subzero =
-      current_cpu != "arm64" && current_cpu != "mips64el" && current_cpu != "ppc64" && !is_msan
-  supports_llvm = is_linux || is_chromeos || is_fuchsia || is_win || is_android || is_mac
-}
-
 config("swiftshader_reactor_private_config") {
   if (is_win) {
     cflags = [
@@ -54,7 +47,7 @@
   ]
 }
 
-if (use_swiftshader_with_subzero) {
+if (supports_subzero) {
   # Need a separate config to ensure the warnings are added to the end.
   config("swiftshader_subzero_common_private_config") {
     defines = [
@@ -299,6 +292,7 @@
       ":swiftshader_subzero_private_config",
     ]
   }
+
   swiftshader_source_set("swiftshader_subzero_reactor") {
     deps = [
       "../../third_party/marl:Marl",
diff --git a/src/Reactor/reactor.gni b/src/Reactor/reactor.gni
new file mode 100644
index 0000000..3744d07
--- /dev/null
+++ b/src/Reactor/reactor.gni
@@ -0,0 +1,21 @@
+# Copyright (c) 2020 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# This file contains configs that need to be added or removed to all
+# SwiftShader libraries
+
+import("../swiftshader.gni")
+import("//build_overrides/build.gni")
+
+declare_args() {
+  # Subzero doesn't support ARM64, MIPS64, and PPC64.
+  supports_subzero = current_cpu != "arm64" && current_cpu != "mips64el" && current_cpu != "ppc64"
+  supports_llvm = is_linux || is_chromeos || is_fuchsia || is_win || is_android || is_mac
+}
+
+declare_args() {
+  # Subzero produces much smaller binaries, so always use it when available,
+  # except for MSan builds which only get Reactor code instrumented with LLVM.
+  use_swiftshader_with_subzero = supports_subzero && !is_msan
+}
\ No newline at end of file
diff --git a/tests/ReactorBenchmarks/CMakeLists.txt b/tests/ReactorBenchmarks/CMakeLists.txt
index c3c8d01..5f25670 100644
--- a/tests/ReactorBenchmarks/CMakeLists.txt
+++ b/tests/ReactorBenchmarks/CMakeLists.txt
@@ -22,9 +22,8 @@
     ${SWIFTSHADER_LIBS}
 )
 
-# TODO(b/154013190): move source file to separate folder for this target.
 set(REACTOR_BENCHMARKS_SRC_FILES
-    ../../src/Reactor/ReactorBenchmarks.cpp
+    ReactorBenchmarks.cpp
 )
 
 add_executable(ReactorBenchmarks
diff --git a/tests/ReactorUnitTests/BUILD.gn b/tests/ReactorUnitTests/BUILD.gn
new file mode 100644
index 0000000..1d6507c
--- /dev/null
+++ b/tests/ReactorUnitTests/BUILD.gn
@@ -0,0 +1,53 @@
+# 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.
+
+import("//testing/test.gni")
+
+test("swiftshader_reactor_llvm_unittests") {
+  deps = [
+    "//base",
+    "//base/test:test_support",
+    "//testing/gmock",
+    "//testing/gtest",
+    "//third_party/swiftshader/src/Reactor:swiftshader_llvm_reactor",
+  ]
+
+  sources = [
+    "//gpu/swiftshader_tests_main.cc",
+    "ReactorUnitTests.cpp",
+  ]
+
+  include_dirs = [
+    "../../src/Reactor"
+  ]
+}
+
+test("swiftshader_reactor_subzero_unittests") {
+  deps = [
+    "//base",
+    "//base/test:test_support",
+    "//testing/gmock",
+    "//testing/gtest",
+    "//third_party/swiftshader/src/Reactor:swiftshader_subzero_reactor",
+  ]
+
+  sources = [
+    "//gpu/swiftshader_tests_main.cc",
+    "ReactorUnitTests.cpp",
+  ]
+
+  include_dirs = [
+    "../../src/Reactor"
+  ]
+}
\ No newline at end of file
diff --git a/tests/ReactorUnitTests/CMakeLists.txt b/tests/ReactorUnitTests/CMakeLists.txt
index 39295f8..59e0406 100644
--- a/tests/ReactorUnitTests/CMakeLists.txt
+++ b/tests/ReactorUnitTests/CMakeLists.txt
@@ -22,9 +22,9 @@
     ${SWIFTSHADER_LIBS}
 )
 
-# TODO(b/154013190): move source file to separate folder for this target.
 set(REACTOR_UNIT_TESTS_SRC_FILES
-    ../../src/Reactor/ReactorUnitTests.cpp
+    main.cpp
+    ReactorUnitTests.cpp
 )
 
 add_executable(ReactorUnitTests
diff --git a/src/Reactor/ReactorBenchmarks.cpp b/tests/ReactorUnitTests/ReactorBenchmarks.cpp
similarity index 100%
rename from src/Reactor/ReactorBenchmarks.cpp
rename to tests/ReactorUnitTests/ReactorBenchmarks.cpp
diff --git a/src/Reactor/ReactorUnitTests.cpp b/tests/ReactorUnitTests/ReactorUnitTests.cpp
similarity index 99%
rename from src/Reactor/ReactorUnitTests.cpp
rename to tests/ReactorUnitTests/ReactorUnitTests.cpp
index 5482f82..e66611b 100644
--- a/src/Reactor/ReactorUnitTests.cpp
+++ b/tests/ReactorUnitTests/ReactorUnitTests.cpp
@@ -3232,12 +3232,6 @@
 	EXPECT_EQ(result, expected);
 }
 
-int main(int argc, char **argv)
-{
-	::testing::InitGoogleTest(&argc, argv);
-	return RUN_ALL_TESTS();
-}
-
 ////////////////////////////////
 // Trait compile time checks. //
 ////////////////////////////////
diff --git a/tests/ReactorUnitTests/main.cpp b/tests/ReactorUnitTests/main.cpp
new file mode 100644
index 0000000..00464ae
--- /dev/null
+++ b/tests/ReactorUnitTests/main.cpp
@@ -0,0 +1,21 @@
+// 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.
+
+#include "gtest/gtest.h"
+
+int main(int argc, char **argv)
+{
+	::testing::InitGoogleTest(&argc, argv);
+	return RUN_ALL_TESTS();
+}
diff --git a/third_party/llvm-10.0/BUILD.gn b/third_party/llvm-10.0/BUILD.gn
index 70e14c0..1fb3567 100644
--- a/third_party/llvm-10.0/BUILD.gn
+++ b/third_party/llvm-10.0/BUILD.gn
@@ -733,6 +733,7 @@
     "llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp",
     "llvm/lib/Transforms/Instrumentation/InstrOrderFile.cpp",
     "llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp",
+    "llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp",
     "llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp",
     "llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp",
     "llvm/lib/Transforms/Instrumentation/ValueProfileCollector.cpp",