This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
build_labsjdk.py
executable file
·391 lines (343 loc) · 16.3 KB
/
build_labsjdk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/env python3 -u
#
# ----------------------------------------------------------------------------------------------------
#
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#
# ----------------------------------------------------------------------------------------------------
"""
Utility for building a labsjdk-ce-11 binary from the sources in this repo.
$ ./build_labsjdk.py --clean-after-build
This script assumes all the necessary build dependencies are installed.
See: https://wiki.openjdk.java.net/display/Build/Supported+Build+Platforms
"""
from __future__ import print_function
import zipfile
import tarfile
import os
import stat
import glob
import shutil
import pipes
import time
import subprocess
import sys
import re
import platform
from os.path import join, exists, isdir, basename, abspath, dirname, getsize
from argparse import ArgumentParser
from datetime import timedelta
# Temporary imports and (re)definitions to support Python 2 and Python 3
if sys.version_info[0] < 3:
def _decode(x):
return x
def _encode(x):
return x
else:
def _decode(x):
return x.decode()
def _encode(x):
return x.encode()
def abort(code_or_message):
raise SystemExit(code_or_message)
def timestamp():
return time.strftime('%Y-%m-%d %H:%M:%S')
def human_fmt(num):
for unit in ['', 'K', 'M', 'G']:
if abs(num) < 1024.0:
return "%3.1f%sB" % (num, unit)
num /= 1024.0
return "%.1fTB" % (num)
def log(msg):
if hasattr(shutil, 'disk_usage'):
total, _, free = shutil.disk_usage('.')
print('{} [{} of {} free]: {}'.format(timestamp(), human_fmt(free), human_fmt(total), str(msg)))
else:
print('{}: {}'.format(timestamp(), str(msg)))
def log_call(args, **kwargs):
cwd = kwargs.get('cwd')
if cwd:
log('(in directory {})'.format(cwd))
log(' '.join(map(pipes.quote, args)))
def check_call(args, **kwargs):
log_call(args, **kwargs)
return subprocess.check_call(args, **kwargs)
def call(args, **kwargs):
log_call(args, **kwargs)
return subprocess.call(args, **kwargs)
def check_output(args, **kwargs):
log_call(args, **kwargs)
return _decode(subprocess.check_output(args, **kwargs))
def get_java_version(version_numbers_file):
values = {}
with open(version_numbers_file) as fp:
for l in fp:
line = l.strip()
if line and not line.startswith('#'):
key, value = [e.strip() for e in line.split('=', 1)]
values[key] = value
return '{}.{}.{}'.format(values['DEFAULT_VERSION_FEATURE'], values['DEFAULT_VERSION_INTERIM'], values['DEFAULT_VERSION_UPDATE'])
def create_bundle(input_bundles, bundle, jdk_debug_level, install_prefix, extract=False, clean_install_dir=False):
"""
Creates a gzipped tar in `bundle` composed of the contents in `input_bundles`.
The top level directory in the gzipped tar will be `install_prefix`.
The gzipped tar is extracted in the parent directory of `bundle` if `extract` is `True`.
"""
tmp_dir = bundle + '-' + str(os.getpid())
rmtree(tmp_dir)
os.makedirs(tmp_dir)
def get_root(names, path, expected=None):
names = [n[2:] if n.startswith('./') else n for n in names if n not in ['.', './']]
roots = set([n.split('/', 1)[0] for n in names])
assert len(roots) == 1, '{} contains {} roots: {}'.format(path, len(roots), roots)
root = next(iter(roots))
assert expected is None or root == expected, 'expected single root in {} to be {} but was {}'.format(path, expected, root)
return root
root_name = None
for input_bundle in input_bundles:
log('Extracting {}'.format(input_bundle))
if input_bundle.endswith('.zip'):
with zipfile.ZipFile(input_bundle) as zf:
root_name = get_root(zf.namelist(), input_bundle, root_name)
zf.extractall(tmp_dir)
else:
with tarfile.open(input_bundle, 'r:gz') as tf:
root_name = get_root(tf.getnames(), input_bundle, root_name)
tf.extractall(tmp_dir)
os.remove(input_bundle)
root_dir = join(tmp_dir, root_name)
if jdk_debug_level == 'fastdebug':
if 'darwin' in bundle:
contents_dir = join(root_dir, 'Contents')
os.makedirs(contents_dir)
os.rename(join(root_dir, 'fastdebug'), join(contents_dir, 'Home'))
else:
for e in glob.glob(join(root_dir, 'fastdebug', '*')):
os.rename(e, join(root_dir, basename(e)))
os.rmdir(join(root_dir, 'fastdebug'))
if jdk_debug_level == 'slowdebug':
if 'darwin' in bundle:
contents_dir = join(root_dir, 'Contents')
os.makedirs(contents_dir)
os.rename(join(root_dir, 'slowdebug'), join(contents_dir, 'Home'))
else:
for e in glob.glob(join(root_dir, 'slowdebug', '*')):
os.rename(e, join(root_dir, basename(e)))
os.rmdir(join(root_dir, 'slowdebug'))
def on_error(err):
raise err
log('Archiving {}'.format(bundle))
with tarfile.open(bundle, 'w:gz') as tf:
for root, _, filenames in os.walk(root_dir, onerror=on_error):
for name in filenames:
f = join(root, name)
# Make sure files in the image are readable by everyone
file_mode = os.stat(f).st_mode
mode = stat.S_IRGRP | stat.S_IROTH | file_mode
if isdir(f) or (file_mode & stat.S_IXUSR):
mode = mode | stat.S_IXGRP | stat.S_IXOTH
os.chmod(f, mode)
arcname = install_prefix + '/' + os.path.relpath(f, root_dir)
tf.add(name=f, arcname=arcname, recursive=False)
rmtree(tmp_dir)
if clean_install_dir:
rmtree(join(dirname(bundle), install_prefix))
if extract:
with tarfile.open(bundle, 'r:gz') as tf:
log('Extracting {}'.format(bundle))
tf.extractall(dirname(bundle))
def get_os():
p = sys.platform
if p.startswith('darwin'):
return 'darwin'
if p.startswith('linux'):
return 'linux'
if p.startswith('sunos'):
return 'solaris'
if p.startswith('win32') or p.startswith('cygwin'):
return 'windows'
abort('Unknown operating system ' + sys.platform)
def get_arch():
machine = platform.uname()[4]
if machine in ['aarch64']:
return 'aarch64'
if machine in ['amd64', 'AMD64', 'x86_64', 'i86pc']:
return 'amd64'
if machine in ['sun4v', 'sun4u', 'sparc64']:
return 'sparcv9'
abort('unknown or unsupported architecture: os=' + get_os() + ', machine=' + machine)
def is_musl(build_os):
if build_os == 'linux':
# To check, we run ldd with --version. In case of musl, --version does not exist but outputs the name of the dynamic linker which contains musl in the name.
try:
ldd_version_output = check_output(['ldd', '--version'], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
ldd_version_output = _decode(e.output)
return ldd_version_output.find('musl') != -1
return False
def rmtree(path, ignore_errors=False):
if not exists(path):
return
if get_os() == 'windows':
# https://stackoverflow.com/questions/1889597/deleting-directory-in-python
def on_error(func, _path, exc_info):
os.chmod(_path, stat.S_IWRITE)
func(_path)
else:
on_error = None
log('Removing ' + path)
shutil.rmtree(path, onerror=on_error)
def get_jvmci_version_from_tags(repo):
tags = check_output(['git', '-C', repo, 'tag']).split()
jvmci_re = re.compile(r'jvmci-(\d+)\.(\d+)-b(\d+)')
tags = [t for t in tags if jvmci_re.match(t)]
if not tags:
return None
tags = [jvmci_re.match(t).group(1, 2, 3) for t in tags]
latest = sorted(tags, reverse=True)[0]
version = '{}.{}-b{:02d}'.format(latest[0], latest[1], int(latest[2]))
# Bump the build number in the version and add a "-dev" suffix if
# the current working directory has modified files or if its
# commit hash is not equal to the commit hash of the selected jvmci tag
latest_commit = check_output(['git', '-C', repo, 'show', '--pretty=%H', '-s', 'jvmci-' + version])
head_commit = check_output(['git', '-C', repo, 'show', '--pretty=%H', '-s'])
is_dirty = check_output(['git', '-C', repo, 'status', '--untracked-files=no', '--porcelain']) != ''
if is_dirty or latest_commit != head_commit:
return '{}.{}-b{:02d}-dev'.format(latest[0], latest[1], int(latest[2]) + 1)
return version
def main():
env = os.environ
parser = ArgumentParser()
parser.add_argument('--make', action='store', help='GNU make executable', default=env.get('MAKE', 'make'), metavar='<path>')
parser.add_argument('--boot-jdk', action='store', help='value for --boot-jdk configure option (default: $JAVA_HOME)',
default=env.get('JAVA_HOME'), required='JAVA_HOME' not in env, metavar='<path>')
parser.add_argument('--clean-after-build', action='store_true', help='clean build directory after producing labsjdk binaries')
parser.add_argument('--jdk-debug-level', action='store', help='value for --with-debug-level JDK config option', default='release', choices=['release', 'fastdebug', 'slowdebug'])
parser.add_argument('--devkit', action='store', help='value for --with-devkit configure option', default=env.get('DEVKIT', ''), metavar='<path>')
parser.add_argument('--jvmci-version', action='store', help='JVMCI version (e.g., 19.3-b03)', metavar='<version>')
extra_config = parser.add_mutually_exclusive_group()
extra_config.add_argument('--configure-options', action='store', help='File containing extra options for configure script, one option per line', metavar='<path>')
extra_config.add_argument('--configure-option', action='append', help='Extra option appended to configure script', metavar='<path>')
opts = parser.parse_args()
build_os = get_os()
build_arch = get_arch()
jdk_debug_level = opts.jdk_debug_level
jdk_src_dir = abspath(dirname(__file__))
jvmci_version = opts.jvmci_version or get_jvmci_version_from_tags(jdk_src_dir)
if not jvmci_version:
abort('Could not derive JVMCI version from git tags - please specify it with --jvmci-version option')
build_dir = join(jdk_src_dir, 'build')
labsjdks_dir = join(build_dir, 'labsjdks')
target_dir = join(labsjdks_dir, jdk_debug_level)
if not exists(target_dir):
os.makedirs(target_dir)
version_numbers_file = join(jdk_src_dir, 'make', 'autoconf', 'version-numbers')
java_version = get_java_version(version_numbers_file)
tag_prefix = 'jdk-' + java_version + '+'
tag_command = ['git', '-C', jdk_src_dir, 'tag']
build_nums = [int(line[len(tag_prefix):]) for line in check_output(tag_command).split() if line.startswith(tag_prefix)]
if len(build_nums) == 0:
abort('No tags matching prefix "{}" in output of `{}`'.format(tag_prefix, ' '.join(tag_command)))
build_num = sorted(build_nums, reverse=True)[0]
debug_qualifier = '' if jdk_debug_level == 'release' else '-debug'
debug_qualifier = '-slowdebug' if jdk_debug_level == 'slowdebug' else debug_qualifier
jdk_bundle_prefix = 'labsjdk-ce-{}+{}-jvmci-{}{}'.format(java_version, build_num, jvmci_version, debug_qualifier)
install_prefix = 'labsjdk-ce-{}-jvmci-{}{}'.format(java_version, jvmci_version, debug_qualifier)
jdk_bundle_name = jdk_bundle_prefix + '-{}-{}.tar.gz'.format(build_os, build_arch)
jdk_bundle = join(target_dir, jdk_bundle_name)
conf_name = build_os + '-' + build_arch + debug_qualifier
# zlib should only be bundled on Windows
zlib_bundling = 'bundled' if build_os == 'windows' else 'system'
configure_options = [
"--with-debug-level=" + jdk_debug_level,
"--with-extra-asflags=", # To prevent a compilation fail when building with libmusl
"--enable-aot=no", # HotSpot AOT is omitted from labsjdk
"--with-jvm-features=-graal", # Do not build Graal since it is effectively dead in OpenJDK (JDK-8263327)
"--with-jvm-variants=server",
"--with-conf-name=" + conf_name,
"--with-boot-jdk=" + opts.boot_jdk,
"--with-devkit=" + opts.devkit,
"--with-zlib=" + zlib_bundling,
"--with-version-build=" + str(build_num),
"--with-version-opt=" + "jvmci-" + jvmci_version,
"--with-version-pre="
]
if build_arch != 'aarch64':
configure_options.append("--disable-precompiled-headers")
if is_musl(build_os):
# If we are building on musl, some warnings are produced which would abort the compilation
configure_options.append("--disable-warnings-as-errors")
if opts.configure_options:
with open(opts.configure_options, 'r') as conf:
for line in conf:
option = line.strip()
if not option.startswith('#'):
configure_options.append(option)
elif opts.configure_option:
configure_options.extend(opts.configure_option)
check_call(["sh", "configure"] + configure_options, cwd=jdk_src_dir)
check_call([opts.make, "LOG=info", "CONF=" + conf_name, "product-bundles", "static-libs-bundles"], cwd=jdk_src_dir)
bundles_dir = join(build_dir, conf_name, 'bundles')
if opts.clean_after_build:
new_bundles_dir = join(labsjdks_dir, jdk_debug_level + '-bundles')
rmtree(new_bundles_dir)
os.rename(bundles_dir, new_bundles_dir)
bundles_dir = new_bundles_dir
rmtree(join(build_dir, conf_name))
# Create labsjdk bundles and image
jdk_bundle_ext = '.zip' if 'windows' in conf_name else '.tar.gz'
input_bundles = glob.glob(join(bundles_dir, '*_bin' + debug_qualifier + jdk_bundle_ext)) + \
glob.glob(join(bundles_dir, '*_bin-static-libs' + debug_qualifier + '.tar.gz'))
input_symbols_bundles = glob.glob(join(bundles_dir, '*_bin' + debug_qualifier + '-symbols.tar.gz'))
create_bundle(input_bundles, jdk_bundle, jdk_debug_level, install_prefix, extract=True, clean_install_dir=True)
if input_symbols_bundles:
symbols_bundle = jdk_bundle.replace('.tar.gz', '.symbols.tar.gz')
create_bundle(input_symbols_bundles, symbols_bundle, jdk_debug_level, install_prefix)
else:
symbols_bundle = None
if opts.clean_after_build:
rmtree(bundles_dir)
java_home = join(target_dir, install_prefix)
if build_os == 'darwin':
java_home = join(java_home, 'Contents', 'Home')
java_home_symlink = os.path.relpath(join(target_dir, 'java_home'), target_dir)
if exists(java_home_symlink):
os.unlink(java_home_symlink)
if 'windows' in conf_name:
subprocess.check_call(['mklink', '/D', java_home_symlink, java_home], shell=True)
else:
check_call(['ln', '-s', java_home, java_home_symlink])
java_exe = join(java_home_symlink, 'bin', 'java')
if build_os == 'windows':
java_exe += '.exe'
check_call([java_exe, "-version"])
log('--- Build Succeeded ---')
log('JDK bundle: {} [{}]'.format(jdk_bundle, human_fmt(getsize(jdk_bundle))))
if symbols_bundle:
log('Symbols bundle: {} [{}]'.format(symbols_bundle, human_fmt(getsize(symbols_bundle))))
if __name__ == '__main__':
start = time.time()
try:
main()
finally:
duration = time.time() - start
log('Total build time: {}'.format(timedelta(seconds=duration)))