Config changes to support target_os="fuchsia"

Fuchsia support has been upstreamed into Clang, so the existing
Linux-hosted Clang toolchain binaries in third_party with the correct
--target are sufficient.  Fuchsia is POSIXy so a lot of things will just
work, but some places will require different implementations, so this CL
adds support for is_fuchsia as well as a basic implementation of the
toolchain config.

The SDK (headers, sysroot, etc.) is currently available at
go/fuchsia-sdk until a standard location for it is decided upon.

The near term goal behind this CL is to bring up base/ and net/ on an
fyi bot running Fuchsia.

BUG=706592

Review-Url: https://codereview.chromium.org/2784063002
Cr-Commit-Position: refs/heads/master@{#471360}
diff --git a/BUILD.gn b/BUILD.gn
index 0687919..b5fedfe 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -69,14 +69,9 @@
     ":gn_visibility",
     "//base:base_unittests",
     "//chrome/installer",
-    "//components:components_unittests",
     "//net:net_unittests",
-    "//skia:skia_unittests",
     "//sql:sql_unittests",
     "//tools/ipc_fuzzer:ipc_fuzzer_all",
-    "//tools/metrics:metrics_metadata",
-    "//ui/base:ui_base_unittests",
-    "//ui/gfx:gfx_unittests",
     "//url:url_unittests",
   ]
 
@@ -87,7 +82,17 @@
     ]
   }
 
-  if (!is_ios && !is_android && !is_chromecast) {
+  if (!is_fuchsia) {
+    deps += [
+      "//components:components_unittests",
+      "//skia:skia_unittests",
+      "//tools/metrics:metrics_metadata",
+      "//ui/base:ui_base_unittests",
+      "//ui/gfx:gfx_unittests",
+    ]
+  }
+
+  if (!is_ios && !is_android && !is_chromecast && !is_fuchsia) {
     deps += [
       "//chrome",
       "//chrome/test:browser_tests",
@@ -146,7 +151,7 @@
     ]
   }
 
-  if (!is_ios) {
+  if (!is_ios && !is_fuchsia) {
     deps += [
       "//cc:cc_unittests",
       "//chrome/test:telemetry_perf_unittests",
@@ -193,7 +198,7 @@
       "//url/ipc:url_ipc_unittests",
       "//v8:gn_all",
     ]
-  } else {
+  } else if (is_ios) {
     deps += [ "//ios:all" ]
   }
 
@@ -397,7 +402,7 @@
 
   # TODO(GYP): Figure out which of these should (and can) build
   # for chromeos/ios.
-  if (!is_chromeos && !is_ios) {
+  if (!is_chromeos && !is_ios && !is_fuchsia) {
     deps += [
       "//base:build_utf8_validator_tables",
       "//base:check_example",
@@ -574,11 +579,11 @@
     ]
   }
 
-  if (!is_android && !is_ios) {
+  if (!is_android && !is_ios && !is_fuchsia) {
     deps += [ "//content/browser/bluetooth/tools:bluetooth_metrics_hash" ]
   }
 
-  if (!is_android && !is_ios && !is_chromeos) {
+  if (!is_android && !is_ios && !is_chromeos && !is_fuchsia) {
     deps += [ "//components/proximity_auth:proximity_auth_unittests" ]
   }
 
@@ -812,7 +817,7 @@
   }
 }
 
-if (!is_ios) {
+if (!is_ios && !is_fuchsia) {
   # This group includes all of the targets needed to build and test Blink,
   # including running the layout tests (see below).
   group("blink_tests") {
@@ -942,7 +947,7 @@
 group("chromium_builder_perf") {
   testonly = true
 
-  if (!is_ios && !is_android && !is_chromecast) {
+  if (!is_ios && !is_android && !is_chromecast && !is_fuchsia) {
     data_deps = [
       "//cc:cc_perftests",
       "//chrome/test:load_library_perf_tests",
@@ -982,7 +987,7 @@
   }
 }
 
-if (!is_ios && !is_android && !is_chromecast) {
+if (!is_ios && !is_android && !is_chromecast && !is_fuchsia) {
   group("chromium_builder_asan") {
     testonly = true
 
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index faaa672..a320354 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -369,6 +369,7 @@
     'OS_CAT',       # For testing.
     'OS_CHROMEOS',
     'OS_FREEBSD',
+    'OS_FUCHSIA',
     'OS_IOS',
     'OS_LINUX',
     'OS_MACOSX',
diff --git a/build/build_config.h b/build/build_config.h
index a672380..0906419 100644
--- a/build/build_config.h
+++ b/build/build_config.h
@@ -49,6 +49,8 @@
 #endif
 #elif defined(_WIN32)
 #define OS_WIN 1
+#elif defined(__Fuchsia__)
+#define OS_FUCHSIA 1
 #elif defined(__FreeBSD__)
 #define OS_FREEBSD 1
 #elif defined(__NetBSD__)
@@ -77,10 +79,10 @@
 
 // For access to standard POSIXish features, use OS_POSIX instead of a
 // more specific macro.
-#if defined(OS_AIX) || defined(OS_ANDROID) || defined(OS_FREEBSD) || \
-    defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_NACL) ||   \
-    defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_QNX) ||  \
-    defined(OS_SOLARIS)
+#if defined(OS_AIX) || defined(OS_ANDROID) || defined(OS_FREEBSD) ||  \
+    defined(OS_FUCHSIA) || defined(OS_LINUX) || defined(OS_MACOSX) || \
+    defined(OS_NACL) || defined(OS_NETBSD) || defined(OS_OPENBSD) ||  \
+    defined(OS_QNX) || defined(OS_SOLARIS)
 #define OS_POSIX 1
 #endif
 
@@ -165,12 +167,10 @@
 // Type detection for wchar_t.
 #if defined(OS_WIN)
 #define WCHAR_T_IS_UTF16
-#elif defined(OS_POSIX) && defined(COMPILER_GCC) && \
-    defined(__WCHAR_MAX__) && \
+#elif defined(OS_POSIX) && defined(COMPILER_GCC) && defined(__WCHAR_MAX__) && \
     (__WCHAR_MAX__ == 0x7fffffff || __WCHAR_MAX__ == 0xffffffff)
 #define WCHAR_T_IS_UTF32
-#elif defined(OS_POSIX) && defined(COMPILER_GCC) && \
-    defined(__WCHAR_MAX__) && \
+#elif defined(OS_POSIX) && defined(COMPILER_GCC) && defined(__WCHAR_MAX__) && \
     (__WCHAR_MAX__ == 0x7fff || __WCHAR_MAX__ == 0xffff)
 // On Posix, we'll detect short wchar_t, but projects aren't guaranteed to
 // compile in this mode (in particular, Chrome doesn't). This is intended for
diff --git a/build/config/BUILDCONFIG.gn b/build/config/BUILDCONFIG.gn
index 4dddecd..ba1e5c9 100644
--- a/build/config/BUILDCONFIG.gn
+++ b/build/config/BUILDCONFIG.gn
@@ -137,6 +137,7 @@
   # to configure warnings.
   is_clang =
       current_os == "mac" || current_os == "ios" || current_os == "chromeos" ||
+      current_os == "fuchsia" ||
       (current_os == "linux" && current_cpu != "s390x" &&
        current_cpu != "s390" && current_cpu != "ppc64" && current_cpu != "ppc")
 
@@ -163,7 +164,8 @@
   #
   # For more information see
   # https://chromium.googlesource.com/chromium/src/+/master/docs/component_build.md
-  is_component_build = is_debug && current_os != "ios"
+  is_component_build =
+      is_debug && current_os != "ios" && current_os != "fuchsia"
 }
 
 assert(!(is_debug && is_official_build), "Can't do official debug builds")
@@ -233,6 +235,8 @@
   } else {
     _default_toolchain = "//build/toolchain/linux:$target_cpu"
   }
+} else if (target_os == "fuchsia") {
+  _default_toolchain = "//build/toolchain/fuchsia:$target_cpu"
 } else if (target_os == "ios") {
   _default_toolchain = "//build/toolchain/mac:ios_clang_$target_cpu"
 } else if (target_os == "mac") {
@@ -285,6 +289,7 @@
     current_os == "winrt_81_phone" || current_os == "winrt_10") {
   is_android = false
   is_chromeos = false
+  is_fuchsia = false
   is_ios = false
   is_linux = false
   is_mac = false
@@ -294,6 +299,7 @@
 } else if (current_os == "mac") {
   is_android = false
   is_chromeos = false
+  is_fuchsia = false
   is_ios = false
   is_linux = false
   is_mac = true
@@ -303,6 +309,7 @@
 } else if (current_os == "android") {
   is_android = true
   is_chromeos = false
+  is_fuchsia = false
   is_ios = false
   is_linux = false
   is_mac = false
@@ -312,6 +319,7 @@
 } else if (current_os == "chromeos") {
   is_android = false
   is_chromeos = true
+  is_fuchsia = false
   is_ios = false
   is_linux = true
   is_mac = false
@@ -324,15 +332,27 @@
   # Posix variant.
   is_android = false
   is_chromeos = false
+  is_fuchsia = false
   is_ios = false
   is_linux = false
   is_mac = false
   is_nacl = true
   is_posix = true
   is_win = false
+} else if (current_os == "fuchsia") {
+  is_android = false
+  is_chromeos = false
+  is_fuchsia = true
+  is_ios = false
+  is_linux = false
+  is_mac = false
+  is_nacl = false
+  is_posix = true
+  is_win = false
 } else if (current_os == "ios") {
   is_android = false
   is_chromeos = false
+  is_fuchsia = false
   is_ios = true
   is_linux = false
   is_mac = false
@@ -342,6 +362,7 @@
 } else if (current_os == "linux") {
   is_android = false
   is_chromeos = false
+  is_fuchsia = false
   is_ios = false
   is_linux = true
   is_mac = false
diff --git a/build/config/allocator.gni b/build/config/allocator.gni
index 6c3b48f..09f5ce8 100644
--- a/build/config/allocator.gni
+++ b/build/config/allocator.gni
@@ -6,7 +6,7 @@
 
 # Temporarily disable tcmalloc on arm64 linux to get rid of compilation errors.
 if (is_android || current_cpu == "mipsel" || is_mac || is_ios || is_asan ||
-    is_lsan || is_tsan || is_msan || is_win || is_syzyasan ||
+    is_lsan || is_tsan || is_msan || is_win || is_syzyasan || is_fuchsia ||
     (is_linux && target_cpu == "arm64")) {
   _default_allocator = "none"
 } else {
diff --git a/build/config/compiler/BUILD.gn b/build/config/compiler/BUILD.gn
index 5961d2f..75bf5eff 100644
--- a/build/config/compiler/BUILD.gn
+++ b/build/config/compiler/BUILD.gn
@@ -168,6 +168,8 @@
     configs += [ "//build/config/mac:compiler" ]
   } else if (is_ios) {
     configs += [ "//build/config/ios:compiler" ]
+  } else if (is_fuchsia) {
+    configs += [ "//build/config/fuchsia:compiler" ]
   } else if (current_os == "aix") {
     configs += [ "//build/config/aix:compiler" ]
   }
@@ -308,7 +310,7 @@
 
   # Linux/Android common flags setup.
   # ---------------------------------
-  if (is_linux || is_android) {
+  if (is_linux || is_android || is_fuchsia) {
     if (use_pic) {
       cflags += [ "-fPIC" ]
       ldflags += [ "-fPIC" ]
diff --git a/build/config/compiler/compiler.gni b/build/config/compiler/compiler.gni
index d1bcc4c..46b048f 100644
--- a/build/config/compiler/compiler.gni
+++ b/build/config/compiler/compiler.gni
@@ -121,12 +121,14 @@
 
 declare_args() {
   # Whether to use the gold linker from binutils instead of lld or bfd.
-  use_gold = !use_lld && !(is_chromecast && is_linux &&
-                           (current_cpu == "arm" || current_cpu == "mipsel")) &&
-             ((is_linux && (current_cpu == "x64" || current_cpu == "x86" ||
-                            current_cpu == "arm" || current_cpu == "mipsel")) ||
-              (is_android && (current_cpu == "x86" || current_cpu == "x64" ||
-                              current_cpu == "arm" || current_cpu == "arm64")))
+  use_gold =
+      !use_lld && !(is_chromecast && is_linux &&
+                    (current_cpu == "arm" || current_cpu == "mipsel")) &&
+      ((is_linux && (current_cpu == "x64" || current_cpu == "x86" ||
+                     current_cpu == "arm" || current_cpu == "mipsel")) ||
+       (is_android && (current_cpu == "x86" || current_cpu == "x64" ||
+                       current_cpu == "arm" || current_cpu == "arm64")) ||
+       is_fuchsia)
 }
 
 # If it wasn't manually set, set to an appropriate default.
diff --git a/build/config/fuchsia/BUILD.gn b/build/config/fuchsia/BUILD.gn
new file mode 100644
index 0000000..9ec7b11
--- /dev/null
+++ b/build/config/fuchsia/BUILD.gn
@@ -0,0 +1,30 @@
+# Copyright 2017 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import("//build/config/sysroot.gni")
+
+assert(is_fuchsia)
+assert(is_posix)
+
+config("compiler") {
+  defines = [ "SYSROOT_VERSION=$sysroot_version" ]
+  cflags = []
+  ldflags = []
+  if (current_cpu == "arm64") {
+    cflags += [ "--target=aarch64-fuchsia" ]
+    ldflags += [ "--target=aarch64-fuchsia" ]
+  } else if (current_cpu == "x64") {
+    cflags += [ "--target=x86_64-fuchsia" ]
+    ldflags += [ "--target=x86_64-fuchsia" ]
+  } else {
+    assert(false, "Unsupported architecture")
+  }
+  asmflags = cflags
+
+  libs = [
+    "mxio",
+    "magenta",
+    "unwind",
+  ]
+}
diff --git a/build/config/fuchsia/OWNERS b/build/config/fuchsia/OWNERS
new file mode 100644
index 0000000..3f809e82
--- /dev/null
+++ b/build/config/fuchsia/OWNERS
@@ -0,0 +1 @@
[email protected]
diff --git a/build/config/fuchsia/config.gni b/build/config/fuchsia/config.gni
new file mode 100644
index 0000000..d1d1104
--- /dev/null
+++ b/build/config/fuchsia/config.gni
@@ -0,0 +1,10 @@
+# Copyright 2017 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+assert(current_os == "fuchsia")
+
+declare_args() {
+  # Path to Fuchsia SDK.
+  fuchsia_sdk = "//third_party/fuchsia-sdk"
+}
diff --git a/build/config/sysroot.gni b/build/config/sysroot.gni
index 2c26c34..51f106e 100644
--- a/build/config/sysroot.gni
+++ b/build/config/sysroot.gni
@@ -75,6 +75,17 @@
 } else if (is_ios) {
   import("//build/config/ios/ios_sdk.gni")
   sysroot = ios_sdk_path
+} else if (is_fuchsia) {
+  import("//build/config/fuchsia/config.gni")
+  if (current_cpu == "arm64") {
+    sysroot = fuchsia_sdk + "/sysroot/aarch64-fuchsia"
+  } else if (current_cpu == "x64") {
+    sysroot = fuchsia_sdk + "/sysroot/x86_64-fuchsia"
+  } else {
+    sysroot = ""
+  }
+  sysroot_stamp = rebase_path("$sysroot/.stamp")
+  sysroot_version = read_file(sysroot_stamp, "trim string")
 } else {
   sysroot = ""
 }
diff --git a/build/toolchain/fuchsia/BUILD.gn b/build/toolchain/fuchsia/BUILD.gn
new file mode 100644
index 0000000..ec94b982
--- /dev/null
+++ b/build/toolchain/fuchsia/BUILD.gn
@@ -0,0 +1,16 @@
+# Copyright 2017 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import("//build/toolchain/gcc_toolchain.gni")
+import("//build/config/fuchsia/config.gni")
+
+clang_toolchain("x64") {
+  assert(current_cpu == "x64")
+  assert(host_os == "linux")
+
+  toolchain_args = {
+    current_cpu = "x64"
+    current_os = "fuchsia"
+  }
+}
diff --git a/build/toolchain/fuchsia/OWNERS b/build/toolchain/fuchsia/OWNERS
new file mode 100644
index 0000000..3f809e82
--- /dev/null
+++ b/build/toolchain/fuchsia/OWNERS
@@ -0,0 +1 @@
[email protected]
diff --git a/extensions/features/features.gni b/extensions/features/features.gni
index e469c79..6836e4a 100644
--- a/extensions/features/features.gni
+++ b/extensions/features/features.gni
@@ -5,7 +5,7 @@
 import("//build/config/features.gni")
 
 declare_args() {
-  enable_extensions = !is_android && !is_ios
+  enable_extensions = !is_android && !is_ios && !is_fuchsia
 
   # Enables Wi-Fi Display functionality
   # WARNING: This enables MPEG Transport Stream (MPEG-TS) encoding!
diff --git a/remoting/remoting_enable.gni b/remoting/remoting_enable.gni
index a031976..ceb53310 100644
--- a/remoting/remoting_enable.gni
+++ b/remoting/remoting_enable.gni
@@ -6,5 +6,5 @@
 import("//media/media_options.gni")
 
 declare_args() {
-  enable_remoting = !is_chromecast && enable_webrtc
+  enable_remoting = !is_chromecast && !is_fuchsia && enable_webrtc
 }
diff --git a/third_party/.gitignore b/third_party/.gitignore
index 47c52825..95f49c1 100644
--- a/third_party/.gitignore
+++ b/third_party/.gitignore
@@ -58,6 +58,7 @@
 /flatbuffers/src
 /fontconfig/src
 /freetype/src
+/fuchsia-sdk
 /gestures/gestures
 /gles2_conform
 /glslang/src
diff --git a/third_party/libxml/BUILD.gn b/third_party/libxml/BUILD.gn
index 28ac4d0..430a46e 100644
--- a/third_party/libxml/BUILD.gn
+++ b/third_party/libxml/BUILD.gn
@@ -4,7 +4,7 @@
 
 # Define an "os_include" variable that points at the OS-specific generated
 # headers.  These were generated by running the configure script offline.
-if (is_linux || is_android || is_nacl) {
+if (is_linux || is_android || is_nacl || is_fuchsia) {
   os_include = "linux"
 } else if (is_mac || is_ios) {
   os_include = "mac"
@@ -71,6 +71,7 @@
 
 static_library("libxml") {
   output_name = "libxml2"
+
   # Commented out sources are libxml2 files we do not want to include. They are
   # here to make it easy to identify files which are new.
   sources = [
@@ -80,16 +81,20 @@
     "linux/include/libxml/xmlversion.h",
     "mac/config.h",
     "mac/include/libxml/xmlversion.h",
+
     #"src/DOCBparser.c",
     "src/HTMLparser.c",
     "src/HTMLtree.c",
+
     #"src/SAX.c",
     "src/SAX2.c",
     "src/buf.c",
     "src/buf.h",
+
     #"src/c14n.c",
     #"src/catalog.c",
     "src/chvalid.c",
+
     #"src/debugXML.c",
     "src/dict.c",
     "src/elfgcchack.h",
@@ -147,20 +152,24 @@
     "src/include/libxml/xpointer.h",
     "src/include/win32config.h",
     "src/include/wsockcompat.h",
+
     #"src/legacy.c",
     "src/libxml.h",
     "src/list.c",
     "src/parser.c",
     "src/parserInternals.c",
     "src/pattern.c",
+
     #"src/relaxng.c",
     "src/save.h",
+
     #"src/schematron.c",
     "src/threads.c",
     "src/timsort.h",
     "src/tree.c",
     "src/triodef.h",
     "src/trionan.h",
+
     #"src/trio.c",
     #"src/trio.h",
     #"src/triodef.h",
@@ -171,20 +180,25 @@
     #"src/triostr.h",
     "src/uri.c",
     "src/valid.c",
+
     #"src/xinclude.c",
     #"src/xlink.c",
     "src/xmlIO.c",
     "src/xmlmemory.c",
+
     #"src/xmlmodule.c",
     "src/xmlreader.c",
+
     #"src/xmlregexp.c",
     "src/xmlsave.c",
+
     #"src/xmlschemas.c",
     #"src/xmlschemastypes.c",
     "src/xmlstring.c",
     "src/xmlunicode.c",
     "src/xmlwriter.c",
     "src/xpath.c",
+
     #"src/xpointer.c",
     #"src/xzlib.c",
     "src/xzlib.h",