BUILD.gn: Only expose the GLFW target on platforms that support it

Due to the way GN target discovery works, glfw would get discovered in
Fuchsia / Android / ChromeOS builds when it isn't supported causing
compilation failures. This changes third_party/BUILD.gn so that glfw
targets are only created on supported platforms. It also changes
dawn_glfw in BUILD.gn to be more robust to building on all platforms.

Bug=dawn:221
BUG=chromium:1002895

Change-Id: I8f40b06f680094406d24e9a6dea44b128e59b854
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/11160
Reviewed-by: David Turner <digit@google.com>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 0a3fe45..64d1cfe 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -580,10 +580,17 @@
 # GLFW wrapping target
 ###############################################################################
 
-# GLFW does not support Android or Fuchsia, so provide a small mock library
-# that can be linked into the Dawn tests on these platforms. Otherwise, use
-# the real library from third_party/.
-if (is_fuchsia) {
+# GLFW does not support ChromeOS, Android or Fuchsia, so provide a small mock
+# library that can be linked into the Dawn tests on these platforms. Otherwise,
+# use the real library from third_party/.
+if (is_win || is_linux || is_mac) {
+  group("dawn_glfw") {
+    public_deps = [
+      "third_party:glfw",
+    ]
+  }
+} else if (is_fuchsia) {
+  # The mock implementation of GLFW on Fuchsia
   config("dawn_glfw_public_config") {
     # Allow inclusion of <GLFW/glfw3.h>
     configs = [ "third_party:glfw_public" ]
@@ -610,10 +617,8 @@
     ]
   }
 } else {
+  # Just skip GLFW on other systems
   group("dawn_glfw") {
-    public_deps = [
-      "third_party:glfw",
-    ]
   }
 }
 
@@ -815,7 +820,6 @@
   testonly = true
 
   deps = [
-    ":dawn_glfw",
     ":dawn_utils",
     ":libdawn_native",
     ":libdawn_wire",
diff --git a/third_party/BUILD.gn b/third_party/BUILD.gn
index 53e56db..d45df8e 100644
--- a/third_party/BUILD.gn
+++ b/third_party/BUILD.gn
@@ -231,44 +231,44 @@
 # GLFW - good enough build targets
 ###############################################################################
 
-glfw_dir = dawn_glfw_dir
+# Only expose GLFW targets on platforms where GLFW is supported: otherwise they
+# might get discovered by GN when another target in this file is referenced,
+# and GLFW will bbe built as part of "all" builds, causing compilation failures.
+if (is_win || is_linux || is_mac) {
+  glfw_dir = dawn_glfw_dir
 
-config("glfw_public") {
-  include_dirs = [ "${glfw_dir}/include" ]
+  config("glfw_public") {
+    include_dirs = [ "${glfw_dir}/include" ]
 
-  if (is_win) {
-    defines = [ "_GLFW_WIN32" ]
+    if (is_win) {
+      defines = [ "_GLFW_WIN32" ]
+    }
+
+    if (is_mac) {
+      defines = [ "_GLFW_COCOA" ]
+    }
+
+    if (is_linux) {
+      defines = [ "_GLFW_X11" ]
+    }
   }
 
-  if (is_mac) {
-    defines = [ "_GLFW_COCOA" ]
-  }
+  static_library("glfw") {
+    public_configs = [ ":glfw_public" ]
 
-  if (is_linux) {
-    defines = [ "_GLFW_X11" ]
-  }
-}
+    configs -= [ "//build/config/compiler:chromium_code" ]
+    configs += [ "//build/config/compiler:no_chromium_code" ]
 
-static_library("glfw") {
-  public_configs = [ ":glfw_public" ]
+    if (is_msvc) {
+      # nonstandard extension, function/data pointer conversion in expression
+      cflags_c = [ "/wd4152" ]
+    } else {
+      cflags_c = [
+        "-Wno-sign-compare",
+        "-Wno-missing-field-initializers",
+      ]
+    }
 
-  configs -= [ "//build/config/compiler:chromium_code" ]
-  configs += [ "//build/config/compiler:no_chromium_code" ]
-
-  if (is_msvc) {
-    # nonstandard extension, function/data pointer conversion in expression
-    cflags_c = [ "/wd4152" ]
-  } else {
-    cflags_c = [
-      "-Wno-sign-compare",
-      "-Wno-missing-field-initializers",
-    ]
-  }
-
-  # NOTE: For some unknown reason, this library gets build when is_fuchsia
-  # is true, even if absolutely nothing references it, as checked with
-  # `gn refs out/Fuchsia third_party/dawn/third_party:glfw --all`
-  if (!is_fuchsia) {
     sources = [
       "${glfw_dir}/include/GLFW/glfw3.h",
       "${glfw_dir}/include/GLFW/glfw3native.h",
@@ -284,79 +284,79 @@
       "${glfw_dir}/src/vulkan.c",
       "${glfw_dir}/src/window.c",
     ]
-  }
-  libs = []
+    libs = []
 
-  if (is_win) {
-    sources += [
-      "${glfw_dir}/src/wgl_context.c",
-      "${glfw_dir}/src/wgl_context.h",
-      "${glfw_dir}/src/win32_init.c",
-      "${glfw_dir}/src/win32_joystick.c",
-      "${glfw_dir}/src/win32_joystick.h",
-      "${glfw_dir}/src/win32_monitor.c",
-      "${glfw_dir}/src/win32_platform.h",
-      "${glfw_dir}/src/win32_thread.c",
-      "${glfw_dir}/src/win32_time.c",
-      "${glfw_dir}/src/win32_window.c",
-    ]
-  }
+    if (is_win) {
+      sources += [
+        "${glfw_dir}/src/wgl_context.c",
+        "${glfw_dir}/src/wgl_context.h",
+        "${glfw_dir}/src/win32_init.c",
+        "${glfw_dir}/src/win32_joystick.c",
+        "${glfw_dir}/src/win32_joystick.h",
+        "${glfw_dir}/src/win32_monitor.c",
+        "${glfw_dir}/src/win32_platform.h",
+        "${glfw_dir}/src/win32_thread.c",
+        "${glfw_dir}/src/win32_time.c",
+        "${glfw_dir}/src/win32_window.c",
+      ]
+    }
 
-  if (is_linux || is_mac) {
-    sources += [
-      "${glfw_dir}/src/posix_thread.c",
-      "${glfw_dir}/src/posix_thread.h",
-    ]
-  }
+    if (is_linux || is_mac) {
+      sources += [
+        "${glfw_dir}/src/posix_thread.c",
+        "${glfw_dir}/src/posix_thread.h",
+      ]
+    }
 
-  if (is_linux) {
-    sources += [
-      "${glfw_dir}/src/glx_context.c",
-      "${glfw_dir}/src/glx_context.h",
-      "${glfw_dir}/src/linux_joystick.c",
-      "${glfw_dir}/src/linux_joystick.h",
-      "${glfw_dir}/src/posix_time.c",
-      "${glfw_dir}/src/posix_time.h",
-      "${glfw_dir}/src/x11_init.c",
-      "${glfw_dir}/src/x11_monitor.c",
-      "${glfw_dir}/src/x11_platform.h",
-      "${glfw_dir}/src/x11_window.c",
-      "${glfw_dir}/src/xkb_unicode.c",
-      "${glfw_dir}/src/xkb_unicode.h",
-    ]
+    if (is_linux) {
+      sources += [
+        "${glfw_dir}/src/glx_context.c",
+        "${glfw_dir}/src/glx_context.h",
+        "${glfw_dir}/src/linux_joystick.c",
+        "${glfw_dir}/src/linux_joystick.h",
+        "${glfw_dir}/src/posix_time.c",
+        "${glfw_dir}/src/posix_time.h",
+        "${glfw_dir}/src/x11_init.c",
+        "${glfw_dir}/src/x11_monitor.c",
+        "${glfw_dir}/src/x11_platform.h",
+        "${glfw_dir}/src/x11_window.c",
+        "${glfw_dir}/src/xkb_unicode.c",
+        "${glfw_dir}/src/xkb_unicode.h",
+      ]
 
-    libs += [
-      "rt",
-      "dl",
-      "X11",
-      "Xcursor",
-      "Xinerama",
-      "Xrandr",
-    ]
-  }
+      libs += [
+        "rt",
+        "dl",
+        "X11",
+        "Xcursor",
+        "Xinerama",
+        "Xrandr",
+      ]
+    }
 
-  if (is_mac) {
-    sources += [
-      "${glfw_dir}/src/cocoa_init.m",
-      "${glfw_dir}/src/cocoa_joystick.h",
-      "${glfw_dir}/src/cocoa_joystick.m",
-      "${glfw_dir}/src/cocoa_monitor.m",
-      "${glfw_dir}/src/cocoa_platform.h",
-      "${glfw_dir}/src/cocoa_time.c",
-      "${glfw_dir}/src/cocoa_window.m",
-      "${glfw_dir}/src/nsgl_context.h",
-      "${glfw_dir}/src/nsgl_context.m",
-    ]
-    libs += [
-      "Cocoa.framework",
-      "IOKit.framework",
-      "CoreFoundation.framework",
-      "CoreVideo.framework",
-    ]
-    cflags_objc = [
-      "-Wno-sign-compare",
-      "-Wno-unguarded-availability",
-    ]
+    if (is_mac) {
+      sources += [
+        "${glfw_dir}/src/cocoa_init.m",
+        "${glfw_dir}/src/cocoa_joystick.h",
+        "${glfw_dir}/src/cocoa_joystick.m",
+        "${glfw_dir}/src/cocoa_monitor.m",
+        "${glfw_dir}/src/cocoa_platform.h",
+        "${glfw_dir}/src/cocoa_time.c",
+        "${glfw_dir}/src/cocoa_window.m",
+        "${glfw_dir}/src/nsgl_context.h",
+        "${glfw_dir}/src/nsgl_context.m",
+      ]
+      libs += [
+        "Cocoa.framework",
+        "IOKit.framework",
+        "CoreFoundation.framework",
+        "CoreVideo.framework",
+      ]
+      cflags_objc = [
+        "-Wno-sign-compare",
+        "-Wno-unguarded-availability",
+      ]
+    }
   }
 }