Bazel to CMake: Some tweaks to make it work upstream
There's not a lot of point in having separate target-mapping dicts if they're a direct mapping and it causes some issues because it introduces extra reliance on prefix matches to partition between the dicts. We could have multiple dict vars and merge them, but that just seemed more complicated.
Bare "//" prefix matching causes issues for our migration tooling because it's pretty hard to detect that that's the repo root in general.
Tested:
Ran upstream and on git in strict mode and confirmed no diffs in CMakeLists.txt files and no new errors.
Depends on https://github.com/google/iree/pull/777
PiperOrigin-RevId: 295813382
diff --git a/build_tools/bazel_to_cmake/bazel_to_cmake.py b/build_tools/bazel_to_cmake/bazel_to_cmake.py
index 6977ad8..17d058b 100755
--- a/build_tools/bazel_to_cmake/bazel_to_cmake.py
+++ b/build_tools/bazel_to_cmake/bazel_to_cmake.py
@@ -38,7 +38,7 @@
repo_root = None
EDIT_BLOCKING_PATTERN = re.compile(
- "bazel[\s_]*to[\s_]*cmake[\s_]*:?[\s_]*do[\s_]*not[\s_]*edit",
+ r"bazel[\s_]*to[\s_]*cmake[\s_]*:?[\s_]*do[\s_]*not[\s_]*edit",
flags=re.IGNORECASE)
@@ -128,7 +128,7 @@
if translate_tool:
# Bazel `//iree/base` -> CMake `iree::base`
# Bazel `//iree/base:api` -> CMake `iree::base::api`
- translate_tool = translate_tool.replace("//", "") # iree/base:api
+ translate_tool = translate_tool.replace("//iree", "iree") # iree/base:api
translate_tool = translate_tool.replace(":", "_") # iree/base::api
translate_tool = translate_tool.replace("/", "_") # iree::base::api
return " TRANSLATE_TOOL\n %s\n" % (translate_tool)
@@ -187,7 +187,7 @@
# -> CMake `${IREE_ROOT_DIR}/iree/dir/td_file.td
# Bazel `//iree/dir/IR:td_file.td`
# -> CMake `${IREE_ROOT_DIR}/iree/dir/IR/td_file.td
- td_file = td_file.replace("//", "${IREE_ROOT_DIR}/")
+ td_file = td_file.replace("//iree", "${IREE_ROOT_DIR}/iree")
td_file = td_file.replace(":", "/")
return " TD_FILE\n \"%s\"\n" % (td_file)
@@ -216,7 +216,7 @@
# Bazel `:api` -> CMake `::api`
# Bazel `//iree/base` -> CMake `iree::base`
# Bazel `//iree/base:api` -> CMake `iree::base::api`
- target = target.replace("//", "") # iree/base:api
+ target = target.replace("//iree", "iree") # iree/base:api
target = target.replace(":", "::") # iree/base::api or ::api
target = target.replace("/", "::") # iree::base::api
target = [target]
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 1cdedfb..2d733bb 100644
--- a/build_tools/bazel_to_cmake/bazel_to_cmake_targets.py
+++ b/build_tools/bazel_to_cmake/bazel_to_cmake_targets.py
@@ -15,53 +15,20 @@
# Bazel to CMake target name conversions used by bazel_to_cmake.py.
-ABSL_EXPLICIT_TARGET_MAPPING = {
+EXPLICIT_TARGET_MAPPING = {
+ # absl
"@com_google_absl//absl/flags:flag": ["absl::flags"],
"@com_google_absl//absl/flags:parse": ["absl::flags_parse"],
-}
-
-
-def _convert_absl_target(target):
- if target in ABSL_EXPLICIT_TARGET_MAPPING:
- return ABSL_EXPLICIT_TARGET_MAPPING[target]
-
- # Default to a pattern substitution approach.
- # Take "absl::" and append the name part of the full target identifier, e.g.
- # "@com_google_absl//absl/memory" -> "absl::memory"
- # "@com_google_absl//absl/types:optional" -> "absl::optional"
- # "@com_google_absl//absl/types:span" -> "absl::span"
- if ":" in target:
- target_name = target.rsplit(":")[-1]
- else:
- target_name = target.rsplit("/")[-1]
- return ["absl::" + target_name]
-
-
-DEAR_IMGUI_EXPLICIT_TARGET_MAPPING = {
+ # dear_imgui
"@dear_imgui": ["dear_imgui::dear_imgui"],
"@dear_imgui//:imgui_sdl_vulkan": [
"dear_imgui::impl_sdl", "dear_imgui::impl_vulkan"
],
-}
-
-RENDERDOC_API_MAPPING = {
- "@renderdoc_api//:renderdoc_app": ["renderdoc_api::renderdoc_app"]
-}
-
-LLVM_TARGET_MAPPING = {
+ # LLVM
"@llvm-project//llvm:core": ["LLVMCore"],
"@llvm-project//llvm:support": ["LLVMSupport"],
"@llvm-project//llvm:tablegen": ["LLVMTableGen"],
-}
-
-VULKAN_HEADERS_MAPPING = {
- # TODO(scotttodd): Set -DVK_NO_PROTOTYPES to COPTS for _no_prototypes.
- # Maybe add a wrapper CMake lib within build_tools/third_party/?
- "@vulkan_headers//:vulkan_headers": ["Vulkan::Headers"],
- "@vulkan_headers//:vulkan_headers_no_prototypes": ["Vulkan::Headers"],
-}
-
-MLIR_EXPLICIT_TARGET_MAPPING = {
+ # MLIR
"@llvm-project//mlir:AllPassesAndDialects": ["MLIRAllDialects"],
"@llvm-project//mlir:AllPassesAndDialectsNoRegistration": [
"MLIRAllDialects"
@@ -89,13 +56,36 @@
"@llvm-project//mlir:mlir-translate": ["mlir-translate"],
"@llvm-project//mlir:MlirTableGenMain": ["MLIRTableGen"],
"@llvm-project//mlir:MlirOptLib": ["MLIROptLib"],
+ # Vulkan
+ # TODO(scotttodd): Set -DVK_NO_PROTOTYPES to COPTS for _no_prototypes.
+ # Maybe add a wrapper CMake lib within build_tools/third_party/?
+ "@vulkan_headers//:vulkan_headers": ["Vulkan::Headers"],
+ "@vulkan_headers//:vulkan_headers_no_prototypes": ["Vulkan::Headers"],
+ # The Bazel target maps to the IMPORTED target defined by FindVulkan().
+ "@vulkan_sdk//:sdk": ["Vulkan::Vulkan"],
+ # Misc single targets
+ "@com_google_benchmark//:benchmark": ["benchmark"],
+ "@com_github_google_flatbuffers//:flatbuffers": ["flatbuffers"],
+ "@com_google_googletest//:gtest": ["gtest"],
+ "@renderdoc_api//:renderdoc_app": ["renderdoc_api::renderdoc_app"],
+ "@sdl2//:SDL2": ["SDL2-static"]
}
-def _convert_mlir_target(target):
- if target in MLIR_EXPLICIT_TARGET_MAPPING:
- return MLIR_EXPLICIT_TARGET_MAPPING[target]
+def _convert_absl_target(target):
+ # Default to a pattern substitution approach.
+ # Take "absl::" and append the name part of the full target identifier, e.g.
+ # "@com_google_absl//absl/memory" -> "absl::memory"
+ # "@com_google_absl//absl/types:optional" -> "absl::optional"
+ # "@com_google_absl//absl/types:span" -> "absl::span"
+ if ":" in target:
+ target_name = target.rsplit(":")[-1]
+ else:
+ target_name = target.rsplit("/")[-1]
+ return ["absl::" + target_name]
+
+def _convert_mlir_target(target):
# Default to a pattern substitution approach.
# Take "MLIR" and append the name part of the full target identifier, e.g.
# "@llvm-project//mlir:IR" -> "MLIRIR"
@@ -104,35 +94,25 @@
def convert_external_target(target):
- """Converts an external (doesn't start with //iree) Bazel target to Cmake.
+ """Converts an external (non-IREE) Bazel target to a list of CMake targets.
IREE targets are expected to follow a standard form between Bazel and CMake
that facilitates conversion. External targets *may* have their own patterns,
or they may be purely special cases.
- Multiple target in Bazel may map to a single target in CMake.
- A Bazel target may *not* map to multiple CMake targets.
+ Multiple target in Bazel may map to a single target in CMake and a Bazel
+ target may map to multiple CMake targets.
Returns:
- The converted target if it was successfully converted.
+ A list of converted targets if it was successfully converted.
Raises:
KeyError: No conversion was found for the target.
"""
- if target.startswith("@com_google_absl"):
+ if target in EXPLICIT_TARGET_MAPPING:
+ return EXPLICIT_TARGET_MAPPING[target]
+ if target.startswith("@com_google_absl//absl"):
return _convert_absl_target(target)
- if target == "@com_google_benchmark//:benchmark":
- return ["benchmark"]
- if target == "@com_github_google_flatbuffers//:flatbuffers":
- return ["flatbuffers"]
- if target.startswith("@dear_imgui"):
- return DEAR_IMGUI_EXPLICIT_TARGET_MAPPING[target]
- if target.startswith("@renderdoc_api"):
- return RENDERDOC_API_MAPPING[target]
- if target == "@com_google_googletest//:gtest":
- return ["gtest"]
- if target.startswith("@llvm-project//llvm"):
- return LLVM_TARGET_MAPPING[target]
if target.startswith("@llvm-project//mlir"):
return _convert_mlir_target(target)
if target.startswith("@org_tensorflow//tensorflow/compiler/mlir"):
@@ -141,12 +121,5 @@
if target.startswith("@org_tensorflow//tensorflow/lite/experimental/ruy"):
# All Bazel targets map to a single CMake target.
return ["ruy"]
- if target == "@sdl2//:SDL2":
- return ["SDL2-static"]
- if target.startswith("@vulkan_headers"):
- return VULKAN_HEADERS_MAPPING[target]
- if target == "@vulkan_sdk//:sdk":
- # The Bazel target maps to the IMPORTED target defined by FindVulkan().
- return ["Vulkan::Vulkan"]
raise KeyError("No conversion found for target '%s'" % target)