|
| 1 | +/* |
| 2 | +* Copyright (c) 2020, 2022 Oracle and/or its affiliates. All rights reserved. |
| 3 | +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | +* |
| 5 | +* The Universal Permissive License (UPL), Version 1.0 |
| 6 | +* |
| 7 | +* Subject to the condition set forth below, permission is hereby granted to any |
| 8 | +* person obtaining a copy of this software, associated documentation and/or |
| 9 | +* data (collectively the "Software"), free of charge and under any and all |
| 10 | +* copyright rights in the Software, and any and all patent rights owned or |
| 11 | +* freely licensable by each licensor hereunder covering either (i) the |
| 12 | +* unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | +* the Larger Works (as defined below), to deal in both |
| 14 | +* |
| 15 | +* (a) the Software, and |
| 16 | +* |
| 17 | +* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | +* one is included with the Software each a "Larger Work" to which the Software |
| 19 | +* is contributed by such licensors), |
| 20 | +* |
| 21 | +* without restriction, including without limitation the rights to copy, create |
| 22 | +* derivative works of, display, perform, and distribute the Software and make, |
| 23 | +* use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | +* Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | +* either these or other terms. |
| 26 | +* |
| 27 | +* This license is subject to the following condition: |
| 28 | +* |
| 29 | +* The above copyright notice and either this complete permission notice or at a |
| 30 | +* minimum a reference to the UPL must be included in all copies or substantial |
| 31 | +* portions of the Software. |
| 32 | +* |
| 33 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | +* SOFTWARE. |
| 40 | +*/ |
| 41 | +package org.graalvm.buildtools.gradle; |
| 42 | + |
| 43 | +import org.graalvm.buildtools.gradle.dsl.NativeImageOptions; |
| 44 | +import org.graalvm.buildtools.gradle.internal.GraalVMLogger; |
| 45 | +import org.gradle.api.Action; |
| 46 | +import org.gradle.api.Task; |
| 47 | +import org.gradle.api.file.Directory; |
| 48 | +import org.gradle.api.file.FileSystemOperations; |
| 49 | +import org.gradle.api.logging.Logger; |
| 50 | +import org.gradle.api.provider.Provider; |
| 51 | +import org.gradle.process.ExecOperations; |
| 52 | +import org.gradle.process.ExecResult; |
| 53 | + |
| 54 | +import java.io.File; |
| 55 | +import java.util.ArrayList; |
| 56 | +import java.util.Arrays; |
| 57 | +import java.util.List; |
| 58 | +import java.util.stream.Stream; |
| 59 | + |
| 60 | +import static org.graalvm.buildtools.gradle.internal.NativeImageExecutableLocator.findNativeImageExecutable; |
| 61 | +import static org.graalvm.buildtools.utils.SharedConstants.GRAALVM_EXE_EXTENSION; |
| 62 | + |
| 63 | +class MergeAgentFiles implements Action<Task> { |
| 64 | +private final Provider<Boolean> agent; |
| 65 | +private final Provider<String> graalvmHomeProvider; |
| 66 | +private final Provider<Directory> outputDir; |
| 67 | +private final Provider<Boolean> disableToolchainDetection; |
| 68 | +private final ExecOperations execOperations; |
| 69 | +private final NativeImageOptions options; |
| 70 | +private final FileSystemOperations fileOperations; |
| 71 | +private final Logger logger; |
| 72 | + |
| 73 | +MergeAgentFiles(Provider<Boolean> agent, |
| 74 | +Provider<String> graalvmHomeProvider, |
| 75 | +Provider<Directory> outputDir, |
| 76 | +Provider<Boolean> disableToolchainDetection, |
| 77 | +NativeImageOptions options, |
| 78 | +ExecOperations execOperations, |
| 79 | +FileSystemOperations fileOperations, |
| 80 | +Logger logger) { |
| 81 | +this.agent = agent; |
| 82 | +this.graalvmHomeProvider = graalvmHomeProvider; |
| 83 | +this.outputDir = outputDir; |
| 84 | +this.disableToolchainDetection = disableToolchainDetection; |
| 85 | +this.options = options; |
| 86 | +this.execOperations = execOperations; |
| 87 | +this.fileOperations = fileOperations; |
| 88 | +this.logger = logger; |
| 89 | +} |
| 90 | + |
| 91 | +@Override |
| 92 | +public void execute(Task task) { |
| 93 | +if (agent.get()) { |
| 94 | +File nativeImage = findNativeImageExecutable(options, disableToolchainDetection, graalvmHomeProvider, execOperations, GraalVMLogger.of(logger)); |
| 95 | +File workingDir = nativeImage.getParentFile(); |
| 96 | +File launcher = new File(workingDir, nativeImageConfigureFileName()); |
| 97 | +if (!launcher.exists()) { |
| 98 | +logger.info("Installing native-image-configure"); |
| 99 | +execOperations.exec(spec -> { |
| 100 | +spec.executable(nativeImage); |
| 101 | +spec.args("--macro:native-image-configure-launcher"); |
| 102 | +}); |
| 103 | +} |
| 104 | +if (launcher.exists()) { |
| 105 | +File[] files = outputDir.get().getAsFile().listFiles(); |
| 106 | +List<String> args = new ArrayList<>(files.length + 2); |
| 107 | +args.add("generate"); |
| 108 | +sessionDirectoriesFrom(files) |
| 109 | +.map(f -> "--input-dir=" + f.getAbsolutePath()) |
| 110 | +.forEach(args::add); |
| 111 | +if (args.size() > 1) { |
| 112 | +logger.info("Merging agent files"); |
| 113 | +args.add("--output-dir=" + outputDir.get().getAsFile().getAbsolutePath()); |
| 114 | +ExecResult exec = execOperations.exec(spec -> { |
| 115 | +spec.executable(launcher); |
| 116 | +spec.args(args); |
| 117 | +spec.setStandardOutput(System.out); |
| 118 | +spec.setErrorOutput(System.err); |
| 119 | +}); |
| 120 | +if (exec.getExitValue() == 0) { |
| 121 | +fileOperations.delete(spec -> sessionDirectoriesFrom(files).forEach(spec::delete)); |
| 122 | +} else { |
| 123 | +exec.rethrowFailure(); |
| 124 | +} |
| 125 | +} |
| 126 | +} else { |
| 127 | +logger.warn("Cannot merge agent files because native-image-configure is not installed. Please upgrade to a newer version of GraalVM."); |
| 128 | +} |
| 129 | +} |
| 130 | +} |
| 131 | + |
| 132 | +private String nativeImageConfigureFileName() { |
| 133 | +return "native-image-configure" + GRAALVM_EXE_EXTENSION; |
| 134 | +} |
| 135 | + |
| 136 | +private Stream<File> sessionDirectoriesFrom(File[] files) { |
| 137 | +return Arrays.stream(files) |
| 138 | +.filter(File::isDirectory) |
| 139 | +.filter(f -> f.getName().startsWith("session-")); |
| 140 | +} |
| 141 | +} |
0 commit comments