Split off common and libdawn / dawn_headers from BUILD.gn

This adds an option to dawn_generator to generate files in a different
directory so the generated stay at the same place. Otherwise compilation
errors occur because of stale versions of the headers on the CQ
builders.

BUG=dawn:61

Change-Id: I71ceb3172b5a4e35911973a03be29d90fa684416
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/5304
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index f8b4a71..eb5664f 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -20,142 +20,12 @@
 
 import("//testing/test.gni")
 
-# Use Chromium's dcheck_always_on when available so that we respect it when
-# running tests on the GPU builders
-if (build_with_chromium) {
-  import("//build/config/dcheck_always_on.gni")
-} else {
-  dcheck_always_on = false
-}
-
-###############################################################################
-# Common dawn libraries and configs
-###############################################################################
-
-config("dawn_public") {
-  include_dirs = [
-    target_gen_dir,
-    "src/include",
-  ]
-}
-
-config("dawn_internal") {
-  include_dirs = [ "src" ]
-
-  defines = []
-  if (dawn_always_assert || dcheck_always_on || is_debug) {
-    defines += [ "DAWN_ENABLE_ASSERTS" ]
-  }
-
-  if (dawn_enable_d3d12) {
-    defines += [ "DAWN_ENABLE_BACKEND_D3D12" ]
-  }
-  if (dawn_enable_metal) {
-    defines += [ "DAWN_ENABLE_BACKEND_METAL" ]
-  }
-  if (dawn_enable_null) {
-    defines += [ "DAWN_ENABLE_BACKEND_NULL" ]
-  }
-  if (dawn_enable_opengl) {
-    defines += [ "DAWN_ENABLE_BACKEND_OPENGL" ]
-  }
-  if (dawn_enable_vulkan) {
-    defines += [ "DAWN_ENABLE_BACKEND_VULKAN" ]
-  }
-
-  configs = [ ":dawn_public" ]
-
-  # Only internal Dawn targets can use this config, this means only targets in
-  # this BUILD.gn file.
-  visibility = [ ":*" ]
-}
-
-static_library("dawn_common") {
-  sources = [
-    "src/common/Assert.cpp",
-    "src/common/Assert.h",
-    "src/common/BitSetIterator.h",
-    "src/common/Compiler.h",
-    "src/common/DynamicLib.cpp",
-    "src/common/DynamicLib.h",
-    "src/common/HashUtils.h",
-    "src/common/Math.cpp",
-    "src/common/Math.h",
-    "src/common/Platform.h",
-    "src/common/Result.h",
-    "src/common/Serial.h",
-    "src/common/SerialMap.h",
-    "src/common/SerialQueue.h",
-    "src/common/SerialStorage.h",
-    "src/common/SwapChainUtils.h",
-    "src/common/vulkan_platform.h",
-    "src/common/windows_with_undefs.h",
-  ]
-
-  configs += [ ":dawn_internal" ]
-  deps = [
-    ":dawn_headers",
-  ]
-}
-
-###############################################################################
-# Dawn headers
-###############################################################################
-
-dawn_generator("dawn_headers_gen") {
-  target = "dawn_headers"
-  outputs = [
-    "dawn/dawncpp.h",
-    "dawn/dawn.h",
-    "dawn/dawncpp_traits.h",
-  ]
-}
-
-source_set("dawn_headers") {
-  public_configs = [ ":dawn_public" ]
-  deps = [
-    ":dawn_headers_gen",
-  ]
-
-  sources = get_target_outputs(":dawn_headers_gen")
-  sources += [
-    "src/include/dawn/EnumClassBitmasks.h",
-    "src/include/dawn/dawn_export.h",
-    "src/include/dawn/dawn_wsi.h",
-  ]
-}
-
-###############################################################################
-# libdawn
-###############################################################################
-
-dawn_generator("libdawn_gen") {
-  target = "libdawn"
-  outputs = [
-    "dawn/dawncpp.cpp",
-    "dawn/dawn.c",
-  ]
-}
-
-dawn_component("libdawn") {
-  DEFINE_PREFIX = "DAWN"
-
-  public_deps = [
-    ":dawn_headers",
-  ]
-
-  deps = [
-    ":libdawn_gen",
-  ]
-  sources = get_target_outputs(":libdawn_gen")
-}
-
 ###############################################################################
 # libdawn_native
 ###############################################################################
 
 config("libdawn_native_internal") {
-  configs = [ ":dawn_internal" ]
+  configs = [ "${dawn_root}/src/common:dawn_internal" ]
 
   # Suppress warnings that Metal isn't in the deployment target of Chrome
   if (is_mac) {
@@ -178,9 +48,9 @@
 # dependencies of libdawn_native
 source_set("libdawn_native_headers") {
   public_deps = [
-    ":dawn_headers",
+    "${dawn_root}/src/dawn:dawn_headers",
   ]
-  public_configs = [ ":dawn_public" ]
+  public_configs = [ "${dawn_root}/src/common:dawn_public" ]
   sources = [
     "src/include/dawn_native/DawnNative.h",
     "src/include/dawn_native/dawn_native_export.h",
@@ -200,9 +70,9 @@
 # except those that define exported symbols.
 source_set("libdawn_native_sources") {
   deps = [
-    ":dawn_common",
     ":libdawn_native_headers",
     ":libdawn_native_utils_gen",
+    "${dawn_root}/src/common",
     "${dawn_spirv_tools_dir}:spvtools_val",
     "third_party:spirv_cross",
   ]
@@ -504,8 +374,8 @@
   ]
 
   deps = [
-    ":dawn_common",
     ":libdawn_native_sources",
+    "${dawn_root}/src/common",
   ]
   sources = [
     "src/dawn_native/DawnNative.cpp",
@@ -539,9 +409,9 @@
 # dependencies of libdawn_wire
 source_set("libdawn_wire_headers") {
   public_deps = [
-    ":dawn_headers",
+    "${dawn_root}/src/dawn:dawn_headers",
   ]
-  public_configs = [ ":dawn_public" ]
+  public_configs = [ "${dawn_root}/src/common:dawn_public" ]
   sources = [
     "src/include/dawn_wire/Wire.h",
     "src/include/dawn_wire/WireClient.h",
@@ -575,12 +445,12 @@
   DEFINE_PREFIX = "DAWN_WIRE"
 
   deps = [
-    ":dawn_common",
     ":libdawn_wire_gen",
     ":libdawn_wire_headers",
+    "${dawn_root}/src/common",
   ]
 
-  configs = [ ":dawn_internal" ]
+  configs = [ "${dawn_root}/src/common:dawn_internal" ]
   sources = get_target_outputs(":libdawn_wire_gen")
   sources += [
     "src/dawn_wire/WireClient.cpp",
@@ -619,7 +489,7 @@
 ###############################################################################
 
 static_library("dawn_utils") {
-  configs += [ ":dawn_internal" ]
+  configs += [ "${dawn_root}/src/common:dawn_internal" ]
 
   sources = [
     "src/utils/BackendBinding.cpp",
@@ -635,13 +505,13 @@
   ]
 
   public_deps = [
-    ":dawn_headers_gen",
+    "${dawn_root}/src/dawn:dawn_headers",
   ]
 
   deps = [
-    ":dawn_common",
     ":libdawn_native",
     ":libdawn_wire",
+    "${dawn_root}/src/common",
     "${dawn_shaderc_dir}:libshaderc",
     "third_party:glfw",
   ]
@@ -692,17 +562,16 @@
 }
 
 test("dawn_unittests") {
-  configs += [ ":dawn_internal" ]
+  configs += [ "${dawn_root}/src/common:dawn_internal" ]
 
   deps = [
-    ":dawn_common",
-    ":dawn_headers",
     ":dawn_utils",
-    ":libdawn",
     ":libdawn_native",
     ":libdawn_native_sources",
     ":libdawn_wire",
     ":mock_dawn_gen",
+    "${dawn_root}/src/common",
+    "${dawn_root}/src/dawn:libdawn",
     "third_party:gmock_and_gtest",
   ]
 
@@ -768,14 +637,14 @@
 }
 
 test("dawn_end2end_tests") {
-  configs += [ ":dawn_internal" ]
+  configs += [ "${dawn_root}/src/common:dawn_internal" ]
 
   deps = [
-    ":dawn_common",
     ":dawn_utils",
-    ":libdawn",
     ":libdawn_native",
     ":libdawn_wire",
+    "${dawn_root}/src/common",
+    "${dawn_root}/src/dawn:libdawn",
     "third_party:glfw",
     "third_party:gmock_and_gtest",
   ]
@@ -852,14 +721,14 @@
 
     # Export all of these as public deps so that `gn check` allows includes
     public_deps = [
-      ":dawn_common",
       ":dawn_utils",
-      ":libdawn",
       ":libdawn_native",
       ":libdawn_wire",
+      "${dawn_root}/src/common",
+      "${dawn_root}/src/dawn:libdawn",
       "third_party:glfw",
     ]
-    public_configs = [ ":dawn_internal" ]
+    public_configs = [ "${dawn_root}/src/common:dawn_internal" ]
   }
 
   # Template for samples to avoid listing dawn_sample_utils as a dep every time
diff --git a/generator/dawn_generator.gni b/generator/dawn_generator.gni
index f9fa1f9..52675eb 100644
--- a/generator/dawn_generator.gni
+++ b/generator/dawn_generator.gni
@@ -28,6 +28,9 @@
 #       "MyTarget.cpp",
 #       "MyTarget.h",
 #     ]
+#
+#     # Optional, use a custom generated file directory.
+#     custom_gen_dir = "${target_gen_dir}/.."
 #   }
 #
 # Using the generated files is done like so:
@@ -41,11 +44,11 @@
   # The base arguments for the generator: from this dawn.json, generate this
   # target using templates in this directory.
   generator_args = [
-    rebase_path("dawn.json", root_build_dir),
+    rebase_path("${dawn_root}/dawn.json", root_build_dir),
     "--wire-json",
-    rebase_path("dawn_wire.json", root_build_dir),
+    rebase_path("${dawn_root}/dawn_wire.json", root_build_dir),
     "--template-dir",
-    rebase_path("generator/templates", root_build_dir),
+    rebase_path("${dawn_root}/generator/templates", root_build_dir),
     "--targets",
     invoker.target,
   ]
@@ -58,13 +61,22 @@
     jinja2_python_path,
   ]
 
+  # Chooses either the default gen_dir or the custom one required by the
+  # invoker. This allows moving the definition of code generators in different
+  # BUILD.gn files without changing the location of generated file. Without
+  # this generated headers could cause issues when old headers aren't removed.
+  gen_dir = target_gen_dir
+  if (defined(invoker.custom_gen_dir)) {
+    gen_dir = invoker.custom_gen_dir
+  }
+
   # For build parallelism GN wants to know the exact inputs and outputs of
   # action targets like we use for our code generator. We avoid asking the
   # generator about its inputs by using the "depfile" feature of GN/Ninja.
   #
   # A ninja limitation is that the depfile is a subset of Makefile that can
   # contain a single target, so we output a single "JSON-tarball" instead.
-  json_tarball = "${target_gen_dir}/${target_name}.json_tarball"
+  json_tarball = "${gen_dir}/${target_name}.json_tarball"
   json_tarball_depfile = "${json_tarball}.d"
 
   generator_args += [
@@ -79,7 +91,7 @@
   # outputs. To make sure the list is in sync between the generator and the
   # build files, we write it to a file and ask the generator to assert it is
   # correct.
-  expected_outputs_file = "${target_gen_dir}/${target_name}.expected_outputs"
+  expected_outputs_file = "${gen_dir}/${target_name}.expected_outputs"
   write_file(expected_outputs_file, invoker.outputs)
 
   generator_args += [
@@ -90,7 +102,7 @@
   # The code generator invocation that will write the JSON tarball, check the
   # outputs are what's expected and write a depfile for Ninja.
   action("${target_name}_json_tarball") {
-    script = "generator/main.py"
+    script = "${dawn_root}/generator/main.py"
     outputs = [
       json_tarball,
     ]
@@ -98,12 +110,12 @@
     args = generator_args
   }
 
-  # Extract the JSON tarball into the target_gen_dir
+  # Extract the JSON tarball into the gen_dir
   action("${target_name}") {
-    script = "generator/extract_json.py"
+    script = "${dawn_root}/generator/extract_json.py"
     args = [
       rebase_path(json_tarball, root_build_dir),
-      rebase_path(target_gen_dir, root_build_dir),
+      rebase_path(gen_dir, root_build_dir),
     ]
 
     deps = [
@@ -113,11 +125,11 @@
       json_tarball,
     ]
 
-    # The expected output list is relative to the target_gen_dir but action
+    # The expected output list is relative to the gen_dir but action
     # target outputs are from the root dir so we need to rebase them.
     outputs = []
     foreach(source, invoker.outputs) {
-      outputs += [ "${target_gen_dir}/${source}" ]
+      outputs += [ "${gen_dir}/${source}" ]
     }
   }
 }
diff --git a/generator/templates/apicpp.cpp b/generator/templates/apicpp.cpp
index 4a59356..9e34f5d 100644
--- a/generator/templates/apicpp.cpp
+++ b/generator/templates/apicpp.cpp
@@ -12,7 +12,7 @@
 //* See the License for the specific language governing permissions and
 //* limitations under the License.
 
-#include "dawncpp.h"
+#include "dawn/dawncpp.h"
 
 namespace dawn {
 
diff --git a/scripts/dawn_component.gni b/scripts/dawn_component.gni
index 3e854da..d2af95a 100644
--- a/scripts/dawn_component.gni
+++ b/scripts/dawn_component.gni
@@ -95,6 +95,13 @@
       defines = []
     }
     defines += [ "${invoker.DEFINE_PREFIX}_IMPLEMENTATION" ]
+
+    # TODO(cwallez@chromium.org): Remove this when we are sure the bots have
+    # been clobbered. This is necessary because when moving generated files
+    # around we ended up with bad generated files in cached build
+    # directories that get picked up because of the default inclusion of
+    # target_gen_dir as an include directory.
+    configs -= [ "//build/config/compiler:default_include_dirs" ]
   }
 
   static_library("${libname}_static") {
@@ -107,6 +114,13 @@
     if (defined(invoker.configs)) {
       configs += invoker.configs
     }
+
+    # TODO(cwallez@chromium.org): Remove this when we are sure the bots have
+    # been clobbered. This is necessary because when moving generated files
+    # around we ended up with bad generated files in cached build
+    # directories that get picked up because of the default inclusion of
+    # target_gen_dir as an include directory.
+    configs -= [ "//build/config/compiler:default_include_dirs" ]
   }
 
   group(libname) {
diff --git a/scripts/dawn_overrides_with_defaults.gni b/scripts/dawn_overrides_with_defaults.gni
index 7c47e02..196a448 100644
--- a/scripts/dawn_overrides_with_defaults.gni
+++ b/scripts/dawn_overrides_with_defaults.gni
@@ -24,6 +24,10 @@
   dawn_standalone = false
 }
 
+if (!defined(dawn_root)) {
+  dawn_root = get_path_info("..", "abspath")
+}
+
 if (!defined(dawn_jinja2_dir)) {
   dawn_jinja2_dir = "//third_party/jinja2"
 }
diff --git a/src/common/BUILD.gn b/src/common/BUILD.gn
new file mode 100644
index 0000000..3140b71
--- /dev/null
+++ b/src/common/BUILD.gn
@@ -0,0 +1,100 @@
+# Copyright 2019 The Dawn Authors
+#
+# 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 the 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.
+
+import("../../scripts/dawn_overrides_with_defaults.gni")
+
+import("//build_overrides/build.gni")
+import("${dawn_root}/scripts/dawn_features.gni")
+
+# Use Chromium's dcheck_always_on when available so that we respect it when
+# running tests on the GPU builders
+if (build_with_chromium) {
+  import("//build/config/dcheck_always_on.gni")
+} else {
+  dcheck_always_on = false
+}
+
+###############################################################################
+# Common dawn configs
+###############################################################################
+
+config("dawn_public") {
+  include_dirs = [
+    "${target_gen_dir}/../..",
+    "${dawn_root}/src/include"
+  ]
+}
+
+config("dawn_internal") {
+  include_dirs = [ "${dawn_root}/src" ]
+
+  defines = []
+  if (dawn_always_assert || dcheck_always_on || is_debug) {
+    defines += [ "DAWN_ENABLE_ASSERTS" ]
+  }
+
+  if (dawn_enable_d3d12) {
+    defines += [ "DAWN_ENABLE_BACKEND_D3D12" ]
+  }
+  if (dawn_enable_metal) {
+    defines += [ "DAWN_ENABLE_BACKEND_METAL" ]
+  }
+  if (dawn_enable_null) {
+    defines += [ "DAWN_ENABLE_BACKEND_NULL" ]
+  }
+  if (dawn_enable_opengl) {
+    defines += [ "DAWN_ENABLE_BACKEND_OPENGL" ]
+  }
+  if (dawn_enable_vulkan) {
+    defines += [ "DAWN_ENABLE_BACKEND_VULKAN" ]
+  }
+
+  configs = [ ":dawn_public" ]
+
+  # Only internal Dawn targets can use this config, this means only targets in
+  # this BUILD.gn file.
+  visibility = [ ":*" ]
+}
+
+###############################################################################
+# Common dawn library
+###############################################################################
+
+static_library("common") {
+  sources = [
+    "Assert.cpp",
+    "Assert.h",
+    "BitSetIterator.h",
+    "Compiler.h",
+    "DynamicLib.cpp",
+    "DynamicLib.h",
+    "HashUtils.h",
+    "Math.cpp",
+    "Math.h",
+    "Platform.h",
+    "Result.h",
+    "Serial.h",
+    "SerialMap.h",
+    "SerialQueue.h",
+    "SerialStorage.h",
+    "SwapChainUtils.h",
+    "vulkan_platform.h",
+    "windows_with_undefs.h",
+  ]
+
+  configs += [ ":dawn_internal" ]
+  deps = [
+    "${dawn_root}/src/dawn:dawn_headers",
+  ]
+}
diff --git a/src/dawn/BUILD.gn b/src/dawn/BUILD.gn
new file mode 100644
index 0000000..b2fc3b1
--- /dev/null
+++ b/src/dawn/BUILD.gn
@@ -0,0 +1,74 @@
+# Copyright 2019 The Dawn Authors
+#
+# 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 the 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.
+
+import("../../scripts/dawn_overrides_with_defaults.gni")
+
+import("${dawn_root}/scripts/dawn_component.gni")
+import("${dawn_root}/generator/dawn_generator.gni")
+
+###############################################################################
+# Dawn headers
+###############################################################################
+
+dawn_generator("dawn_headers_gen") {
+  target = "dawn_headers"
+
+  # Generate as if we were in the main BUILD.gn because that was historically
+  # the case and we can't move generated files without clobbering the build.
+  custom_gen_dir = "${target_gen_dir}/../.."
+  outputs = [
+    "dawn/dawncpp.h",
+    "dawn/dawn.h",
+    "dawn/dawncpp_traits.h",
+  ]
+}
+
+source_set("dawn_headers") {
+  public_configs = [ "${dawn_root}/src/common:dawn_public" ]
+  deps = [
+    ":dawn_headers_gen",
+  ]
+
+  sources = get_target_outputs(":dawn_headers_gen")
+  sources += [
+    "${dawn_root}/src/include/dawn/EnumClassBitmasks.h",
+    "${dawn_root}/src/include/dawn/dawn_export.h",
+    "${dawn_root}/src/include/dawn/dawn_wsi.h",
+  ]
+}
+
+###############################################################################
+# libdawn
+###############################################################################
+
+dawn_generator("libdawn_gen") {
+  target = "libdawn"
+  outputs = [
+    "dawn/dawncpp.cpp",
+    "dawn/dawn.c",
+  ]
+}
+
+dawn_component("libdawn") {
+  DEFINE_PREFIX = "DAWN"
+
+  public_deps = [
+    ":dawn_headers",
+  ]
+
+  deps = [
+    ":libdawn_gen",
+  ]
+  sources = get_target_outputs(":libdawn_gen")
+}
diff --git a/src/fuzzers/BUILD.gn b/src/fuzzers/BUILD.gn
index 0c48266..2b151dc 100644
--- a/src/fuzzers/BUILD.gn
+++ b/src/fuzzers/BUILD.gn
@@ -15,8 +15,6 @@
 import("../../scripts/dawn_overrides_with_defaults.gni")
 import("//build_overrides/build.gni")
 
-dawn_top_level = "../.."
-
 # We only have libfuzzer in Chromium builds but if we build fuzzer targets only
 # there, we would risk breaking fuzzer targets all the time when making changes
 # to Dawn. To avoid that, we make fuzzer targets compile in standalone builds
@@ -165,11 +163,11 @@
   ]
 
   deps = [
-    "${dawn_top_level}:dawn_common",
-    "${dawn_top_level}:libdawn_native_static",
-    "${dawn_top_level}:libdawn_static",
-    "${dawn_top_level}:libdawn_wire_static",
+    "${dawn_root}/:libdawn_native_static",
+    "${dawn_root}/:libdawn_wire_static",
+    "${dawn_root}/src/common",
+    "${dawn_root}/src/dawn:libdawn_static",
   ]
 
-  additional_configs = [ "${dawn_top_level}:dawn_internal" ]
+  additional_configs = [ "${dawn_root}/src/common:dawn_internal" ]
 }