CMake / Kokoro: Build and run system-unittests.

These were added for the .gn build, but were never enabled for the CMake build.

Test these as part of the Kokoro presubmits.

Bug: b/153193374
Change-Id: I36e7428bfe1a4867bea39b2fbb302aed6c3dff7b
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/43488
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Ben Clayton <bclayton@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 587a131..53f7921 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1167,6 +1167,40 @@
     if(NOT WIN32)
         target_link_libraries(math-unittests pthread ${SWIFTSHADER_LIBS})
     endif()
+
+    # System unit tests
+    set(SYSTEM_UNITTESTS_LIST
+        ${SOURCE_DIR}/System/Debug.cpp
+        ${SOURCE_DIR}/System/Memory.cpp
+
+        ${TESTS_DIR}/SystemUnitTests/main.cpp
+        ${TESTS_DIR}/SystemUnitTests/unittests.cpp
+    )
+
+    if(LINUX OR ANDROID)
+        list(APPEND SYSTEM_UNITTESTS_LIST
+            ${SOURCE_DIR}/System/Linux/MemFd.cpp
+            ${SOURCE_DIR}/System/Linux/MemFd.hpp
+        )
+    endif()
+
+    set(SYSTEM_UNITTESTS_INCLUDE_DIR
+        ${CMAKE_CURRENT_SOURCE_DIR}/src/
+    )
+
+    add_executable(system-unittests ${SYSTEM_UNITTESTS_LIST})
+    set_target_properties(system-unittests PROPERTIES
+        INCLUDE_DIRECTORIES "${SYSTEM_UNITTESTS_INCLUDE_DIR}"
+        FOLDER "Tests"
+        COMPILE_OPTIONS "${SWIFTSHADER_COMPILE_OPTIONS};${WARNINGS_AS_ERRORS}"
+        LINK_FLAGS "${SWIFTSHADER_LINK_FLAGS}"
+    )
+
+    target_link_libraries(system-unittests gtest gmock)
+    if(NOT WIN32)
+        target_link_libraries(system-unittests pthread ${SWIFTSHADER_LIBS})
+    endif()
+
 endif(SWIFTSHADER_BUILD_TESTS)
 
 if(SWIFTSHADER_BUILD_BENCHMARKS)
diff --git a/tests/SystemUnitTests/main.cpp b/tests/SystemUnitTests/main.cpp
new file mode 100644
index 0000000..7a9424d
--- /dev/null
+++ b/tests/SystemUnitTests/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/tests/kokoro/gcp_ubuntu/continuous.sh b/tests/kokoro/gcp_ubuntu/continuous.sh
index 0f5b872..9b6f2ce 100755
--- a/tests/kokoro/gcp_ubuntu/continuous.sh
+++ b/tests/kokoro/gcp_ubuntu/continuous.sh
@@ -35,6 +35,7 @@
 
 build/ReactorUnitTests
 build/gles-unittests
+build/system-unittests
 build/vk-unittests
 
 # Incrementally build and run rr::Print unit tests
diff --git a/tests/kokoro/gcp_windows/continuous.bat b/tests/kokoro/gcp_windows/continuous.bat
index b9195d4..b5e8106 100644
--- a/tests/kokoro/gcp_windows/continuous.bat
+++ b/tests/kokoro/gcp_windows/continuous.bat
@@ -44,6 +44,9 @@
 build\Debug\gles-unittests.exe
 if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!
 
+build\Debug\system-unittests.exe
+if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!
+
 build\Debug\vk-unittests.exe
 if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!
 
diff --git a/tests/kokoro/macos/continuous.sh b/tests/kokoro/macos/continuous.sh
index 855dfe4..80b0bfb 100755
--- a/tests/kokoro/macos/continuous.sh
+++ b/tests/kokoro/macos/continuous.sh
@@ -43,6 +43,7 @@
 
 build/ReactorUnitTests
 build/gles-unittests
+build/system-unittests
 build/vk-unittests
 
 # Incrementally build and run rr::Print unit tests