Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-30433] Make native-image driver buildable via --module-path. #3382

Merged
merged 24 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1bb37e5
Ensure dwarf debuginfo remains compatible with gdb-7.x
olpaw Apr 27, 2021
473fdc3
Prevent duplicate instances for same APIOptionGroup classes
olpaw Apr 28, 2021
40e08e0
Ensure Graal InvocationPlugins can access required packages
olpaw Apr 28, 2021
8f6de0b
Provide more specific parameter name
olpaw Apr 28, 2021
e93c262
Revisit qualified exports bootlayer modules to take modules from imag…
olpaw Apr 28, 2021
0af7b33
Refine moduleInfo for SubstrateVM components
olpaw Apr 28, 2021
fdd7b69
Only relativize paths that are both absolute
olpaw Apr 28, 2021
c2f5cf1
Prevent TzdbZoneRulesProvider.regionToRules modification during image…
olpaw Apr 29, 2021
12438d3
Print built artifacts info after printing total buildtime
olpaw Apr 30, 2021
7bb479d
Ensure mx_substratevm.graal_compiler_flags() defaults to exports with…
olpaw Apr 30, 2021
a8b4d48
Implement launcher-image building via module-path
olpaw May 5, 2021
0debaec
Remove native-image build server implementation and use
olpaw May 5, 2021
fbfbdb1
Refine is_module_launcher into use_modules to allow differentiating m…
olpaw May 6, 2021
119b007
Improve backward compatibility to removed --server-* options
olpaw May 6, 2021
86bed14
Stylefix
olpaw May 6, 2021
27bac1b
[GR-31019] Implement --add-exports and --add-opens for native-image
olpaw May 7, 2021
6adf167
Extend mx hellomodule to test --add-exports and --add-opens
olpaw May 12, 2021
52f0f02
Remove hack to get correct provides-directives for Java 11 NativeImag…
olpaw May 12, 2021
e09cd51
[GR-31299] Allow OptionDescriptors to be versioned like any other ser…
olpaw May 27, 2021
b4acd92
Add code comments for clarification
olpaw May 27, 2021
48dbbcb
Fix `mx hellomodule` GraalVMConfig
olpaw May 27, 2021
cd1123e
Style fix
olpaw May 27, 2021
8e3ef41
Spotbugs fix: task might be null
olpaw May 27, 2021
1e3a59b
Fix 3rd argument passed to java_properties_escape
olpaw May 31, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions compiler/mx.compiler/mx_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,16 @@ def __opened__(self, arc, srcArc, services):
self.arc = arc

def __add__(self, arcname, contents): # pylint: disable=unexpected-special-method-signature

def add_serviceprovider(service, provider, version):
if version is None:
# Non-versioned service
self.services.setdefault(service, []).append(provider)
else:
# Versioned service
services = self.services.setdefault(int(version), {})
services.setdefault(service, []).append(provider)

m = GraalArchiveParticipant.providersRE.match(arcname)
if m:
if self.isTest:
Expand All @@ -1005,23 +1015,25 @@ def __add__(self, arcname, contents): # pylint: disable=unexpected-special-metho
for service in _decode(contents).strip().split(os.linesep):
assert service
version = m.group(1)
if version is None:
# Non-versioned service
self.services.setdefault(service, []).append(provider)
else:
# Versioned service
services = self.services.setdefault(int(version), {})
services.setdefault(service, []).append(provider)
add_serviceprovider(service, provider, version)
return True
elif arcname.endswith('_OptionDescriptors.class') and not arcname.startswith('META-INF/'):
elif arcname.endswith('_OptionDescriptors.class'):
if self.isTest:
mx.warn('@Option defined in test code will be ignored: ' + arcname)
else:
# Need to create service files for the providers of the
# jdk.internal.vm.ci.options.Options service created by
# jdk.internal.vm.ci.options.processor.OptionProcessor.
version_prefix = 'META-INF/versions/'
if arcname.startswith(version_prefix):
# If OptionDescriptor is version-specific, get version
# from arcname and adjust arcname to non-version form
version, _, arcname = arcname[len(version_prefix):].partition('/')
else:
version = None
provider = arcname[:-len('.class'):].replace('/', '.')
self.services.setdefault('org.graalvm.compiler.options.OptionDescriptors', []).append(provider)
service = 'org.graalvm.compiler.options.OptionDescriptors'
add_serviceprovider(service, provider, version)
return False

def __addsrc__(self, arcname, contents):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ public final void doCompile(TruffleDebugContext truffleDebug,
TruffleCompilationTask task,
TruffleCompilerListener inListener) {
Objects.requireNonNull(compilation, "Compilation must be non null.");
Objects.requireNonNull(task, "Compilation task must be non null.");
org.graalvm.options.OptionValues options = getOptionsForCompiler(optionsMap);
TruffleCompilationIdentifier compilationId = asTruffleCompilationIdentifier(compilation);
CompilableTruffleAST compilable = compilationId.getCompilable();
Expand Down
16 changes: 10 additions & 6 deletions sdk/mx.sdk/mx_sdk_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,19 @@ def __prepare__(mcs, name, this_bases):


class AbstractNativeImageConfig(_with_metaclass(ABCMeta, object)):
def __init__(self, destination, jar_distributions, build_args, links=None, is_polyglot=False, dir_jars=False, home_finder=False, build_time=1): # pylint: disable=super-init-not-called
def __init__(self, destination, jar_distributions, build_args, use_modules=None, links=None, is_polyglot=False, dir_jars=False, home_finder=False, build_time=1): # pylint: disable=super-init-not-called
"""
:type destination: str
:type jar_distributions: list[str]
:type build_args: list[str]
:type links: list[str]
:param str use_modules: Run (with 'laucher') or run and build image with module support (with 'image').
:param bool dir_jars: If true, all jars in the component directory are added to the classpath.
"""
self.destination = mx_subst.path_substitutions.substitute(destination)
self.jar_distributions = jar_distributions
self.build_args = build_args
self.use_modules = use_modules
self.links = [mx_subst.path_substitutions.substitute(link) for link in links] if links else []
self.is_polyglot = is_polyglot
self.dir_jars = dir_jars
Expand Down Expand Up @@ -133,19 +135,21 @@ def get_add_exports_list(required_exports, custom_target_module_str=None):
class LauncherConfig(AbstractNativeImageConfig):
def __init__(self, destination, jar_distributions, main_class, build_args, is_main_launcher=True,
default_symlinks=True, is_sdk_launcher=False, custom_launcher_script=None, extra_jvm_args=None,
is_module_launcher=False, option_vars=None, home_finder=True, **kwargs):
use_modules=None, main_module=None, option_vars=None, home_finder=True, **kwargs):
"""
:param str main_class
:param bool is_main_launcher
:param bool default_symlinks
:param bool is_sdk_launcher: Whether it uses org.graalvm.launcher.Launcher
:param bool is_module_launcher: Whether it uses classpath or module-path for the application
:param str use_modules: Run (with 'laucher') or run and build image with module support (with 'image').
:param str main_module: Specifies the main module. Mandatory if use_modules is not None
:param str custom_launcher_script: Custom launcher script, to be used when not compiled as a native image
"""
super(LauncherConfig, self).__init__(destination, jar_distributions, build_args, home_finder=home_finder, **kwargs)
super(LauncherConfig, self).__init__(destination, jar_distributions, build_args, use_modules, home_finder=home_finder, **kwargs)
self.main_class = main_class
self.is_main_launcher = is_main_launcher
self.module_launcher = is_module_launcher
assert use_modules is None or main_module
self.main_module = main_module
self.default_symlinks = default_symlinks
self.is_sdk_launcher = is_sdk_launcher
self.custom_launcher_script = custom_launcher_script
Expand All @@ -161,7 +165,7 @@ def add_relative_home_path(self, language, path):
self.relative_home_paths[language] = path

def get_add_exports(self, missing_jars):
if not self.module_launcher:
if self.use_modules is None:
return ''
distributions = self.jar_distributions
distributions_transitive = mx.classpath_entries(distributions)
Expand Down
13 changes: 10 additions & 3 deletions sdk/mx.sdk/mx_sdk_vm_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,6 @@ def native_image(self, build_args, output_file, allow_server=False, nonZeroIsFat
native_image_project_name = GraalVmLauncher.launcher_project_name(mx_sdk.LauncherConfig(mx.exe_suffix('native-image'), [], "", []), stage1=True)
native_image_bin = join(stage1.output, stage1.find_single_source_location('dependency:' + native_image_project_name))
native_image_command = [native_image_bin] + build_args
# currently, when building with the bash version of native-image, --no-server is implied (and can not be passed)
output_directory = dirname(output_file)
native_image_command += [
'-H:Path=' + output_directory or ".",
Expand Down Expand Up @@ -1292,10 +1291,14 @@ def _write_ln(s):
_write_ln(u'ImagePath=' + java_properties_escape("${.}/" + relpath(dirname(graalvm_image_destination), graalvm_location).replace(os.sep, '/')))
if requires:
_write_ln(u'Requires=' + java_properties_escape(' '.join(requires), ' ', len('Requires')))
build_with_module_path = image_config.use_modules == 'image'
if isinstance(image_config, mx_sdk.LauncherConfig):
_write_ln(u'ImageClass=' + java_properties_escape(image_config.main_class))
if build_with_module_path:
_write_ln(u'ImageModule=' + java_properties_escape(image_config.main_module))
if location_classpath:
_write_ln(u'ImageClasspath=' + java_properties_escape(':'.join(("${.}/" + e.replace(os.sep, '/') for e in location_classpath)), ':', len('ImageClasspath')))
image_path_arg = u'ImageModulePath=' if build_with_module_path else u'ImageClasspath='
_write_ln(image_path_arg + java_properties_escape(':'.join(("${.}/" + e.replace(os.sep, '/') for e in location_classpath)), ':', len(image_path_arg)))
_write_ln(u'Args=' + java_properties_escape(' '.join(build_args), ' ', len('Args')))
return self._contents

Expand Down Expand Up @@ -1848,7 +1851,10 @@ def _get_main_class():
return self.subject.native_image_config.main_class

def _is_module_launcher():
return str(self.subject.native_image_config.module_launcher)
return str(self.subject.native_image_config.use_modules is not None)

def _get_main_module():
return str(self.subject.native_image_config.main_module)

def _get_extra_jvm_args():
image_config = self.subject.native_image_config
Expand Down Expand Up @@ -1885,6 +1891,7 @@ def _get_add_exports():
_template_subst.register_no_arg('classpath', _get_classpath)
_template_subst.register_no_arg('jre_bin', _get_jre_bin)
_template_subst.register_no_arg('main_class', _get_main_class)
_template_subst.register_no_arg('main_module', _get_main_module)
_template_subst.register_no_arg('extra_jvm_args', _get_extra_jvm_args)
_template_subst.register_no_arg('macro_name', GraalVmNativeProperties.macro_name(self.subject.native_image_config))
_template_subst.register_no_arg('option_vars', _get_option_vars)
Expand Down
4 changes: 3 additions & 1 deletion sdk/mx.sdk/vm/launcher_template.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ set "module_launcher=<module_launcher>"
:: The list of --add-exports can easily exceed the 8191 command
:: line character limit so pass them in a command line arguments file.
if "%module_launcher%"=="True" (
set "main_class=--module <main_module>/<main_class>"
set "app_path_arg=--module-path"
set exports_file="%location%.!basename!.exports"
if not exist "!exports_file!" (
Expand All @@ -87,12 +88,13 @@ if "%module_launcher%"=="True" (
)
set "jvm_args=!jvm_args! @!exports_file!"
) else (
set "main_class=<main_class>"
set "app_path_arg=-cp"
)

if "%VERBOSE_GRAALVM_LAUNCHERS%"=="true" echo on

"%location%<jre_bin>\java" <extra_jvm_args> %jvm_args% %app_path_arg% "%absolute_cp%" <main_class> %launcher_args%
"%location%<jre_bin>\java" <extra_jvm_args> %jvm_args% %app_path_arg% "%absolute_cp%" %main_class% %launcher_args%

exit /b %errorlevel%
:: Function are defined via labels, so have to be defined at the end of the file and skipped
Expand Down
4 changes: 3 additions & 1 deletion sdk/mx.sdk/vm/launcher_template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,19 @@ cp_or_mp="$(IFS=: ; echo "${absolute_cp[*]}")"

module_launcher="<module_launcher>"
if [[ "${module_launcher}" == "True" ]]; then
main_class='--module <main_module>/<main_class>'
app_path_arg="--module-path"
IFS=" " read -ra add_exports <<< "<add_exports>"
for e in "${add_exports[@]}"; do
jvm_args+=("${e}")
done
else
app_path_arg="-cp"
main_class='<main_class>'
fi

if [[ "${VERBOSE_GRAALVM_LAUNCHERS}" == "true" ]]; then
set -x
fi

exec "${location}/<jre_bin>/java" <extra_jvm_args> "${jvm_args[@]}" ${app_path_arg} "${cp_or_mp}" '<main_class>' "${launcher_args[@]}"
exec "${location}/<jre_bin>/java" <extra_jvm_args> "${jvm_args[@]}" ${app_path_arg} "${cp_or_mp}" ${main_class} "${launcher_args[@]}"
63 changes: 22 additions & 41 deletions substratevm/mx.substratevm/mx_substratevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from __future__ import print_function

import os
import time
import re
import tempfile
from glob import glob
Expand Down Expand Up @@ -73,18 +72,13 @@ def svm_java_compliance():
def svm_java8():
return svm_java_compliance() <= mx.JavaCompliance('1.8')

def graal_compiler_flags(version_tag=None):
version_tag = version_tag or svm_java_compliance().value
config_path = mx.dependency('substratevm:svm-compiler-flags-builder').result_file_path(version_tag)
if not exists(config_path):
missing_flags_message = '''
Missing graal-compiler-flags config-file {0}. Possible causes:
* Forgot to run "mx build" before using SubstrateVM.
* Generating config-file for Java {1} missing in SubstrateCompilerFlagsBuilder.compute_graal_compiler_flags_map().
'''
mx.abort(missing_flags_message.format(config_path, version_tag))
with open(config_path, 'r') as config_file:
return config_file.read().splitlines()
def graal_compiler_flags(all_unnamed=True):
version_tag = svm_java_compliance().value
compiler_flags = mx.dependency('substratevm:svm-compiler-flags-builder').compute_graal_compiler_flags_map(all_unnamed=all_unnamed)
if version_tag not in compiler_flags:
missing_flags_message = 'Missing graal-compiler-flags for {0}.\n Did you forget to run "mx build"?'
mx.abort(missing_flags_message.format(version_tag))
return compiler_flags[version_tag]

def svm_unittest_config_participant(config):
vmArgs, mainClass, mainClassArgs = config
Expand Down Expand Up @@ -241,14 +235,10 @@ def native_image_context(common_args=None, hosted_assertions=True, native_image_
common_args = [] if common_args is None else common_args
base_args = ['--no-fallback', '-H:+EnforceMaxRuntimeCompileMethods']
base_args += ['-H:Path=' + svmbuild_dir()]
has_server = mx.get_os() != 'windows'
if mx.get_opts().verbose:
base_args += ['--verbose']
if mx.get_opts().very_verbose:
if has_server:
base_args += ['--verbose-server']
else:
base_args += ['--verbose']
base_args += ['--verbose']
if hosted_assertions:
base_args += native_image_context.hosted_assertions
if native_image_cmd:
Expand Down Expand Up @@ -301,30 +291,15 @@ def remove_quotes(val):

return result

server_use = set()
def native_image_func(args, **kwargs):
all_args = base_args + common_args + args
if '--experimental-build-server' in all_args:
server_use.add(True)
path = query_native_image(all_args, r'^-H:Path(@[^=]*)?=')
name = query_native_image(all_args, r'^-H:Name(@[^=]*)?=')
image = join(path, name)
if not has_server and '--no-server' in all_args:
all_args = [arg for arg in all_args if arg != '--no-server']

_native_image(all_args, **kwargs)
return image
try:
if exists(native_image_cmd) and has_server and server_use:
_native_image(['--server-wipe'])
yield native_image_func
finally:
if exists(native_image_cmd) and has_server and server_use:
def timestr():
return time.strftime('%d %b %Y %H:%M:%S') + ' - '
mx.log(timestr() + 'Shutting down image build servers for ' + native_image_cmd)
_native_image(['--server-shutdown'])
mx.log(timestr() + 'Shutting down completed')

yield native_image_func

native_image_context.hosted_assertions = ['-J-ea', '-J-esa']
_native_unittest_features = '--features=com.oracle.svm.test.ImageInfoTest$TestFeature,com.oracle.svm.test.ServiceLoaderTest$TestFeature,com.oracle.svm.test.SecurityServiceTest$TestFeature'
Expand Down Expand Up @@ -569,7 +544,7 @@ def stderr_collector(x):


def build_js(native_image):
return native_image(['--macro:js-launcher', '--no-server'])
return native_image(['--macro:js-launcher'])

def test_js(js, benchmarks, bin_args=None):
bench_location = join(suite.dir, '..', '..', 'js-benchmarks')
Expand Down Expand Up @@ -854,9 +829,10 @@ def _native_image_launcher_extra_jvm_args():
support_distributions=['substratevm:NATIVE_IMAGE_GRAALVM_SUPPORT'],
launcher_configs=[
mx_sdk_vm.LauncherConfig(
is_module_launcher=not svm_java8(),
use_modules='image' if USE_NI_JPMS else 'launcher' if not svm_java8() else None,
destination="bin/<exe:native-image>",
jar_distributions=["substratevm:SVM_DRIVER"],
main_module="org.graalvm.nativeimage.driver",
main_class=_native_image_launcher_main_class(),
build_args=[],
extra_jvm_args=_native_image_launcher_extra_jvm_args(),
Expand Down Expand Up @@ -1108,12 +1084,17 @@ def hellomodule(args):
proj_dir = join(suite.dir, 'src', 'native-image-module-tests', 'hello.app')
mx.run_maven(['-e', 'install'], cwd=proj_dir)
module_path.append(join(proj_dir, 'target', 'hello-app-1.0-SNAPSHOT.jar'))
with native_image_context(hosted_assertions=False, build_if_missing=False) as native_image:
config = GraalVMConfig.build(native_images=['native-image'])
with native_image_context(hosted_assertions=False, config=config) as native_image:
build_dir = join(svmbuild_dir(), 'hellomodule')
# Build module into native image
mx.log('Building image from java modules: ' + str(module_path))
module_path_sep = ';' if mx.is_windows() else ':'
built_image = native_image(['--verbose', '-ea', '-H:Path=' + build_dir, '-p', module_path_sep.join(module_path), '-m', 'moduletests.hello.app'])
built_image = native_image([
'--verbose', '-ea', '-H:Path=' + build_dir,
'--add-exports=moduletests.hello.lib/hello.privateLib=moduletests.hello.app',
'--add-exports=moduletests.hello.lib/hello.privateLib2=moduletests.hello.app',
'-p', module_path_sep.join(module_path), '-m', 'moduletests.hello.app'])
mx.log('Running image ' + built_image + ' built from module:')
mx.run([built_image])

Expand Down Expand Up @@ -1424,7 +1405,7 @@ def config_file_update(self, file_path, lines, file_paths):

# If renaming or moving this method, please update the error message in
# com.oracle.svm.driver.NativeImage.BuildConfiguration.getBuilderJavaArgs().
def compute_graal_compiler_flags_map(self):
def compute_graal_compiler_flags_map(self, all_unnamed=not USE_NI_JPMS):
graal_compiler_flags_map = dict()
graal_compiler_flags_map[8] = [
'-d64',
Expand All @@ -1441,7 +1422,7 @@ def compute_graal_compiler_flags_map(self):
distributions_transitive = mx.classpath_entries(self.deps)
jdk = mx.get_jdk(tag='default')
required_exports = mx_javamodules.requiredExports(distributions_transitive, jdk)
target_module = None if USE_NI_JPMS else 'ALL-UNNAMED'
target_module = 'ALL-UNNAMED' if all_unnamed else None
exports_flags = mx_sdk_vm.AbstractNativeImageConfig.get_add_exports_list(required_exports, target_module)
graal_compiler_flags_map[11].extend(exports_flags)

Expand Down
4 changes: 0 additions & 4 deletions substratevm/mx.substratevm/rebuild-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ fi
function common() {
cmd_line+=("${graalvm_home}/bin/native-image")

if $(${graalvm_home}/bin/native-image --help-extra | grep -q "\-\-no\-server"); then
cmd_line+=("--no-server")
fi

if [[ -f "${graalvm_home}/lib/svm/builder/svm-enterprise.jar" ]]; then
cmd_line+=("-g")
fi
Expand Down
Loading