|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package com.oracle.svm.driver; |
| 26 | + |
| 27 | +import java.io.BufferedReader; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStreamReader; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Path; |
| 32 | +import java.nio.file.Paths; |
| 33 | +import java.util.Comparator; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.function.BiConsumer; |
| 38 | +import java.util.stream.Stream; |
| 39 | + |
| 40 | +/** |
| 41 | + * This utility helps set up a build environment for Windows users automatically. |
| 42 | + */ |
| 43 | +class WindowsBuildEnvironmentUtil { |
| 44 | + private static final String VSINSTALLDIR_ENV = "VSINSTALLDIR"; |
| 45 | + private static final Path[] KNOWN_VS_LOCATIONS = { |
| 46 | + Paths.get("C:", "Program Files", "Microsoft Visual Studio"), |
| 47 | + // prefer x64 location over x86 |
| 48 | + Paths.get("C:", "Program Files (x86)", "Microsoft Visual Studio")}; |
| 49 | + private static final String[] KNOWN_VS_EDITIONS = { |
| 50 | + // prefer Enterprise over Professional over Community |
| 51 | + "Enterprise", "Professional", "Community"}; |
| 52 | + private static final String VCVARSALL = "vcvarsall.bat"; |
| 53 | + private static final Path VCVARSALL_SUBPATH = Paths.get("VC", "Auxiliary", "Build", VCVARSALL); |
| 54 | + private static final String OUTPUT_SEPARATOR = "!NEXTCOMMAND!"; |
| 55 | + |
| 56 | + static void propagateEnv(Map<String, String> environment) { |
| 57 | + if (isCCompilerOnPath()) { |
| 58 | + return; // nothing to do, build environment initialized by user |
| 59 | + } |
| 60 | + Path vcVarsAllLocation = null; |
| 61 | + for (Path visualStudioLocation : KNOWN_VS_LOCATIONS) { |
| 62 | + if (!Files.isDirectory(visualStudioLocation)) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + List<Path> installationCandidates; |
| 66 | + try (Stream<Path> pathStream = Files.list(visualStudioLocation)) { |
| 67 | + installationCandidates = pathStream |
| 68 | + // only keep sub-directories matching 20\d\d |
| 69 | + .filter(p -> p.toFile().getName().matches("20\\d\\d")) |
| 70 | + // sort years |
| 71 | + .sorted(Comparator.comparing(p -> p.toFile().getName())) |
| 72 | + // reverse order to ensure latest year is first |
| 73 | + .sorted(Comparator.reverseOrder()) |
| 74 | + .toList(); |
| 75 | + } catch (IOException e) { |
| 76 | + throw fail("Failed to traverse known Visual Studio locations.", e); |
| 77 | + } |
| 78 | + for (Path installation : installationCandidates) { |
| 79 | + for (String edition : KNOWN_VS_EDITIONS) { |
| 80 | + Path possibleLocation = installation.resolve(edition).resolve(VCVARSALL_SUBPATH); |
| 81 | + if (Files.isRegularFile(possibleLocation) && Files.isReadable(possibleLocation)) { |
| 82 | + vcVarsAllLocation = possibleLocation; |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + if (vcVarsAllLocation == null) { |
| 89 | + throw fail(String.format("Failed to find '%s' in a Visual Studio installation.", VCVARSALL)); |
| 90 | + } |
| 91 | + Map<String, String> originalEnv = new HashMap<>(); |
| 92 | + int numSeenOutputSeparators = 0; |
| 93 | + try { |
| 94 | + // call `set`, then `vcvarsall.bat`, and then `set` again with separators in between |
| 95 | + String commandSequence = String.format("cmd.exe /c set && echo %s && \"%s\" x64 && echo %s && set", |
| 96 | + OUTPUT_SEPARATOR, vcVarsAllLocation, OUTPUT_SEPARATOR); |
| 97 | + Process p = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", commandSequence}); |
| 98 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()))) { |
| 99 | + String line = null; |
| 100 | + while ((line = reader.readLine()) != null) { |
| 101 | + if (line.startsWith(OUTPUT_SEPARATOR)) { |
| 102 | + numSeenOutputSeparators++; |
| 103 | + } else if (numSeenOutputSeparators == 0) { |
| 104 | + // collect environment variables from 1st `set` invocation |
| 105 | + processLineWithKeyValue(line, (key, value) -> originalEnv.put(key, value)); |
| 106 | + } else if (numSeenOutputSeparators == 2) { |
| 107 | + // iterate through updated environment variables from 2nd `set` invocation |
| 108 | + processLineWithKeyValue(line, (key, value) -> { |
| 109 | + boolean isNewOrModifiedEnvVar = !originalEnv.getOrDefault(key, "").equals(value); |
| 110 | + if (isNewOrModifiedEnvVar) { |
| 111 | + environment.put(key, value); |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + p.waitFor(); |
| 118 | + } catch (IOException | InterruptedException e) { |
| 119 | + throw fail("Failed to detect variables of Windows build environment.", e); |
| 120 | + } |
| 121 | + if (!environment.containsKey(VSINSTALLDIR_ENV)) { |
| 122 | + throw fail("Failed to automatically set up Windows build environment."); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private static boolean isCCompilerOnPath() { |
| 127 | + try { |
| 128 | + return Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "where", "cl.exe"}).waitFor() == 0; |
| 129 | + } catch (IOException | InterruptedException e) { |
| 130 | + throw NativeImage.showError("Failed to check for 'cl.exe'.", e); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private static void processLineWithKeyValue(String line, BiConsumer<String, String> consumeKeyValue) { |
| 135 | + String[] parts = line.split("="); |
| 136 | + if (parts.length == 2) { |
| 137 | + consumeKeyValue.accept(parts[0], parts[1]); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static Error fail(String reason) { |
| 142 | + return fail(reason, null); |
| 143 | + } |
| 144 | + |
| 145 | + private static Error fail(String reason, Throwable e) { |
| 146 | + throw NativeImage.showError(reason + System.lineSeparator() + |
| 147 | + "Please make sure that Visual Studio 2017 version 15.5 or later (C/C++ Optimizing Compiler Version 19.12 or later) is installed on your system. " + |
| 148 | + "You can download it at https://visualstudio.microsoft.com/downloads/. " + |
| 149 | + "If this error persists, please try and run GraalVM Native Image in an x64 Native Tools Command Prompt or file a ticket.", |
| 150 | + e); |
| 151 | + } |
| 152 | +} |
0 commit comments