[Bazel] Fix flatcc and bitcode library include paths when IREE is an external repo (#25101)

The fixed paths ("-I runtime/src", "-I .", fixed llvm-project header
paths) just work if IREE is the main bazel module. For IREE as a
dependency embedded in another bazel module, the paths need to be
prefixed with "external/" + repo-name with repo-name being the canonical
repo-name used by the main bazel module.

Co-Authored-By: Claude Code

---------

Signed-off-by: Stefan Schuermans <schuermans@roofline.ai>
diff --git a/build_tools/bazel/iree_bitcode_library.bzl b/build_tools/bazel/iree_bitcode_library.bzl
index dd9a41d..4cb48d3 100644
--- a/build_tools/bazel/iree_bitcode_library.bzl
+++ b/build_tools/bazel/iree_bitcode_library.bzl
@@ -67,7 +67,13 @@
     clang_tool = "@llvm-project//clang:clang"
     link_tool = "@llvm-project//llvm:llvm-link"
     builtin_headers_dep = "@llvm-project//clang:builtin_headers_gen"
-    builtin_headers_path = "external/+_repo_rules+llvm-project/clang/staging/include/"
+
+    # Paths are resolved relative to the exec root, so they need the canonical
+    # repo names when IREE is not the main repo.
+    llvm_project_repo_name = Label("@llvm-project//clang:builtin_headers_gen").repo_name
+    repo_name = Label("//:MODULE.bazel").repo_name
+    builtin_headers_path = "external/%s/clang/staging/include/" % llvm_project_repo_name
+    runtime_src_path = "external/%s/runtime/src" % repo_name if repo_name else "runtime/src"
 
     base_copts = [
         # Target architecture
@@ -127,8 +133,8 @@
                     "$(location %s)" % (clang_tool),
                     "-isystem $(BINDIR)/%s" % builtin_headers_path,
                     " ".join(base_copts + copts),
-                    " ".join(["-I $(BINDIR)/runtime/src"]),
-                    " ".join(["-I runtime/src"]),
+                    " ".join(["-I $(BINDIR)/%s" % runtime_src_path]),
+                    " ".join(["-I %s" % runtime_src_path]),
                     "-o $(location %s)" % (bitcode_out),
                     "$(location %s)" % (src),
                 ]),
@@ -189,7 +195,13 @@
     clang_tool = "@llvm-project//clang:clang"
     link_tool = "@llvm-project//llvm:llvm-link"
     builtin_headers_dep = "@llvm-project//clang:builtin_headers_gen"
-    builtin_headers_path = "external/+_repo_rules+llvm-project/clang/staging/include/"
+
+    # Paths are resolved relative to the exec root, so they need the canonical
+    # repo names when IREE is not the main repo.
+    llvm_project_repo_name = Label("@llvm-project//clang:builtin_headers_gen").repo_name
+    repo_name = Label("//:MODULE.bazel").repo_name
+    builtin_headers_path = "external/%s/clang/staging/include/" % llvm_project_repo_name
+    runtime_src_path = "external/%s/runtime/src" % repo_name if repo_name else "runtime/src"
 
     base_copts = [
         "-x",
@@ -314,6 +326,11 @@
     if not out:
         out = "%s.%s.bc" % (src, gpu_arch)
 
+    # "-I ." is relative to the exec root, so it needs the repo prefix when
+    # IREE is embedded in another Bazel module.
+    repo_name = Label("//:MODULE.bazel").repo_name
+    include_root = ("external/%s" % repo_name) if repo_name else "."
+
     native.genrule(
         name = "gen_%s" % (out),
         srcs = srcs,
@@ -322,7 +339,7 @@
             "$(location %s)" % (clang_tool),
             "$(location %s)" % (src),
             "-o $(location %s)" % (out),
-            "-I .",
+            "-I " + include_root,
         ] + base_copts + copts),
         tools = [
             clang_tool,
diff --git a/build_tools/bazel/iree_flatcc.bzl b/build_tools/bazel/iree_flatcc.bzl
index 73f89d3..e147856 100644
--- a/build_tools/bazel/iree_flatcc.bzl
+++ b/build_tools/bazel/iree_flatcc.bzl
@@ -17,9 +17,15 @@
         **kwargs):
     flatcc = Label("@com_github_dvidelabs_flatcc//:flatcc")
 
+    # "-I" flags are resolved by flatcc relative to working directory that
+    # bazel uses to call it. So if iree is not the main module, but only a
+    # dependency, the include dir needs to be adapted.
+    repo_name = Label("//:MODULE.bazel").repo_name
+    include_root = ("external/%s/runtime/src" % repo_name) if repo_name else "runtime/src"
+
     flags = [
         "-o$(RULEDIR)",
-        "-I runtime/src",
+        "-I " + include_root,
     ] + flatcc_args
 
     out_stem = "%s" % (srcs[0].replace(".fbs", ""))