Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ab371fe

Browse files
hvadehracopybara-github
authored andcommittedJun 18, 2025·
Fix singlejar's handling of unicode filenames on windows
Unicode command line arguments on windows require using the `wmain` entry point with the `wchar_t**` argument type. This is then converted into multi-byte `char**` arguments. Work towards bazelbuild/rules_java#295 PiperOrigin-RevId: 772959857 Change-Id: I8aa1cf54aa0291c40c7e5e62d14decc56f3780bd
1 parent 5d2427d commit ab371fe

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed
 

‎src/main/cpp/util/strings.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,18 @@ std::wstring CstringToWstring(const std::string &input) {
381381
return result;
382382
}
383383

384+
char** WArgsToCArgs(int argc, wchar_t **wargv) {
385+
char **argv = new char*[argc];
386+
for (int i = 0; i < argc; i++) {
387+
std::string arg = WstringToCstring(std::wstring(wargv[i]));
388+
argv[i] = new char[arg.length()+1];
389+
for (int j = 0; j < arg.length(); j++) {
390+
argv[i][j] = arg[j];
391+
}
392+
argv[i][arg.length()] = 0;
393+
}
394+
return argv;
395+
}
384396
#endif // defined(_WIN32) || defined(__CYGWIN__)
385397

386398
} // namespace blaze_util

‎src/main/cpp/util/strings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ std::string WstringToCstring(const std::wstring &input);
135135

136136
// Deprecated. Use AcpToWcs or Utf8ToWcs.
137137
std::wstring CstringToWstring(const std::string &input);
138+
139+
char** WArgsToCArgs(int argc, wchar_t **wargv);
138140
#endif // defined(_WIN32) || defined(__CYGWIN__)
139141

140142
} // namespace blaze_util

‎src/tools/singlejar/BUILD

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
12
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
23
load("@rules_cc//cc:cc_library.bzl", "cc_library")
34
load("@rules_cc//cc:cc_test.bzl", "cc_test")
@@ -588,3 +589,24 @@ genrule(
588589
toolchains = ["@rules_java//toolchains:current_java_runtime"],
589590
tools = ["@rules_java//toolchains:current_java_runtime"],
590591
)
592+
593+
build_test(
594+
name = "singlejar_unicode_filenames_test",
595+
targets = [
596+
":gen_singlejar_unicode_filenames",
597+
],
598+
)
599+
600+
genrule(
601+
name = "gen_singlejar_unicode_filenames",
602+
outs = [
603+
"unicode_filenames.jar",
604+
],
605+
cmd = """
606+
touch रैन्डम.txt らんどみ.txt 艾诶艾迪勒开.txt
607+
$(location :singlejar) --resources रैन्डम.txt らんどみ.txt 艾诶艾迪勒开.txt --output $@
608+
""",
609+
tools = [
610+
":singlejar",
611+
],
612+
)

‎src/tools/singlejar/singlejar_local_main.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919
#include "src/tools/singlejar/options.h"
2020
#include "src/tools/singlejar/output_jar.h"
2121

22+
#ifdef _WIN32
23+
#include "src/main/cpp/util/strings.h"
24+
int wmain(int argc, wchar_t *wargv[]) {
25+
char **argv = blaze_util::WArgsToCArgs(argc, wargv);
26+
#else
2227
int main(int argc, char *argv[]) {
28+
#endif
2329
Options options;
2430
options.ParseCommandLine(argc - 1, argv + 1);
2531
OutputJar output_jar;

‎src/tools/singlejar/singlejar_main.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
#include "src/tools/singlejar/options.h"
1919
#include "src/tools/singlejar/output_jar.h"
2020

21+
#ifdef _WIN32
22+
#include "src/main/cpp/util/strings.h"
23+
int wmain(int argc, wchar_t *wargv[]) {
24+
char **argv = blaze_util::WArgsToCArgs(argc, wargv);
25+
#else
2126
int main(int argc, char *argv[]) {
27+
#endif
2228
Options options;
2329
options.ParseCommandLine(argc - 1, argv + 1);
2430
OutputJar output_jar;

0 commit comments

Comments
 (0)
Please sign in to comment.