blob: edc987aa47b3a359e415d0ef61381c6c73557177 [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#
Googlerbd7a6b92022-02-24 07:38:58 -08009# http://www.apache.org/licenses/LICENSE-2.0
Philipp Wollermann4c558982017-07-27 18:01:12 +020010#
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 = [
wyv156d9c02022-05-11 04:45:59 -070031 ('*MODULE.tools', lambda x: 'MODULE.bazel'),
Ivo List504a5b72020-12-03 04:00:13 -080032 ('*tools/jdk/BUILD.tools', lambda x: 'tools/jdk/BUILD'),
Googler7ae201b2023-09-19 07:32:41 -070033 (
34 '*tools/build_defs/repo/BUILD.repo',
35 lambda x: 'tools/build_defs/repo/BUILD',
36 ),
37 (
38 '*tools/build_defs/build_info/BUILD.tools',
39 lambda x: 'tools/build_defs/build_info/BUILD',
40 ),
Googler59a44942023-09-22 05:38:58 -070041 (
42 '*tools/build_defs/build_info/templates/BUILD.tools',
43 lambda x: 'tools/build_defs/build_info/templates/BUILD',
44 ),
ilist84464b62021-11-29 23:47:39 -080045 ('*tools/j2objc/BUILD.tools', lambda x: 'tools/j2objc/BUILD'),
cpeyser8613c902017-09-01 00:15:29 +020046 ('*tools/platforms/*', lambda x: 'platforms/' + os.path.basename(x)),
hlopkobea9d252019-11-15 07:30:16 -080047 ('*tools/cpp/BUILD.tools', lambda x: 'tools/cpp/BUILD'),
Laszlo Csomorea642d62018-09-03 05:37:04 -070048 ('*tools/cpp/runfiles/generated_*',
49 lambda x: 'tools/cpp/runfiles/' + os.path.basename(x)[len('generated_'):]),
Philipp Wollermann4c558982017-07-27 18:01:12 +020050 ('*launcher.exe', lambda x: 'tools/launcher/launcher.exe'),
Richard Levasseurf898da52023-11-07 09:09:17 -080051 ('*launcher_maker.exe', lambda x: 'tools/launcher/launcher_maker.exe'),
Yun Peng394211b2017-09-15 15:59:14 +020052 ('*def_parser.exe', lambda x: 'tools/def_parser/def_parser.exe'),
Philipp Wollermann4c558982017-07-27 18:01:12 +020053 ('*zipper.exe', lambda x: 'tools/zip/zipper/zipper.exe'),
54 ('*zipper', lambda x: 'tools/zip/zipper/zipper'),
Philipp Wollermann4c558982017-07-27 18:01:12 +020055 ('*xcode*xcode-locator', lambda x: 'tools/objc/xcode-locator'),
philwoe67c9612019-05-06 04:28:48 -070056 ('*src/tools/xcode/*', lambda x: 'tools/objc/' + os.path.basename(x)),
Googler3d994df2021-02-11 07:23:35 -080057 # --experimental_sibling_repository_layout=false
Philipp Wollermann4c558982017-07-27 18:01:12 +020058 ('*external/openjdk_*/file/*.tar.gz', lambda x: 'jdk.tar.gz'),
59 ('*external/openjdk_*/file/*.zip', lambda x: 'jdk.zip'),
Googler3d994df2021-02-11 07:23:35 -080060 # --experimental_sibling_repository_layout=true
61 ('*openjdk_*/file/*.tar.gz', lambda x: 'jdk.tar.gz'),
62 ('*openjdk_*/file/*.zip', lambda x: 'jdk.zip'),
twerth683922c2018-12-20 05:12:08 -080063 ('*src/minimal_jdk.tar.gz', lambda x: 'jdk.tar.gz'),
64 ('*src/minimal_jdk.zip', lambda x: 'jdk.zip'),
laszlocsomora6545052019-11-05 05:18:32 -080065 ('*.bzl.tools', lambda x: x[:-6]),
Philipp Wollermann4c558982017-07-27 18:01:12 +020066 ('*', lambda x: re.sub(r'^.*bazel-out/[^/]*/bin/', '', x, count=1)),
67]
68
69
70def get_output_path(path):
71 for pattern, transformer in output_paths:
72 if fnmatch.fnmatch(path.replace('\\', '/'), pattern):
73 # BUILD.tools are stored as BUILD files.
74 return transformer(path).replace('/BUILD.tools', '/BUILD')
75
76
Philipp Wollermann4c558982017-07-27 18:01:12 +020077def get_input_files(argsfile):
philwoe67c9612019-05-06 04:28:48 -070078 """Returns a dict of archive_file to input_file.
Philipp Wollermann4c558982017-07-27 18:01:12 +020079
80 This describes the files that should be put into the generated archive.
81
82 Args:
83 argsfile: The file containing the list of input files.
philwoe67c9612019-05-06 04:28:48 -070084
85 Raises:
86 ValueError: When two input files map to the same output file.
Philipp Wollermann4c558982017-07-27 18:01:12 +020087 """
88 with open(argsfile, 'r') as f:
philwoe67c9612019-05-06 04:28:48 -070089 input_files = sorted(set(x.strip() for x in f.readlines()))
Philipp Wollermann4c558982017-07-27 18:01:12 +020090
91 result = {}
92 for input_file in input_files:
93 # If we have both a BUILD and a BUILD.tools file, take the latter only.
94 if (os.path.basename(input_file) == 'BUILD' and
95 input_file + '.tools' in input_files):
96 continue
97
philwoe67c9612019-05-06 04:28:48 -070098 # It's an error to have two files map to the same output file, because the
99 # result is hard to predict and can easily be wrong.
100 output_path = get_output_path(input_file)
101 if output_path in result:
102 raise ValueError(
103 'Duplicate output file: Both {} and {} map to {}'.format(
104 result[output_path], input_file, output_path))
105 result[output_path] = input_file
Philipp Wollermann4c558982017-07-27 18:01:12 +0200106
philwoe67c9612019-05-06 04:28:48 -0700107 return result
Philipp Wollermann4c558982017-07-27 18:01:12 +0200108
109
110def copy_jdk_into_archive(output_zip, archive_file, input_file):
Laszlo Csomorea642d62018-09-03 05:37:04 -0700111 """Extract the JDK and adds it to the archive under jdk/*."""
László Csomor5f99fda2017-08-11 09:28:12 +0200112
113 def _replace_dirname(filename):
114 # Rename the first folder to 'jdk', because Bazel looks for a
115 # bundled JDK in the embedded tools using that folder name.
116 return 'jdk/' + '/'.join(filename.split('/')[1:])
117
Philipp Wollermann4c558982017-07-27 18:01:12 +0200118 # The JDK is special - it's extracted instead of copied.
119 if archive_file.endswith('.tar.gz'):
László Csomor5f99fda2017-08-11 09:28:12 +0200120 copy_tar_to_zip(output_zip, input_file, _replace_dirname)
Philipp Wollermann4c558982017-07-27 18:01:12 +0200121 elif archive_file.endswith('.zip'):
László Csomor5f99fda2017-08-11 09:28:12 +0200122 copy_zip_to_zip(output_zip, input_file, _replace_dirname)
Philipp Wollermann4c558982017-07-27 18:01:12 +0200123
124
125def main():
126 output_zip = os.path.join(os.getcwd(), sys.argv[1])
127 input_files = get_input_files(sys.argv[2])
128
129 # Copy all the input_files into output_zip.
hlopko5a661c72017-08-09 12:03:03 +0200130 # Adding contextlib.closing to be python 2.6 (for centos 6.7) compatible
131 with contextlib.closing(
132 zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED)) as output_zip:
Philipp Wollermann4c558982017-07-27 18:01:12 +0200133 zipinfo = zipfile.ZipInfo('WORKSPACE', (1980, 1, 1, 0, 0, 0))
134 zipinfo.external_attr = 0o644 << 16
135 output_zip.writestr(zipinfo, 'workspace(name = "bazel_tools")\n')
136
philwoe67c9612019-05-06 04:28:48 -0700137 # By sorting the file list, the resulting ZIP file will be reproducible and
138 # deterministic.
139 for archive_file, input_file in sorted(input_files.items()):
Philipp Wollermann4c558982017-07-27 18:01:12 +0200140 if os.path.basename(archive_file) in ('jdk.tar.gz', 'jdk.zip'):
141 copy_jdk_into_archive(output_zip, archive_file, input_file)
142 else:
143 zipinfo = zipfile.ZipInfo(archive_file, (1980, 1, 1, 0, 0, 0))
144 zipinfo.external_attr = 0o755 << 16 if is_executable(
145 input_file) else 0o644 << 16
146 zipinfo.compress_type = zipfile.ZIP_DEFLATED
147 with open(input_file, 'rb') as f:
148 output_zip.writestr(zipinfo, f.read())
149
150
151if __name__ == '__main__':
152 main()