Define docs target as a toolchain

This change creates a toolchain that configures the docs target and adds
it to the default build.

Change-Id: Ib2a8387253eef809f94411780174377e0d23e1a9
diff --git a/BUILD.gn b/BUILD.gn
index c5e5af3..e7116b9 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -17,7 +17,11 @@
 
 import("$dir_pigweed/modules.gni")
 import("$dir_pw_build/host_tool.gni")
+import("$dir_pw_docgen/docs.gni")
 import("$dir_pw_unit_test/test.gni")
+
+# Main build file for upstream Pigweed.
+
 declare_args() {
   pw_IS_HOST_TOOLCHAIN = false
 }
@@ -26,7 +30,10 @@
 group("default") {
   _host_target = "$dir_pigweed/targets/host/linux:clang_og"
 
-  deps = [ ":pigweed_default($_host_target)" ]
+  deps = [
+    ":pigweed_default($_host_target)",
+    ":pigweed_default($dir_pigweed/targets/docs)",
+  ]
 }
 
 # By default, Pigweed will build this target when invoking ninja.
@@ -35,7 +42,7 @@
 
   # Prevent the default toolchain from parsing any other BUILD.gn files.
   if (current_toolchain != default_toolchain) {
-    if (defined(pw_is_docs_target) && pw_is_docs_target) {
+    if (pw_docgen_BUILD_DOCS) {
       deps += [ "$dir_pigweed/docs" ]
     } else {
       if (pw_unit_test_AUTOMATIC_RUNNER == "") {
diff --git a/pw_cpu_exception/BUILD.gn b/pw_cpu_exception/BUILD.gn
index d90b1c7..137fd84 100644
--- a/pw_cpu_exception/BUILD.gn
+++ b/pw_cpu_exception/BUILD.gn
@@ -17,6 +17,7 @@
 
 import("$dir_pw_build/facade.gni")
 import("$dir_pw_docgen/docs.gni")
+import("backend.gni")
 config("default_config") {
   include_dirs = [ "public" ]
 }
diff --git a/pw_cpu_exception_armv7m/BUILD.gn b/pw_cpu_exception_armv7m/BUILD.gn
index 7c45e0e..186fcb4 100644
--- a/pw_cpu_exception_armv7m/BUILD.gn
+++ b/pw_cpu_exception_armv7m/BUILD.gn
@@ -15,47 +15,39 @@
 # gn-format disable
 import("//build_overrides/pigweed.gni")
 
-import("$dir_pigweed/legacy_target.gni")
 import("$dir_pw_build/target_types.gni")
 import("$dir_pw_docgen/docs.gni")
 import("$dir_pw_unit_test/test.gni")
+config("default_config") {
+  include_dirs = [ "public" ]
+}
 
-# TODO(pwbug/17): Replace when Pigweed config system is added.
-# This if statement allows docs to always build even if the target isn't
-# compatible with this backend.
-if (dir_pw_cpu_exception_backend == dir_pw_cpu_exception_armv7m) {
-  config("default_config") {
-    include_dirs = [ "public" ]
-  }
+pw_source_set("pw_cpu_exception_armv7m") {
+  public_configs = [ ":default_config" ]
+  deps = [
+    "$dir_pw_cpu_exception:facade",
+    "$dir_pw_preprocessor",
+  ]
+  public = [ "public/pw_cpu_exception_armv7m/cpu_state.h" ]
+  sources = [
+    "cpu_exception_entry.cc",
+    "cpu_state.cc",
+  ]
+}
 
-  pw_source_set("pw_cpu_exception_armv7m") {
-    public_configs = [ ":default_config" ]
-    deps = [
-      "$dir_pw_cpu_exception:facade",
-      "$dir_pw_preprocessor",
-    ]
-    public = [ "public/pw_cpu_exception_armv7m/cpu_state.h" ]
-    sources = [
-      "cpu_exception_entry.cc",
-      "cpu_state.cc",
-    ]
-    sources += public
-  }
+pw_test_group("tests") {
+  tests = [ ":cpu_exception_entry_test" ]
+}
 
-  pw_test_group("tests") {
-    tests = [ ":cpu_exception_entry_test" ]
-  }
+pw_test("cpu_exception_entry_test") {
+  deps = [
+    ":pw_cpu_exception_armv7m",
+    "$dir_pw_cpu_exception",
+  ]
+  sources = [ "exception_entry_test.cc" ]
 
-  pw_test("cpu_exception_entry_test") {
-    deps = [
-      ":pw_cpu_exception_armv7m",
-      "$dir_pw_cpu_exception",
-    ]
-    sources = [ "exception_entry_test.cc" ]
-
-    # TODO(frolv): Fix this test on the QEMU target.
-    enable_if = pw_executable_config.target_type != "lm3s6965evb_executable"
-  }
+  # TODO(frolv): Fix this test on the QEMU target.
+  enable_if = pw_build_EXECUTABLE_TARGET_TYPE != "lm3s6965evb_executable"
 }
 
 pw_doc_group("docs") {
diff --git a/pw_docgen/docs.gni b/pw_docgen/docs.gni
index a840f47..593dc11 100644
--- a/pw_docgen/docs.gni
+++ b/pw_docgen/docs.gni
@@ -18,6 +18,10 @@
 import("$dir_pigweed/legacy_target.gni")
 import("$dir_pw_build/input_group.gni")
 import("$dir_pw_build/python_script.gni")
+declare_args() {
+  # Whether or not the current target should build docs.
+  pw_docgen_BUILD_DOCS = false
+}
 
 # Defines a group of documentation files and assets.
 #
@@ -86,9 +90,9 @@
 
   _script_args = [
     "--gn-root",
-    rebase_path("//", root_out_dir),
+    rebase_path("//", root_build_dir),
     "--gn-gen-root",
-    rebase_path(root_gen_dir, root_out_dir) + "/",
+    rebase_path(root_gen_dir, root_build_dir) + "/",
     "--sphinx-build-dir",
     get_path_info("$target_gen_dir/pw_docgen_tree", "abspath"),
     "--conf",
@@ -106,8 +110,7 @@
     _script_args += [ get_path_info(path, "abspath") ]
   }
 
-  # TODO(frolv): Have a way of indicating that a toolchain should build docs.
-  if (current_toolchain == default_toolchain) {
+  if (pw_docgen_BUILD_DOCS) {
     pw_python_script(target_name) {
       script = "$dir_pw_docgen/py/docgen.py"
       args = _script_args
diff --git a/pw_sys_io_baremetal_stm32f429/BUILD.gn b/pw_sys_io_baremetal_stm32f429/BUILD.gn
index f1d9466..cc232fc 100644
--- a/pw_sys_io_baremetal_stm32f429/BUILD.gn
+++ b/pw_sys_io_baremetal_stm32f429/BUILD.gn
@@ -15,25 +15,19 @@
 # gn-format disable
 import("//build_overrides/pigweed.gni")
 
-import("$dir_pigweed/legacy_target.gni")
 import("$dir_pw_build/target_types.gni")
 import("$dir_pw_docgen/docs.gni")
-
-# This if statement allows docs to always build even if the target isn't
-# compatible with this backend.
-if (dir_pw_sys_io_backend == dir_pw_sys_io_baremetal_stm32f429) {
-  pw_source_set("pw_sys_io_baremetal_stm32f429") {
-    public_deps = [ "$dir_pw_boot_armv7m" ]
-    deps = [
-      "$dir_pw_preprocessor",
-      "$dir_pw_sys_io:default_putget_bytes",
-      "$dir_pw_sys_io:facade",
-    ]
-    sources = [
-      "early_boot.c",
-      "sys_io_baremetal.cc",
-    ]
-  }
+pw_source_set("pw_sys_io_baremetal_stm32f429") {
+  public_deps = [ "$dir_pw_boot_armv7m" ]
+  deps = [
+    "$dir_pw_preprocessor",
+    "$dir_pw_sys_io:default_putget_bytes",
+    "$dir_pw_sys_io:facade",
+  ]
+  sources = [
+    "early_boot.c",
+    "sys_io_baremetal.cc",
+  ]
 }
 
 pw_doc_group("docs") {
diff --git a/pw_toolchain/arm_gcc/toolchains.gni b/pw_toolchain/arm_gcc/toolchains.gni
index 90a26e4..fe90a67 100644
--- a/pw_toolchain/arm_gcc/toolchains.gni
+++ b/pw_toolchain/arm_gcc/toolchains.gni
@@ -48,6 +48,14 @@
   "$dir_pw_toolchain/arm_gcc:cortex_hardware_fpu",
 ]
 
+arm_gcc_cortex_m4_os = {
+  name = "arm_gcc_cortex_m4_os"
+  forward_variables_from(arm_gcc_toolchain_tools, "*")
+  defaults = {
+    default_configs = _arm_gcc + _cortex_m4 + [ "$dir_pw_build:optimize_size" ]
+  }
+}
+
 # Describes ARM GCC toolchains for specific targets.
 arm_gcc_toolchains = [
   {
diff --git a/pw_toolchain/dummy/BUILD.gn b/pw_toolchain/dummy/BUILD.gn
index ea9d683..f7e6d81 100644
--- a/pw_toolchain/dummy/BUILD.gn
+++ b/pw_toolchain/dummy/BUILD.gn
@@ -12,18 +12,15 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
-# gn-format disable
-import("//build_overrides/pigweed.gni")
-
-import("$dir_pigweed/legacy_target.gni")
-import("$dir_pw_toolchain/generate_toolchain.gni")
-
 # A dummy toolchain which is set as the default for Pigweed. This is never used;
 # the top-level BUILD.gn enumerates the toolchains for each build.
-generate_toolchain("dummy") {
-  cc = "gcc"
-  cxx = "g++"
-  ar = "ar"
-  defaults = {
+toolchain("dummy") {
+  tool("stamp") {
+    if (host_os == "win") {
+      command = "cmd /c type nul > \"{{output}}\""
+    } else {
+      command = "touch {{output}}"
+    }
+    description = "stamp {{output}}"
   }
 }
diff --git a/pw_toolchain/host_clang/toolchains.gni b/pw_toolchain/host_clang/toolchains.gni
index 8737b61..a4e64cc 100644
--- a/pw_toolchain/host_clang/toolchains.gni
+++ b/pw_toolchain/host_clang/toolchains.gni
@@ -52,7 +52,8 @@
     "$dir_pw_toolchain/host_clang:xcode_sysroot",
   ]
   if (pw_toolchain_SANITIZER != "") {
-    configs += [ "$dir_pw_toolchain/host_clang:sanitize_$pw_toolchain_SANITIZER" ]
+    configs +=
+        [ "$dir_pw_toolchain/host_clang:sanitize_$pw_toolchain_SANITIZER" ]
   }
   if (pw_toolchain_OSS_FUZZ_ENABLED) {
     default_configs += oss_fuzz_added_configs
diff --git a/targets/docs/BUILD.gn b/targets/docs/BUILD.gn
index 82d2f30..9f1f394 100644
--- a/targets/docs/BUILD.gn
+++ b/targets/docs/BUILD.gn
@@ -15,8 +15,50 @@
 # gn-format disable
 import("//build_overrides/pigweed.gni")
 
-import("$dir_pigweed/legacy_target.gni")
 import("$dir_pw_docgen/docs.gni")
+import("$dir_pw_toolchain/arm_gcc/toolchains.gni")
+import("$dir_pw_toolchain/generate_toolchain.gni")
+
+# Toolchain for generating upstream Pigweed documentation.
+generate_toolchain("docs") {
+  # Use the Cortex M4 toolchain for regular pw_size_report targets.
+  forward_variables_from(arm_gcc_cortex_m4_os,
+                         "*",
+                         [
+                           "defaults",
+                           "name",
+                         ])
+  defaults = {
+    forward_variables_from(arm_gcc_cortex_m4_os.defaults, "*")
+
+    # This is the docs target.
+    pw_docgen_BUILD_DOCS = true
+
+    _arm_bloaty_config = "$dir_pw_boot_armv7m/bloaty_config.bloaty"
+
+    pw_bloat_BLOATY_CONFIG = _arm_bloaty_config
+
+    # Toolchains to compare in documentation size reports.
+    pw_bloat_TOOLCHAINS = [
+      {
+        name = "arm-none-eabi-gcc -Og -mcpu=cortex-m4"
+        target = "$dir_pw_toolchain:arm_gcc_cortex_m4_og"
+        bloaty_config = _arm_bloaty_config
+      },
+      {
+        name = "arm-none-eabi-gcc -Os -mcpu=cortex-m4"
+        target = "$dir_pw_toolchain:arm_gcc_cortex_m4_os"
+        bloaty_config = _arm_bloaty_config
+      },
+      {
+        name = "arm-none-eabi-gcc -O2 -mcpu=cortex-m4"
+        target = "$dir_pw_toolchain:arm_gcc_cortex_m4_o2"
+        bloaty_config = _arm_bloaty_config
+      },
+    ]
+  }
+}
+
 pw_doc_group("target_docs") {
   sources = [ "target_docs.rst" ]
 }
diff --git a/targets/docs/target_config.gni b/targets/docs/target_config.gni
deleted file mode 100644
index 2405726..0000000
--- a/targets/docs/target_config.gni
+++ /dev/null
@@ -1,47 +0,0 @@
-# Copyright 2020 The Pigweed 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
-#
-#     https://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.
-
-# Get location of dir_pigweed.
-import("//build_overrides/pigweed.gni")
-
-import("$dir_pigweed/pw_vars_default.gni")
-_arm_bloaty_config = "$dir_pw_boot_armv7m/bloaty_config.bloaty"
-
-# Targets are required to set a default toolchain. Use the Cortex M4 ARM
-# toolchain for old pw_size_report targets.
-pw_target_toolchain = "$dir_pw_toolchain:arm_gcc_cortex_m4_og"
-
-pw_executable_config.bloaty_config_file = _arm_bloaty_config
-
-# Special upstream build variable to indicate that this is a docs-only target.
-pw_is_docs_target = true
-
-# Toolchains to compare in documentation size reports.
-pw_size_report_toolchains = [
-  {
-    name = "arm-none-eabi-gcc -Og -mcpu=cortex-m4"
-    target = "$dir_pw_toolchain:arm_gcc_cortex_m4_og"
-    bloaty_config = _arm_bloaty_config
-  },
-  {
-    name = "arm-none-eabi-gcc -Os -mcpu=cortex-m4"
-    target = "$dir_pw_toolchain:arm_gcc_cortex_m4_os"
-    bloaty_config = _arm_bloaty_config
-  },
-  {
-    name = "arm-none-eabi-gcc -O2 -mcpu=cortex-m4"
-    target = "$dir_pw_toolchain:arm_gcc_cortex_m4_o2"
-    bloaty_config = _arm_bloaty_config
-  },
-]
diff --git a/targets/host/linux/BUILD.gn b/targets/host/linux/BUILD.gn
index 8627a81..b14c133 100644
--- a/targets/host/linux/BUILD.gn
+++ b/targets/host/linux/BUILD.gn
@@ -17,16 +17,20 @@
 
 import("$dir_pw_toolchain/generate_toolchain.gni")
 import("$dir_pw_toolchain/host_clang/toolchains.gni")
-
 linux_toolchains = [
   {
     name = "clang_og"
-    forward_variables_from(host_clang_og, "*", [ "defaults", "name" ])
+    forward_variables_from(host_clang_og,
+                           "*",
+                           [
+                             "defaults",
+                             "name",
+                           ])
     defaults = {
       forward_variables_from(host_clang_og.defaults, "*")
       import("linux.gni")
     }
-  }
+  },
 ]
 
 generate_toolchains("linux") {