blob: 17d747fafca5721fe233aaf352f469619735025d [file] [log] [blame]
László Csomor5f99fda2017-08-11 09:28:12 +02001# pylint: disable=g-direct-third-party-import
Philipp Wollermann4c558982017-07-27 18:01:12 +02002# 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
hlopko5a661c72017-08-09 12:03:03 +020018import contextlib
Philipp Wollermann4c558982017-07-27 18:01:12 +020019import fnmatch
20import os
21import os.path
22import re
Philipp Wollermann4c558982017-07-27 18:01:12 +020023import sys
Philipp Wollermann4c558982017-07-27 18:01:12 +020024import zipfile
25
László Csomor5f99fda2017-08-11 09:28:12 +020026from src.create_embedded_tools_lib import copy_tar_to_zip
27from src.create_embedded_tools_lib import copy_zip_to_zip
28from src.create_embedded_tools_lib import is_executable
29
Philipp Wollermann4c558982017-07-27 18:01:12 +020030output_paths = [
31 ('*tools/jdk/BUILD*', lambda x: 'tools/jdk/BUILD'),
cpeyser8613c902017-09-01 00:15:29 +020032 ('*tools/platforms/platforms.BUILD', lambda x: 'platforms/BUILD'),
33 ('*tools/platforms/*', lambda x: 'platforms/' + os.path.basename(x)),
Philipp Wollermann4c558982017-07-27 18:01:12 +020034 ('*JavaBuilder*_deploy.jar', lambda x: 'tools/jdk/' + os.path.basename(x)),
35 ('*JacocoCoverage*_deploy.jar',
36 lambda x: 'tools/jdk/JacocoCoverage_deploy.jar'),
37 ('*turbine_deploy.jar', lambda x: 'tools/jdk/turbine_deploy.jar'),
Liam Miller-Cushone2eea442017-08-22 01:48:38 +020038 ('*javac-9-dev-r4023-3.jar',
39 lambda x: 'third_party/java/jdk/langtools/javac-9-dev-r4023-3.jar'),
Philipp Wollermann4c558982017-07-27 18:01:12 +020040 ('*SingleJar_deploy.jar',
41 lambda x: 'tools/jdk/singlejar/SingleJar_deploy.jar'),
42 ('*GenClass_deploy.jar', lambda x: 'tools/jdk/GenClass_deploy.jar'),
43 ('*ExperimentalRunner_deploy.jar',
44 lambda x: 'tools/jdk/ExperimentalTestRunner_deploy.jar'),
45 ('*Runner_deploy.jar', lambda x: 'tools/jdk/TestRunner_deploy.jar'),
46 ('*singlejar', lambda x: 'tools/jdk/singlejar/singlejar'),
47 ('*launcher.exe', lambda x: 'tools/launcher/launcher.exe'),
Yun Peng394211b2017-09-15 15:59:14 +020048 ('*def_parser.exe', lambda x: 'tools/def_parser/def_parser.exe'),
Philipp Wollermann4c558982017-07-27 18:01:12 +020049 ('*ijar.exe', lambda x: 'tools/jdk/ijar/ijar.exe'),
50 ('*ijar', lambda x: 'tools/jdk/ijar/ijar'),
51 ('*zipper.exe', lambda x: 'tools/zip/zipper/zipper.exe'),
52 ('*zipper', lambda x: 'tools/zip/zipper/zipper'),
53 ('*src/objc_tools/*',
54 lambda x: 'tools/objc/precomp_' + os.path.basename(x)),
55 ('*xcode*StdRedirect.dylib', lambda x: 'tools/objc/StdRedirect.dylib'),
56 ('*xcode*make_hashed_objlist.py',
57 lambda x: 'tools/objc/make_hashed_objlist.py'),
58 ('*xcode*realpath', lambda x: 'tools/objc/realpath'),
59 ('*xcode*xcode-locator', lambda x: 'tools/objc/xcode-locator'),
60 ('*src/tools/xcode/*.sh', lambda x: 'tools/objc/' + os.path.basename(x)),
61 ('*src/tools/xcode/*',
62 lambda x: 'tools/objc/' + os.path.basename(x) + '.sh'),
63 ('*external/openjdk_*/file/*.tar.gz', lambda x: 'jdk.tar.gz'),
64 ('*external/openjdk_*/file/*.zip', lambda x: 'jdk.zip'),
65 ('*', lambda x: re.sub(r'^.*bazel-out/[^/]*/bin/', '', x, count=1)),
66]
67
68
69def get_output_path(path):
70 for pattern, transformer in output_paths:
71 if fnmatch.fnmatch(path.replace('\\', '/'), pattern):
72 # BUILD.tools are stored as BUILD files.
73 return transformer(path).replace('/BUILD.tools', '/BUILD')
74
75
Philipp Wollermann4c558982017-07-27 18:01:12 +020076def get_input_files(argsfile):
77 """Returns a sorted list of tuples (archive_file, input_file).
78
79 This describes the files that should be put into the generated archive.
80
81 Args:
82 argsfile: The file containing the list of input files.
83 """
84 with open(argsfile, 'r') as f:
85 input_files = set(x.strip() for x in f.readlines())
86
87 result = {}
88 for input_file in input_files:
89 # If we have both a BUILD and a BUILD.tools file, take the latter only.
90 if (os.path.basename(input_file) == 'BUILD' and
91 input_file + '.tools' in input_files):
92 continue
93
94 # This gives us the same behavior as the older bash version of this
95 # tool: If two input files map to the same output files, the one that
96 # comes last in the list of input files overrides all earlier ones.
97 result[get_output_path(input_file)] = input_file
98
99 # By sorting the file list, the resulting ZIP file will not be reproducible
100 # and deterministic.
101 return sorted(result.items())
102
103
104def copy_jdk_into_archive(output_zip, archive_file, input_file):
László Csomor5f99fda2017-08-11 09:28:12 +0200105
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 Wollermann4c558982017-07-27 18:01:12 +0200111 # The JDK is special - it's extracted instead of copied.
112 if archive_file.endswith('.tar.gz'):
László Csomor5f99fda2017-08-11 09:28:12 +0200113 copy_tar_to_zip(output_zip, input_file, _replace_dirname)
Philipp Wollermann4c558982017-07-27 18:01:12 +0200114 elif archive_file.endswith('.zip'):
László Csomor5f99fda2017-08-11 09:28:12 +0200115 copy_zip_to_zip(output_zip, input_file, _replace_dirname)
Philipp Wollermann4c558982017-07-27 18:01:12 +0200116
117
118def 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.
hlopko5a661c72017-08-09 12:03:03 +0200123 # 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 Wollermann4c558982017-07-27 18:01:12 +0200126 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
130 zipinfo = zipfile.ZipInfo('tools/defaults/BUILD', (1980, 1, 1, 0, 0, 0))
131 zipinfo.external_attr = 0o644 << 16
132 output_zip.writestr(zipinfo, '')
133
134 for archive_file, input_file in input_files:
135 if os.path.basename(archive_file) in ('jdk.tar.gz', 'jdk.zip'):
136 copy_jdk_into_archive(output_zip, archive_file, input_file)
137 else:
138 zipinfo = zipfile.ZipInfo(archive_file, (1980, 1, 1, 0, 0, 0))
139 zipinfo.external_attr = 0o755 << 16 if is_executable(
140 input_file) else 0o644 << 16
141 zipinfo.compress_type = zipfile.ZIP_DEFLATED
142 with open(input_file, 'rb') as f:
143 output_zip.writestr(zipinfo, f.read())
144
145
146if __name__ == '__main__':
147 main()