[singlejar] Various portability fixes for MSVC
- MSVC does not have `errx` functions, so use `diag_errx` etc. instead.
- Fix format when trying to print `size_t`, use `%zu` so that the function will handle 32/64-bit `size_t` according to target system automatically.
- Adding/guarding a few includes for MSVC.
- MSVC does not have `ssize_t`, so replace it with `ptrdiff_t`
#2241
/cc @laszlocsomor
Closes #5499.
PiperOrigin-RevId: 204074420
diff --git a/src/tools/singlejar/combiners.cc b/src/tools/singlejar/combiners.cc
index 1da86e6..26fcc58 100644
--- a/src/tools/singlejar/combiners.cc
+++ b/src/tools/singlejar/combiners.cc
@@ -13,6 +13,9 @@
// limitations under the License.
#include "src/tools/singlejar/combiners.h"
+
+#include <cctype>
+
#include "src/tools/singlejar/diag.h"
Combiner::~Combiner() {}
@@ -33,7 +36,7 @@
}
buffer_->DecompressEntryContents(cdh, lh, inflater_.get());
} else {
- errx(2, "%s is neither stored nor deflated", filename_.c_str());
+ diag_errx(2, "%s is neither stored nor deflated", filename_.c_str());
}
return true;
}
@@ -138,7 +141,7 @@
}
bytes_.DecompressEntryContents(cdh, lh, inflater_.get());
} else {
- errx(2, "%s is neither stored nor deflated", filename_.c_str());
+ diag_errx(2, "%s is neither stored nor deflated", filename_.c_str());
}
uint32_t checksum;
char *buf = reinterpret_cast<char *>(malloc(bytes_.data_size()));
diff --git a/src/tools/singlejar/desugar_checking.cc b/src/tools/singlejar/desugar_checking.cc
index 13f45f9..1fa6e50 100644
--- a/src/tools/singlejar/desugar_checking.cc
+++ b/src/tools/singlejar/desugar_checking.cc
@@ -27,7 +27,7 @@
}
buffer_->DecompressEntryContents(cdh, lh, inflater_.get());
} else {
- errx(2, "META-INF/desugar_deps is neither stored nor deflated");
+ diag_errx(2, "META-INF/desugar_deps is neither stored nor deflated");
}
// TODO(kmb): Wrap buffer_ as ZeroCopyInputStream to avoid copying out.
@@ -41,10 +41,10 @@
bazel::tools::desugar::DesugarDepsInfo deps_info;
google::protobuf::io::CodedInputStream content(buf, data_size);
if (!deps_info.ParseFromCodedStream(&content)) {
- errx(2, "META-INF/desugar_deps: unable to parse");
+ diag_errx(2, "META-INF/desugar_deps: unable to parse");
}
if (!content.ConsumedEntireMessage()) {
- errx(2, "META-INF/desugar_deps: unexpected trailing content");
+ diag_errx(2, "META-INF/desugar_deps: unexpected trailing content");
}
free(buf);
@@ -91,10 +91,10 @@
void *Java8DesugarDepsChecker::OutputEntry(bool compress) {
if (verbose_) {
- fprintf(stderr, "Needed deps: %lu\n", needed_deps_.size());
- fprintf(stderr, "Interfaces to check: %lu\n", missing_interfaces_.size());
- fprintf(stderr, "Sub-interfaces: %lu\n", extended_interfaces_.size());
- fprintf(stderr, "Interfaces w/ default methods: %lu\n",
+ fprintf(stderr, "Needed deps: %zu\n", needed_deps_.size());
+ fprintf(stderr, "Interfaces to check: %zu\n", missing_interfaces_.size());
+ fprintf(stderr, "Sub-interfaces: %zu\n", extended_interfaces_.size());
+ fprintf(stderr, "Interfaces w/ default methods: %zu\n",
has_default_methods_.size());
}
for (auto needed : needed_deps_) {
@@ -103,9 +103,10 @@
}
if (!known_member_(needed.first)) {
if (fail_on_error_) {
- errx(2, "%s referenced by %s but not found. Is the former defined in "
- "a neverlink library?",
- needed.first.c_str(), needed.second.c_str());
+ diag_errx(2,
+ "%s referenced by %s but not found. Is the former defined"
+ " in a neverlink library?",
+ needed.first.c_str(), needed.second.c_str());
} else {
error_ = true;
}
@@ -118,9 +119,11 @@
}
if (HasDefaultMethods(missing.first)) {
if (fail_on_error_) {
- errx(2, "%s needed on the classpath for desugaring %s. Please add the "
- "missing dependency to the target containing the latter.",
- missing.first.c_str(), missing.second.c_str());
+ diag_errx(
+ 2,
+ "%s needed on the classpath for desugaring %s. Please add"
+ " the missing dependency to the target containing the latter.",
+ missing.first.c_str(), missing.second.c_str());
} else {
error_ = true;
}
diff --git a/src/tools/singlejar/input_jar.cc b/src/tools/singlejar/input_jar.cc
index 08007d5..ac6433b 100644
--- a/src/tools/singlejar/input_jar.cc
+++ b/src/tools/singlejar/input_jar.cc
@@ -27,8 +27,8 @@
}
if (mapped_file_.size() < sizeof(ECD)) {
diag_warnx(
- "%s:%d: %s is only 0x%lx"
- " bytes long, should be at least 0x%lx bytes long",
+ "%s:%d: %s is only 0x%zx"
+ " bytes long, should be at least 0x%zx bytes long",
__FILE__, __LINE__, path.c_str(), mapped_file_.size(), sizeof(ECD));
mapped_file_.Close();
return false;
diff --git a/src/tools/singlejar/output_jar.cc b/src/tools/singlejar/output_jar.cc
index aec2cc1..6728d59 100644
--- a/src/tools/singlejar/output_jar.cc
+++ b/src/tools/singlejar/output_jar.cc
@@ -17,14 +17,12 @@
*/
#include "src/tools/singlejar/output_jar.h"
-#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
-#include <unistd.h>
#include "src/tools/singlejar/combiners.h"
#include "src/tools/singlejar/diag.h"
@@ -100,8 +98,8 @@
fprintf(stderr, "java_launcher_file=%s\n",
options_->java_launcher.c_str());
}
- fprintf(stderr, "%ld source files\n", options_->input_jars.size());
- fprintf(stderr, "%ld manifest lines\n", options_->manifest_lines.size());
+ fprintf(stderr, "%zu source files\n", options_->input_jars.size());
+ fprintf(stderr, "%zu manifest lines\n", options_->manifest_lines.size());
}
if (!Open()) {
@@ -124,7 +122,7 @@
diag_err(1, "%s:%d: Cannot copy %s to %s", __FILE__, __LINE__,
launcher_path, options_->output_jar.c_str());
} else if (byte_count != statbuf.st_size) {
- diag_err(1, "%s:%d: Copied only %ld bytes out of %" PRIu64 " from %s",
+ diag_err(1, "%s:%d: Copied only %zu bytes out of %" PRIu64 " from %s",
__FILE__, __LINE__, byte_count, statbuf.st_size, launcher_path);
}
close(in_fd);
@@ -489,7 +487,7 @@
// Do the actual copy.
if (!WriteBytes(input_jar.mapped_start() + copy_from, num_bytes)) {
- diag_err(1, "%s:%d: Cannot write %ld bytes of %.*s from %s", __FILE__,
+ diag_err(1, "%s:%d: Cannot write %zu bytes of %.*s from %s", __FILE__,
__LINE__, num_bytes, file_name_length, file_name,
input_jar_path.c_str());
}
@@ -520,7 +518,7 @@
}
LH *entry = reinterpret_cast<LH *>(buffer);
if (options_->verbose) {
- fprintf(stderr, "%-.*s combiner has %lu bytes, %s to %lu\n",
+ fprintf(stderr, "%-.*s combiner has %zu bytes, %s to %zu\n",
entry->file_name_length(), entry->file_name(),
entry->uncompressed_file_size(),
entry->compression_method() == Z_NO_COMPRESSION ? "copied"
@@ -750,7 +748,7 @@
cen_capacity_ += 1000000;
cen_ = reinterpret_cast<uint8_t *>(realloc(cen_, cen_capacity_));
if (!cen_) {
- diag_errx(1, "%s:%d: Cannot allocate %ld bytes for the directory",
+ diag_errx(1, "%s:%d: Cannot allocate %zu bytes for the directory",
__FILE__, __LINE__, cen_capacity_);
}
}
diff --git a/src/tools/singlejar/transient_bytes.h b/src/tools/singlejar/transient_bytes.h
index 4ecba54..7c70870 100644
--- a/src/tools/singlejar/transient_bytes.h
+++ b/src/tools/singlejar/transient_bytes.h
@@ -276,9 +276,9 @@
// Returns the old write position.
uint8_t *advance(size_t amount) {
if (amount > free_size()) {
- diag_errx(2, "%s: %d: Cannot advance %ld bytes, only %" PRIu64
- " is available",
- __FILE__, __LINE__, amount, free_size());
+ diag_errx(
+ 2, "%s: %d: Cannot advance %zu bytes, only %" PRIu64 " is available",
+ __FILE__, __LINE__, amount, free_size());
}
uint8_t *pos = append_position();
data_size_ += amount;