Skip to content

Commit

Permalink
Use different UTF-8 locales for Java compilation depending on exec OS
Browse files Browse the repository at this point in the history
Linux doesn't have `en_US.UTF-8` (anymore), macOS doesn't have `C.UTF-8`. Since the JDK always uses UTF-8 on macOS with Java 18+, this only matters when JDK 17 and lower are used as compilation JDKs.

Fixes bazelbuild#24459

Closes bazelbuild#24461.

PiperOrigin-RevId: 699151996
Change-Id: Ibef2a9d5536c08d3984930b2fe0ea052e8704d77
  • Loading branch information
fmeum authored and copybara-github committed Nov 22, 2024
1 parent 6f29096 commit 26386ef
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ java_library(
name = "bazel_java_semantics",
srcs = ["BazelJavaSemantics.java"],
deps = [
"//src/main/java/com/google/devtools/build/lib/analysis:constraints/constraint_constants",
"//src/main/java/com/google/devtools/build/lib/analysis/platform",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/rules/java:java-compilation",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec:serialization-constant",
"//src/main/java/com/google/devtools/build/lib/util:os",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//third_party:guava",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
package com.google.devtools.build.lib.bazel.rules.java;

import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.constraints.ConstraintConstants;
import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.rules.java.JavaSemantics;
import com.google.devtools.build.lib.rules.java.JavaUtil;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.List;

Expand All @@ -27,11 +30,16 @@ public class BazelJavaSemantics implements JavaSemantics {

/**
* {@code C.UTF-8} is now the universally accepted standard UTF-8 locale, to the point where some
* minimal distributions no longer ship with {@code en_US.UTF-8}.
* minimal Linux distributions no longer ship with {@code en_US.UTF-8}. macOS doesn't have it
* though.
*/
private static final ImmutableMap<String, String> UTF8_ENVIRONMENT =
private static final ImmutableMap<String, String> DEFAULT_UTF8_ENVIRONMENT =
ImmutableMap.of("LC_CTYPE", "C.UTF-8");

/** macOS doesn't have {@code C.UTF-8}, so we use {@code en_US.UTF-8} instead. */
private static final ImmutableMap<String, String> MACOS_UTF8_ENVIRONMENT =
ImmutableMap.of("LC_CTYPE", "en_US.UTF-8");

@SerializationConstant public static final BazelJavaSemantics INSTANCE = new BazelJavaSemantics();

private BazelJavaSemantics() {}
Expand Down Expand Up @@ -65,7 +73,9 @@ public PathFragment getDefaultJavaResourcePath(PathFragment path) {
}

@Override
public ImmutableMap<String, String> utf8Environment() {
return UTF8_ENVIRONMENT;
public ImmutableMap<String, String> utf8Environment(PlatformInfo executionPlatform) {
return ConstraintConstants.getOsFromConstraints(executionPlatform.constraints()) == OS.DARWIN
? MACOS_UTF8_ENVIRONMENT
: DEFAULT_UTF8_ENVIRONMENT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ && getJavaConfiguration().experimentalEnableJspecify()
builder.setSourceFiles(sourceFiles);
builder.setSourceJars(sourceJars);
builder.setJavacOpts(javacopts);
builder.setUtf8Environment(semantics.utf8Environment());
builder.setUtf8Environment(semantics.utf8Environment(ruleContext.getExecutionPlatform()));
builder.setJavacExecutionInfo(executionInfoInterner.intern(getExecutionInfo()));
builder.setCompressJar(true);
builder.setExtraData(JavaCommon.computePerPackageData(ruleContext, javaToolchain));
Expand Down Expand Up @@ -462,7 +462,7 @@ private JavaHeaderCompileAction.Builder getJavaHeaderCompileActionBuilder()
builder.setAdditionalInputs(additionalInputsForDatabinding);
builder.setToolsJars(javaToolchain.getTools());
builder.setExecGroup(execGroup);
builder.setUtf8Environment(semantics.utf8Environment());
builder.setUtf8Environment(semantics.utf8Environment(ruleContext.getExecutionPlatform()));
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.OutputGroupInfo;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.Attribute.LabelListLateBoundDefault;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant;
Expand Down Expand Up @@ -78,6 +79,6 @@ static Label javaToolchainAttribute(RuleDefinitionEnvironment environment) {
*/
PathFragment getDefaultJavaResourcePath(PathFragment path);

/** Environment variable that sets the UTF-8 charset. */
ImmutableMap<String, String> utf8Environment();
/** Environment variable that sets the UTF-8 charset for the given execution platform. */
ImmutableMap<String, String> utf8Environment(PlatformInfo executionPlatform);
}

0 comments on commit 26386ef

Please sign in to comment.