diff options
author | Loo Rong Jie <loorongjie@gmail.com> | 2018-07-10 23:34:50 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-07-10 23:36:12 -0700 |
commit | 30f38d0e7deed375c1aeb2693ee6b2c571c99c55 (patch) | |
tree | ebec0fd72cfeac5b662ce18efb16629e6d22442a /src/tools | |
parent | f600b693642b3ee2475c6222a0eebb25371c6ca6 (diff) |
[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
Diffstat (limited to 'src/tools')
-rw-r--r-- | src/tools/singlejar/combiners.cc | 7 | ||||
-rw-r--r-- | src/tools/singlejar/desugar_checking.cc | 29 | ||||
-rw-r--r-- | src/tools/singlejar/input_jar.cc | 4 | ||||
-rw-r--r-- | src/tools/singlejar/output_jar.cc | 14 | ||||
-rw-r--r-- | src/tools/singlejar/transient_bytes.h | 6 |
5 files changed, 32 insertions, 28 deletions
diff --git a/src/tools/singlejar/combiners.cc b/src/tools/singlejar/combiners.cc index 1da86e626a..26fcc58004 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 @@ bool Concatenator::Merge(const CDH *cdh, const LH *lh) { } 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 @@ bool XmlCombiner::Merge(const CDH *cdh, const LH *lh) { } 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 13f45f9583..1fa6e50ac0 100644 --- a/src/tools/singlejar/desugar_checking.cc +++ b/src/tools/singlejar/desugar_checking.cc @@ -27,7 +27,7 @@ bool Java8DesugarDepsChecker::Merge(const CDH *cdh, const LH *lh) { } 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 @@ bool Java8DesugarDepsChecker::Merge(const CDH *cdh, const LH *lh) { 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 @@ bool Java8DesugarDepsChecker::Merge(const CDH *cdh, const LH *lh) { 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 @@ void *Java8DesugarDepsChecker::OutputEntry(bool compress) { } 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 @@ void *Java8DesugarDepsChecker::OutputEntry(bool compress) { } 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 08007d5fd4..ac6433bffd 100644 --- a/src/tools/singlejar/input_jar.cc +++ b/src/tools/singlejar/input_jar.cc @@ -27,8 +27,8 @@ bool InputJar::Open(const std::string &path) { } 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 aec2cc15a3..6728d59de8 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 @@ int OutputJar::Doit(Options *options) { 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 @@ int OutputJar::Doit(Options *options) { 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 @@ bool OutputJar::AddJar(int jar_path_index) { // 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 @@ void OutputJar::WriteEntry(void *buffer) { } 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 @@ uint8_t *OutputJar::ReserveCdr(size_t chunk_size) { 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 4ecba54af5..7c708707d2 100644 --- a/src/tools/singlejar/transient_bytes.h +++ b/src/tools/singlejar/transient_bytes.h @@ -276,9 +276,9 @@ class TransientBytes { // 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; |