Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2020 Google LLC |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 15 | """This script assists with converting from Bazel BUILD files to CMakeLists.txt. |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 16 | |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 17 | Bazel BUILD files should, where possible, be written to use simple features |
| 18 | that can be directly evaluated and avoid more advanced features like |
| 19 | variables, list comprehensions, etc. |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 20 | |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 21 | Generated CMake files will be similar in structure to their source BUILD |
| 22 | files by using the functions in build_tools/cmake/ that imitate corresponding |
| 23 | Bazel rules (e.g. cc_library -> iree_cc_library.cmake). |
| 24 | |
| 25 | For usage, see: |
| 26 | python3 build_tools/bazel_to_cmake/bazel_to_cmake.py --help |
| 27 | """ |
Geoffrey Martin-Noble | ea9c683 | 2020-02-29 20:26:35 -0800 | [diff] [blame] | 28 | # pylint: disable=missing-docstring |
Geoffrey Martin-Noble | ea9c683 | 2020-02-29 20:26:35 -0800 | [diff] [blame] | 29 | |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 30 | import argparse |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 31 | import datetime |
| 32 | import os |
Geoffrey Martin-Noble | 2a36c38 | 2020-01-28 15:04:30 -0800 | [diff] [blame] | 33 | import re |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 34 | import sys |
Geoffrey Martin-Noble | ea9c683 | 2020-02-29 20:26:35 -0800 | [diff] [blame] | 35 | |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 36 | import bazel_to_cmake_converter |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 37 | |
| 38 | repo_root = None |
| 39 | |
Geoffrey Martin-Noble | 2a36c38 | 2020-01-28 15:04:30 -0800 | [diff] [blame] | 40 | EDIT_BLOCKING_PATTERN = re.compile( |
Geoffrey Martin-Noble | c9784d6 | 2020-02-18 13:49:49 -0800 | [diff] [blame] | 41 | r"bazel[\s_]*to[\s_]*cmake[\s_]*:?[\s_]*do[\s_]*not[\s_]*edit", |
Geoffrey Martin-Noble | 2a36c38 | 2020-01-28 15:04:30 -0800 | [diff] [blame] | 42 | flags=re.IGNORECASE) |
| 43 | |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 44 | |
| 45 | def parse_arguments(): |
| 46 | global repo_root |
| 47 | |
| 48 | parser = argparse.ArgumentParser( |
| 49 | description="Bazel to CMake conversion helper.") |
| 50 | parser.add_argument( |
| 51 | "--preview", |
| 52 | help="Prints results instead of writing files", |
| 53 | action="store_true", |
| 54 | default=False) |
Scott Todd | bb79717 | 2020-04-15 17:20:33 -0700 | [diff] [blame] | 55 | parser.add_argument( |
| 56 | "--allow_partial_conversion", |
| 57 | help="Generates partial files, ignoring errors during conversion", |
Geoffrey Martin-Noble | f7be55a | 2020-01-28 17:47:42 -0800 | [diff] [blame] | 58 | action="store_true", |
| 59 | default=False) |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 60 | |
| 61 | # Specify only one of these (defaults to --root_dir=iree). |
| 62 | group = parser.add_mutually_exclusive_group() |
| 63 | group.add_argument( |
| 64 | "--dir", |
| 65 | help="Converts the BUILD file in the given directory", |
| 66 | default=None) |
| 67 | group.add_argument( |
| 68 | "--root_dir", |
| 69 | help="Converts all BUILD files under a root directory (defaults to iree/)", |
| 70 | default="iree") |
| 71 | |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 72 | args = parser.parse_args() |
| 73 | |
| 74 | # --dir takes precedence over --root_dir. |
| 75 | # They are mutually exclusive, but the default value is still set. |
| 76 | if args.dir: |
| 77 | args.root_dir = None |
| 78 | |
| 79 | return args |
| 80 | |
| 81 | |
| 82 | def setup_environment(): |
| 83 | """Sets up some environment globals.""" |
| 84 | global repo_root |
| 85 | |
| 86 | # Determine the repository root (two dir-levels up). |
| 87 | repo_root = os.path.dirname( |
| 88 | os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 89 | |
| 90 | |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 91 | def log(*args, **kwargs): |
| 92 | print(*args, **kwargs, file=sys.stderr) |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 93 | |
| 94 | |
Scott Todd | bb79717 | 2020-04-15 17:20:33 -0700 | [diff] [blame] | 95 | def convert_directory_tree(root_directory_path, write_files, |
| 96 | allow_partial_conversion): |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 97 | log(f"convert_directory_tree: {root_directory_path}") |
Geoffrey Martin-Noble | 1b4ac81 | 2020-03-09 11:21:03 -0700 | [diff] [blame] | 98 | for root, _, _ in os.walk(root_directory_path): |
Scott Todd | bb79717 | 2020-04-15 17:20:33 -0700 | [diff] [blame] | 99 | convert_directory(root, write_files, allow_partial_conversion) |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 100 | |
| 101 | |
Scott Todd | bb79717 | 2020-04-15 17:20:33 -0700 | [diff] [blame] | 102 | def convert_directory(directory_path, write_files, allow_partial_conversion): |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 103 | if not os.path.isdir(directory_path): |
Phoenix Meadowlark | 01bbb6c | 2020-03-30 14:20:11 -0700 | [diff] [blame] | 104 | raise FileNotFoundError(f"Cannot find directory '{directory_path}'") |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 105 | |
Stella Laurenzo | 66578b0 | 2020-07-20 17:34:44 -0700 | [diff] [blame^] | 106 | skip_file_path = os.path.join(directory_path, ".skip_bazel_to_cmake") |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 107 | build_file_path = os.path.join(directory_path, "BUILD") |
| 108 | cmakelists_file_path = os.path.join(directory_path, "CMakeLists.txt") |
| 109 | |
Stella Laurenzo | 66578b0 | 2020-07-20 17:34:44 -0700 | [diff] [blame^] | 110 | if os.path.isfile(skip_file_path) or not os.path.isfile(build_file_path): |
| 111 | # No Bazel BUILD file in this directory or explicit skip. |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 112 | return |
| 113 | |
| 114 | global repo_root |
| 115 | rel_build_file_path = os.path.relpath(build_file_path, repo_root) |
| 116 | rel_cmakelists_file_path = os.path.relpath(cmakelists_file_path, repo_root) |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 117 | log(f"Converting {rel_build_file_path} to {rel_cmakelists_file_path}") |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 118 | |
Geoffrey Martin-Noble | 2a36c38 | 2020-01-28 15:04:30 -0800 | [diff] [blame] | 119 | cmake_file_exists = os.path.isfile(cmakelists_file_path) |
Phoenix Meadowlark | 01bbb6c | 2020-03-30 14:20:11 -0700 | [diff] [blame] | 120 | copyright_line = f"# Copyright {datetime.date.today().year} Google LLC" |
Geoffrey Martin-Noble | 2a36c38 | 2020-01-28 15:04:30 -0800 | [diff] [blame] | 121 | write_allowed = write_files |
| 122 | if cmake_file_exists: |
| 123 | with open(cmakelists_file_path) as f: |
| 124 | for i, line in enumerate(f): |
| 125 | if line.startswith("# Copyright"): |
| 126 | copyright_line = line.rstrip() |
| 127 | if EDIT_BLOCKING_PATTERN.search(line): |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 128 | log(f" {rel_cmakelists_file_path} already exists, and " |
| 129 | f"line {i + 1}: '{line.strip()}' prevents edits. " |
| 130 | f"Falling back to preview") |
Geoffrey Martin-Noble | 2a36c38 | 2020-01-28 15:04:30 -0800 | [diff] [blame] | 131 | write_allowed = False |
| 132 | |
| 133 | if write_allowed: |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 134 | # TODO(scotttodd): Attempt to merge instead of overwrite? |
| 135 | # Existing CMakeLists.txt may have special logic that should be preserved |
Geoffrey Martin-Noble | 2a36c38 | 2020-01-28 15:04:30 -0800 | [diff] [blame] | 136 | if cmake_file_exists: |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 137 | log(f" {rel_cmakelists_file_path} already exists; overwriting") |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 138 | else: |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 139 | log(f" {rel_cmakelists_file_path} does not exist yet; creating") |
| 140 | log("") |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 141 | |
| 142 | with open(build_file_path, "rt") as build_file: |
| 143 | build_file_code = compile(build_file.read(), build_file_path, "exec") |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 144 | try: |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 145 | converted_text = bazel_to_cmake_converter.convert_build_file( |
Scott Todd | bb79717 | 2020-04-15 17:20:33 -0700 | [diff] [blame] | 146 | build_file_code, |
| 147 | copyright_line, |
| 148 | allow_partial_conversion=allow_partial_conversion) |
Geoffrey Martin-Noble | 2a36c38 | 2020-01-28 15:04:30 -0800 | [diff] [blame] | 149 | if write_allowed: |
Ben Vanik | 679424c | 2020-07-03 17:31:22 -0700 | [diff] [blame] | 150 | with open(cmakelists_file_path, "wt") as cmakelists_file: |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 151 | cmakelists_file.write(converted_text) |
| 152 | else: |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 153 | print(converted_text, end="") |
Geoffrey Martin-Noble | f7be55a | 2020-01-28 17:47:42 -0800 | [diff] [blame] | 154 | except (NameError, NotImplementedError) as e: |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 155 | log(f"Failed to convert {rel_build_file_path}.", end=" ") |
| 156 | log("Missing a rule handler in bazel_to_cmake.py?") |
| 157 | log(f" Reason: `{type(e).__name__}: {e}`") |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 158 | except KeyError as e: |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame] | 159 | log(f"Failed to convert {rel_build_file_path}.", end=" ") |
| 160 | log("Missing a conversion in bazel_to_cmake_targets.py?") |
| 161 | log(f" Reason: `{type(e).__name__}: {e}`") |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 162 | |
| 163 | |
| 164 | def main(args): |
| 165 | """Runs Bazel to CMake conversion.""" |
| 166 | global repo_root |
| 167 | |
| 168 | write_files = not args.preview |
| 169 | |
| 170 | if args.root_dir: |
Geoffrey Martin-Noble | f7be55a | 2020-01-28 17:47:42 -0800 | [diff] [blame] | 171 | convert_directory_tree( |
Scott Todd | bb79717 | 2020-04-15 17:20:33 -0700 | [diff] [blame] | 172 | os.path.join(repo_root, args.root_dir), write_files, |
| 173 | args.allow_partial_conversion) |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 174 | elif args.dir: |
Geoffrey Martin-Noble | f7be55a | 2020-01-28 17:47:42 -0800 | [diff] [blame] | 175 | convert_directory( |
Scott Todd | bb79717 | 2020-04-15 17:20:33 -0700 | [diff] [blame] | 176 | os.path.join(repo_root, args.dir), write_files, |
| 177 | args.allow_partial_conversion) |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 178 | |
| 179 | |
| 180 | if __name__ == "__main__": |
| 181 | setup_environment() |
| 182 | main(parse_arguments()) |