[build] add build config for wayland

This change adds build config files for wayland, enabling it to be used
by other targets in-tree.

Test: added wayland as local dep and built fuchsia
Change-Id: I837b0b668ccf6dfcdd5164faf83a5625ef2fc655
diff --git a/BUILD.gn b/BUILD.gn
new file mode 100644
index 0000000..5a52ba6
--- /dev/null
+++ b/BUILD.gn
@@ -0,0 +1,239 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#    * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#    * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#    * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import("//third_party/wayland/wayland.gni")
+
+group("wayland") {
+  public_deps = [
+    ":client",
+    ":server",
+  ]
+}
+
+group("client") {
+  public_deps = [
+    ":client_static",
+  ]
+}
+
+group("server") {
+  public_deps = [
+    ":server_static",
+  ]
+}
+
+group("client_headers") {
+  public_configs = [
+    ":client_config",
+    ":common_config",
+  ]
+  deps = [
+    ":headers_client",
+    ":headers_common",
+    ":protocol_client",
+    ":protocol_client_core",
+  ]
+}
+
+group("server_headers") {
+  public_configs = [
+    ":server_config",
+    ":common_config",
+  ]
+  deps = [
+    ":headers_common",
+    ":headers_server",
+    ":protocol_server",
+    ":protocol_server_core",
+  ]
+}
+
+executable("scanner") {
+  output_name = "wayland-scanner"
+  deps = [
+    ":scanner_static",
+  ]
+}
+
+config("common_config") {
+  visibility = [ ":*" ]
+  include_dirs = [ "${target_gen_dir}/common" ]
+}
+
+config("client_config") {
+  visibility = [ ":*" ]
+  include_dirs = [ "${target_gen_dir}/client" ]
+}
+
+config("server_config") {
+  visibility = [ ":*" ]
+  include_dirs = [ "${target_gen_dir}/server" ]
+}
+
+config("private_config") {
+  visibility = [ ":*" ]
+  configs = [
+    ":client_config",
+    ":server_config",
+    ":common_config",
+  ]
+  include_dirs = [
+    ".",
+    "./src",
+    "./protocol",
+  ]
+  defines = [ "_POSIX_C_SOURCE=200809L" ]
+  cflags = [ "-pthread" ]
+}
+
+source_set("scanner_static") {
+  visibility = [ ":*" ]
+  deps = [
+    "//third_party/expat",
+    "//third_party/libxml2",
+  ]
+
+  sources = [
+    "src/dtddata.S",
+    "src/scanner.c",
+    "src/wayland-util.c",
+  ]
+
+  configs += [ ":private_config" ]
+}
+
+source_set("client_static") {
+  visibility = [ ":*" ]
+  sources = [
+    "src/wayland-client.c",
+  ]
+  deps = [
+    ":common_static",
+  ]
+  public_deps = [
+    ":client_headers",
+  ]
+  configs += [ ":private_config" ]
+}
+
+source_set("server_static") {
+  visibility = [ ":*" ]
+  sources = [
+    "src/wayland-server.c",
+  ]
+  deps = [
+    ":common_static",
+  ]
+  public_deps = [
+    ":server_headers",
+  ]
+  configs += [ ":private_config" ]
+}
+
+source_set("common_static") {
+  visibility = [ ":*" ]
+  sources = [
+    "${target_gen_dir}/wayland-protocol.c",
+    "src/connection.c",
+    "src/event-loop.c",
+    "src/wayland-os.c",
+    "src/wayland-util.c",
+  ]
+  deps = [
+    ":protocol_client",
+    ":protocol_client_core",
+    ":protocol_code",
+    ":protocol_server",
+    ":protocol_server_core",
+    "//third_party/libffi",
+  ]
+  configs += [ ":private_config" ]
+}
+
+wayland_protocol_code("protocol_code") {
+  protocol = "protocol/wayland.xml"
+  output = "${target_gen_dir}/wayland-protocol.c"
+}
+
+wayland_protocol_header("protocol_client") {
+  protocol = "protocol/wayland.xml"
+  variant = "client"
+  core = false
+  output = "${target_gen_dir}/client/wayland-client-protocol.h"
+}
+
+wayland_protocol_header("protocol_client_core") {
+  protocol = "protocol/wayland.xml"
+  variant = "client"
+  core = true
+  output = "${target_gen_dir}/client/wayland-client-protocol-core.h"
+}
+
+wayland_protocol_header("protocol_server") {
+  protocol = "protocol/wayland.xml"
+  variant = "server"
+  core = false
+  output = "${target_gen_dir}/server/wayland-server-protocol.h"
+}
+
+wayland_protocol_header("protocol_server_core") {
+  protocol = "protocol/wayland.xml"
+  variant = "server"
+  core = true
+  output = "${target_gen_dir}/server/wayland-server-protocol-core.h"
+}
+
+copy("headers_common") {
+  sources = [
+    "src/wayland-util.h",
+    "src/wayland-version.h",
+  ]
+  outputs = [
+    "${target_gen_dir}/common/{{source_file_part}}",
+  ]
+}
+
+copy("headers_client") {
+  sources = [
+    "src/wayland-client-core.h",
+    "src/wayland-client.h",
+  ]
+  outputs = [
+    "${target_gen_dir}/client/{{source_file_part}}",
+  ]
+}
+
+copy("headers_server") {
+  sources = [
+    "src/wayland-server-core.h",
+    "src/wayland-server.h",
+  ]
+  outputs = [
+    "${target_gen_dir}/server/{{source_file_part}}",
+  ]
+}
diff --git a/README.fuchsia b/README.fuchsia
new file mode 100644
index 0000000..83b7209
--- /dev/null
+++ b/README.fuchsia
@@ -0,0 +1,13 @@
+Name: wayland
+URL: https://wayland.freedesktop.org
+License: MIT
+License File: COPYING
+Description:
+Wayland is a project to define a protocol for a compositor to talk to its clients as well as a
+library implementation of the protocol.
+
+After updating this library, maintainers should regenerate dependent configuration files:
+1. git clone -n <upstream> && cd wayland && git checkout <commit>
+2. ./autogen.sh --disable-documentation
+3. cp ./src/wayland-version.h ${FUCHSIA_DIR}/third_party/wayland/src
+4. cp ./config.h ${FUCHSIA_DIR}/third_party/wayland
diff --git a/config.h b/config.h
new file mode 100644
index 0000000..5f4849c
--- /dev/null
+++ b/config.h
@@ -0,0 +1,80 @@
+/* config.h.  Generated from config.h.in by configure.  */
+/* config.h.in.  Generated from configure.ac by autoheader.  */
+
+/* Define to 1 if you have the `accept4' function. */
+#define HAVE_ACCEPT4 1
+
+/* Define to 1 if you have the <dlfcn.h> header file. */
+#define HAVE_DLFCN_H 1
+
+/* Define to 1 if you have the <inttypes.h> header file. */
+#define HAVE_INTTYPES_H 1
+
+/* libxml-2.0 is available */
+#define HAVE_LIBXML 1
+
+/* Define to 1 if you have the <memory.h> header file. */
+#define HAVE_MEMORY_H 1
+
+/* Define to 1 if you have the `mkostemp' function. */
+#define HAVE_MKOSTEMP 1
+
+/* Define to 1 if you have the `posix_fallocate' function. */
+#define HAVE_POSIX_FALLOCATE 1
+
+/* Define to 1 if you have the `prctl' function. */
+#define HAVE_PRCTL 1
+
+/* Define to 1 if you have the <stdint.h> header file. */
+#define HAVE_STDINT_H 1
+
+/* Define to 1 if you have the <stdlib.h> header file. */
+#define HAVE_STDLIB_H 1
+
+/* Define to 1 if you have the <strings.h> header file. */
+#define HAVE_STRINGS_H 1
+
+/* Define to 1 if you have the <string.h> header file. */
+#define HAVE_STRING_H 1
+
+/* Define to 1 if you have the <sys/prctl.h> header file. */
+#define HAVE_SYS_PRCTL_H 1
+
+/* Define to 1 if you have the <sys/stat.h> header file. */
+#define HAVE_SYS_STAT_H 1
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#define HAVE_SYS_TYPES_H 1
+
+/* Define to 1 if you have the <unistd.h> header file. */
+#define HAVE_UNISTD_H 1
+
+/* Define to the sub-directory where libtool stores uninstalled libraries. */
+#define LT_OBJDIR ".libs/"
+
+/* Name of package */
+#define PACKAGE "wayland"
+
+/* Define to the address where bug reports for this package should be sent. */
+#define PACKAGE_BUGREPORT "https://gitlab.freedesktop.org/wayland/wayland/issues/"
+
+/* Define to the full name of this package. */
+#define PACKAGE_NAME "wayland"
+
+/* Define to the full name and version of this package. */
+#define PACKAGE_STRING "wayland 1.16.90"
+
+/* Define to the one symbol short name of this package. */
+#define PACKAGE_TARNAME "wayland"
+
+/* Define to the home page for this package. */
+#define PACKAGE_URL "https://wayland.freedesktop.org/"
+
+/* Define to the version of this package. */
+#define PACKAGE_VERSION "1.16.90"
+
+/* Define to 1 if you have the ANSI C header files. */
+#define STDC_HEADERS 1
+
+/* Version number of package */
+#define VERSION "1.16.90"
diff --git a/src/dtddata.S b/src/dtddata.S
index 2405066..71ec989 100644
--- a/src/dtddata.S
+++ b/src/dtddata.S
@@ -47,4 +47,4 @@
 .endm
 
 .section .rodata
-binfile DTD_DATA src/wayland.dtd.embed
+binfile DTD_DATA protocol/wayland.dtd
diff --git a/src/wayland-version.h b/src/wayland-version.h
new file mode 100644
index 0000000..2ae610b
--- /dev/null
+++ b/src/wayland-version.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef WAYLAND_VERSION_H
+#define WAYLAND_VERSION_H
+
+#define WAYLAND_VERSION_MAJOR 1
+#define WAYLAND_VERSION_MINOR 16
+#define WAYLAND_VERSION_MICRO 90
+#define WAYLAND_VERSION "1.16.90"
+
+#endif
diff --git a/wayland.gni b/wayland.gni
new file mode 100644
index 0000000..fde6eee
--- /dev/null
+++ b/wayland.gni
@@ -0,0 +1,86 @@
+# Copyright 2019 The Fuchsia Authors
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice (including the next
+# paragraph) shall be included in all copies or substantial portions of the
+# Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+# IN THE SOFTWARE.
+
+import("//build/compiled_action.gni")
+
+template("_wayland_protocol") {
+  assert(defined(invoker.protocol),
+         "${target_name} must have a 'protocol' property, e.g. 'wayland.xml'")
+  assert(
+      defined(invoker.output),
+      "${target_name} must have an 'output' property, e.g. 'wayland-client-protocol-core.h'")
+  compiled_action("${target_name}") {
+    tool = "//third_party/wayland:scanner"
+    tool_output_name = "wayland-scanner"
+    sources = [
+      invoker.protocol,
+    ]
+    outputs = [
+      invoker.output,
+    ]
+    args = invoker.scanner_args
+    args += [
+      rebase_path(invoker.protocol, root_build_dir),
+      rebase_path(invoker.output, root_build_dir),
+    ]
+  }
+}
+
+template("wayland_protocol_header") {
+  assert(defined(invoker.protocol),
+         "${target_name} must have a 'protocol' property, e.g. 'wayland.xml'")
+  assert(defined(invoker.variant),
+         "${target_name} must have a 'variant' property, e.g. 'client'")
+  assert(invoker.variant == "client" || invoker.variant == "server",
+         "${target_name}.variant must be 'client' or 'server'")
+  assert(defined(invoker.core),
+         "${target_name} must have a 'core' property, e.g. 'true'")
+  assert(
+      defined(invoker.output),
+      "${target_name} must have an 'output' property, e.g. 'wayland-client-protocol-core.h'")
+  _wayland_protocol("${target_name}") {
+    forward_variables_from(invoker,
+                           [
+                             "protocol",
+                             "output",
+                           ])
+    scanner_args = [ "${invoker.variant}-header" ]
+    if (invoker.core) {
+      scanner_args += [ "-c" ]
+    }
+  }
+}
+
+template("wayland_protocol_code") {
+  assert(defined(invoker.protocol),
+         "${target_name} must have a 'protocol' property, e.g. 'wayland.xml'")
+  assert(
+      defined(invoker.output),
+      "${target_name} must have an 'output' property, e.g. 'wayland-protocol.c'")
+  _wayland_protocol("${target_name}") {
+    forward_variables_from(invoker,
+                           [
+                             "protocol",
+                             "output",
+                           ])
+    scanner_args = [ "private-code" ]
+  }
+}