Allow Linux build without X11 dependency

ChromeOS runs tests on some bots which run on Linux, but do not support X11.
In order to allow SwiftShader to successfully build on these bots, all X11
dependencies are #ifdefed behind the USE_X11 flag, which is already supplied
by the Chromium build system.

Change-Id: I6b914b1e662d9fbf101eb7caea7ac59e43cc7b56
Reviewed-on: https://swiftshader-review.googlesource.com/19488
Tested-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f526116..eb03feb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -174,6 +174,9 @@
         set_cpp_flag("-march=x86-64")
         set_cpp_flag("-mtune=generic")
     endif()
+    if(LINUX)
+        set_cpp_flag("-DUSE_X11=1")
+    endif()
 
     # Use -g3 to have even more debug info
     set_cpp_flag("-g -g3" DEBUG)
diff --git a/src/Main/BUILD.gn b/src/Main/BUILD.gn
index 3641a76..ee76067 100644
--- a/src/Main/BUILD.gn
+++ b/src/Main/BUILD.gn
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import("//ui/ozone/ozone.gni")
+import("//build/config/ui.gni")
 import("../swiftshader.gni")
 
 # Need a separate config to ensure the warnings are added to the end.
@@ -50,10 +50,12 @@
   if (use_ozone && !is_win) {
     sources += [ "FrameBufferOzone.cpp" ]
   } else if (is_linux) {
-    sources += [
-      "FrameBufferX11.cpp",
-      "libX11.cpp",
-    ]
+    if (use_x11) {
+      sources += [
+        "FrameBufferX11.cpp",
+        "libX11.cpp",
+      ]
+    }
   } else if (is_mac) {
     sources += [ "FrameBufferOSX.mm" ]
   } else if (is_win) {
diff --git a/src/OpenGL/libEGL/BUILD.gn b/src/OpenGL/libEGL/BUILD.gn
index 1694381..6c2d803 100644
--- a/src/OpenGL/libEGL/BUILD.gn
+++ b/src/OpenGL/libEGL/BUILD.gn
@@ -12,6 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import("//build/config/ui.gni")
 import("../../swiftshader.gni")
 
 # Need a separate config to ensure the warnings are added to the end.
@@ -70,7 +71,9 @@
   } else if (is_win) {
     ldflags = [ "/DEF:" + rebase_path("libGLESv2.def", root_build_dir) ]
   } else if (is_linux) {
-    sources += [ "../../Main/libX11.cpp" ]
+    if (use_x11) {
+      sources += [ "../../Main/libX11.cpp" ]
+    }
     ldflags =
         [ "-Wl,--version-script=" + rebase_path("libEGL.lds", root_build_dir) ]
   }
diff --git a/src/OpenGL/libEGL/Display.cpp b/src/OpenGL/libEGL/Display.cpp
index 1f6b37f..343e4ce 100644
--- a/src/OpenGL/libEGL/Display.cpp
+++ b/src/OpenGL/libEGL/Display.cpp
@@ -30,7 +30,7 @@
 #include <sys/ioctl.h>
 #include <linux/fb.h>
 #include <fcntl.h>
-#elif defined(__linux__)
+#elif defined(USE_X11)
 #include "Main/libX11.hpp"
 #elif defined(__APPLE__)
 #include "OSXUtils.hpp"
@@ -66,7 +66,7 @@
 
 	static void *nativeDisplay = nullptr;
 
-	#if defined(__linux__) && !defined(__ANDROID__)
+	#if defined(USE_X11)
 		// Even if the application provides a native display handle, we open (and close) our own connection
 		if(!nativeDisplay && dpy != HEADLESS_DISPLAY && libX11 && libX11->XOpenDisplay)
 		{
@@ -89,7 +89,7 @@
 {
 	terminate();
 
-	#if defined(__linux__) && !defined(__ANDROID__)
+	#if defined(USE_X11)
 		if(nativeDisplay && libX11->XCloseDisplay)
 		{
 			libX11->XCloseDisplay((::Display*)nativeDisplay);
@@ -677,7 +677,7 @@
 			return false;
 		}
 		return true;
-	#elif defined(__linux__)
+	#elif defined(USE_X11)
 		if(nativeDisplay)
 		{
 			XWindowAttributes windowAttributes;
@@ -686,6 +686,8 @@
 			return status != 0;
 		}
 		return false;
+	#elif defined(__linux__)
+		return false;  // Non X11 linux is headless only
 	#elif defined(__APPLE__)
 		return sw::OSX::IsValidWindow(window);
 	#elif defined(__Fuchsia__)
@@ -843,7 +845,7 @@
 
 		// No framebuffer device found, or we're in user space
 		return sw::FORMAT_X8B8G8R8;
-	#elif defined(__linux__)
+	#elif defined(USE_X11)
 		if(nativeDisplay)
 		{
 			Screen *screen = libX11->XDefaultScreenOfDisplay((::Display*)nativeDisplay);
@@ -861,6 +863,8 @@
 		{
 			return sw::FORMAT_X8R8G8B8;
 		}
+	#elif defined(__linux__)  // Non X11 linux is headless only
+		return sw::FORMAT_A8B8G8R8;
 	#elif defined(__APPLE__)
 		return sw::FORMAT_A8B8G8R8;
 	#elif defined(__Fuchsia__)
diff --git a/src/OpenGL/libEGL/Surface.cpp b/src/OpenGL/libEGL/Surface.cpp
index 237804b..b8bba5f 100644
--- a/src/OpenGL/libEGL/Surface.cpp
+++ b/src/OpenGL/libEGL/Surface.cpp
@@ -26,7 +26,7 @@
 #include "common/debug.h"
 #include "Main/FrameBuffer.hpp"
 
-#if defined(__linux__) && !defined(__ANDROID__)
+#if defined(USE_X11)
 #include "Main/libX11.hpp"
 #elif defined(_WIN32)
 #include <tchar.h>
@@ -341,7 +341,7 @@
 	#elif defined(__ANDROID__)
 		int windowWidth;  window->query(window, NATIVE_WINDOW_WIDTH, &windowWidth);
 		int windowHeight; window->query(window, NATIVE_WINDOW_HEIGHT, &windowHeight);
-	#elif defined(__linux__)
+	#elif defined(USE_X11)
 		XWindowAttributes windowAttributes;
 		Status status = libX11->XGetWindowAttributes((::Display*)display->getNativeDisplay(), window, &windowAttributes);
 
@@ -352,6 +352,10 @@
 
 		int windowWidth = windowAttributes.width;
 		int windowHeight = windowAttributes.height;
+	#elif defined(__linux__)
+		// Non X11 linux is headless only
+		int windowWidth = 100;
+		int windowHeight = 100;
 	#elif defined(__APPLE__)
 		int windowWidth;
 		int windowHeight;
diff --git a/src/OpenGL/libEGL/libEGL.cpp b/src/OpenGL/libEGL/libEGL.cpp
index 8e0be66..952fb88 100644
--- a/src/OpenGL/libEGL/libEGL.cpp
+++ b/src/OpenGL/libEGL/libEGL.cpp
@@ -25,7 +25,7 @@
 
 #if defined(__ANDROID__)
 #include <system/window.h>
-#elif defined(__linux__)
+#elif defined(USE_X11)
 #include "Main/libX11.hpp"
 #endif
 
@@ -120,7 +120,9 @@
 	}
 
 	#if defined(__linux__) && !defined(__ANDROID__)
+		#if defined(USE_X11)
 		if(!libX11)
+		#endif  // Non X11 linux is headless only
 		{
 			return success(HEADLESS_DISPLAY);
 		}
@@ -178,6 +180,8 @@
 			"EGL_KHR_client_get_all_proc_addresses "
 #if defined(__linux__) && !defined(__ANDROID__)
 			"EGL_KHR_platform_gbm "
+#endif
+#if defined(USE_X11)
 			"EGL_KHR_platform_x11 "
 #endif
 			"EGL_EXT_client_extensions "
@@ -1002,7 +1006,7 @@
 
 	if(context)
 	{
-		#if defined(__linux__) && !defined(__ANDROID__)
+		#if defined(USE_X11)
 			egl::Display *display = context->getDisplay();
 
 			if(!display)
@@ -1171,13 +1175,25 @@
 	#if defined(__linux__) && !defined(__ANDROID__)
 		switch(platform)
 		{
+		#if defined(USE_X11)
 		case EGL_PLATFORM_X11_EXT: break;
+		#endif
 		case EGL_PLATFORM_GBM_KHR: break;
 		default:
 			return error(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
 		}
 
-		if(platform == EGL_PLATFORM_X11_EXT)
+		if(platform == EGL_PLATFORM_GBM_KHR)
+		{
+			if(native_display != (void*)EGL_DEFAULT_DISPLAY || attrib_list != NULL)
+			{
+				return error(EGL_BAD_ATTRIBUTE, EGL_NO_DISPLAY);   // Unimplemented
+			}
+
+			return success(HEADLESS_DISPLAY);
+		}
+		#if defined(USE_X11)
+		else if(platform == EGL_PLATFORM_X11_EXT)
 		{
 			if(!libX11)
 			{
@@ -1189,15 +1205,7 @@
 				return error(EGL_BAD_ATTRIBUTE, EGL_NO_DISPLAY);   // Unimplemented
 			}
 		}
-		else if(platform == EGL_PLATFORM_GBM_KHR)
-		{
-			if(native_display != (void*)EGL_DEFAULT_DISPLAY || attrib_list != NULL)
-			{
-				return error(EGL_BAD_ATTRIBUTE, EGL_NO_DISPLAY);   // Unimplemented
-			}
-
-			return success(HEADLESS_DISPLAY);
-		}
+		#endif
 
 		return success(PRIMARY_DISPLAY);   // We only support the default display
 	#else