CMake: add graphviz generation scripts

Script runs a cmake command to generate and open a png with a dependency
graph of currently enabled SwiftShader targets. Note that you should use
CMake 3.17 to get the best looking graph, as many fixes and improvements
were made to the graphviz support in 3.17.

Bug: none
Change-Id: Ie84eb18377b15f823bcdfe34612bf532502adf48
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/43748
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Tested-by: Antonio Maiorano <amaiorano@google.com>
diff --git a/tools/cmake_generate_graphviz/CMakeGraphVizOptions.cmake b/tools/cmake_generate_graphviz/CMakeGraphVizOptions.cmake
new file mode 100644
index 0000000..9555183
--- /dev/null
+++ b/tools/cmake_generate_graphviz/CMakeGraphVizOptions.cmake
@@ -0,0 +1,52 @@
+# 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 te 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.
+
+# This file sets configuration values for CMake's GraphViz generation.
+# See https://cmake.org/cmake/help/latest/module/CMakeGraphVizOptions.html
+
+# Defaults
+set(GRAPHVIZ_EXECUTABLES TRUE)
+set(GRAPHVIZ_STATIC_LIBS TRUE)
+set(GRAPHVIZ_SHARED_LIBS TRUE)
+set(GRAPHVIZ_MODULE_LIBS TRUE)
+set(GRAPHVIZ_INTERFACE_LIBS TRUE)
+set(GRAPHVIZ_OBJECT_LIBS TRUE)
+set(GRAPHVIZ_UNKNOWN_LIBS TRUE)
+set(GRAPHVIZ_CUSTOM_TARGETS TRUE)
+
+# Below are non-defaults
+
+# Render larger and with a nicer font. Default clips bottom of some characters.
+set(GRAPHVIZ_GRAPH_HEADER "node [ fontsize=16; fontname=Helvetica ];")
+
+# Don't show external lib targets
+set(GRAPHVIZ_EXTERNAL_LIBS FALSE)
+
+# Don't generate per-target dot files
+set(GRAPHVIZ_GENERATE_PER_TARGET FALSE)
+
+# Don't generate per-target depender dot files
+set(GRAPHVIZ_GENERATE_DEPENDERS FALSE)
+
+# List of targets to ignore
+set(GRAPHVIZ_IGNORE_TARGETS
+    core_tables
+    enum_string_mapping
+    extinst_tables
+    gmock_main
+    gtest_main
+    spirv-*
+    SPIRV-Tools-*
+    spv-*
+)
diff --git a/tools/cmake_generate_graphviz/cmake_generate_graphviz.bat b/tools/cmake_generate_graphviz/cmake_generate_graphviz.bat
new file mode 100644
index 0000000..df03cc3
--- /dev/null
+++ b/tools/cmake_generate_graphviz/cmake_generate_graphviz.bat
@@ -0,0 +1,40 @@
+@echo on
+setlocal enabledelayedexpansion
+pushd %~dp0
+
+where /q cmake.exe
+if %errorlevel% neq 0 (
+    echo "CMake not found. Please install it from https://cmake.org/"
+    exit /b 1
+)
+
+where /q dot.exe
+if %errorlevel% neq 0 (
+    echo "GraphViz (dot.exe) not found. Please install it from https://graphviz.gitlab.io/"
+    exit /b 1
+)
+
+set cmake_binary_dir=%1
+
+if "%cmake_binary_dir%" == "" (
+    set cmake_binary_dir=..\..\build
+)
+
+rem Copy options to binary dir
+copy /y CMakeGraphVizOptions.cmake "%cmake_binary_dir%\"
+if %errorlevel% neq 0 exit /b %errorlevel%
+
+rem Run cmake commands from the binary dir
+pushd %cmake_binary_dir%
+
+cmake --graphviz=SwiftShader.dot ..
+if %errorlevel% neq 0 exit /b %errorlevel%
+
+dot -Tpng -o SwiftShader.png SwiftShader.dot
+if %errorlevel% neq 0 exit /b %errorlevel%
+
+rem Open the file
+start SwiftShader.png
+
+popd
+popd
diff --git a/tools/cmake_generate_graphviz/cmake_generate_graphviz.sh b/tools/cmake_generate_graphviz/cmake_generate_graphviz.sh
new file mode 100644
index 0000000..e6e9178
--- /dev/null
+++ b/tools/cmake_generate_graphviz/cmake_generate_graphviz.sh
@@ -0,0 +1,38 @@
+#!/bin/bash
+
+set -e # Fail on any error.
+set -x # Display commands being run.
+
+pushd `dirname $0`
+
+if ! [ -x "$(command -v cmake)" ]; then
+  echo 'cmake is not found. Please install it (e.g. sudo apt install cmake)' >&2
+  exit 1
+fi
+
+if ! [ -x "$(command -v dot)" ]; then
+  echo 'graphviz (dot) is not found. Please install it (e.g. sudo apt install graphviz)' >&2
+  exit 1
+fi
+
+cmake_binary_dir=$1
+
+if [[ -z "${cmake_binary_dir}" ]]; then
+  cmake_binary_dir="../../build"
+fi
+
+cp ./CMakeGraphVizOptions.cmake ${cmake_binary_dir}/
+
+pushd ${cmake_binary_dir}
+
+cmake --graphviz=SwiftShader.dot ..
+dot -Tpng -o SwiftShader.png SwiftShader.dot
+
+if [ "$(uname)" == "Darwin" ]; then
+  open SwiftShader.png
+else
+  xdg-open SwiftShader.png &>/dev/null &
+fi
+
+popd
+popd