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 = [ |
| 31 | ('*tools/jdk/BUILD*', lambda x: 'tools/jdk/BUILD'), |
cpeyser | 8613c90 | 2017-09-01 00:15:29 +0200 | [diff] [blame] | 32 | ('*tools/platforms/platforms.BUILD', lambda x: 'platforms/BUILD'), |
| 33 | ('*tools/platforms/*', lambda x: 'platforms/' + os.path.basename(x)), |
Laszlo Csomor | ea642d6 | 2018-09-03 05:37:04 -0700 | [diff] [blame] | 34 | ('*tools/cpp/runfiles/generated_*', |
| 35 | lambda x: 'tools/cpp/runfiles/' + os.path.basename(x)[len('generated_'):]), |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 36 | ('*JavaBuilder*_deploy.jar', lambda x: 'tools/jdk/' + os.path.basename(x)), |
| 37 | ('*JacocoCoverage*_deploy.jar', |
| 38 | lambda x: 'tools/jdk/JacocoCoverage_deploy.jar'), |
| 39 | ('*turbine_deploy.jar', lambda x: 'tools/jdk/turbine_deploy.jar'), |
Klaus Aehlig | 76eb2a4 | 2018-01-19 02:44:58 -0800 | [diff] [blame] | 40 | ('*javac-9+181-r4173-1.jar', |
| 41 | lambda x: 'third_party/java/jdk/langtools/javac-9+181-r4173-1.jar'), |
Pascal Terjan | 6b29a14 | 2018-03-05 09:56:23 -0800 | [diff] [blame] | 42 | ('*bazel-singlejar_deploy.jar', |
| 43 | lambda x: 'tools/jdk/singlejar/bazel-singlejar_deploy.jar'), |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 44 | ('*GenClass_deploy.jar', lambda x: 'tools/jdk/GenClass_deploy.jar'), |
| 45 | ('*ExperimentalRunner_deploy.jar', |
| 46 | lambda x: 'tools/jdk/ExperimentalTestRunner_deploy.jar'), |
| 47 | ('*Runner_deploy.jar', lambda x: 'tools/jdk/TestRunner_deploy.jar'), |
xingao | 5dcf548 | 2018-06-29 07:35:32 -0700 | [diff] [blame] | 48 | ('*singlejar_local', lambda x: 'tools/jdk/singlejar/singlejar'), |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 49 | ('*launcher.exe', lambda x: 'tools/launcher/launcher.exe'), |
Yun Peng | 394211b | 2017-09-15 15:59:14 +0200 | [diff] [blame] | 50 | ('*def_parser.exe', lambda x: 'tools/def_parser/def_parser.exe'), |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 51 | ('*ijar.exe', lambda x: 'tools/jdk/ijar/ijar.exe'), |
| 52 | ('*ijar', lambda x: 'tools/jdk/ijar/ijar'), |
| 53 | ('*zipper.exe', lambda x: 'tools/zip/zipper/zipper.exe'), |
| 54 | ('*zipper', lambda x: 'tools/zip/zipper/zipper'), |
| 55 | ('*src/objc_tools/*', |
| 56 | lambda x: 'tools/objc/precomp_' + os.path.basename(x)), |
| 57 | ('*xcode*StdRedirect.dylib', lambda x: 'tools/objc/StdRedirect.dylib'), |
| 58 | ('*xcode*make_hashed_objlist.py', |
| 59 | lambda x: 'tools/objc/make_hashed_objlist.py'), |
| 60 | ('*xcode*realpath', lambda x: 'tools/objc/realpath'), |
| 61 | ('*xcode*xcode-locator', lambda x: 'tools/objc/xcode-locator'), |
| 62 | ('*src/tools/xcode/*.sh', lambda x: 'tools/objc/' + os.path.basename(x)), |
| 63 | ('*src/tools/xcode/*', |
| 64 | lambda x: 'tools/objc/' + os.path.basename(x) + '.sh'), |
| 65 | ('*external/openjdk_*/file/*.tar.gz', lambda x: 'jdk.tar.gz'), |
| 66 | ('*external/openjdk_*/file/*.zip', lambda x: 'jdk.zip'), |
| 67 | ('*', lambda x: re.sub(r'^.*bazel-out/[^/]*/bin/', '', x, count=1)), |
| 68 | ] |
| 69 | |
| 70 | |
| 71 | def get_output_path(path): |
| 72 | for pattern, transformer in output_paths: |
| 73 | if fnmatch.fnmatch(path.replace('\\', '/'), pattern): |
| 74 | # BUILD.tools are stored as BUILD files. |
| 75 | return transformer(path).replace('/BUILD.tools', '/BUILD') |
| 76 | |
| 77 | |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 78 | def get_input_files(argsfile): |
| 79 | """Returns a sorted list of tuples (archive_file, input_file). |
| 80 | |
| 81 | This describes the files that should be put into the generated archive. |
| 82 | |
| 83 | Args: |
| 84 | argsfile: The file containing the list of input files. |
| 85 | """ |
| 86 | with open(argsfile, 'r') as f: |
| 87 | input_files = set(x.strip() for x in f.readlines()) |
| 88 | |
| 89 | result = {} |
| 90 | for input_file in input_files: |
| 91 | # If we have both a BUILD and a BUILD.tools file, take the latter only. |
| 92 | if (os.path.basename(input_file) == 'BUILD' and |
| 93 | input_file + '.tools' in input_files): |
| 94 | continue |
| 95 | |
| 96 | # This gives us the same behavior as the older bash version of this |
| 97 | # tool: If two input files map to the same output files, the one that |
| 98 | # comes last in the list of input files overrides all earlier ones. |
| 99 | result[get_output_path(input_file)] = input_file |
| 100 | |
| 101 | # By sorting the file list, the resulting ZIP file will not be reproducible |
| 102 | # and deterministic. |
| 103 | return sorted(result.items()) |
| 104 | |
| 105 | |
| 106 | def copy_jdk_into_archive(output_zip, archive_file, input_file): |
Laszlo Csomor | ea642d6 | 2018-09-03 05:37:04 -0700 | [diff] [blame] | 107 | """Extract the JDK and adds it to the archive under jdk/*.""" |
László Csomor | 5f99fda | 2017-08-11 09:28:12 +0200 | [diff] [blame] | 108 | |
| 109 | def _replace_dirname(filename): |
| 110 | # Rename the first folder to 'jdk', because Bazel looks for a |
| 111 | # bundled JDK in the embedded tools using that folder name. |
| 112 | return 'jdk/' + '/'.join(filename.split('/')[1:]) |
| 113 | |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 114 | # The JDK is special - it's extracted instead of copied. |
| 115 | if archive_file.endswith('.tar.gz'): |
László Csomor | 5f99fda | 2017-08-11 09:28:12 +0200 | [diff] [blame] | 116 | copy_tar_to_zip(output_zip, input_file, _replace_dirname) |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 117 | elif archive_file.endswith('.zip'): |
László Csomor | 5f99fda | 2017-08-11 09:28:12 +0200 | [diff] [blame] | 118 | copy_zip_to_zip(output_zip, input_file, _replace_dirname) |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 119 | |
| 120 | |
| 121 | def main(): |
| 122 | output_zip = os.path.join(os.getcwd(), sys.argv[1]) |
| 123 | input_files = get_input_files(sys.argv[2]) |
| 124 | |
| 125 | # Copy all the input_files into output_zip. |
hlopko | 5a661c7 | 2017-08-09 12:03:03 +0200 | [diff] [blame] | 126 | # Adding contextlib.closing to be python 2.6 (for centos 6.7) compatible |
| 127 | with contextlib.closing( |
| 128 | zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED)) as output_zip: |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 129 | zipinfo = zipfile.ZipInfo('WORKSPACE', (1980, 1, 1, 0, 0, 0)) |
| 130 | zipinfo.external_attr = 0o644 << 16 |
| 131 | output_zip.writestr(zipinfo, 'workspace(name = "bazel_tools")\n') |
| 132 | |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 133 | for archive_file, input_file in input_files: |
| 134 | if os.path.basename(archive_file) in ('jdk.tar.gz', 'jdk.zip'): |
| 135 | copy_jdk_into_archive(output_zip, archive_file, input_file) |
| 136 | else: |
| 137 | zipinfo = zipfile.ZipInfo(archive_file, (1980, 1, 1, 0, 0, 0)) |
| 138 | zipinfo.external_attr = 0o755 << 16 if is_executable( |
| 139 | input_file) else 0o644 << 16 |
| 140 | zipinfo.compress_type = zipfile.ZIP_DEFLATED |
| 141 | with open(input_file, 'rb') as f: |
| 142 | output_zip.writestr(zipinfo, f.read()) |
| 143 | |
| 144 | |
| 145 | if __name__ == '__main__': |
| 146 | main() |