Update CMake packages to anchor on iree:: for top level dirs. (#9220)

Progress on https://github.com/google/iree/issues/8955, forked from the discussion at https://github.com/google/iree/pull/9203#discussion_r881838068.

This updates our CMake target mappings / package namespaces to be implicitly rooted on `iree::`, notably for the top level `build_tools/`, `samples/`, `tests/`, and `tools/` directories.

The only exceptions are
* `llvm-external-projects/`
* `tools::` targets which sit at the root (`iree-compile` instead of `iree_iree-compile`)
* `compiler/src/iree/` and `runtime/src/iree/`

With this, all of our CMake target and test names begin with `iree_`, `iree-`, or `IREE` again, so we don't have loose / unqualified names like `build_tools/benchmarks/common/benchmark_config_test`, `tools/test/benchmark_flags.txt.test`, or `runtime_bindings_python_PyExtRt`.
diff --git a/build_tools/bazel_to_cmake/bazel_to_cmake_targets.py b/build_tools/bazel_to_cmake/bazel_to_cmake_targets.py
index d071aa1..7946697 100644
--- a/build_tools/bazel_to_cmake/bazel_to_cmake_targets.py
+++ b/build_tools/bazel_to_cmake/bazel_to_cmake_targets.py
@@ -197,15 +197,15 @@
   return [target.rsplit(":")[-1]]
 
 
-def _convert_bazel_path(bazel_path: str) -> str:
-  # Bazel `:api`            -> CMake `::api`
+def _convert_to_cmake_path(bazel_path_fragment: str) -> str:
+  cmake_path = bazel_path_fragment
   # Bazel `//iree/base`     -> CMake `iree::base`
   # Bazel `//iree/base:foo` -> CMake `iree::base::foo`
-  if bazel_path.startswith("//"):
-    bazel_path = bazel_path[len("//"):]
-  bazel_path = bazel_path.replace(":", "::")  # iree/base::foo or ::foo
-  bazel_path = bazel_path.replace("/", "::")  # iree::base
-  return bazel_path
+  if cmake_path.startswith("//"):
+    cmake_path = cmake_path[len("//"):]
+  cmake_path = cmake_path.replace(":", "::")  # iree/base::foo or ::foo
+  cmake_path = cmake_path.replace("/", "::")  # iree::base
+  return cmake_path
 
 
 def convert_target(target):
@@ -230,40 +230,40 @@
     return _convert_llvm_target(target)
   if target.startswith("@llvm-project//mlir"):
     return _convert_mlir_target(target)
+  if target.startswith("@"):
+    raise KeyError(f"No conversion found for target '{target}'")
+
   if target.startswith("//llvm-external-projects/iree-dialects"):
     return _convert_iree_dialects_target(target)
 
   # IREE root paths map to package names based on explicit rules.
+  #   * src/iree/ directories (compiler/src/iree/ and runtime/src/iree/)
+  #     creating their own root paths by trimming down to just "iree"
+  #   * tools/ uses an empty root, for binary targets names like "iree-compile"
+  #   * other top level directories add back an 'iree' prefix
   # If changing these, make the corresponding change in iree_macros.cmake
   # (iree_package_ns function).
 
   # Map //compiler/src/iree/(.*) -> iree::\1 (i.e. iree::compiler::\1)
   m = re.match("^//compiler/src/iree/(.+)", target)
   if m:
-    return ["iree::" + _convert_bazel_path(m.group(1))]
+    return ["iree::" + _convert_to_cmake_path(m.group(1))]
 
   # Map //runtime/src/iree/(.*) -> iree::\1
   m = re.match("^//runtime/src/iree/(.+)", target)
   if m:
-    return ["iree::" + _convert_bazel_path(m.group(1))]
-
-  # Map //runtime/bindings/(.*) -> iree::bindings::\1
-  m = re.match("^//runtime/bindings/(.+)", target)
-  if m:
-    return ["iree::bindings::" + _convert_bazel_path(m.group(1))]
-
-  # Map //samples/(.*) -> iree::samples::\1
-  m = re.match("^//samples[/|:](.+)", target)
-  if m:
-    return ["iree::samples::" + _convert_bazel_path(m.group(1))]
+    return ["iree::" + _convert_to_cmake_path(m.group(1))]
 
   # Map //tools/(.*) -> \1
   m = re.match("^//tools[/|:](.+)", target)
   if m:
-    return [_convert_bazel_path(m.group(1))]
+    return [_convert_to_cmake_path(m.group(1))]
 
-  # Default (legacy) rewrite.
-  if not target.startswith("@"):
-    return [_convert_bazel_path(target)]
+  # Pass through package-relative targets
+  #   :target_name
+  #   file_name.txt
+  if target.startswith(":") or ":" not in target:
+    return [_convert_to_cmake_path(target)]
 
-  raise KeyError(f"No conversion found for target '{target}'")
+  # Default rewrite: prefix with "iree::", without pruning the path.
+  return ["iree::" + _convert_to_cmake_path(target)]
diff --git a/build_tools/cmake/iree_c_module.cmake b/build_tools/cmake/iree_c_module.cmake
index f57ca45..f6ccc68 100644
--- a/build_tools/cmake/iree_c_module.cmake
+++ b/build_tools/cmake/iree_c_module.cmake
@@ -70,12 +70,12 @@
     HDRS "${_RULE_H_FILE_OUTPUT}"
     SRCS "${IREE_SOURCE_DIR}/runtime/src/iree/vm/module_impl_emitc.c"
     INCLUDES "${CMAKE_CURRENT_BINARY_DIR}"
-    COPTS 
+    COPTS
       "-DEMITC_IMPLEMENTATION=\"${_RULE_H_FILE_OUTPUT}\""
       "${_TESTONLY_ARG}"
     DEPS
       # Include paths and options for the runtime sources.
-      iree::runtime::defs
+      iree::runtime::src::defs
   )
 
   set(_GEN_TARGET "${_NAME}_gen")
diff --git a/build_tools/cmake/iree_cc_binary.cmake b/build_tools/cmake/iree_cc_binary.cmake
index 9f1049f..c89d802 100644
--- a/build_tools/cmake/iree_cc_binary.cmake
+++ b/build_tools/cmake/iree_cc_binary.cmake
@@ -23,9 +23,7 @@
 #
 # Note:
 # iree_cc_binary will create a binary called ${PACKAGE_NAME}_${NAME}, e.g.
-# iree_base_foo with two alias (readonly) targets, a qualified
-# ${PACKAGE_NS}::${NAME} and an unqualified ${NAME}. Thus NAME must be globally
-# unique in the project.
+# iree_base_foo with an alias to ${PACKAGE_NS}::${NAME}.
 #
 # Usage:
 # iree_cc_library(
@@ -84,12 +82,6 @@
     if("${_RULE_NAME}" STREQUAL "${_PACKAGE_DIR}")
       add_executable(${_PACKAGE_NS} ALIAS ${_NAME})
     endif()
-
-    # Finally, since we have so few binaries and we also want to support
-    # installing from a separate host build, binaries get an unqualified global
-    # alias. This means binary names must be unique across the whole project.
-    # (We could consider making this configurable).
-    add_executable(${_RULE_NAME} ALIAS ${_NAME})
   endif()
 
   set_target_properties(${_NAME} PROPERTIES OUTPUT_NAME "${_RULE_NAME}")
diff --git a/build_tools/cmake/iree_macros.cmake b/build_tools/cmake/iree_macros.cmake
index e3e38f2..a9098c5 100644
--- a/build_tools/cmake/iree_macros.cmake
+++ b/build_tools/cmake/iree_macros.cmake
@@ -53,8 +53,10 @@
 # Sets ${PACKAGE_NS} to the IREE-root relative package name in C++ namespace
 # format (::).
 #
-# Example when called from iree/base/CMakeLists.txt:
-#   iree::base
+# Examples:
+#   compiler/src/iree/compiler/Utils/CMakeLists.txt -> iree::compiler::Utils
+#   runtime/src/iree/base/CMakeLists.txt -> iree::base
+#   tests/e2e/CMakeLists.txt -> iree::tests::e2e
 function(iree_package_ns PACKAGE_NS)
   # Get the relative path of the current dir (i.e. runtime/src/iree/vm).
   string(REPLACE ${IREE_ROOT_DIR} "" _RELATIVE_PATH ${CMAKE_CURRENT_LIST_DIR})
@@ -67,25 +69,16 @@
   if(_RELATIVE_PATH MATCHES "^compiler/src/(.*)")
     # compiler/src/iree/compiler -> iree/compiler
     set(_PACKAGE "${CMAKE_MATCH_1}")
-  elseif(_RELATIVE_PATH MATCHES "^compiler/src$")
-    # compiler/src (defs) -> iree::compiler::defs
-    set(_PACKAGE "iree::compiler")
   elseif(_RELATIVE_PATH MATCHES "^runtime/src/(.*)")
     # runtime/src/iree/base -> iree/base
     set(_PACKAGE "${CMAKE_MATCH_1}")
-  elseif(_RELATIVE_PATH MATCHES "^runtime/src$")
-    # runtime/src (defs) -> iree::runtime::defs
-    set(_PACKAGE "iree::runtime")
-  elseif(_RELATIVE_PATH MATCHES "^samples/(.*)")
-    set(_PACKAGE "iree::samples::${CMAKE_MATCH_1}")
-  elseif(_RELATIVE_PATH MATCHES "^samples$")
-    set(_PACKAGE "iree::samples")
   elseif(_RELATIVE_PATH MATCHES "^tools$")
-    # tools/ -> "" (empty string), e.g. iree-opt -> iree-opt
+    # Special case for tools/ -> "" (empty string)
+    # For example, tools/iree-compile -> iree-compile (no namespace)
     set(_PACKAGE "")
   else()
-    # Default to pass-through.
-    set(_PACKAGE "${_RELATIVE_PATH}")
+    # Default to prefixing with iree/
+    set(_PACKAGE "iree/${_RELATIVE_PATH}")
   endif()
 
   string(REPLACE "/" "::" _PACKAGE_NS "${_PACKAGE}")
diff --git a/compiler/src/CMakeLists.txt b/compiler/src/CMakeLists.txt
index 7ccabcd..d6a8419 100644
--- a/compiler/src/CMakeLists.txt
+++ b/compiler/src/CMakeLists.txt
@@ -15,5 +15,6 @@
 
 # Configures all iree_cc_* targets to take this implicit dep,
 # which provides common includes and copts for the tree.
-set(IREE_IMPLICIT_DEFS_CC_DEPS iree::compiler::defs)
+set(IREE_IMPLICIT_DEFS_CC_DEPS iree::compiler::src::defs)
+
 add_subdirectory(iree/compiler)
diff --git a/runtime/bindings/python/CMakeLists.txt b/runtime/bindings/python/CMakeLists.txt
index 78a4829..5e1fff3 100644
--- a/runtime/bindings/python/CMakeLists.txt
+++ b/runtime/bindings/python/CMakeLists.txt
@@ -183,9 +183,7 @@
 
 # _runtime.so -> python_packages/iree_runtime/iree/_runtime.so
 install(
-  # TODO: Update CMake target/path mangling rules to make this syntactically
-  # rooted on "iree" in some way.
-  TARGETS runtime_bindings_python_PyExtRt
+  TARGETS iree_runtime_bindings_python_PyExtRt
   DESTINATION "${_INSTALL_DIR}/iree"
   COMPONENT "${_INSTALL_COMPONENT}"
 )
diff --git a/runtime/bindings/tflite/CMakeLists.txt b/runtime/bindings/tflite/CMakeLists.txt
index 5860072..3d2724a 100644
--- a/runtime/bindings/tflite/CMakeLists.txt
+++ b/runtime/bindings/tflite/CMakeLists.txt
@@ -53,9 +53,9 @@
     "smoke_test.cc"
   DEPS
     ::shim
-    runtime::bindings::tflite::testdata::add_dynamic_c
-    runtime::bindings::tflite::testdata::add_multi_c
-    runtime::bindings::tflite::testdata::add_static_c
+    iree::runtime::bindings::tflite::testdata::add_dynamic_c
+    iree::runtime::bindings::tflite::testdata::add_multi_c
+    iree::runtime::bindings::tflite::testdata::add_static_c
     iree::testing::gtest
     iree::testing::gtest_main
 )
diff --git a/runtime/bindings/tflite/java/org/tensorflow/lite/native/CMakeLists.txt b/runtime/bindings/tflite/java/org/tensorflow/lite/native/CMakeLists.txt
index c1eb4c3..c168a92 100644
--- a/runtime/bindings/tflite/java/org/tensorflow/lite/native/CMakeLists.txt
+++ b/runtime/bindings/tflite/java/org/tensorflow/lite/native/CMakeLists.txt
@@ -20,7 +20,7 @@
   PUBLIC
     iree::base
     iree::base::logging
-    runtime::bindings::tflite::shim
+    iree::runtime::bindings::tflite::shim
 )
 
 target_link_options(${_NAME}
diff --git a/runtime/src/CMakeLists.txt b/runtime/src/CMakeLists.txt
index c55bbd9..d9d95ff 100644
--- a/runtime/src/CMakeLists.txt
+++ b/runtime/src/CMakeLists.txt
@@ -15,6 +15,6 @@
 
 # Configures all iree_cc_* targets to take this implicit dep,
 # which provides common includes and copts for the tree.
-set(IREE_IMPLICIT_DEFS_CC_DEPS iree::runtime::defs)
+set(IREE_IMPLICIT_DEFS_CC_DEPS iree::runtime::src::defs)
 
 add_subdirectory(iree)
diff --git a/tests/e2e/linalg_transform/CMakeLists.txt b/tests/e2e/linalg_transform/CMakeLists.txt
index 0d974ac..e2a0b45 100644
--- a/tests/e2e/linalg_transform/CMakeLists.txt
+++ b/tests/e2e/linalg_transform/CMakeLists.txt
@@ -20,7 +20,7 @@
     FileCheck
     iree-run-mlir
   DATA
-    tests::e2e::linalg_transform::linalg_transform_spec.mlir
+    iree::tests::e2e::linalg_transform::linalg_transform_spec.mlir
   LABELS
     "hostonly"
 )
diff --git a/tools/BUILD b/tools/BUILD
index 27757f5..2d52f64 100644
--- a/tools/BUILD
+++ b/tools/BUILD
@@ -4,8 +4,15 @@
 # See https://llvm.org/LICENSE.txt for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-# Misc tools used to optimize, translate, and evaluate IREE.
-# Compiler tooling, like the compiler, is not designed to run on device and is tagged as "hostonly".
+# Tools IREE provides for compiling, executing, and benchmarking programs, as
+# well as other utilities.
+#
+# Only binary targets and their associated main files should go in this
+# directory. Library targets and header files should be placed in the
+# appropriate subtree, e.g. `compiler/src/iree/compiler/Tools/`.
+#
+# Programs with a dependency on the compiler are not designed to run on device
+# platforms (e.g. Android), so they are tagged "hostonly".
 
 package(
     default_visibility = ["//visibility:public"],
@@ -89,9 +96,10 @@
     ],
 )
 
-cc_library(
-    name = "iree_opt_main",
+cc_binary(
+    name = "iree-opt",
     srcs = ["iree-opt-main.cc"],
+    tags = ["hostonly"],
     deps = [
         "//compiler/src/iree/compiler/Tools:init_passes_and_dialects",
         "//compiler/src/iree/compiler/Tools:init_targets",
@@ -103,14 +111,6 @@
 )
 
 cc_binary(
-    name = "iree-opt",
-    tags = ["hostonly"],
-    deps = [
-        ":iree_opt_main",
-    ],
-)
-
-cc_binary(
     name = "iree-mlir-lsp-server",
     srcs = ["iree-mlir-lsp-server.cc"],
     deps = [
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index e9645b4..1e46674 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -4,7 +4,17 @@
 # See https://llvm.org/LICENSE.txt for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-# Doesn't use bazel_to_cmake because of various special logic throughout.
+# Tools IREE provides for compiling, executing, and benchmarking programs, as
+# well as other utilities.
+#
+# Only binary targets and their associated main files should go in this
+# directory. Library targets and header files should be placed in the
+# appropriate subtree, e.g. `compiler/src/iree/compiler/Tools/`.
+#
+# Programs with a dependency on the compiler are not designed to run on device
+# platforms (e.g. Android), so they are tagged "hostonly".
+#
+# This file does not use bazel_to_cmake because of special logic throughout.
 
 # TODO(#6353): Tools has thread dependencies in gtest, benchmark, yaml, etc.
 # This should be split between runtime/compiler with optional threading support.
@@ -197,9 +207,9 @@
     HOSTONLY
   )
 
-  iree_cc_library(
+  iree_cc_binary(
     NAME
-      iree_opt_main
+      iree-opt
     SRCS
       "iree-opt-main.cc"
     DEPS
@@ -209,14 +219,6 @@
       MLIRSupport
       iree::compiler::Tools::init_passes_and_dialects
       iree::compiler::Tools::init_targets
-    PUBLIC
-  )
-
-  iree_cc_binary(
-    NAME
-      iree-opt
-    DEPS
-      ::iree_opt_main
     DATA
       ${IREE_LLD_TARGET}
     HOSTONLY