Roll-up of changes needed to support the nvgpu out of tree project. (#12888)

* Adds CMake scoped IREE_PACKAGE_ROOT_DIR and IREE_PACKAGE_ROOT_PREFIX
to replace hard-coded path to namespace logic in iree_package_ns (and
uses within IREE/removes the special casing).
* Adds support for `BAZEL_TO_CMAKE_PRESERVES_ALL_CONTENT_ABOVE_THIS_LINE
` to bazel_to_cmake. Been carrying this patch for a while and ended up
needing it.
* Further generalizes bazel_to_cmake target resolution so that it is
customizable as needed out of tree.
* Moves the `iree::runtime::src::defs` target to `iree::defs` and puts
it in the right place in the tree to avoid special casing.
* Ditto but for `iree::compiler::src::defs`
* Adds a bit more logging to `_DEBUG_IREE_PACKAGE_NAME` mode.
* Makes iree_tablegen_library consult a scoped
`IREE_COMPILER_TABLEGEN_INCLUDE_DIRS` var for additional include
directories (makes it possible to use out of tree).
* Adds `NVPTXDesc` and `NVPTXInfo` targets to HAL_Target_CUDA. No idea
why this was triggering for me but was getting undefined deps. Must have
been coming in elsewhere in a more full featured build.
* Fixes iree-opt initialization sequence with respect to command line
options. Also fixed the test which should have been verifying this.
* Fixed pytype issue in bazel_to_cmake that could theoretically happen.

Fixes build issues related to the out of tree build for
https://github.com/openxla/community/issues/71
diff --git a/build_tools/cmake/iree_macros.cmake b/build_tools/cmake/iree_macros.cmake
index de83daf..fa24bb7 100644
--- a/build_tools/cmake/iree_macros.cmake
+++ b/build_tools/cmake/iree_macros.cmake
@@ -131,40 +131,44 @@
 #   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} "" _IREE_RELATIVE_PATH ${CMAKE_CURRENT_LIST_DIR})
-  string(SUBSTRING ${_IREE_RELATIVE_PATH} 1 -1 _IREE_RELATIVE_PATH)
-
-  if(NOT ${CMAKE_CURRENT_LIST_DIR} MATCHES "^${IREE_ROOT_DIR}/.*")
-    # Function is being called from outside IREE. Use the source-relative path.
-    # Please check the README.md to see the potential risk.
-    string(REPLACE ${PROJECT_SOURCE_DIR} "" _SOURCE_RELATIVE_PATH ${CMAKE_CURRENT_LIST_DIR})
-    string(SUBSTRING ${_SOURCE_RELATIVE_PATH} 1 -1 _SOURCE_RELATIVE_PATH)
-    set(_PACKAGE "${_SOURCE_RELATIVE_PATH}")
-
-  # If changing the directory/package mapping rules, please also implement
-  # the corresponding rule in:
-  #   build_tools/bazel_to_cmake/bazel_to_cmake_targets.py
-  # Some sub-trees form their own roots for package purposes. Rewrite them.
-  elseif(_IREE_RELATIVE_PATH MATCHES "^compiler/src/(.*)")
-    # compiler/src/iree/compiler -> iree/compiler
-    set(_PACKAGE "${CMAKE_MATCH_1}")
-  elseif(_IREE_RELATIVE_PATH MATCHES "^runtime/src/(.*)")
-    # runtime/src/iree/base -> iree/base
-    set(_PACKAGE "${CMAKE_MATCH_1}")
-  elseif(_IREE_RELATIVE_PATH MATCHES "^tools$")
-    # Special case for tools/ -> "" (empty string)
-    # For example, tools/iree-compile -> iree-compile (no namespace)
-    set(_PACKAGE "")
+  if(DEFINED IREE_PACKAGE_ROOT_DIR)
+    # If an enclosing package root dir is set, then the package is just the
+    # relative part after that.
+    cmake_path(RELATIVE_PATH CMAKE_CURRENT_LIST_DIR
+      BASE_DIRECTORY "${IREE_PACKAGE_ROOT_DIR}"
+      OUTPUT_VARIABLE _PACKAGE)
+    if(_PACKAGE STREQUAL ".")
+      set(_PACKAGE "")
+    endif()
+    if(IREE_PACKAGE_ROOT_PREFIX)
+      set(_PACKAGE "${IREE_PACKAGE_ROOT_PREFIX}${_PACKAGE}")
+    endif()
   else()
-    # Default to prefixing with iree/
-    set(_PACKAGE "iree/${_IREE_RELATIVE_PATH}")
+    # Get the relative path of the current dir (i.e. runtime/src/iree/vm).
+    string(REPLACE ${IREE_ROOT_DIR} "" _IREE_RELATIVE_PATH ${CMAKE_CURRENT_LIST_DIR})
+    string(SUBSTRING ${_IREE_RELATIVE_PATH} 1 -1 _IREE_RELATIVE_PATH)
+
+    if(NOT ${CMAKE_CURRENT_LIST_DIR} MATCHES "^${IREE_ROOT_DIR}/.*")
+      # Function is being called from outside IREE. Use the source-relative path.
+      # Please check the README.md to see the potential risk.
+      string(REPLACE ${PROJECT_SOURCE_DIR} "" _SOURCE_RELATIVE_PATH ${CMAKE_CURRENT_LIST_DIR})
+      string(SUBSTRING ${_SOURCE_RELATIVE_PATH} 1 -1 _SOURCE_RELATIVE_PATH)
+      set(_PACKAGE "${_SOURCE_RELATIVE_PATH}")
+
+    # If changing the directory/package mapping rules, please also implement
+    # the corresponding rule in:
+    #   build_tools/bazel_to_cmake/bazel_to_cmake_targets.py
+    # Some sub-trees form their own roots for package purposes. Rewrite them.
+    else()
+      message(SEND_ERROR "iree_package_ns(): Could not determine package for ${CMAKE_CURRENT_LIST_DIR}")
+      set(_PACKAGE "iree/unknown")
+    endif()
   endif()
 
   string(REPLACE "/" "::" _PACKAGE_NS "${_PACKAGE}")
 
   if(_DEBUG_IREE_PACKAGE_NAME)
-    message(STATUS "iree_package_ns(): map ${_IREE_RELATIVE_PATH} -> ${_PACKAGE_NS}")
+    message(STATUS "iree_package_ns(): map ${CMAKE_CURRENT_LIST_DIR} -> ${_PACKAGE_NS}")
   endif()
 
   set(${PACKAGE_NS} ${_PACKAGE_NS} PARENT_SCOPE)