我有一个外部源存储库,显然存储了一些使用ISO-8859-1字符编码的源文件。当通过Bazel调用时,我无法让javac从默认的UTF-8更改为ISO-8859-1。
我通过Bazel获取外部存储库,可以确定获取文件的字符集:
> cd bazel-PROJECT/external/third-party/src
> file -i LibraryCode.java
LibraryCode.java: text/x-c; charset=iso-8859-1
通过Bazel的java_library构建外部源,或者尝试直接从命令行通过javac编译外部存储库源文件失败(预期):
error: unmappable character for encoding UTF8
尝试使用javac的-encoding参数解决了从命令行使用Bazel提取的外部存储库文件时的编译问题:
> javac -encoding iso-8859-1 LibraryCode.java
但是,我无法通过Bazel将-encoding选项成功传递给javac。
到目前为止我已经尝试过了:
这些尝试都没有解决字符集不匹配和编译器错误。
1)repository_rule build_file:thirdparty.BUILD
java_library(
name = "thirdparty",
srcs = glob(["src/**/*.java"]),
javacopts = ["-encoding iso-8859-1"],
visibility = ["//visibility:public"]
)
2)Bazel命令行:
> bazel build --javacopt="-encoding iso-8859-1" target
3)使用编码设置定义Java工具链目标:
java_toolchain(
name = "toolchain",
bootclasspath = ["@bazel_tools//tools/jdk:bootclasspath"],
encoding = "iso-8859-1",
extclasspath = ["@bazel_tools//tools/jdk:extdir"],
forcibly_disable_header_compilation = 0,
genclass = ["@bazel_tools//tools/jdk:GenClass_deploy.jar"],
header_compiler = ["@bazel_tools//tools/jdk:turbine_deploy.jar"],
ijar = ["@bazel_tools//tools/jdk:ijar"],
javabuilder = ["@bazel_tools//tools/jdk:JavaBuilder_deploy.jar"],
javac = ["@bazel_tools//third_party/java/jdk/langtools:javac_jar"],
javac_supports_workers = 1,
jvm_opts = [
"-XX:+TieredCompilation",
"-XX:TieredStopAtLevel=1",
],
misc = [
"-XDskipDuplicateBridges=true",
],
singlejar = ["@bazel_tools//tools/jdk:SingleJar_deploy.jar"],
source_version = "8",
target_version = "8",
visibility = ["//visibility:public"]
)
所有都以错误结束:编码UTF8的不可映射的字符。
我在通过Bazel设置javac编码时犯的错误是什么?
我可以尝试通过iconv转换外部存储库源文件来解决这个问题,但我更愿意通过javac的编码设置来解决它。
未被识别的java_toolchain编码似乎是一个错误。我在我的本地Bazel副本上对此进行了初步修复 - java_toolchain改变字符集的方法(上面的选项#3)似乎有效。
中跟踪此问题和建议的修复程序答案 0 :(得分:1)
不幸的是,从基于命令行/目标开始,没有好办法。你必须写一个java_toolchain并指向它。导出导致的one from bazel:
java_toolchain(
name = "toolchain",
bootclasspath = ["@bazel_tools//tools/jdk:bootclasspath"],
encoding = "iso-8859-1",
extclasspath = ["@bazel_tools//tools/jdk:extclasspath"],
forcibly_disable_header_compilation = 0,
genclass = ["@bazel_tools//tools/jdk:genclass"],
header_compiler = ["@bazel_tools//tools/jdk:turbine"],
ijar = ["@bazel_tools//tools/jdk:ijar"],
javabuilder = ["@bazel_tools//tools/jdk:javabuilder"],
javac = ["@bazel_tools//third_party/java/jdk/langtools:javac_jar"],
javac_supports_workers = 1,
jvm_opts = [
"-XX:+TieredCompilation",
"-XX:TieredStopAtLevel=1",
],
misc = [
"-XDskipDuplicateBridges=true",
],
singlejar = ["@bazel_tools//tools/jdk:SingleJar_deploy.jar"],
source_version = "8",
target_version = "8",
)
(出于性能原因,您可能希望将单一目标目标更改为C ++二进制文件:@bazel_tools//tools/jdk:singlejar
IIRC)
然后您可以使用--java_toolchain=//my:toolchain
指向该工具链(请参阅java_toolchain flag)