From 1b24a1d614228b2919589d0d5ce91c18778a1119 Mon Sep 17 00:00:00 2001 From: Kristof Dhondt Date: Thu, 3 Aug 2023 00:06:32 +0200 Subject: [PATCH] set up basic support for copying files from the java.home directory to the build output --- .../com/oracle/svm/core/BuildArtifacts.java | 4 +- .../oracle/svm/hosted/jdk/JDKConfigFiles.java | 86 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/JDKConfigFiles.java diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/BuildArtifacts.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/BuildArtifacts.java index c28733966d839..b375b587424ee 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/BuildArtifacts.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/BuildArtifacts.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2023, 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 @@ -47,6 +47,8 @@ enum ArtifactType { JDK_LIBRARY("jdk_libraries"), /* For all library shims for the JDK needed at run-time. */ JDK_LIBRARY_SHIM(JDK_LIBRARY.getJsonKey()), // distinction should not be important to users. + /* For all configuration files from java.home needed at run-time */ + JDK_CONFIG_FILE("jdk_config"), /* Language home artifacts for Truffle languages needed at run-time. */ LANGUAGE_HOME("language_home"), diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/JDKConfigFiles.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/JDKConfigFiles.java new file mode 100644 index 0000000000000..37f13434bca30 --- /dev/null +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/JDKConfigFiles.java @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2023, 2023, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + */ +package com.oracle.svm.hosted.jdk; + +import com.oracle.svm.core.BuildArtifacts; +import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature; +import com.oracle.svm.core.feature.InternalFeature; +import com.oracle.svm.core.util.VMError; +import org.graalvm.nativeimage.ImageSingletons; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collections; +import java.util.Set; +import java.util.TreeSet; +import java.util.concurrent.ConcurrentHashMap; + +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; + +public final class JDKConfigFiles { + + public static JDKConfigFiles singleton() { + return ImageSingletons.lookup(JDKConfigFiles.class); + } + + final Set configFiles = Collections.newSetFromMap(new ConcurrentHashMap<>()); + + JDKConfigFiles() { + } + + public void register(String configFile) { + if (configFile.startsWith("/")) { + configFiles.add(configFile.substring(1)); + } else { + configFiles.add(configFile); + } + } +} + +@AutomaticallyRegisteredFeature +final class JDKConfigFilesFeature implements InternalFeature { + @Override + public void afterRegistration(AfterRegistrationAccess access) { + ImageSingletons.add(JDKConfigFiles.class, new JDKConfigFiles()); + } + + @Override + public void afterImageWrite(AfterImageWriteAccess access) { + Path jdkHomeDir = Path.of(System.getProperty("java.home")).normalize(); + for (String configFile : new TreeSet<>(JDKConfigFiles.singleton().configFiles)) { + Path jdkConfigPath = jdkHomeDir.resolve(configFile).normalize(); + VMError.guarantee(jdkConfigPath.startsWith(jdkHomeDir)); + Path imageConfigPath = access.getImagePath().resolveSibling(configFile); + try { + Files.createDirectories(imageConfigPath); + Files.copy(jdkConfigPath, imageConfigPath, REPLACE_EXISTING); + BuildArtifacts.singleton().add(BuildArtifacts.ArtifactType.JDK_CONFIG_FILE, imageConfigPath); + } catch (IOException e) { + VMError.shouldNotReachHere(e); + } + } + } +}