Add the PowerVR SDK as a git submodule

The primary purpose of doing this is to have all the PowerVR SDK
examples in one place and remain up-to-date. This change also lets us
simplify a bit of our CMakeLists.txt as we only need to use
add_subdirectory to build everything we need.

The PowerVR SDK examples are built by explicitly enabling
SWIFTSHADER_BUILD_PVR_EXAMPLES (either through a cmake gui, or
-DSWIFTSHADER_BUILD_PVR_EXAMPLES=ON on the command line). You must also specify
a window system for the examples to use with something like
"-DPVR_WINDOW_SYSTEM=X11".

Several cmake option flags were renamed to start with "SWIFTSHADER_" to avoid
name collisions with subdirectories.

Bug: b/141361933
Change-Id: I6d2fac579f62ff4bc211d5fd5127fb670eb9b4bf
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/39909
Tested-by: Sean Risser <srisser@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/.gitmodules b/.gitmodules
index 391b6f4..6f9424d 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -13,3 +13,7 @@
 [submodule "third_party/libbacktrace/src"]
 	path = third_party/libbacktrace/src
 	url = https://github.com/ianlancetaylor/libbacktrace.git
+
+[submodule "third_party/PowerVR_Examples"]
+	path = third_party/PowerVR_Examples
+	url = https://github.com/powervr-graphics/Native_SDK.git
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
index 2a7e260..11a1dc3 100644
--- a/.vscode/tasks.json
+++ b/.vscode/tasks.json
@@ -68,8 +68,8 @@
                 "..",
                 "-GNinja",
                 "-DCMAKE_BUILD_TYPE=${input:buildType}",
-                "-DWARNINGS_AS_ERRORS=1",
-                "-DDCHECK_ALWAYS_ON=1",
+                "-DSWIFTSHADER_WARNINGS_AS_ERRORS=1",
+                "-DSWIFTSHADER_DCHECK_ALWAYS_ON=1",
                 "-DREACTOR_VERIFY_LLVM_IR=1",
             ],
             "options": {
@@ -106,4 +106,4 @@
             "description": "The type of build",
         },
     ]
-}
\ No newline at end of file
+}
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 341435a..56f517c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -88,31 +88,32 @@
 endif()
 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS Debug Release MinSizeRel RelWithDebInfo)
 
-option(BUILD_EGL "Build the EGL library" 1)
-option(BUILD_GLESv2 "Build the OpenGL ES 2 library" 1)
-option(BUILD_GLES_CM "Build the OpenGL ES 1.1 library" 1)
-option(BUILD_VULKAN "Build the Vulkan library" 1)
+option(SWIFTSHADER_BUILD_EGL "Build the EGL library" 1)
+option(SWIFTSHADER_BUILD_GLESv2 "Build the OpenGL ES 2 library" 1)
+option(SWIFTSHADER_BUILD_GLES_CM "Build the OpenGL ES 1.1 library" 1)
+option(SWIFTSHADER_BUILD_VULKAN "Build the Vulkan library" 1)
+option(SWIFTSHADER_BUILD_PVR "Fetch and build the PowerVR examples" 0)
 
-option(USE_GROUP_SOURCES "Group the source files in a folder tree for Visual Studio" 1)
+option(SWIFTSHADER_USE_GROUP_SOURCES "Group the source files in a folder tree for Visual Studio" 1)
 
-option(BUILD_SAMPLES "Build sample programs" 1)
-option(BUILD_TESTS "Build test programs" 1)
+option(SWIFTSHADER_BUILD_SAMPLES "Build sample programs" 1)
+option(SWIFTSHADER_BUILD_TESTS "Build test programs" 1)
 
-option(MSAN "Build with memory sanitizer" 0)
-option(ASAN "Build with address sanitizer" 0)
-option(TSAN "Build with thread sanitizer" 0)
-option(UBSAN "Build with undefined behavior sanitizer" 0)
-option(WARNINGS_AS_ERRORS "Treat all warnings as errors" 1)
-option(DCHECK_ALWAYS_ON "Check validation macros even in release builds" 0)
+option(SWIFTSHADER_MSAN "Build with memory sanitizer" 0)
+option(SWIFTSHADER_ASAN "Build with address sanitizer" 0)
+option(SWIFTSHADER_TSAN "Build with thread sanitizer" 0)
+option(SWIFTSHADER_UBSAN "Build with undefined behavior sanitizer" 0)
+option(SWIFTSHADER_WARNINGS_AS_ERRORS "Treat all warnings as errors" 1)
+option(SWIFTSHADER_DCHECK_ALWAYS_ON "Check validation macros even in release builds" 0)
 option(REACTOR_EMIT_DEBUG_INFO "Emit debug info for JIT functions" 0)
 option(REACTOR_EMIT_PRINT_LOCATION "Emit printing of location info for JIT functions" 0)
 option(REACTOR_VERIFY_LLVM_IR "Check reactor-generated LLVM IR is valid even in release builds" 0)
-option(LESS_DEBUG_INFO "Generate less debug info to reduce file size" 0)
-option(ENABLE_VULKAN_DEBUGGER "Enable vulkan debugger support" 0)
+option(SWIFTSHADER_LESS_DEBUG_INFO "Generate less debug info to reduce file size" 0)
+option(SWIFTSHADER_ENABLE_VULKAN_DEBUGGER "Enable vulkan debugger support" 0)
 
-set(BUILD_MARL ${BUILD_VULKAN})
+set(BUILD_MARL ${SWIFTSHADER_BUILD_VULKAN})
 
-if(${BUILD_VULKAN} AND ${ENABLE_VULKAN_DEBUGGER})
+if(${SWIFTSHADER_BUILD_VULKAN} AND ${SWIFTSHADER_ENABLE_VULKAN_DEBUGGER})
     set(BUILD_CPPDAP 1)
 else()
     set(BUILD_CPPDAP 0)
@@ -154,7 +155,7 @@
       Running 'git submodule update --init' to download it:
         ")
 
-        execute_process(COMMAND git submodule update --init)
+        execute_process(COMMAND git submodule update --init ${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest)
     endif()
 endif()
 
@@ -165,7 +166,7 @@
       Running 'git submodule update --init' to download it:
         ")
 
-        execute_process(COMMAND git submodule update --init)
+        execute_process(COMMAND git submodule update --init ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libbacktrace)
     endif()
 endif()
 
@@ -211,6 +212,19 @@
     endif()
 endfunction()
 
+if (SWIFTSHADER_BUILD_PVR)
+    if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/PowerVR_Examples/.git)
+        message(WARNING "
+        third_party/PowerVR_Examples submodule missing.
+        Running 'git submodule update --init' to download it:
+        ")
+
+        execute_process(COMMAND git submodule update --init ${CMAKE_CURRENT_SOURCE_DIR}/third_party/PowerVR_Examples)
+    endif()
+    set(PVR_BUILD_EXAMPLES ON CACHE BOOL "Build the PowerVR SDK Examples" FORCE)
+    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third_party/PowerVR_Examples)
+endif()
+
 ###########################################################
 # Convenience macros
 ###########################################################
@@ -241,7 +255,7 @@
         set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS "-exported_symbols_list ${DIR}/${TARGET}.exports")
         set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_DEPENDS "${DIR}/${TARGET}.exports;")
         # Don't allow undefined symbols, unless it's a Sanitizer build.
-        if(NOT MSAN AND NOT ASAN AND NOT TSAN AND NOT UBSAN)
+        if(NOT SWIFTSHADER_MSAN AND NOT SWIFTSHADER_ASAN AND NOT SWIFTSHADER_TSAN AND NOT SWIFTSHADER_UBSAN)
             set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-undefined,error")
         endif()
     elseif(LINUX)
@@ -267,13 +281,13 @@
         set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--gc-sections")
 
         # Don't allow undefined symbols, unless it's a Sanitizer build.
-        if(NOT MSAN AND NOT ASAN AND NOT TSAN AND NOT UBSAN)
+        if(NOT SWIFTSHADER_MSAN AND NOT SWIFTSHADER_ASAN AND NOT SWIFTSHADER_TSAN AND NOT SWIFTSHADER_UBSAN)
             set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--no-undefined")
         endif()
     endif()
 endmacro()
 
-if(USE_GROUP_SOURCES)
+if(SWIFTSHADER_USE_GROUP_SOURCES)
     group_all_sources(src)
 endif()
 
@@ -330,7 +344,7 @@
     add_definitions(-D_SBCS)  # Single Byte Character Set (ASCII)
     add_definitions(-D_ENABLE_EXTENDED_ALIGNED_STORAGE)  # Disable MSVC warnings about std::aligned_storage being broken before VS 2017 15.8
 
-    if(WARNINGS_AS_ERRORS)
+    if(SWIFTSHADER_WARNINGS_AS_ERRORS)
         set_cpp_flag("/WX") # Treat all warnings as errors
     endif()
 
@@ -418,7 +432,7 @@
         set_cpp_flag("-fPIC")
     endif()
 
-    if(LESS_DEBUG_INFO)
+    if(SWIFTSHADER_LESS_DEBUG_INFO)
         # Use -g1 to be able to get stack traces
         set_cpp_flag("-g -g1" DEBUG)
         set_cpp_flag("-g -g1" RELWITHDEBINFO)
@@ -433,11 +447,11 @@
         set_cpp_flag("-s" RELEASE)
     endif()
 
-    if(WARNINGS_AS_ERRORS)
+    if(SWIFTSHADER_WARNINGS_AS_ERRORS)
         set_cpp_flag("-Werror") # Treat all warnings as errors
     endif()
 
-    if(DCHECK_ALWAYS_ON)
+    if(SWIFTSHADER_DCHECK_ALWAYS_ON)
         list(APPEND SWIFTSHADER_COMPILE_OPTIONS "-DDCHECK_ALWAYS_ON")
     endif()
 
@@ -473,16 +487,16 @@
     set_cpp_flag("-fdata-sections" RELEASE)
     set_cpp_flag("-fomit-frame-pointer" RELEASE)
 
-    if(MSAN)
+    if(SWIFTSHADER_MSAN)
         set_cpp_flag("-fsanitize=memory")
         set_linker_flag("-fsanitize=memory")
-    elseif(ASAN)
+    elseif(SWIFTSHADER_ASAN)
         set_cpp_flag("-fsanitize=address")
         set_linker_flag("-fsanitize=address")
-    elseif(TSAN)
+    elseif(SWIFTSHADER_TSAN)
         set_cpp_flag("-fsanitize=thread")
         set_linker_flag("-fsanitize=thread")
-    elseif(UBSAN)
+    elseif(SWIFTSHADER_UBSAN)
         set_cpp_flag("-fsanitize=undefined")
         set_linker_flag("-fsanitize=undefined")
     endif()
@@ -1798,7 +1812,7 @@
     ${CMAKE_CURRENT_SOURCE_DIR}/include/vulkan/*.h}
 )
 
-if(ENABLE_VULKAN_DEBUGGER)
+if(SWIFTSHADER_ENABLE_VULKAN_DEBUGGER)
     file(GLOB_RECURSE VULKAN_DEBUG_LIST
         ${VULKAN_DIR}/Debug/*.cpp
         ${VULKAN_DIR}/Debug/*.h
@@ -1806,7 +1820,7 @@
     )
     list(APPEND VULKAN_LIST ${VULKAN_DEBUG_LIST})
     list(APPEND SWIFTSHADER_COMPILE_OPTIONS "-DENABLE_VK_DEBUGGER")
-endif(ENABLE_VULKAN_DEBUGGER)
+endif(SWIFTSHADER_ENABLE_VULKAN_DEBUGGER)
 
 if(LINUX OR ANDROID)
     list(APPEND VULKAN_LIST
@@ -1984,7 +1998,7 @@
     set(LIB_PREFIX "lib")
 endif()
 
-if(BUILD_EGL)
+if(SWIFTSHADER_BUILD_EGL)
     add_library(libEGL SHARED ${EGL_LIST})
     set_target_properties(libEGL PROPERTIES
         INCLUDE_DIRECTORIES "${OPENGL_INCLUDE_DIR}"
@@ -2013,7 +2027,7 @@
     )
 endif()
 
-if(BUILD_GLESv2)
+if(SWIFTSHADER_BUILD_GLESv2)
     add_library(libGLESv2 SHARED ${GLES2_LIST})
     set_target_properties(libGLESv2 PROPERTIES
         INCLUDE_DIRECTORIES "${OPENGL_INCLUDE_DIR}"
@@ -2037,7 +2051,7 @@
     )
 endif()
 
-if(BUILD_GLES_CM)
+if(SWIFTSHADER_BUILD_GLES_CM)
     add_library(libGLES_CM SHARED ${GLES_CM_LIST})
     set_target_properties(libGLES_CM PROPERTIES
         INCLUDE_DIRECTORIES "${OPENGL_INCLUDE_DIR}"
@@ -2069,7 +2083,7 @@
     add_subdirectory(${CPPDAP_DIR})
 endif(BUILD_CPPDAP)
 
-if(BUILD_VULKAN)
+if(SWIFTSHADER_BUILD_VULKAN)
     if (NOT TARGET SPIRV-Tools)
         # This variable is also used by SPIRV-Tools to locate SPIRV-Headers
         set(SPIRV-Headers_SOURCE_DIR "${THIRD_PARTY_DIR}/SPIRV-Headers")
@@ -2107,9 +2121,9 @@
     set_shared_library_export_map(vk_swiftshader ${SOURCE_DIR}/Vulkan)
 
     set(VK_SWIFTSHADER_LIBS ${Reactor} marl ${OS_LIBS} SPIRV-Tools SPIRV-Tools-opt)
-    if(ENABLE_VULKAN_DEBUGGER)
+    if(SWIFTSHADER_ENABLE_VULKAN_DEBUGGER)
         list(APPEND VK_SWIFTSHADER_LIBS cppdap)
-    endif(ENABLE_VULKAN_DEBUGGER)
+    endif(SWIFTSHADER_ENABLE_VULKAN_DEBUGGER)
     target_link_libraries(vk_swiftshader ${VK_SWIFTSHADER_LIBS})
 
     add_custom_command(
@@ -2140,7 +2154,7 @@
 # Sample programs
 ###########################################################
 
-if(BUILD_SAMPLES)
+if(SWIFTSHADER_BUILD_SAMPLES)
     if(WIN32)
         add_executable(OGLES2HelloAPI ${HELLO2_DIR}/OGLES2HelloAPI_Windows.cpp)
         set_target_properties(OGLES2HelloAPI PROPERTIES
@@ -2178,7 +2192,8 @@
     endif()
 endif()
 
-if(BUILD_TESTS)
+
+if(SWIFTSHADER_BUILD_TESTS)
     set(REACTOR_UNIT_TESTS_LIST
         ${SOURCE_DIR}/Reactor/ReactorUnitTests.cpp
         ${THIRD_PARTY_DIR}/googletest/googletest/src/gtest-all.cc
@@ -2203,7 +2218,7 @@
     endif()
 endif()
 
-if(BUILD_TESTS)
+if(SWIFTSHADER_BUILD_TESTS)
     set(GLES_UNITTESTS_LIST
         ${CMAKE_CURRENT_SOURCE_DIR}/tests/GLESUnitTests/main.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/tests/GLESUnitTests/unittests.cpp
@@ -2229,9 +2244,9 @@
     if(ANDROID)
         target_link_libraries(gles-unittests -landroid)
     endif()
-endif(BUILD_TESTS)
+endif(SWIFTSHADER_BUILD_TESTS)
 
-if(BUILD_TESTS)
+if(SWIFTSHADER_BUILD_TESTS)
     # Math unit tests
     set(MATH_UNITTESTS_LIST
         ${CMAKE_CURRENT_SOURCE_DIR}/tests/MathUnitTests/main.cpp
@@ -2258,7 +2273,7 @@
     endif()
 endif()
 
-if(BUILD_TESTS AND BUILD_VULKAN)
+if(SWIFTSHADER_BUILD_TESTS AND SWIFTSHADER_BUILD_VULKAN)
     set(VK_UNITTESTS_LIST
         ${CMAKE_CURRENT_SOURCE_DIR}/tests/VulkanUnitTests/Device.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/tests/VulkanUnitTests/Driver.cpp
@@ -2286,4 +2301,4 @@
     )
 
     target_link_libraries(vk-unittests ${OS_LIBS} SPIRV-Tools)
-endif(BUILD_TESTS AND BUILD_VULKAN)
+endif(SWIFTSHADER_BUILD_TESTS AND SWIFTSHADER_BUILD_VULKAN)
diff --git a/tests/kokoro/macos/continuous.sh b/tests/kokoro/macos/continuous.sh
index 4233aec..3b6a664 100755
--- a/tests/kokoro/macos/continuous.sh
+++ b/tests/kokoro/macos/continuous.sh
@@ -22,7 +22,7 @@
   ASAN="OFF"
 fi
 
-cmake .. "-DASAN=${ASAN}" "-DCMAKE_BUILD_TYPE=${BUILD_TYPE}" "-DREACTOR_BACKEND=${REACTOR_BACKEND}" "-DREACTOR_VERIFY_LLVM_IR=1"
+cmake .. "-DSWIFTSHADER_ASAN=${ASAN}" "-DCMAKE_BUILD_TYPE=${BUILD_TYPE}" "-DREACTOR_BACKEND=${REACTOR_BACKEND}" "-DREACTOR_VERIFY_LLVM_IR=1"
 make -j$(sysctl -n hw.logicalcpu)
 
 # Run unit tests
@@ -31,4 +31,4 @@
 
 build/ReactorUnitTests
 build/gles-unittests
-build/vk-unittests
\ No newline at end of file
+build/vk-unittests
diff --git a/third_party/PowerVR_Examples b/third_party/PowerVR_Examples
new file mode 160000
index 0000000..c1605c9
--- /dev/null
+++ b/third_party/PowerVR_Examples
@@ -0,0 +1 @@
+Subproject commit c1605c99281797e5cd4c8439e1bc679706bbb311