Update to new bazel rules

- Update WORKSPACE
- Remove sdk-integration and mako
- Update instructions and bootstrap/build scripts
- Migrate BUILD.bazel files to switch from _ to . in library names
- Migrate BUILD.bazel files to reflect new bind rule names

Change-Id: I6db333e50c3e9e0799cf12d2c2cc9f44407ee899
Reviewed-on: https://fuchsia-review.googlesource.com/c/drivers/wlan/intel/iwlwifi/+/642663
Fuchsia-Auto-Submit: Renato Mangini Dias <mangini@google.com>
Reviewed-by: Sakthi Vignesh Radhakrishnan <rsakthi@google.com>
Commit-Queue: Auto-Submit <auto-submit@fuchsia-infra.iam.gserviceaccount.com>
diff --git a/.bazelrc b/.bazelrc
index 9085af9..d0a32bc 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -1,8 +1,8 @@
-build:fuchsia_x64 --crosstool_top=@fuchsia_crosstool//:toolchain
+build:fuchsia_x64 --crosstool_top=@fuchsia_clang//:toolchain
 build:fuchsia_x64 --cpu=x86_64
 build:fuchsia_x64 --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
 
-build:fuchsia_arm64 --crosstool_top=@fuchsia_crosstool//:toolchain
+build:fuchsia_arm64 --crosstool_top=@fuchsia_clang//:toolchain
 build:fuchsia_arm64 --cpu=aarch64
 build:fuchsia_arm64 --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
 
diff --git a/.gitignore b/.gitignore
index 63d1bec..a7b3245 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,7 +19,7 @@
 bazel-*
 
 # fuchsia sdk downloaded manually by the user (see README.md)
-sdk
+/fuchsia_sdk.tar.gz
 
 # compiledb information for IDE integration
 **/.cache/clangd
diff --git a/.gitmodules b/.gitmodules
index 61c63b7..e69de29 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,6 +0,0 @@
-[submodule "sdk-integration"]
-	path = sdk-integration
-	url = https://fuchsia.googlesource.com/sdk-integration
-[submodule "third_party/mako"]
-	path = third_party/mako
-	url = https://fuchsia.googlesource.com/third_party/mako
diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel
index 7a9b345..caabebe 100644
--- a/WORKSPACE.bazel
+++ b/WORKSPACE.bazel
@@ -1,37 +1,67 @@
-# Copyright 2021 The Fuchsia Authors. All rights reserved.
+# Copyright 2022 The Fuchsia Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-local_repository(
-    name = "fuchsia_sdk",
-    path = "bazel-rules",
-)
+load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
 
-load("@fuchsia_sdk//build_defs:fuchsia_setup.bzl", "fuchsia_setup")
-fuchsia_setup(
-    with_toolchain = True,
-)
-
-# Setup the rules imported from workstation. Eventually we should use only one set of rules.
-local_repository(
+# use the "--override_repository" argument to load a local sdk-integration repository for development.
+# for example:
+#    tools/bazel build --config=fuchsia_x64 --override_repository=rules_fuchsia=<absolute_path_to>/sdk-integration/bazel_rules_fuchsia "target"
+git_repository(
     name = "rules_fuchsia",
-    path = "sdk-integration/bazel_rules_fuchsia",
+    remote = "https://fuchsia.googlesource.com/sdk-integration",
+
+    # TODO: use a release tag or a specific commit hash to ensure
+    # hermeciticy and reproductability.
+    branch = "main",
+
+    # use patch_cmds instead of strip_prefix because of https://github.com/bazelbuild/bazel/issues/10062
+    # we need to remove /tools before moving because there is a bazel_rules_fuchsia/tools directory
+    patch_cmds = ["rm -R tools", "mv bazel_rules_fuchsia/* ."],
 )
 
-load("@rules_fuchsia//fuchsia:deps.bzl", "rules_fuchsia_deps")
-rules_fuchsia_deps()
-
-
 load(
     "@rules_fuchsia//fuchsia:deps.bzl",
+    "fuchsia_clang_repository",
     "fuchsia_sdk_repository",
+    "rules_fuchsia_deps",
 )
+
+rules_fuchsia_deps()
+
+# set an environment variable "BAZEL_FUCHSIA_SDK_ARCHIVE=" to use a local SDK tarball instead
+# fetching from CIPD.
 fuchsia_sdk_repository(
-    name = "fuchsia_sdk_from_workstation",
+    name = "fuchsia_sdk",
+    cipd_tag = "version:7.20220201.2.1",
     sha256 = {
-        "linux": "ffe0d1c48ddc300383f5b645ee0bd79229feccfd5f6c205e112fec2cf976be86",
-        "mac": "4c93c6a530354747d3de18804d420b2ed455f6dc943421b1497200331d5ab4cc",
+        "linux": "cd27c5acd36f71a2c20c77d4ef8b16bd4bc4c8bab6f9f979b8c66183d6145df1",
+        "mac": "16a34eccf3b0e871aa4f40331ed14f40ff35ad190c93004a813361f9491bc5fa",
     },
-    cipd_tag = "version:6.20211029.2.1",
 )
-register_toolchains("@fuchsia_sdk_from_workstation//:fuchsia_toolchain_sdk")
+
+register_toolchains("@fuchsia_sdk//:fuchsia_toolchain_sdk")
+
+fuchsia_clang_repository(
+    name = "fuchsia_clang",
+    cipd_tag = "git_revision:c9e46219f38da5c3fbfe41012173dc893516826e",
+    sdk_root_label = "@fuchsia_sdk",
+    sha256 = {
+        "linux": "573ebb62fc5cd9d2357be630cc079fa97b028cd99e2e87132dcd8be31c425984",
+        "mac": "fb9e478d18f35d0a9bb186138a85cf7ef5f9078fbf432fc3ad8f0660b233b3b4",
+    },
+)
+
+# Compilation database
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+
+http_archive(
+    name = "com_grail_bazel_compdb",
+    sha256 = "d32835b26dd35aad8fd0ba0d712265df6565a3ad860d39e4c01ad41059ea7eda",
+    strip_prefix = "bazel-compilation-database-0.5.2",
+    urls = ["https://github.com/grailbio/bazel-compilation-database/archive/0.5.2.tar.gz"],
+)
+
+load("@com_grail_bazel_compdb//:deps.bzl", "bazel_compdb_deps")
+
+bazel_compdb_deps()
diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh
index 458114b..94fe178 100755
--- a/scripts/bootstrap.sh
+++ b/scripts/bootstrap.sh
@@ -10,7 +10,9 @@
 
 readonly REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/..
 readonly BAZELISK_TOOL="${REPO_ROOT}/tools/bazel"
-readonly LOCAL_SDK_OUT="${REPO_ROOT}/sdk"
+readonly OVERRIDE_SDK="${REPO_ROOT}/fuchsia_sdk.tar.gz"
+readonly WORKSPACE_NAME="$(basename "$REPO_ROOT")"
+readonly SDK_WORKSPACE_DIR="${REPO_ROOT}/bazel-${WORKSPACE_NAME}/external/fuchsia_sdk_dynamic"
 
 function get_os {
   uname -s | tr '[:upper:]' '[:lower:]'
@@ -47,7 +49,7 @@
 }
 
 create_tools_symlinks() {
-  create_symlink "${LOCAL_SDK_OUT}/tools/x64/ffx" ${REPO_ROOT}/tools/ffx
+  create_symlink "${SDK_WORKSPACE_DIR}/tools/x64/ffx" ${REPO_ROOT}/tools/ffx
 }
 
 # TODO: this is probably not needed when the DDK becomes part of the core SDK.
@@ -57,13 +59,11 @@
     echo "ERROR: sdk archive cannot be found: $SDK_ARCHIVE"
     exit 1
   fi
-  mkdir -p sdk
-  echo "Extracting SDK from archive..."
-  tar xzf "$SDK_ARCHIVE" -C sdk
-  echo "Done extracting SDK from archive."
+  echo "Copying SDK archive to ${OVERRIDE_SDK}..."
+  rsync --checksum "$SDK_ARCHIVE" "$OVERRIDE_SDK"
+  echo "Done copying SDK archive."
 }
 
-
 # TODO: this is probably not needed when the DDK becomes part of the core SDK.
 update_sdk_from_local_build() {
   SDK_OUT="$1"
@@ -110,62 +110,40 @@
     fi
   done
 
-  if [[ -d "$LOCAL_SDK_OUT" ]]; then
-    echo "Removing old SDK in $LOCAL_SDK_OUT"
-    rm -Rf "${LOCAL_SDK_OUT}"
+  if [[ -d "$OVERRIDE_SDK" ]]; then
+    echo "Removing old SDK in $OVERRIDE_SDK"
+    rm -f "$OVERRIDE_SDK"
   fi
+
   echo "Syncing SDK from local build..."
-  mkdir "${LOCAL_SDK_OUT}"
-  tar xzf "${SDK_OUT}/sdk/archive/core.tar.gz" -C "${LOCAL_SDK_OUT}"
-  tar xzf "${SDK_OUT}/sdk/archive/ddk.tar.gz" -C "${LOCAL_SDK_OUT}" --transform='s/meta\/manifest.json/meta\/ddk_manifest.json/'
-  rsync -aR "${PBM_FILES[@]}" "${LOCAL_SDK_OUT}"
-}
+  TMP_CORE_DIR="$(mktemp -d)"
+  TMP_DDK_DIR="$(mktemp -d)"
+  tar xzf "${SDK_OUT}/sdk/archive/core.tar.gz" -C "${TMP_CORE_DIR}"
+  tar xzf "${SDK_OUT}/sdk/archive/ddk.tar.gz" -C "${TMP_DDK_DIR}"
+  mv "${TMP_DDK_DIR}/meta/manifest.json" "${TMP_DDK_DIR}/meta/ddk_manifest.json" 
 
-generate_rules_fuchsia() {
-  echo "Generating Fuchsia Bazel rules..."
-  if [[ ! -d "${REPO_ROOT}/sdk" ]]; then
-    echo "Warning! Cannot find directory 'sdk', please use argument --sdk-archive or --sdk-local and --product-bundle-local"
-    echo "Bazel rules will not be generated for the Fuchsia SDK. Please run bootstrap.sh again with the proper argument."
-    return
-    # TODO(mangini) exit on error here. Disabling error for now to avoid
-    # triggering CI/CQ, since we cannot run this in CI/CQ until the DDK is
-    # published in an accessible place.
-    # exit 1
-  fi
-  pushd "${REPO_ROOT}/sdk-integration/bazel_rules_generator/" > /dev/null || exit
-  ./generate.py \
-    --directory "${REPO_ROOT}/sdk" \
-    --output "${REPO_ROOT}/bazel-rules"
-  popd > /dev/null || exit
-  echo "Done generating Fuchsia Bazel rules."
+  rsync -aR "${PBM_FILES[@]}" "$TMP_CORE_DIR"
+  tar czvf "$OVERRIDE_SDK" -C "$TMP_CORE_DIR" . -C "$TMP_DDK_DIR" .
+  rm -Rf "$TMP_CORE_DIR" "$TMP_DDK_DIR"
+  echo "Done syncing SDK from local build."
 }
 
-
 syntax() {
     echo "   $(basename $0)"
-    echo "      assume that there is a directory 'sdk' with an SDK, DDK and"
-    echo "      product bundle files for the emulator"
+    echo "      fetch from CIPD the SDK defined in WORKSPACE.bazel or use"
+    echo "      ${OVERRIDE_SDK##REPO_ROOT/} if one is present."
     echo
     echo "   $(basename $0) --sdk-archive SDK_TARGZ"
     echo "      load an SDK and the emulator product bundle files from the"
-    echo "      given tar.gz archive."
+    echo "      given tar.gz archive. See README.md on how to generate this archive."
     echo
     echo "   $(basename $0) --sdk-local FUCHSIA_OUT_SDK --product-bundle-local FUCHSIA_OUT_PRODUCT_BUNDLE"
     echo "      copy the SDK from a build directory of a local Fuchsia tree,"
     echo "      and copy the product bundle files for the emulator from another"
-    echo "      build directory."
-}
-
-
-update_git_submodules() {
-  echo "Updating git submodules"
-  git submodule update --init --recursive
+    echo "      build directory. See README.md for more information."
 }
 
 main() {
-
-  update_git_submodules
-
   if [[ $# -gt 0 ]]; then
     if [[ "$1" == "--sdk-archive" && $# -eq 2 ]]; then
       update_sdk_from_archive "$2"
@@ -182,7 +160,8 @@
 
   create_bazel_symlink
   create_tools_symlinks
-  generate_rules_fuchsia
+  # symlink to compile commands
+  ln -f -s "${REPO_ROOT}/bazel-bin/src/compile_commands.json" "${REPO_ROOT}/compile_commands.json"
 }
 
 main "$@"
diff --git a/scripts/build.sh b/scripts/build.sh
index d80dc10..d4a8ea8 100755
--- a/scripts/build.sh
+++ b/scripts/build.sh
@@ -6,15 +6,24 @@
 
 set -e
 
-# This script is used by infra to validate the driver can be built and tests
-# can pass.
+# This script is used by infra to validate that the driver can be built and
+# tests pass.
 
 readonly REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/..
 readonly BAZELISK_TOOL="${REPO_ROOT}/tools/bazel"
+readonly OVERRIDE_SDK="${REPO_ROOT}/fuchsia_sdk.tar.gz"
 
 main() {
-  if [[ -d "${REPO_ROOT}/sdk" ]]; then
-    "$BAZELISK_TOOL" build --config=fuchsia_x64 third_party/iwlwifi:core
+  if [[ -f "$OVERRIDE_SDK" && -z "$BAZEL_FUCHSIA_SDK_ARCHIVE" ]]; then
+    export BAZEL_FUCHSIA_SDK_ARCHIVE="$OVERRIDE_SDK"
+  fi
+  if [[ -n "$BAZEL_FUCHSIA_SDK_ARCHIVE" ]]; then
+    if [[ ! -f "$BAZEL_FUCHSIA_SDK_ARCHIVE" ]]; then
+      echo -e >&2 "\033[1;31mERROR:\033[0m Local SDK does not exist, please fix or unset variable BAZEL_FUCHSIA_SDK_ARCHIVE: ${BAZEL_FUCHSIA_SDK_ARCHIVE}"
+      exit 1
+    fi
+    echo -e >&2 "\033[1;33mINFO:\033[0m Using a local SDK: ${BAZEL_FUCHSIA_SDK_ARCHIVE##$REPO_ROOT/}"
+    "$BAZELISK_TOOL" build --config=fuchsia_x64 third_party/iwlwifi/platform:fuchsia_device "$@"
     echo "Everything build and all tests passed."
   else
     # TODO(mangini): handle this appropriately when the DDK is published by
diff --git a/sdk-integration b/sdk-integration
deleted file mode 160000
index efc106c..0000000
--- a/sdk-integration
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit efc106c53609c74db4335368dc6b3a3c6f9c3d72
diff --git a/third_party/iwlwifi/fw/BUILD.bazel b/third_party/iwlwifi/fw/BUILD.bazel
index 7a841ef..440949c 100644
--- a/third_party/iwlwifi/fw/BUILD.bazel
+++ b/third_party/iwlwifi/fw/BUILD.bazel
@@ -52,7 +52,7 @@
     deps = [
         "//third_party/iwlwifi/platform:platform",
         "@fuchsia_sdk//pkg/ddk",
-        "@fuchsia_sdk//fidl/fuchsia_wlan_ieee80211:fuchsia_wlan_ieee80211_banjo_cc",
+        "@fuchsia_sdk//fidl/fuchsia.wlan.ieee80211:fuchsia.wlan.ieee80211_banjo_cc",
         ],
 )
 
diff --git a/third_party/iwlwifi/mvm/BUILD.bazel b/third_party/iwlwifi/mvm/BUILD.bazel
index 8a9a5dc..1d96a6c 100644
--- a/third_party/iwlwifi/mvm/BUILD.bazel
+++ b/third_party/iwlwifi/mvm/BUILD.bazel
@@ -43,10 +43,10 @@
    "tof.h",
   ],
   deps = [
-    "@fuchsia_sdk//fidl/fuchsia_hardware_wlan_phyinfo:fuchsia_hardware_wlan_phyinfo_banjo_cc",
-    "@fuchsia_sdk//fidl/fuchsia_hardware_wlan_associnfo:fuchsia_hardware_wlan_associnfo_banjo_cc",
-    "@fuchsia_sdk//fidl/fuchsia_hardware_wlanphyimpl:fuchsia_hardware_wlanphyimpl_banjo_cc",
-    "@fuchsia_sdk//fidl/fuchsia_wlan_ieee80211:fuchsia_wlan_ieee80211_llcpp_cc",
+    "@fuchsia_sdk//fidl/fuchsia.hardware.wlan.phyinfo:fuchsia.hardware.wlan.phyinfo_banjo_cc",
+    "@fuchsia_sdk//fidl/fuchsia.hardware.wlan.associnfo:fuchsia.hardware.wlan.associnfo_banjo_cc",
+    "@fuchsia_sdk//fidl/fuchsia.hardware.wlanphyimpl:fuchsia.hardware.wlanphyimpl_banjo_cc",
+    "@fuchsia_sdk//fidl/fuchsia.wlan.ieee80211:fuchsia.wlan.ieee80211_llcpp_cc",
     "//third_party/iwlwifi:core",
     "//third_party/iwlwifi/fw:fw",
     "//third_party/iwlwifi/fw:api",
diff --git a/third_party/iwlwifi/platform/BUILD.bazel b/third_party/iwlwifi/platform/BUILD.bazel
index c4e9a94..10e1ba2 100644
--- a/third_party/iwlwifi/platform/BUILD.bazel
+++ b/third_party/iwlwifi/platform/BUILD.bazel
@@ -5,22 +5,26 @@
 
 package(default_visibility = ["//visibility:public"])
 
-load("@fuchsia_sdk//build_defs:driver_bind_rules.bzl", "driver_header_bind_rules", "driver_bytecode_bind_rules")
+load(
+    "@rules_fuchsia//fuchsia:defs.bzl",
+    "fuchsia_driver_header_bind_rules",
+    "fuchsia_driver_bytecode_bind_rules",
+)
 
-driver_header_bind_rules(
+fuchsia_driver_header_bind_rules(
     name = "fuchsia_bind_header",
     rules = "iwlwifi.bind",
     output = "iwlwifi-bind.h",
 
-    deps = ["@fuchsia_sdk//bind/fuchsia_pci"],
+    deps = ["@fuchsia_sdk//bind/fuchsia.pci"],
 )
 
-driver_bytecode_bind_rules(
+fuchsia_driver_bytecode_bind_rules(
     name = "fuchsia_bind_bytecode",
     rules = "iwlwifi.bind",
     output = "iwlwifi-bind.bc",
 
-    deps = ["@fuchsia_sdk//bind/fuchsia_usb"],
+    deps = ["@fuchsia_sdk//bind/fuchsia.usb"],
 )
 
 cc_library(
@@ -78,14 +82,14 @@
         ":driver_inspector",
         ":rcu_manager",
         "@fuchsia_sdk//pkg/ddk",
-        "@fuchsia_sdk//fidl/fuchsia_hardware_pci:fuchsia_hardware_pci_banjo_cc",
-        "@fuchsia_sdk//fidl/fuchsia_hardware_wlan_phyinfo:fuchsia_hardware_wlan_phyinfo_banjo_cc",
-        "@fuchsia_sdk//fidl/fuchsia_hardware_wlan_softmac:fuchsia_hardware_wlan_softmac_banjo_cc",
+        "@fuchsia_sdk//fidl/fuchsia.hardware.pci:fuchsia.hardware.pci_banjo_cc",
+        "@fuchsia_sdk//fidl/fuchsia.hardware.wlan.phyinfo:fuchsia.hardware.wlan.phyinfo_banjo_cc",
+        "@fuchsia_sdk//fidl/fuchsia.hardware.wlan.softmac:fuchsia.hardware.wlan.softmac_banjo_cc",
 
         # Needed only for library purposes (channel and ieee includes)
-        "@fuchsia_sdk//fidl/fuchsia_wlan_common:fuchsia_wlan_common_cc",
-        "@fuchsia_sdk//fidl/fuchsia_wlan_mlme:fuchsia_wlan_mlme_cc",
-        "@fuchsia_sdk//fidl/fuchsia_hardware_wlan_associnfo:fuchsia_hardware_wlan_associnfo_banjo_cc",
+        "@fuchsia_sdk//fidl/fuchsia.wlan.common:fuchsia.wlan.common_cc",
+        "@fuchsia_sdk//fidl/fuchsia.wlan.mlme:fuchsia.wlan.mlme_cc",
+        "@fuchsia_sdk//fidl/fuchsia.hardware.wlan.associnfo:fuchsia.hardware.wlan.associnfo_banjo_cc",
         ],
 )
 
@@ -96,7 +100,7 @@
     hdrs = [ "rcu-manager.h" ],
     deps = [
         "@fuchsia_sdk//pkg/async",
-        "@fuchsia_sdk//pkg/async_cpp",
+        "@fuchsia_sdk//pkg/async-cpp",
     ],
 )
 
@@ -124,19 +128,19 @@
         ":fuchsia_bind_header",
         ":platform",
         ":rcu_manager",
-        "@fuchsia_sdk//pkg/ddktl_experimental_driver_only",
-        "@fuchsia_sdk//fidl/fuchsia_hardware_wlan_phyinfo:fuchsia_hardware_wlan_phyinfo_banjo_cc",
-        "@fuchsia_sdk//fidl/fuchsia_hardware_wlan_softmac:fuchsia_hardware_wlan_softmac_banjo_cc",
-        "@fuchsia_sdk//fidl/fuchsia_hardware_wlanphyimpl:fuchsia_hardware_wlanphyimpl_banjo_cc",
-        "@fuchsia_sdk//fidl/fuchsia_wlan_common:fuchsia_wlan_common_banjo_cc",
-        "@fuchsia_sdk//fidl/fuchsia_wlan_ieee80211:fuchsia_wlan_ieee80211_cc",
-        "@fuchsia_sdk//fidl/fuchsia_wlan_internal:fuchsia_wlan_internal_banjo_cc",
+        "@fuchsia_sdk//pkg/ddktl-experimental-driver-only",
+        "@fuchsia_sdk//fidl/fuchsia.hardware.wlan.phyinfo:fuchsia.hardware.wlan.phyinfo_banjo_cc",
+        "@fuchsia_sdk//fidl/fuchsia.hardware.wlan.softmac:fuchsia.hardware.wlan.softmac_banjo_cc",
+        "@fuchsia_sdk//fidl/fuchsia.hardware.wlanphyimpl:fuchsia.hardware.wlanphyimpl_banjo_cc",
+        "@fuchsia_sdk//fidl/fuchsia.wlan.common:fuchsia.wlan.common_banjo_cc",
+        "@fuchsia_sdk//fidl/fuchsia.wlan.ieee80211:fuchsia.wlan.ieee80211_cc",
+        "@fuchsia_sdk//fidl/fuchsia.wlan.internal:fuchsia.wlan.internal_banjo_cc",
         "//third_party/iwlwifi:core",
         "//third_party/iwlwifi/mvm:mvm",
         "//third_party/iwlwifi/pcie",
         "@fuchsia_sdk//pkg/ddk",
-        "@fuchsia_sdk//pkg/async_loop_cpp",
-        "@fuchsia_sdk//pkg/async_loop_default",
+        "@fuchsia_sdk//pkg/async-loop-cpp",
+        "@fuchsia_sdk//pkg/async-loop-default",
     ],
 
     copts = ["-Ithird_party/iwlwifi"],
diff --git a/third_party/mako b/third_party/mako
deleted file mode 160000
index c374704..0000000
--- a/third_party/mako
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit c37470481fccb86c2ede492cbbbabba2b6c238a1