László Csomor | 5f99fda | 2017-08-11 09:28:12 +0200 | [diff] [blame] | 1 | # pylint: disable=g-direct-third-party-import |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 2 | # pylint: disable=g-bad-file-header |
| 3 | # Copyright 2017 The Bazel Authors. All rights reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http:#www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | """Creates the embedded_tools.zip that is part of the Bazel binary.""" |
| 17 | |
hlopko | 5a661c7 | 2017-08-09 12:03:03 +0200 | [diff] [blame] | 18 | import contextlib |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 19 | import fnmatch |
| 20 | import os |
| 21 | import os.path |
| 22 | import re |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 23 | import sys |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 24 | import zipfile |
| 25 | |
László Csomor | 5f99fda | 2017-08-11 09:28:12 +0200 | [diff] [blame] | 26 | from src.create_embedded_tools_lib import copy_tar_to_zip |
| 27 | from src.create_embedded_tools_lib import copy_zip_to_zip |
| 28 | from src.create_embedded_tools_lib import is_executable |
| 29 | |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 30 | output_paths = [ |
iirina | d4651e8 | 2019-03-15 03:15:37 -0700 | [diff] [blame] | 31 | ('*tools/jdk/BUILD', lambda x: 'tools/jdk/BUILD'), |
Klaus Aehlig | 6f52fca | 2019-03-18 03:43:40 -0700 | [diff] [blame] | 32 | ('*tools/build_defs/repo/BUILD.repo', |
| 33 | lambda x: 'tools/build_defs/repo/BUILD'), |
philwo | e67c961 | 2019-05-06 04:28:48 -0700 | [diff] [blame] | 34 | ('*tools/platforms/BUILD.tools', lambda x: 'platforms/BUILD'), |
cpeyser | 8613c90 | 2017-09-01 00:15:29 +0200 | [diff] [blame] | 35 | ('*tools/platforms/*', lambda x: 'platforms/' + os.path.basename(x)), |
Laszlo Csomor | ea642d6 | 2018-09-03 05:37:04 -0700 | [diff] [blame] | 36 | ('*tools/cpp/runfiles/generated_*', |
| 37 | lambda x: 'tools/cpp/runfiles/' + os.path.basename(x)[len('generated_'):]), |
iirina | 5bd5543 | 2019-01-16 06:36:56 -0800 | [diff] [blame] | 38 | ('*jarjar_command_deploy.jar', |
| 39 | lambda x: 'tools/jdk/jarjar_command_deploy.jar'), |
iirina | d4651e8 | 2019-03-15 03:15:37 -0700 | [diff] [blame] | 40 | ('*BUILD.java_langtools', lambda x: 'third_party/java/jdk/langtools/BUILD'), |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 41 | ('*launcher.exe', lambda x: 'tools/launcher/launcher.exe'), |
Yun Peng | 394211b | 2017-09-15 15:59:14 +0200 | [diff] [blame] | 42 | ('*def_parser.exe', lambda x: 'tools/def_parser/def_parser.exe'), |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 43 | ('*zipper.exe', lambda x: 'tools/zip/zipper/zipper.exe'), |
| 44 | ('*zipper', lambda x: 'tools/zip/zipper/zipper'), |
iirina | ba3ebcb | 2019-03-21 08:02:56 -0700 | [diff] [blame] | 45 | ('*third_party/jarjar/BUILD.tools', lambda x: 'third_party/jarjar/BUILD'), |
| 46 | ('*third_party/jarjar/LICENSE', lambda x: 'third_party/jarjar/LICENSE'), |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 47 | ('*src/objc_tools/*', |
| 48 | lambda x: 'tools/objc/precomp_' + os.path.basename(x)), |
| 49 | ('*xcode*StdRedirect.dylib', lambda x: 'tools/objc/StdRedirect.dylib'), |
| 50 | ('*xcode*make_hashed_objlist.py', |
| 51 | lambda x: 'tools/objc/make_hashed_objlist.py'), |
| 52 | ('*xcode*realpath', lambda x: 'tools/objc/realpath'), |
| 53 | ('*xcode*xcode-locator', lambda x: 'tools/objc/xcode-locator'), |
philwo | e67c961 | 2019-05-06 04:28:48 -0700 | [diff] [blame] | 54 | ('*src/tools/xcode/*', lambda x: 'tools/objc/' + os.path.basename(x)), |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 55 | ('*external/openjdk_*/file/*.tar.gz', lambda x: 'jdk.tar.gz'), |
| 56 | ('*external/openjdk_*/file/*.zip', lambda x: 'jdk.zip'), |
twerth | 683922c | 2018-12-20 05:12:08 -0800 | [diff] [blame] | 57 | ('*src/minimal_jdk.tar.gz', lambda x: 'jdk.tar.gz'), |
| 58 | ('*src/minimal_jdk.zip', lambda x: 'jdk.zip'), |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 59 | ('*', lambda x: re.sub(r'^.*bazel-out/[^/]*/bin/', '', x, count=1)), |
| 60 | ] |
| 61 | |
| 62 | |
| 63 | def get_output_path(path): |
| 64 | for pattern, transformer in output_paths: |
| 65 | if fnmatch.fnmatch(path.replace('\\', '/'), pattern): |
| 66 | # BUILD.tools are stored as BUILD files. |
| 67 | return transformer(path).replace('/BUILD.tools', '/BUILD') |
| 68 | |
| 69 | |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 70 | def get_input_files(argsfile): |
philwo | e67c961 | 2019-05-06 04:28:48 -0700 | [diff] [blame] | 71 | """Returns a dict of archive_file to input_file. |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 72 | |
| 73 | This describes the files that should be put into the generated archive. |
| 74 | |
| 75 | Args: |
| 76 | argsfile: The file containing the list of input files. |
philwo | e67c961 | 2019-05-06 04:28:48 -0700 | [diff] [blame] | 77 | |
| 78 | Raises: |
| 79 | ValueError: When two input files map to the same output file. |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 80 | """ |
| 81 | with open(argsfile, 'r') as f: |
philwo | e67c961 | 2019-05-06 04:28:48 -0700 | [diff] [blame] | 82 | input_files = sorted(set(x.strip() for x in f.readlines())) |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 83 | |
| 84 | result = {} |
| 85 | for input_file in input_files: |
| 86 | # If we have both a BUILD and a BUILD.tools file, take the latter only. |
| 87 | if (os.path.basename(input_file) == 'BUILD' and |
| 88 | input_file + '.tools' in input_files): |
| 89 | continue |
| 90 | |
philwo | e67c961 | 2019-05-06 04:28:48 -0700 | [diff] [blame] | 91 | # It's an error to have two files map to the same output file, because the |
| 92 | # result is hard to predict and can easily be wrong. |
| 93 | output_path = get_output_path(input_file) |
| 94 | if output_path in result: |
| 95 | raise ValueError( |
| 96 | 'Duplicate output file: Both {} and {} map to {}'.format( |
| 97 | result[output_path], input_file, output_path)) |
| 98 | result[output_path] = input_file |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 99 | |
philwo | e67c961 | 2019-05-06 04:28:48 -0700 | [diff] [blame] | 100 | return result |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 101 | |
| 102 | |
| 103 | def copy_jdk_into_archive(output_zip, archive_file, input_file): |
Laszlo Csomor | ea642d6 | 2018-09-03 05:37:04 -0700 | [diff] [blame] | 104 | """Extract the JDK and adds it to the archive under jdk/*.""" |
László Csomor | 5f99fda | 2017-08-11 09:28:12 +0200 | [diff] [blame] | 105 | |
| 106 | def _replace_dirname(filename): |
| 107 | # Rename the first folder to 'jdk', because Bazel looks for a |
| 108 | # bundled JDK in the embedded tools using that folder name. |
| 109 | return 'jdk/' + '/'.join(filename.split('/')[1:]) |
| 110 | |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 111 | # The JDK is special - it's extracted instead of copied. |
| 112 | if archive_file.endswith('.tar.gz'): |
László Csomor | 5f99fda | 2017-08-11 09:28:12 +0200 | [diff] [blame] | 113 | copy_tar_to_zip(output_zip, input_file, _replace_dirname) |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 114 | elif archive_file.endswith('.zip'): |
László Csomor | 5f99fda | 2017-08-11 09:28:12 +0200 | [diff] [blame] | 115 | copy_zip_to_zip(output_zip, input_file, _replace_dirname) |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 116 | |
| 117 | |
| 118 | def main(): |
| 119 | output_zip = os.path.join(os.getcwd(), sys.argv[1]) |
| 120 | input_files = get_input_files(sys.argv[2]) |
| 121 | |
| 122 | # Copy all the input_files into output_zip. |
hlopko | 5a661c7 | 2017-08-09 12:03:03 +0200 | [diff] [blame] | 123 | # Adding contextlib.closing to be python 2.6 (for centos 6.7) compatible |
| 124 | with contextlib.closing( |
| 125 | zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED)) as output_zip: |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 126 | zipinfo = zipfile.ZipInfo('WORKSPACE', (1980, 1, 1, 0, 0, 0)) |
| 127 | zipinfo.external_attr = 0o644 << 16 |
| 128 | output_zip.writestr(zipinfo, 'workspace(name = "bazel_tools")\n') |
| 129 | |
philwo | e67c961 | 2019-05-06 04:28:48 -0700 | [diff] [blame] | 130 | # By sorting the file list, the resulting ZIP file will be reproducible and |
| 131 | # deterministic. |
| 132 | for archive_file, input_file in sorted(input_files.items()): |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 133 | if os.path.basename(archive_file) in ('jdk.tar.gz', 'jdk.zip'): |
| 134 | copy_jdk_into_archive(output_zip, archive_file, input_file) |
| 135 | else: |
| 136 | zipinfo = zipfile.ZipInfo(archive_file, (1980, 1, 1, 0, 0, 0)) |
| 137 | zipinfo.external_attr = 0o755 << 16 if is_executable( |
| 138 | input_file) else 0o644 << 16 |
| 139 | zipinfo.compress_type = zipfile.ZIP_DEFLATED |
| 140 | with open(input_file, 'rb') as f: |
| 141 | output_zip.writestr(zipinfo, f.read()) |
| 142 | |
| 143 | |
| 144 | if __name__ == '__main__': |
| 145 | main() |