Skip to content

Commit b5bd5d5

Browse files
committed
[GR-44872] Normalize env-var keys to upperCase on Windows.
PullRequest: graal/14056
2 parents fca4119 + ee9144d commit b5bd5d5

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

substratevm/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This changelog summarizes major changes to GraalVM Native Image.
2727
* (GR-40463) Red Hat added experimental support for JMX, which can be enabled with the `--enable-monitoring` option (e.g. `--enable-monitoring=jmxclient,jmxserver`).
2828
* (GR-42740) Together with Red Hat, we added experimental support for JFR event streaming.
2929
* (GR-44110) Native Image now targets `x86-64-v3` by default on AMD64 and supports a new `-march` option. Use `-march=compatibility` for best compatibility (previous default) or `-march=native` for best performance if the native executable is deployed on the same machine or on a machine with the same CPU features. To list all available machine types, use `-march=list`.
30-
* (GR-43971) Add native-image option `-E<env-var-key>[=<env-var-value>]` and support environment variable capturing in bundles. Previously almost all environment variables were available in the builder. To temporarily revert back to the old behaviour, env setting `NATIVE_IMAGE_SLOPPY_BUILDER_SANITATION=true` can be used. The old behaviour will be removed in a future release.
30+
* (GR-43971) Add native-image option `-E<env-var-key>[=<env-var-value>]` and support environment variable capturing in bundles. Previously almost all environment variables were available in the builder. To temporarily revert back to the old behaviour, env setting `NATIVE_IMAGE_DEPRECATED_BUILDER_SANITATION=true` can be used. The old behaviour will be removed in a future release.
3131
* (GR-43382) The build output now includes a section with recommendations that help you get the best out of Native Image.
3232

3333
## Version 22.3.0

substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@
4444
import java.util.Collections;
4545
import java.util.Comparator;
4646
import java.util.HashMap;
47+
import java.util.HashSet;
4748
import java.util.Iterator;
4849
import java.util.LinkedHashSet;
4950
import java.util.List;
5051
import java.util.ListIterator;
5152
import java.util.Map;
5253
import java.util.Optional;
5354
import java.util.Properties;
55+
import java.util.Set;
5456
import java.util.StringJoiner;
5557
import java.util.function.BiConsumer;
5658
import java.util.function.BiFunction;
@@ -1470,17 +1472,17 @@ protected int buildImage(List<String> javaArgs, LinkedHashSet<Path> cp, LinkedHa
14701472
ProcessBuilder pb = new ProcessBuilder();
14711473
pb.command(command);
14721474
Map<String, String> environment = pb.environment();
1473-
String sloppySanitationKey = "NATIVE_IMAGE_SLOPPY_BUILDER_SANITATION";
1474-
String sloppySanitationValue = System.getenv().getOrDefault(sloppySanitationKey, "false");
1475-
if (Boolean.parseBoolean(sloppySanitationValue)) {
1475+
String deprecatedSanitationKey = "NATIVE_IMAGE_DEPRECATED_BUILDER_SANITATION";
1476+
String deprecatedSanitationValue = System.getenv().getOrDefault(deprecatedSanitationKey, "false");
1477+
if (Boolean.parseBoolean(deprecatedSanitationValue)) {
14761478
if (useBundle()) {
14771479
bundleSupport = null;
1478-
throw showError("Bundle support is not compatible with environment variable %s=%s.".formatted(sloppySanitationKey, sloppySanitationValue));
1480+
throw showError("Bundle support is not compatible with environment variable %s=%s.".formatted(deprecatedSanitationKey, deprecatedSanitationValue));
14791481
}
14801482
if (!imageBuilderEnvironment.isEmpty()) {
1481-
throw showError("Option -E<env-var-key>[=<env-var-value>] is not compatible with environment variable %s=%s.".formatted(sloppySanitationKey, sloppySanitationValue));
1483+
throw showError("Option -E<env-var-key>[=<env-var-value>] is not compatible with environment variable %s=%s.".formatted(deprecatedSanitationKey, deprecatedSanitationValue));
14821484
}
1483-
sloppySanitizeJVMEnvironment(environment);
1485+
deprecatedSanitizeJVMEnvironment(environment);
14841486
} else {
14851487
sanitizeJVMEnvironment(environment, imageBuilderEnvironment);
14861488
}
@@ -1528,40 +1530,43 @@ boolean useBundle() {
15281530
}
15291531

15301532
@Deprecated
1531-
private static void sloppySanitizeJVMEnvironment(Map<String, String> environment) {
1533+
private static void deprecatedSanitizeJVMEnvironment(Map<String, String> environment) {
15321534
String[] jvmAffectingEnvironmentVariables = {"JAVA_COMPILER", "_JAVA_OPTIONS", "JAVA_TOOL_OPTIONS", "JDK_JAVA_OPTIONS", "CLASSPATH"};
15331535
for (String affectingEnvironmentVariable : jvmAffectingEnvironmentVariables) {
15341536
environment.remove(affectingEnvironmentVariable);
15351537
}
15361538
}
15371539

15381540
private static void sanitizeJVMEnvironment(Map<String, String> environment, Map<String, String> imageBuilderEnvironment) {
1539-
Map<String, String> restrictedEnvironment = new HashMap<>();
1540-
List<String> jvmRequiredEnvironmentVariables = new ArrayList<>(List.of("PATH", "PWD", "HOME", "LANG", "LC_ALL"));
1541-
jvmRequiredEnvironmentVariables.add("SRCHOME"); // FIXME
1541+
Set<String> requiredKeys = new HashSet<>(List.of("PATH", "PWD", "HOME", "LANG", "LC_ALL"));
1542+
requiredKeys.add("SRCHOME"); /* Remove once GR-44676 is fixed */
1543+
Function<String, String> keyMapper;
15421544
if (OS.WINDOWS.isCurrent()) {
1543-
jvmRequiredEnvironmentVariables.addAll(List.of("TEMP", "INCLUDE", "LIB"));
1545+
requiredKeys.addAll(List.of("TEMP", "INCLUDE", "LIB"));
1546+
keyMapper = String::toUpperCase;
1547+
} else {
1548+
keyMapper = Function.identity();
15441549
}
1545-
for (String requiredEnvironmentVariable : jvmRequiredEnvironmentVariables) {
1546-
String val = environment.get(requiredEnvironmentVariable);
1547-
if (val != null) {
1548-
restrictedEnvironment.put(requiredEnvironmentVariable, val);
1550+
Map<String, String> restrictedEnvironment = new HashMap<>();
1551+
environment.forEach((key, val) -> {
1552+
if (requiredKeys.contains(keyMapper.apply(key))) {
1553+
restrictedEnvironment.put(key, val);
15491554
}
1550-
}
1555+
});
15511556
for (Iterator<Map.Entry<String, String>> iterator = imageBuilderEnvironment.entrySet().iterator(); iterator.hasNext();) {
15521557
Map.Entry<String, String> entry = iterator.next();
1553-
String requiredKey = entry.getKey();
1554-
String requiredValue = entry.getValue();
1555-
if (requiredValue != null) {
1556-
restrictedEnvironment.put(requiredKey, requiredValue);
1558+
if (entry.getValue() != null) {
1559+
restrictedEnvironment.put(entry.getKey(), entry.getValue());
15571560
} else {
1558-
String existingValue = environment.get(requiredKey);
1559-
if (existingValue != null) {
1560-
restrictedEnvironment.put(requiredKey, existingValue);
1561-
/* Capture found existingValue for storing vars in bundle */
1562-
entry.setValue(existingValue);
1563-
} else {
1564-
NativeImage.showWarning("Environment variable '" + requiredKey + "' is undefined and therefore not available during image build-time.");
1561+
environment.forEach((key, val) -> {
1562+
if (keyMapper.apply(key).equals(keyMapper.apply(entry.getKey()))) {
1563+
restrictedEnvironment.put(entry.getKey(), val);
1564+
/* Capture found value for storing vars in bundle */
1565+
entry.setValue(val);
1566+
}
1567+
});
1568+
if (entry.getValue() == null) {
1569+
NativeImage.showWarning("Environment variable '" + entry.getKey() + "' is undefined and therefore not available during image build-time.");
15651570
/* Remove undefined environment for storing vars in bundle */
15661571
iterator.remove();
15671572
}

0 commit comments

Comments
 (0)