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/bazel_to_cmake/bazel_to_cmake.py b/build_tools/bazel_to_cmake/bazel_to_cmake.py
index 947774e..44e2b8a 100755
--- a/build_tools/bazel_to_cmake/bazel_to_cmake.py
+++ b/build_tools/bazel_to_cmake/bazel_to_cmake.py
@@ -62,7 +62,8 @@
     r"bazel[\s_]*to[\s_]*cmake[\s_]*:?[\s_]*do[\s_]*not[\s_]*edit",
     flags=re.IGNORECASE)
 
-PRESERVE_TAG = "### BAZEL_TO_CMAKE_PRESERVES_ALL_CONTENT_BELOW_THIS_LINE ###"
+PRESERVE_ABOVE_TAG = "### BAZEL_TO_CMAKE_PRESERVES_ALL_CONTENT_ABOVE_THIS_LINE ###"
+PRESERVE_BELOW_TAG = "### BAZEL_TO_CMAKE_PRESERVES_ALL_CONTENT_BELOW_THIS_LINE ###"
 REPO_CFG_FILE = ".bazel_to_cmake.cfg.py"
 REPO_CFG_MODULE_NAME = "bazel_to_cmake_repo_config"
 
@@ -140,12 +141,17 @@
   # Dynamically load the config file as a module.
   orig_dont_write_bytecode = sys.dont_write_bytecode
   sys.dont_write_bytecode = True  # Don't generate __pycache__ dir
-  spec = importlib.util.spec_from_file_location(
-      REPO_CFG_MODULE_NAME, os.path.join(repo_root, REPO_CFG_FILE))
-  repo_cfg = importlib.util.module_from_spec(spec)
-  sys.modules[REPO_CFG_MODULE_NAME] = repo_cfg
-  spec.loader.exec_module(repo_cfg)
-  sys.dont_write_bytecode = orig_dont_write_bytecode
+  repo_cfg_path = os.path.join(repo_root, REPO_CFG_FILE)
+  spec = importlib.util.spec_from_file_location(REPO_CFG_MODULE_NAME,
+                                                repo_cfg_path)
+  if spec and spec.loader:
+    repo_cfg = importlib.util.module_from_spec(spec)
+    sys.modules[REPO_CFG_MODULE_NAME] = repo_cfg
+    spec.loader.exec_module(repo_cfg)
+    sys.dont_write_bytecode = orig_dont_write_bytecode
+  else:
+    print(f"INTERNAL ERROR: Could not evaluate {repo_cfg_path} as module")
+    sys.exit(1)
 
 
 def repo_relpath(path):
@@ -229,26 +235,34 @@
   ] + ["#" * 80])
 
   old_lines = []
-  preserved_footer_lines = ["\n" + PRESERVE_TAG + "\n"]
+  possible_preserved_header_lines = []
+  preserved_footer_lines = ["\n" + PRESERVE_BELOW_TAG + "\n"]
 
   # Read CMakeLists.txt and check if it has the auto-generated header.
+  found_preserve_below_tag = False
+  found_preserve_above_tag = False
   if os.path.isfile(cmakelists_file_path):
     found_autogeneration_tag = False
-    found_preserve_tag = False
     with open(cmakelists_file_path) as f:
       old_lines = f.readlines()
 
     for line in old_lines:
+      if not found_preserve_above_tag:
+        possible_preserved_header_lines.append(line)
       if not found_autogeneration_tag and autogeneration_tag in line:
         found_autogeneration_tag = True
-      if not found_preserve_tag and PRESERVE_TAG in line:
-        found_preserve_tag = True
-      elif found_preserve_tag:
+      if not found_preserve_below_tag and PRESERVE_BELOW_TAG in line:
+        found_preserve_below_tag = True
+      elif not found_preserve_above_tag and PRESERVE_ABOVE_TAG in line:
+        found_preserve_above_tag = True
+      elif found_preserve_below_tag:
         preserved_footer_lines.append(line)
     if not found_autogeneration_tag:
       if verbosity >= 1:
         log(f"Skipped. Did not find autogeneration line.", indent=2)
       return Status.SKIPPED
+  preserved_header = ("".join(possible_preserved_header_lines)
+                      if found_preserve_above_tag else "")
   preserved_footer = "".join(preserved_footer_lines)
 
   # Read the Bazel BUILD file and interpret it.
@@ -276,7 +290,8 @@
         f"Reason: `{type(e).__name__}: {e}`",
         indent=2)
     return Status.FAILED
-  converted_content = header + converted_build_file + preserved_footer
+  converted_content = (preserved_header + header + converted_build_file +
+                       preserved_footer)
   if write_files:
     with open(cmakelists_file_path, "wt") as cmakelists_file:
       cmakelists_file.write(converted_content)