• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""
16Determines the environment required for Java actions to support UTF-8.
17"""
18
19visibility("private")
20
21Utf8EnvironmentInfo = provider(
22    doc = "The environment required for Java actions to support UTF-8.",
23    fields = {
24        "environment": "The environment to use for Java actions to support UTF-8.",
25    },
26)
27
28# The default UTF-8 locale on all recent Linux distributions. It is also available in Cygwin and
29# MSYS2, but doesn't matter for determining the JVM's platform encoding on Windows, which always
30# uses the active code page.
31_DEFAULT_UTF8_ENVIRONMENT = Utf8EnvironmentInfo(environment = {"LC_CTYPE": "C.UTF-8"})
32
33# macOS doesn't have the C.UTF-8 locale, but en_US.UTF-8 is available and works the same way.
34_MACOS_UTF8_ENVIRONMENT = Utf8EnvironmentInfo(environment = {"LC_CTYPE": "en_US.UTF-8"})
35
36def _utf8_environment_impl(ctx):
37    if ctx.target_platform_has_constraint(ctx.attr._macos_constraint[platform_common.ConstraintValueInfo]):
38        return _MACOS_UTF8_ENVIRONMENT
39    else:
40        return _DEFAULT_UTF8_ENVIRONMENT
41
42utf8_environment = rule(
43    _utf8_environment_impl,
44    attrs = {
45        "_macos_constraint": attr.label(default = "@platforms//os:macos"),
46    },
47    doc = "Returns a suitable environment for Java actions to support UTF-8.",
48)
49