Skip to content

Commit d8d9146

Browse files
committed
[GR-31840] [GR-31681] Allow native-image shared libs to be built on module-path and fix resource bundle lookup.
PullRequest: graal/9053
2 parents be4c9b1 + 11f03c4 commit d8d9146

File tree

23 files changed

+519
-392
lines changed

23 files changed

+519
-392
lines changed

compiler/mx.compiler/suite.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,13 +2169,14 @@
21692169
],
21702170
"exports" : [
21712171
"* to com.oracle.graal.graal_enterprise,org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.llvm,com.oracle.svm.svm_enterprise",
2172-
"org.graalvm.compiler.core.common to jdk.internal.vm.compiler.management",
2172+
"org.graalvm.compiler.core.common to jdk.internal.vm.compiler.management,org.graalvm.nativeimage.agent.tracing",
21732173
"org.graalvm.compiler.debug to jdk.internal.vm.compiler.management,org.graalvm.nativeimage.objectfile",
21742174
"org.graalvm.compiler.hotspot to jdk.internal.vm.compiler.management",
21752175
"org.graalvm.compiler.nodes.graphbuilderconf to org.graalvm.nativeimage.driver",
21762176
"org.graalvm.compiler.options to jdk.internal.vm.compiler.management,org.graalvm.nativeimage.driver,org.graalvm.nativeimage.librarysupport",
2177+
"org.graalvm.compiler.phases.common to org.graalvm.nativeimage.agent.tracing,org.graalvm.nativeimage.configure",
21772178
"org.graalvm.compiler.phases.common.jmx to jdk.internal.vm.compiler.management",
2178-
"org.graalvm.compiler.serviceprovider to jdk.internal.vm.compiler.management,org.graalvm.nativeimage.driver",
2179+
"org.graalvm.compiler.serviceprovider to jdk.internal.vm.compiler.management,org.graalvm.nativeimage.driver,org.graalvm.nativeimage.agent.jvmtibase,org.graalvm.nativeimage.agent.diagnostics",
21792180
"org.graalvm.compiler.truffle.jfr to jdk.internal.vm.compiler.truffle.jfr",
21802181
"org.graalvm.libgraal to jdk.internal.vm.compiler.management",
21812182
"org.graalvm.util to jdk.internal.vm.compiler.management",

sdk/mx.sdk/mx_sdk_vm.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ def get_add_exports_list(required_exports, custom_target_module_str=None):
133133
add_exports.append('--add-exports=' + required_module_name + '/' + required_package_name + "=" + target_modules_str)
134134
return sorted(add_exports)
135135

136+
def get_add_exports(self, missing_jars):
137+
if self.use_modules is None:
138+
return ''
139+
distributions = self.jar_distributions
140+
distributions_transitive = mx.classpath_entries(distributions)
141+
distributions_transitive_clean = [entry for entry in distributions_transitive if str(entry) not in missing_jars]
142+
required_exports = mx_javamodules.requiredExports(distributions_transitive_clean, base_jdk())
143+
return AbstractNativeImageConfig.get_add_exports_list(required_exports)
144+
136145

137146
class LauncherConfig(AbstractNativeImageConfig):
138147
def __init__(self, destination, jar_distributions, main_class, build_args, is_main_launcher=True,
@@ -148,10 +157,10 @@ def __init__(self, destination, jar_distributions, main_class, build_args, is_ma
148157
:param str custom_launcher_script: Custom launcher script, to be used when not compiled as a native image
149158
"""
150159
super(LauncherConfig, self).__init__(destination, jar_distributions, build_args, use_modules, home_finder=home_finder, **kwargs)
160+
self.main_module = main_module
161+
assert self.use_modules is None or self.main_module
151162
self.main_class = main_class
152163
self.is_main_launcher = is_main_launcher
153-
assert use_modules is None or main_module
154-
self.main_module = main_module
155164
self.default_symlinks = default_symlinks
156165
self.is_sdk_launcher = is_sdk_launcher
157166
self.custom_launcher_script = custom_launcher_script
@@ -166,15 +175,6 @@ def add_relative_home_path(self, language, path):
166175
language, self.relative_home_paths[language], path, self.destination))
167176
self.relative_home_paths[language] = path
168177

169-
def get_add_exports(self, missing_jars):
170-
if self.use_modules is None:
171-
return ''
172-
distributions = self.jar_distributions
173-
distributions_transitive = mx.classpath_entries(distributions)
174-
distributions_transitive_clean = [entry for entry in distributions_transitive if str(entry) not in missing_jars]
175-
required_exports = mx_javamodules.requiredExports(distributions_transitive_clean, base_jdk())
176-
return ' '.join(AbstractNativeImageConfig.get_add_exports_list(required_exports))
177-
178178

179179
class LanguageLauncherConfig(LauncherConfig):
180180
def __init__(self, destination, jar_distributions, main_class, build_args, language,
@@ -191,11 +191,11 @@ def __init__(self, destination, jar_distributions, main_class, build_args, langu
191191

192192

193193
class LibraryConfig(AbstractNativeImageConfig):
194-
def __init__(self, destination, jar_distributions, build_args, jvm_library=False, **kwargs):
194+
def __init__(self, destination, jar_distributions, build_args, jvm_library=False, use_modules=None, **kwargs):
195195
"""
196196
:param bool jvm_library
197197
"""
198-
super(LibraryConfig, self).__init__(destination, jar_distributions, build_args, **kwargs)
198+
super(LibraryConfig, self).__init__(destination, jar_distributions, build_args, use_modules, **kwargs)
199199
self.jvm_library = jvm_library
200200

201201

sdk/mx.sdk/mx_sdk_vm_impl.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ def _find_escaping_links(root_dir):
704704
_add(layout, _component_base, 'dependency:{}/polyglot.config'.format(launcher_project), _component)
705705
for _library_config in sorted(_get_library_configs(_component), key=lambda c: c.destination):
706706
graalvm_dists.update(_library_config.jar_distributions)
707+
self.jimage_ignore_jars.update(_library_config.jar_distributions)
707708
if _library_config.jvm_library:
708709
assert isinstance(_component, (mx_sdk.GraalVmJdkComponent, mx_sdk.GraalVmJreComponent))
709710
_svm_library_home = _jvm_library_dest
@@ -1113,8 +1114,11 @@ def __init__(self, component, image_config, **kw_args):
11131114
:type component: mx_sdk.GraalVmComponent | None
11141115
:type image_config: mx_sdk.AbstractNativeImageConfig
11151116
"""
1116-
deps = []
11171117
self.image_config = image_config
1118+
# With Java > 8 there are cases where image_config.get_add_exports is getting called in
1119+
# mx_sdk_vm_impl.NativePropertiesBuildTask.contents. This only works after the jar_distributions
1120+
# are made into proper modules. Therefore they have to be specified as dependencies here.
1121+
deps = [] if _src_jdk_version == 8 else list(image_config.jar_distributions)
11181122
super(GraalVmNativeProperties, self).__init__(component, GraalVmNativeProperties.project_name(image_config), deps=deps, **kw_args)
11191123

11201124
@staticmethod
@@ -1272,6 +1276,11 @@ def contents(self):
12721276
raise mx.abort("Profiles for an image must have unique filenames.\nThis is not the case for {}: {}.".format(canonical_name, profiles))
12731277
build_args += ['--pgo=' + ','.join(('${.}/' + n for n in basenames))]
12741278

1279+
build_with_module_path = image_config.use_modules == 'image'
1280+
if build_with_module_path:
1281+
export_deps_to_exclude = [str(dep) for dep in mx.classpath_entries(['substratevm:LIBRARY_SUPPORT'])] + list(_known_missing_jars)
1282+
build_args += image_config.get_add_exports(set(export_deps_to_exclude))
1283+
12751284
requires = [arg[2:] for arg in build_args if arg.startswith('--language:') or arg.startswith('--tool:') or arg.startswith('--macro:')]
12761285
build_args = [arg for arg in build_args if not (arg.startswith('--language:') or arg.startswith('--tool:') or arg.startswith('--macro:'))]
12771286

@@ -1291,7 +1300,6 @@ def _write_ln(s):
12911300
_write_ln(u'ImagePath=' + java_properties_escape("${.}/" + relpath(dirname(graalvm_image_destination), graalvm_location).replace(os.sep, '/')))
12921301
if requires:
12931302
_write_ln(u'Requires=' + java_properties_escape(' '.join(requires), ' ', len('Requires')))
1294-
build_with_module_path = image_config.use_modules == 'image'
12951303
if isinstance(image_config, mx_sdk.LauncherConfig):
12961304
_write_ln(u'ImageClass=' + java_properties_escape(image_config.main_class))
12971305
if build_with_module_path:
@@ -1898,7 +1906,7 @@ def _get_launcher_args():
18981906
return ''
18991907

19001908
def _get_add_exports():
1901-
res = self.subject.native_image_config.get_add_exports(_known_missing_jars)
1909+
res = ' '.join(self.subject.native_image_config.get_add_exports(_known_missing_jars))
19021910
if mx.is_windows():
19031911
res = ' '.join(('"{}"'.format(a) for a in res.split()))
19041912
return res

substratevm/mx.substratevm/mx_substratevm.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -831,19 +831,21 @@ def _native_image_launcher_extra_jvm_args():
831831
launcher_configs=[
832832
mx_sdk_vm.LauncherConfig(
833833
use_modules='image' if USE_NI_JPMS else 'launcher' if not svm_java8() else None,
834+
main_module="org.graalvm.nativeimage.driver",
834835
destination="bin/<exe:native-image>",
835836
jar_distributions=["substratevm:SVM_DRIVER"],
836-
main_module="org.graalvm.nativeimage.driver",
837837
main_class=_native_image_launcher_main_class(),
838838
build_args=[],
839839
extra_jvm_args=_native_image_launcher_extra_jvm_args(),
840840
),
841841
],
842842
library_configs=[
843843
mx_sdk_vm.LibraryConfig(
844+
use_modules='image' if USE_NI_JPMS else 'launcher' if not svm_java8() else None,
844845
destination="<lib:native-image-agent>",
845846
jvm_library=True,
846847
jar_distributions=[
848+
'substratevm:SVM_CONFIGURE',
847849
'substratevm:JVMTI_AGENT_BASE',
848850
'substratevm:SVM_AGENT',
849851
],
@@ -853,6 +855,7 @@ def _native_image_launcher_extra_jvm_args():
853855
],
854856
),
855857
mx_sdk_vm.LibraryConfig(
858+
use_modules='image' if USE_NI_JPMS else 'launcher' if not svm_java8() else None,
856859
destination="<lib:native-image-diagnostics-agent>",
857860
jvm_library=True,
858861
jar_distributions=[
@@ -1006,6 +1009,8 @@ def _native_image_configure_extra_jvm_args():
10061009
support_distributions=[],
10071010
launcher_configs=[
10081011
mx_sdk_vm.LauncherConfig(
1012+
use_modules='image' if USE_NI_JPMS else 'launcher' if not svm_java8() else None,
1013+
main_module="org.graalvm.nativeimage.configure",
10091014
destination="bin/<exe:native-image-configure>",
10101015
jar_distributions=["substratevm:SVM_CONFIGURE"],
10111016
main_class="com.oracle.svm.configure.ConfigurationTool",
@@ -1086,17 +1091,28 @@ def hellomodule(args):
10861091
proj_dir = join(suite.dir, 'src', 'native-image-module-tests', 'hello.app')
10871092
mx.run_maven(['-e', 'install'], cwd=proj_dir)
10881093
module_path.append(join(proj_dir, 'target', 'hello-app-1.0-SNAPSHOT.jar'))
1089-
config = GraalVMConfig.build(native_images=['native-image'])
1094+
config = GraalVMConfig.build(native_images=['native-image', 'lib:native-image-agent', 'lib:native-image-diagnostics-agent'])
10901095
with native_image_context(hosted_assertions=False, config=config) as native_image:
1096+
module_path_sep = ';' if mx.is_windows() else ':'
1097+
moduletest_run_args = [
1098+
'--add-exports=moduletests.hello.lib/hello.privateLib=moduletests.hello.app',
1099+
'--add-opens=moduletests.hello.lib/hello.privateLib2=moduletests.hello.app',
1100+
'-p', module_path_sep.join(module_path), '-m', 'moduletests.hello.app'
1101+
]
1102+
mx.log('Running module-tests on JVM:')
10911103
build_dir = join(svmbuild_dir(), 'hellomodule')
1104+
mx.run([
1105+
vm_executable_path('java', config),
1106+
# also test if native-image-agent works
1107+
'-agentlib:native-image-agent=config-output-dir=' + join(build_dir, 'config-output-dir-{pid}-{datetime}/'),
1108+
] + moduletest_run_args)
1109+
10921110
# Build module into native image
10931111
mx.log('Building image from java modules: ' + str(module_path))
1094-
module_path_sep = ';' if mx.is_windows() else ':'
10951112
built_image = native_image([
1096-
'--verbose', '-ea', '-H:Path=' + build_dir,
1097-
'--add-exports=moduletests.hello.lib/hello.privateLib=moduletests.hello.app',
1098-
'--add-exports=moduletests.hello.lib/hello.privateLib2=moduletests.hello.app',
1099-
'-p', module_path_sep.join(module_path), '-m', 'moduletests.hello.app'])
1113+
'--verbose', '-H:Path=' + build_dir,
1114+
'--trace-class-initialization=hello.lib.Greeter', # also test native-image-diagnostics-agent
1115+
] + moduletest_run_args)
11001116
mx.log('Running image ' + built_image + ' built from module:')
11011117
mx.run([built_image])
11021118

substratevm/mx.substratevm/suite.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# pylint: disable=line-too-long
12
suite = {
23
"mxversion": "5.301.0",
34
"name": "substratevm",
@@ -1090,7 +1091,8 @@
10901091
"com.oracle.svm.hosted.agent to java.instrument",
10911092
"com.oracle.svm.core.graal.thread to jdk.internal.vm.compiler",
10921093
"com.oracle.svm.core.classinitialization to jdk.internal.vm.compiler",
1093-
"* to org.graalvm.nativeimage.driver,org.graalvm.nativeimage.librarysupport,org.graalvm.nativeimage.llvm,com.oracle.svm.svm_enterprise",
1094+
"com.oracle.svm.truffle.api to org.graalvm.truffle",
1095+
"* to org.graalvm.nativeimage.driver,org.graalvm.nativeimage.configure,org.graalvm.nativeimage.librarysupport,org.graalvm.nativeimage.llvm,org.graalvm.nativeimage.agent.jvmtibase,org.graalvm.nativeimage.agent.tracing,org.graalvm.nativeimage.agent.diagnostics,com.oracle.svm.svm_enterprise",
10941096
],
10951097
"opens" : [
10961098
"com.oracle.svm.core.nodes to jdk.internal.vm.compiler",
@@ -1109,6 +1111,8 @@
11091111
],
11101112
"uses" : [
11111113
"org.graalvm.nativeimage.Platform",
1114+
"com.oracle.truffle.api.TruffleLanguage.Provider",
1115+
"com.oracle.truffle.api.instrumentation.TruffleInstrument.Provider",
11121116
],
11131117
"requiresConcealed": {
11141118
"jdk.internal.vm.ci": [
@@ -1167,6 +1171,13 @@
11671171
"LIBRARY_SUPPORT",
11681172
"SVM_DRIVER",
11691173
],
1174+
"moduleInfo" : {
1175+
"name" : "org.graalvm.nativeimage.agent.jvmtibase",
1176+
"exports" : [
1177+
"com.oracle.svm.jvmtiagentbase",
1178+
"com.oracle.svm.jvmtiagentbase.jvmti",
1179+
],
1180+
},
11701181
},
11711182

11721183
"LIBRARY_SUPPORT": {
@@ -1278,6 +1289,7 @@
12781289
"name" : "org.graalvm.nativeimage.driver",
12791290
"exports" : [
12801291
"com.oracle.svm.driver",
1292+
"com.oracle.svm.driver.metainf",
12811293
],
12821294
"uses" : [
12831295
"org.graalvm.compiler.options.OptionDescriptors",
@@ -1304,10 +1316,19 @@
13041316
"JVMTI_AGENT_BASE",
13051317
"LIBRARY_SUPPORT",
13061318
"SVM_DRIVER",
1319+
"SVM_CONFIGURE"
13071320
],
1308-
"overlaps" : [
1309-
"SVM_CONFIGURE",
1310-
],
1321+
"moduleInfo" : {
1322+
"name" : "org.graalvm.nativeimage.agent.tracing",
1323+
"exports" : [
1324+
"com.oracle.svm.agent",
1325+
],
1326+
"requiresConcealed" : {
1327+
"jdk.internal.vm.ci" : [
1328+
"jdk.vm.ci.meta",
1329+
],
1330+
}
1331+
},
13111332
# vm: included as binary, tool descriptor intentionally not copied
13121333
},
13131334

@@ -1321,6 +1342,12 @@
13211342
"JVMTI_AGENT_BASE",
13221343
"LIBRARY_SUPPORT",
13231344
],
1345+
"moduleInfo" : {
1346+
"name" : "org.graalvm.nativeimage.agent.diagnostics",
1347+
"exports" : [
1348+
"com.oracle.svm.diagnosticsagent",
1349+
],
1350+
},
13241351
},
13251352

13261353
"SVM_CONFIGURE": {
@@ -1333,6 +1360,13 @@
13331360
"distDependencies": [
13341361
"LIBRARY_SUPPORT",
13351362
],
1363+
"moduleInfo" : {
1364+
"name" : "org.graalvm.nativeimage.configure",
1365+
"exports" : [
1366+
"* to org.graalvm.nativeimage.agent.tracing",
1367+
"com.oracle.svm.configure",
1368+
],
1369+
},
13361370
},
13371371

13381372

substratevm/src/com.oracle.objectfile.jdk11/src/com/oracle/objectfile/ModuleAccess.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@ public class ModuleAccess {
3131
public static void openModuleByClass(Class<?> declaringClass, Class<?> accessingClass) {
3232
Module declaringModule = declaringClass.getModule();
3333
String packageName = declaringClass.getPackageName();
34-
Module accessingModule = accessingClass == null ? null : accessingClass.getModule();
35-
if (accessingModule != null && accessingModule.isNamed()) {
36-
if (!declaringModule.isOpen(packageName, accessingModule)) {
37-
Modules.addOpens(declaringModule, packageName, accessingModule);
34+
Module namedAccessingModule = null;
35+
if (accessingClass != null) {
36+
Module accessingModule = accessingClass.getModule();
37+
if (accessingModule.isNamed()) {
38+
namedAccessingModule = accessingModule;
3839
}
40+
}
41+
if (namedAccessingModule != null ? declaringModule.isOpen(packageName, namedAccessingModule) : declaringModule.isOpen(packageName)) {
42+
return;
43+
}
44+
if (namedAccessingModule != null) {
45+
Modules.addOpens(declaringModule, packageName, namedAccessingModule);
3946
} else {
4047
Modules.addOpensToAllUnnamed(declaringModule, packageName);
4148
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/ClassLoaderQuery.java renamed to substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/ClassLoaderSupport.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,11 +24,28 @@
2424
*/
2525
package com.oracle.svm.core;
2626

27+
import java.util.List;
28+
import java.util.Locale;
29+
import java.util.ResourceBundle;
30+
2731
import org.graalvm.nativeimage.Platform;
2832
import org.graalvm.nativeimage.Platforms;
2933

3034
@Platforms(Platform.HOSTED_ONLY.class)
31-
public interface ClassLoaderQuery {
35+
public abstract class ClassLoaderSupport {
36+
37+
public boolean isNativeImageClassLoader(ClassLoader classLoader) {
38+
ClassLoader loader = classLoader;
39+
while (loader != null) {
40+
if (isNativeImageClassLoaderImpl(loader)) {
41+
return true;
42+
}
43+
loader = loader.getParent();
44+
}
45+
return false;
46+
}
47+
48+
protected abstract boolean isNativeImageClassLoaderImpl(ClassLoader classLoader);
3249

33-
boolean isNativeImageClassLoader(ClassLoader c);
50+
public abstract List<ResourceBundle> getResourceBundle(String bundleName, Locale locale);
3451
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/RuntimeAssertionsSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private boolean desiredAssertionStatusImpl(String name, boolean fallback) {
184184
}
185185

186186
private boolean desiredAssertionStatusImpl(String name, ClassLoader classLoader) {
187-
boolean isNativeImageClassLoader = ImageSingletons.lookup(ClassLoaderQuery.class).isNativeImageClassLoader(classLoader);
187+
boolean isNativeImageClassLoader = ImageSingletons.lookup(ClassLoaderSupport.class).isNativeImageClassLoader(classLoader);
188188
return desiredAssertionStatusImpl(name, isNativeImageClassLoader ? defaultAssertionStatus : systemAssertionStatus);
189189
}
190190

0 commit comments

Comments
 (0)