diff options
author | Loo Rong Jie <loorongjie@gmail.com> | 2018-07-17 03:57:01 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-07-17 03:58:16 -0700 |
commit | 8231cd72a4224823653b76a167e0107f6da9719d (patch) | |
tree | be3f8d693ab0f7f1b97f66830a0094b23051702f /src/tools/singlejar | |
parent | 715a7728e7e4a427c8d76545933f86bce5ddfbd9 (diff) |
[singlejar] Add port library for various MSVC hacks
A small `port` library with various MSVC hacks. This is needed for future PRs.
For non-Windows platforms, `port.cc` will produce an empty object file.
#2241 /cc @laszlocsomor
Closes #5501.
PiperOrigin-RevId: 204889516
Diffstat (limited to 'src/tools/singlejar')
-rw-r--r-- | src/tools/singlejar/BUILD | 9 | ||||
-rw-r--r-- | src/tools/singlejar/port.cc | 40 | ||||
-rw-r--r-- | src/tools/singlejar/port.h | 55 |
3 files changed, 104 insertions, 0 deletions
diff --git a/src/tools/singlejar/BUILD b/src/tools/singlejar/BUILD index a855a22064..5c1da1ff21 100644 --- a/src/tools/singlejar/BUILD +++ b/src/tools/singlejar/BUILD @@ -31,6 +31,8 @@ filegroup( "options.h", "output_jar.cc", "output_jar.h", + "port.cc", + "port.h", "singlejar_main.cc", "token_stream.h", "transient_bytes.h", @@ -345,6 +347,13 @@ cc_library( ) cc_library( + name = "port", + srcs = ["port.cc"], + hdrs = ["port.h"], + visibility = ["//visibility:private"], +) + +cc_library( name = "mapped_file", srcs = select({ "//src:windows": ["mapped_file_windows.inc"], diff --git a/src/tools/singlejar/port.cc b/src/tools/singlejar/port.cc new file mode 100644 index 0000000000..298eea173e --- /dev/null +++ b/src/tools/singlejar/port.cc @@ -0,0 +1,40 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifdef _WIN32 + +#include "src/tools/singlejar/port.h" + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include <windows.h> + +ssize_t pread(int fd, void *buf, size_t count, off64_t offset) { + DWORD ret = -1; + HANDLE hFile = reinterpret_cast<HANDLE>(_get_osfhandle(fd)); + if (hFile) { + OVERLAPPED overlap; + memset(&overlap, 0, sizeof(OVERLAPPED)); + overlap.Offset = offset; + if (!::ReadFile(hFile, buf, count, &ret, &overlap)) { + // For this simple implementation, we don't update errno. + // Just return -1 as error. + return -1; + } + } + return ret; +} + +#endif // _WIN32 diff --git a/src/tools/singlejar/port.h b/src/tools/singlejar/port.h new file mode 100644 index 0000000000..8d244602d0 --- /dev/null +++ b/src/tools/singlejar/port.h @@ -0,0 +1,55 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef BAZEL_SRC_TOOLS_SINGLEJAR_PORT_H_ +#define BAZEL_SRC_TOOLS_SINGLEJAR_PORT_H_ 1 + +#ifdef _WIN32 +// This macro is required to tell MSVC headers to also define POSIX names +// without "_" prefix (such as "open" for "_open"). +#define _CRT_DECLARE_NONSTDC_NAMES 1 +#include <fcntl.h> +#include <io.h> +#include <stddef.h> +#include <stdio.h> +#include <sys/types.h> +#include <time.h> + +#ifdef _WIN64 +// MSVC by default defines stat and related functions to a version with 32-bit +// st_size even for Win64. We want 64-bit st_size instead so that we can handle +// large file. +#undef stat +#undef fstat +#define stat _stat64 +#define fstat _fstat64 +#endif // _WIN64 + +typedef ptrdiff ssize_t; + +// Various MSVC polyfills for POSIX functions. + +inline tm* localtime_r(const time_t* tin, tm* tout) { + if (!localtime_s(tout, tin)) return tout; + + return nullptr; +} + +// Make sure that the file HANDLE associated with |fd| is created by CreateFile +// with FILE_FLAG_OVERLAPPED flag for this function to work. +ssize_t pread(int fd, void* buf, size_t count, off64_t offset); + +#endif // _WIN32 + +#endif // BAZEL_SRC_TOOLS_SINGLEJAR_PORT_H_ |