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) |
Geoffrey Martin-Noble | f26a05a | 2020-03-06 13:09:25 -0800 | [diff] [blame] | 55 | # TODO(b/149926655): Invert the default to be strict and rename this flag. |
Geoffrey Martin-Noble | f7be55a | 2020-01-28 17:47:42 -0800 | [diff] [blame] | 56 | parser.add_argument( |
| 57 | "--strict", |
| 58 | help="Does not try to generate files where it cannot convert completely", |
| 59 | action="store_true", |
| 60 | default=False) |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 61 | |
| 62 | # Specify only one of these (defaults to --root_dir=iree). |
| 63 | group = parser.add_mutually_exclusive_group() |
| 64 | group.add_argument( |
| 65 | "--dir", |
| 66 | help="Converts the BUILD file in the given directory", |
| 67 | default=None) |
| 68 | group.add_argument( |
| 69 | "--root_dir", |
| 70 | help="Converts all BUILD files under a root directory (defaults to iree/)", |
| 71 | default="iree") |
| 72 | |
| 73 | # TODO(scotttodd): --check option that returns success/failure depending on |
| 74 | # if files match the converted versions |
| 75 | |
| 76 | args = parser.parse_args() |
| 77 | |
| 78 | # --dir takes precedence over --root_dir. |
| 79 | # They are mutually exclusive, but the default value is still set. |
| 80 | if args.dir: |
| 81 | args.root_dir = None |
| 82 | |
| 83 | return args |
| 84 | |
| 85 | |
| 86 | def setup_environment(): |
| 87 | """Sets up some environment globals.""" |
| 88 | global repo_root |
| 89 | |
| 90 | # Determine the repository root (two dir-levels up). |
| 91 | repo_root = os.path.dirname( |
| 92 | os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 93 | |
| 94 | |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame^] | 95 | def log(*args, **kwargs): |
| 96 | print(*args, **kwargs, file=sys.stderr) |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 97 | |
| 98 | |
Geoffrey Martin-Noble | f7be55a | 2020-01-28 17:47:42 -0800 | [diff] [blame] | 99 | def convert_directory_tree(root_directory_path, write_files, strict): |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame^] | 100 | log(f"convert_directory_tree: {root_directory_path}") |
Geoffrey Martin-Noble | 1b4ac81 | 2020-03-09 11:21:03 -0700 | [diff] [blame] | 101 | for root, _, _ in os.walk(root_directory_path): |
Geoffrey Martin-Noble | f7be55a | 2020-01-28 17:47:42 -0800 | [diff] [blame] | 102 | convert_directory(root, write_files, strict) |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 103 | |
| 104 | |
Geoffrey Martin-Noble | f7be55a | 2020-01-28 17:47:42 -0800 | [diff] [blame] | 105 | def convert_directory(directory_path, write_files, strict): |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 106 | if not os.path.isdir(directory_path): |
Phoenix Meadowlark | 01bbb6c | 2020-03-30 14:20:11 -0700 | [diff] [blame] | 107 | raise FileNotFoundError(f"Cannot find directory '{directory_path}'") |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 108 | |
| 109 | build_file_path = os.path.join(directory_path, "BUILD") |
| 110 | cmakelists_file_path = os.path.join(directory_path, "CMakeLists.txt") |
| 111 | |
| 112 | if not os.path.isfile(build_file_path): |
| 113 | # No Bazel BUILD file in this directory to convert, skip. |
| 114 | return |
| 115 | |
| 116 | global repo_root |
| 117 | rel_build_file_path = os.path.relpath(build_file_path, repo_root) |
| 118 | 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^] | 119 | log(f"Converting {rel_build_file_path} to {rel_cmakelists_file_path}") |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 120 | |
Geoffrey Martin-Noble | 2a36c38 | 2020-01-28 15:04:30 -0800 | [diff] [blame] | 121 | cmake_file_exists = os.path.isfile(cmakelists_file_path) |
Phoenix Meadowlark | 01bbb6c | 2020-03-30 14:20:11 -0700 | [diff] [blame] | 122 | copyright_line = f"# Copyright {datetime.date.today().year} Google LLC" |
Geoffrey Martin-Noble | 2a36c38 | 2020-01-28 15:04:30 -0800 | [diff] [blame] | 123 | write_allowed = write_files |
| 124 | if cmake_file_exists: |
| 125 | with open(cmakelists_file_path) as f: |
| 126 | for i, line in enumerate(f): |
| 127 | if line.startswith("# Copyright"): |
| 128 | copyright_line = line.rstrip() |
| 129 | if EDIT_BLOCKING_PATTERN.search(line): |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame^] | 130 | log(f" {rel_cmakelists_file_path} already exists, and " |
| 131 | f"line {i + 1}: '{line.strip()}' prevents edits. " |
| 132 | f"Falling back to preview") |
Geoffrey Martin-Noble | 2a36c38 | 2020-01-28 15:04:30 -0800 | [diff] [blame] | 133 | write_allowed = False |
| 134 | |
| 135 | if write_allowed: |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 136 | # TODO(scotttodd): Attempt to merge instead of overwrite? |
| 137 | # Existing CMakeLists.txt may have special logic that should be preserved |
Geoffrey Martin-Noble | 2a36c38 | 2020-01-28 15:04:30 -0800 | [diff] [blame] | 138 | if cmake_file_exists: |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame^] | 139 | log(f" {rel_cmakelists_file_path} already exists; overwriting") |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 140 | else: |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame^] | 141 | log(f" {rel_cmakelists_file_path} does not exist yet; creating") |
| 142 | log("") |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 143 | |
| 144 | with open(build_file_path, "rt") as build_file: |
| 145 | build_file_code = compile(build_file.read(), build_file_path, "exec") |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 146 | try: |
Geoffrey Martin-Noble | bdeb1ab | 2020-03-31 13:27:14 -0700 | [diff] [blame^] | 147 | converted_text = bazel_to_cmake_converter.convert_build_file( |
| 148 | build_file_code, copyright_line, strict=strict) |
Geoffrey Martin-Noble | 2a36c38 | 2020-01-28 15:04:30 -0800 | [diff] [blame] | 149 | if write_allowed: |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 150 | with open(cmakelists_file_path, "wt") as cmakelists_file: |
| 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( |
| 172 | os.path.join(repo_root, args.root_dir), write_files, args.strict) |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 173 | elif args.dir: |
Geoffrey Martin-Noble | f7be55a | 2020-01-28 17:47:42 -0800 | [diff] [blame] | 174 | convert_directory( |
| 175 | os.path.join(repo_root, args.dir), write_files, args.strict) |
Scott Todd | d0a0629 | 2020-01-24 14:41:50 -0800 | [diff] [blame] | 176 | |
| 177 | |
| 178 | if __name__ == "__main__": |
| 179 | setup_environment() |
| 180 | main(parse_arguments()) |