Replace CMake `IREE_HOST_BINARY_ROOT` var with `IREE_HOST_BIN_DIR`. (#11812)

The new variable is documented in the root CMakeLists.txt and is
initialized from the (new deprecated) old variable.


```
iree-build/      # CMake build directory
  install/       # Set by -DCMAKE_INSTALL_PREFIX, IREE_HOST_BINARY_ROOT pointed here
    bin/         # IREE_HOST_BIN_DIR should point here
    lib/
    src/
    tests/
```

Now we can also point to other binary distributions that don't have
CMake's build/install structure.

---

Closes https://github.com/iree-org/iree/pull/11441. Fixes
https://github.com/iree-org/iree/issues/4662. This new variable approach
was discussed on
https://github.com/iree-org/iree/pull/11764#discussion_r1066436709.

Co-authored-by: Geoffrey Martin-Noble <gcmn@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 18d6eb6..1690a6b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -69,6 +69,16 @@
 set(IREE_RELEASE_REVISION "HEAD" CACHE STRING "Version control revision information to embed in distributed packages")
 option(IREE_EMBED_RELEASE_INFO "Embed the IREE version information in built artifacts." OFF)
 
+# Using already built host binaries, such as for cross-compilation.
+set(IREE_HOST_BIN_DIR_DEFAULT "")
+if(IREE_HOST_BINARY_ROOT)
+  message(WARNING "IREE_HOST_BINARY_ROOT is deprecated. Use IREE_HOST_BIN_DIR"
+                  " pointing directly to the directory containing binaries"
+                  " instead.")
+  set(IREE_HOST_BIN_DIR_DEFAULT "${IREE_HOST_BINARY_ROOT}/bin")
+endif()
+set(IREE_HOST_BIN_DIR "${IREE_HOST_BIN_DIR_DEFAULT}" CACHE STRING "Path to directory containing IREE binary tools to use instead of building them from source.")
+
 option(IREE_BUILD_BINDINGS_TFLITE "Builds the IREE TFLite C API compatibility shim" ON)
 option(IREE_BUILD_BINDINGS_TFLITE_JAVA "Builds the IREE TFLite Java bindings with the C API compatibility shim" ON)
 
diff --git a/build_tools/cmake/iree_benchmark_suite.cmake b/build_tools/cmake/iree_benchmark_suite.cmake
index 035bbba..af19e49 100644
--- a/build_tools/cmake/iree_benchmark_suite.cmake
+++ b/build_tools/cmake/iree_benchmark_suite.cmake
@@ -190,11 +190,11 @@
   )
 
   # Try to check if the compiler supports the TARGET_BACKEND. If
-  # IREE_HOST_BINARY_ROOT is set, we are using a compiler binary, in which
-  # case we can't check it's supported backends just by looking at this build
+  # IREE_HOST_BIN_DIR is set, we are using a compiler binary, in which
+  # case we can't check its supported backends just by looking at this build
   # dir's cmake variables --- we would have to implement a configure-check
   # executing `iree-compile --iree-hal-list-target-backends`.
-  if(NOT DEFINED IREE_HOST_BINARY_ROOT)
+  if(NOT IREE_HOST_BIN_DIR)
     string(TOUPPER ${_RULE_TARGET_BACKEND} _UPPERCASE_TARGET_BACKEND)
     string(REPLACE "-" "_" _NORMALIZED_TARGET_BACKEND ${_UPPERCASE_TARGET_BACKEND})
     if(NOT IREE_TARGET_BACKEND_${_NORMALIZED_TARGET_BACKEND})
diff --git a/build_tools/cmake/iree_bytecode_module.cmake b/build_tools/cmake/iree_bytecode_module.cmake
index 6120951..ac983b4 100644
--- a/build_tools/cmake/iree_bytecode_module.cmake
+++ b/build_tools/cmake/iree_bytecode_module.cmake
@@ -51,7 +51,7 @@
   endif()
 
   if(_RULE_STATIC_LIB_PATH AND
-     NOT (IREE_TARGET_BACKEND_LLVM_CPU OR DEFINED IREE_HOST_BINARY_ROOT))
+     NOT (IREE_TARGET_BACKEND_LLVM_CPU OR IREE_HOST_BIN_DIR))
     message(SEND_ERROR "Static library only supports llvm-cpu backend")
   endif()
 
@@ -122,8 +122,6 @@
     get_filename_component(_FRIENDLY_NAME "${_RULE_SRC}" NAME)
   endif()
 
-  # Depending on the binary instead of the target here given we might not have
-  # a target in this CMake invocation when cross-compiling.
   add_custom_command(
     OUTPUT
       ${_OUTPUT_FILES}
diff --git a/build_tools/cmake/iree_c_module.cmake b/build_tools/cmake/iree_c_module.cmake
index 65b8c22..f37ed3c 100644
--- a/build_tools/cmake/iree_c_module.cmake
+++ b/build_tools/cmake/iree_c_module.cmake
@@ -41,7 +41,7 @@
   endif()
 
   if(_RULE_STATIC_LIB_PATH AND
-     NOT (IREE_TARGET_BACKEND_LLVM_CPU OR DEFINED IREE_HOST_BINARY_ROOT))
+     NOT (IREE_TARGET_BACKEND_LLVM_CPU OR IREE_HOST_BIN_DIR))
     message(SEND_ERROR "Static library only supports llvm-cpu backend")
   endif()
 
diff --git a/build_tools/cmake/iree_check_test.cmake b/build_tools/cmake/iree_check_test.cmake
index 1aa0854..09112f1 100644
--- a/build_tools/cmake/iree_check_test.cmake
+++ b/build_tools/cmake/iree_check_test.cmake
@@ -83,9 +83,8 @@
   # Check tests require (by way of iree_bytecode_module) some tools.
   #
   # These can either be built from source, if IREE_BUILD_COMPILER is set, or
-  # be located under IREE_HOST_BINARY_ROOT. The latter is required if
-  # cross-compiling.
-  if(NOT IREE_BUILD_COMPILER AND NOT IREE_HOST_BINARY_ROOT)
+  # be located under IREE_HOST_BIN_DIR (required if cross-compiling).
+  if(NOT IREE_BUILD_COMPILER AND NOT IREE_HOST_BIN_DIR)
     return()
   endif()
 
@@ -222,7 +221,7 @@
   if(NOT DEFINED IREE_TARGET_BACKEND_${_NORMALIZED_TARGET_BACKEND})
     message(SEND_ERROR "Unknown backend '${_RULE_TARGET_BACKEND}'. Check IREE_TARGET_BACKEND_* options.")
   endif()
-  if(DEFINED IREE_HOST_BINARY_ROOT)
+  if(IREE_HOST_BIN_DIR)
     # If we're not building the host tools from source under this configuration,
     # such as when cross compiling, then we can't easily check for which
     # compiler target backends are enabled. Just assume all are enabled and only
diff --git a/build_tools/cmake/iree_hal_cts_test_suite.cmake b/build_tools/cmake/iree_hal_cts_test_suite.cmake
index 6e63391..691b76b 100644
--- a/build_tools/cmake/iree_hal_cts_test_suite.cmake
+++ b/build_tools/cmake/iree_hal_cts_test_suite.cmake
@@ -61,7 +61,7 @@
     if(NOT DEFINED IREE_TARGET_BACKEND_${_NORMALIZED_TARGET_BACKEND})
       message(SEND_ERROR "Unknown backend '${_RULE_COMPILER_TARGET_BACKEND}'. Check IREE_TARGET_BACKEND_* options.")
     endif()
-    if(DEFINED IREE_HOST_BINARY_ROOT)
+    if(IREE_HOST_BIN_DIR)
       # If we're not building the host tools from source under this configuration,
       # such as when cross compiling, then we can't easily check for which
       # compiler target backends are enabled. Just assume all are enabled and only
diff --git a/build_tools/cmake/iree_import_binary.cmake b/build_tools/cmake/iree_import_binary.cmake
index d68e734..9575dbe 100644
--- a/build_tools/cmake/iree_import_binary.cmake
+++ b/build_tools/cmake/iree_import_binary.cmake
@@ -9,7 +9,7 @@
 # iree_import_binary()
 #
 # CMake function to import an executable/binary file into a CMake target.
-# This imports from the directory specified by IREE_HOST_BINARY_ROOT/bin, and
+# This imports from the directory specified by IREE_HOST_BIN_DIR, and
 # that variable _must_ be set for calls to the function to be valid.
 #
 # Parameters:
@@ -21,8 +21,8 @@
 #       NAME awesome-tool
 #       SRCS "awesome-tool-main.cc"
 #     )
-#   elseif(DEFINED IREE_HOST_BINARY_ROOT)
-#     # Import '${IREE_HOST_BINARY_ROOT}/bin/awesome-tool[.exe]' into the
+#   elseif(IREE_HOST_BIN_DIR)
+#     # Import '${IREE_HOST_BIN_DIR}/awesome-tool[.exe]' into the
 #     # CMake target 'awesome-tool'.
 #     iree_import_binary(NAME awesome-tool)
 #   else()
@@ -38,11 +38,10 @@
   )
 
   # TODO(scotttodd): optional 'TARGET' argument (that defaults to NAME)
-  # TODO(scotttodd): remove /bin suffix from this rule and update all scripts/docs
   # TODO(scotttodd): SHARED_LIBRARY_DEPS argument?
 
-  if(NOT DEFINED IREE_HOST_BINARY_ROOT)
-    message(FATAL_ERROR "IREE_HOST_BINARY_ROOT must be set to use iree_import_binary")
+  if(NOT IREE_HOST_BIN_DIR)
+    message(FATAL_ERROR "IREE_HOST_BIN_DIR must be set to use iree_import_binary")
   endif()
 
   # We can't use CMAKE_EXECUTABLE_SUFFIX for host tools when cross-compiling for
@@ -54,14 +53,14 @@
   endif()
 
   set(_FULL_BINARY_NAME "${_RULE_NAME}${_HOST_EXECUTABLE_SUFFIX}")
-  set(_BINARY_PATH "${IREE_HOST_BINARY_ROOT}/bin/${_FULL_BINARY_NAME}")
+  set(_BINARY_PATH "${IREE_HOST_BIN_DIR}/${_FULL_BINARY_NAME}")
   file(REAL_PATH "${_BINARY_PATH}" _BINARY_PATH
        BASE_DIRECTORY ${IREE_ROOT_DIR} EXPAND_TILDE)
 
   if(NOT EXISTS ${_BINARY_PATH})
     message(FATAL_ERROR "Could not find '${_FULL_BINARY_NAME}' under "
-            "'${IREE_HOST_BINARY_ROOT}/bin'\n(Expanded to '${_BINARY_PATH}').\n"
-            "Ensure that IREE_HOST_BINARY_ROOT points to a complete install directory.")
+            "'${IREE_HOST_BIN_DIR}'\n(Expanded to '${_BINARY_PATH}').\n"
+            "Ensure that IREE_HOST_BIN_DIR points to a complete binary directory.")
   endif()
 
   add_executable(${_RULE_NAME} IMPORTED GLOBAL)
diff --git a/build_tools/cmake/iree_static_linker_test.cmake b/build_tools/cmake/iree_static_linker_test.cmake
index 51e4ddf..3cc2d26 100644
--- a/build_tools/cmake/iree_static_linker_test.cmake
+++ b/build_tools/cmake/iree_static_linker_test.cmake
@@ -48,11 +48,11 @@
   endif()
 
   # See comment in iree_check_test about this condition.
-  if(NOT IREE_BUILD_COMPILER AND NOT IREE_HOST_BINARY_ROOT)
+  if(NOT IREE_BUILD_COMPILER AND NOT IREE_HOST_BIN_DIR)
     return()
   endif()
 
-  if(NOT (IREE_TARGET_BACKEND_LLVM_CPU OR DEFINED IREE_HOST_BINARY_ROOT) OR
+  if(NOT (IREE_TARGET_BACKEND_LLVM_CPU OR IREE_HOST_BIN_DIR) OR
      NOT IREE_HAL_DRIVER_LOCAL_SYNC)
     return()
   endif()
@@ -66,7 +66,7 @@
   )
 
   if(_RULE_EMITC AND
-     NOT (IREE_OUTPUT_FORMAT_C OR DEFINED IREE_HOST_BINARY_ROOT))
+     NOT (IREE_OUTPUT_FORMAT_C OR IREE_HOST_BIN_DIR))
     return()
   endif()
 
diff --git a/build_tools/cmake/iree_trace_runner_test.cmake b/build_tools/cmake/iree_trace_runner_test.cmake
index cf6f5dd..589f262 100644
--- a/build_tools/cmake/iree_trace_runner_test.cmake
+++ b/build_tools/cmake/iree_trace_runner_test.cmake
@@ -35,7 +35,7 @@
   endif()
 
   # See comment in iree_check_test about this condition.
-  if(NOT IREE_BUILD_COMPILER AND NOT IREE_HOST_BINARY_ROOT)
+  if(NOT IREE_BUILD_COMPILER AND NOT IREE_HOST_BIN_DIR)
     return()
   endif()
 
@@ -130,7 +130,7 @@
   endif()
 
   # Copied from iree_check_test. Refer to the comment there.
-  if(NOT IREE_BUILD_COMPILER AND NOT IREE_HOST_BINARY_ROOT)
+  if(NOT IREE_BUILD_COMPILER AND NOT IREE_HOST_BIN_DIR)
     return()
   endif()
 
@@ -164,7 +164,7 @@
   if(NOT DEFINED IREE_TARGET_BACKEND_${_NORMALIZED_TARGET_BACKEND})
     message(SEND_ERROR "Unknown backend '${_RULE_TARGET_BACKEND}'. Check IREE_TARGET_BACKEND_* options.")
   endif()
-  if(DEFINED IREE_HOST_BINARY_ROOT)
+  if(IREE_HOST_BIN_DIR)
     # If we're not building the host tools from source under this configuration,
     # such as when cross compiling, then we can't easily check for which
     # compiler target backends are enabled. Just assume all are enabled and only
diff --git a/build_tools/embed_data/CMakeLists.txt b/build_tools/embed_data/CMakeLists.txt
index 3ead83e..c22b7a8 100644
--- a/build_tools/embed_data/CMakeLists.txt
+++ b/build_tools/embed_data/CMakeLists.txt
@@ -4,8 +4,8 @@
 # See https://llvm.org/LICENSE.txt for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-# Just import if IREE_HOST_BINARY_ROOT is set (e.g. when cross-compiling).
-if(DEFINED IREE_HOST_BINARY_ROOT)
+# Just import if IREE_HOST_BIN_DIR is set (e.g. when cross-compiling).
+if(IREE_HOST_BIN_DIR)
   iree_import_binary(NAME generate_embed_data)
   install(IMPORTED_RUNTIME_ARTIFACTS generate_embed_data
           COMPONENT generate_embed_data
diff --git a/build_tools/third_party/flatcc/CMakeLists.txt b/build_tools/third_party/flatcc/CMakeLists.txt
index 801d411..df4e902 100644
--- a/build_tools/third_party/flatcc/CMakeLists.txt
+++ b/build_tools/third_party/flatcc/CMakeLists.txt
@@ -79,8 +79,8 @@
   PUBLIC
 )
 
-if(DEFINED IREE_HOST_BINARY_ROOT)
-  # Just import if IREE_HOST_BINARY_ROOT is set (e.g. when cross-compiling).
+if(IREE_HOST_BIN_DIR)
+  # Just import if IREE_HOST_BIN_DIR is set (e.g. when cross-compiling).
   iree_import_binary(NAME iree-flatcc-cli)
   return()
 endif()
diff --git a/samples/simple_embedding/BUILD b/samples/simple_embedding/BUILD
index 7b26e0e..95c7d10 100644
--- a/samples/simple_embedding/BUILD
+++ b/samples/simple_embedding/BUILD
@@ -17,7 +17,7 @@
 iree_cmake_extra_content(
     content = """
 if(IREE_HAL_DRIVER_LOCAL_SYNC AND IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE AND
-   (IREE_TARGET_BACKEND_VMVX OR DEFINED IREE_HOST_BINARY_ROOT))
+   (IREE_TARGET_BACKEND_VMVX OR IREE_HOST_BIN_DIR))
 """,
     inline = True,
 )
@@ -65,7 +65,7 @@
 iree_cmake_extra_content(
     content = """
 if(IREE_HAL_DRIVER_LOCAL_SYNC AND IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF AND
-   (IREE_TARGET_BACKEND_LLVM_CPU OR DEFINED IREE_HOST_BINARY_ROOT))
+   (IREE_TARGET_BACKEND_LLVM_CPU OR IREE_HOST_BIN_DIR))
 """,
     inline = True,
 )
@@ -217,7 +217,7 @@
 endif()
 
 if(IREE_HAL_DRIVER_VULKAN AND
-   (IREE_TARGET_BACKEND_VULKAN_SPIRV OR DEFINED IREE_HOST_BINARY_ROOT))
+   (IREE_TARGET_BACKEND_VULKAN_SPIRV OR IREE_HOST_BIN_DIR))
 """,
     inline = True,
 )
@@ -269,7 +269,7 @@
 # iree_cmake_extra_content(
 #     content = """
 # if(IREE_HAL_DRIVER_CUDA AND
-#    (IREE_TARGET_BACKEND_CUDA OR DEFINED IREE_HOST_BINARY_ROOT))
+#    (IREE_TARGET_BACKEND_CUDA OR IREE_HOST_BIN_DIR))
 # """,
 #     inline = True,
 # )
diff --git a/samples/simple_embedding/CMakeLists.txt b/samples/simple_embedding/CMakeLists.txt
index 8cbd440..5ff7ba3 100644
--- a/samples/simple_embedding/CMakeLists.txt
+++ b/samples/simple_embedding/CMakeLists.txt
@@ -11,7 +11,7 @@
 iree_add_all_subdirs()
 
 if(IREE_HAL_DRIVER_LOCAL_SYNC AND IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE AND
-   (IREE_TARGET_BACKEND_VMVX OR DEFINED IREE_HOST_BINARY_ROOT))
+   (IREE_TARGET_BACKEND_VMVX OR IREE_HOST_BIN_DIR))
 
 iree_cc_binary(
   NAME
@@ -53,7 +53,7 @@
 endif()
 
 if(IREE_HAL_DRIVER_LOCAL_SYNC AND IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF AND
-   (IREE_TARGET_BACKEND_LLVM_CPU OR DEFINED IREE_HOST_BINARY_ROOT))
+   (IREE_TARGET_BACKEND_LLVM_CPU OR IREE_HOST_BIN_DIR))
 
 iree_cc_binary(
   NAME
@@ -207,7 +207,7 @@
 endif()
 
 if(IREE_HAL_DRIVER_VULKAN AND
-   (IREE_TARGET_BACKEND_VULKAN_SPIRV OR DEFINED IREE_HOST_BINARY_ROOT))
+   (IREE_TARGET_BACKEND_VULKAN_SPIRV OR IREE_HOST_BIN_DIR))
 
 iree_cc_binary(
   NAME
diff --git a/samples/static_library/CMakeLists.txt b/samples/static_library/CMakeLists.txt
index 39ad0a6..efce97c 100644
--- a/samples/static_library/CMakeLists.txt
+++ b/samples/static_library/CMakeLists.txt
@@ -91,7 +91,7 @@
     "hostonly"
 )
 
-if(NOT (IREE_OUTPUT_FORMAT_C OR DEFINED IREE_HOST_BINARY_ROOT))
+if(NOT (IREE_OUTPUT_FORMAT_C OR IREE_HOST_BIN_DIR))
   return()
 endif()
 
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 4d7cad6..8640bff 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -14,7 +14,7 @@
 # Compiler tools are designed to run on host platforms (Linux, macOS, Windows),
 # so they are only built when IREE_BUILD_COMPILER is set and are tagged
 # "hostonly". When cross-compiling (or generally wanting to use already-built
-# compiler tools), set the IREE_HOST_BINARY_ROOT CMake option.
+# compiler tools), set the IREE_HOST_BIN_DIR CMake option.
 #
 # This file does not use bazel_to_cmake because of special logic throughout.
 
@@ -26,7 +26,7 @@
 # TODO(scotttodd): Should this be checking IREE_BUILD_COMPILER?
 #   Maybe we should disallow setting both at the same time, since it's
 #   ambigious which should be used
-if(IREE_HOST_BINARY_ROOT AND NOT IREE_BUILD_COMPILER)
+if(IREE_HOST_BIN_DIR AND NOT IREE_BUILD_COMPILER)
   iree_import_binary(NAME iree-tblgen)
   iree_import_binary(NAME iree-compile)
   iree_import_binary(NAME iree-opt)
@@ -292,9 +292,9 @@
     DESTINATION "tests/bin"
     COMPONENT Tests
   )
-elseif(NOT DEFINED IREE_HOST_BINARY_ROOT)
+elseif(NOT IREE_HOST_BIN_DIR)
   message(STATUS
       "*Not* building or importing IREE's compiler tools.\n   "
-      "Set IREE_BUILD_COMPILER to build them or IREE_HOST_BINARY_ROOT to "
+      "Set IREE_BUILD_COMPILER to build them or IREE_HOST_BIN_DIR to "
       "import them.")
 endif()
diff --git a/tools/test/CMakeLists.txt b/tools/test/CMakeLists.txt
index a82b794..2c4d62b 100644
--- a/tools/test/CMakeLists.txt
+++ b/tools/test/CMakeLists.txt
@@ -55,7 +55,7 @@
 ### BAZEL_TO_CMAKE_PRESERVES_ALL_CONTENT_BELOW_THIS_LINE ###
 
 if(IREE_BUILD_TESTS AND
-   (IREE_TARGET_BACKEND_VMVX OR DEFINED IREE_HOST_BINARY_ROOT) AND
+   (IREE_TARGET_BACKEND_VMVX OR IREE_HOST_BIN_DIR) AND
    IREE_HAL_DRIVER_LOCAL_SYNC)
   iree_bytecode_module(
     NAME