Skip to content

Commit

Permalink
build: make it compile with @mode/win (facebook#7406)
Browse files Browse the repository at this point in the history
Summary:
While rocksdb can compile on both macOS and Linux with Buck, it couldn't be
compiled on Windows. The only way to compile it on Windows was with the CMake
build.

To keep the multi-platform complexity low, I've simply included all the Windows
bits in the TARGETS file, and added large #if blocks when not on Windows, the
same was done on the posix specific files.

Pull Request resolved: facebook#7406

Test Plan:
On my devserver:
  buck test //rocksdb/...
On Windows:
  buck build mode/win //rocksdb/src:rocksdb_lib

Reviewed By: pdillinger

Differential Revision: D23874358

Pulled By: xavierd

fbshipit-source-id: 8768b5d16d7e8f44b5ca1e2483881ca4b24bffbe
  • Loading branch information
xavierd authored and facebook-github-bot committed Sep 23, 2020
1 parent ac1734d commit 249f2b5
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 48 deletions.
30 changes: 24 additions & 6 deletions TARGETS
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ ROCKSDB_EXTERNAL_DEPS = [
("gflags", None, "gflags"),
("lz4", None, "lz4"),
("zstd", None),
("tbb", None),
]

ROCKSDB_OS_DEPS = [
(
"linux",
["third-party//numa:numa", "third-party//liburing:uring"],
["third-party//numa:numa", "third-party//liburing:uring", "third-party//tbb:tbb"],
),
(
"macos",
["third-party//tbb:tbb"],
),
]

Expand All @@ -49,17 +52,27 @@ ROCKSDB_OS_PREPROCESSOR_FLAGS = [
"-DHAVE_SSE42",
"-DLIBURING",
"-DNUMA",
"-DROCKSDB_PLATFORM_POSIX",
"-DROCKSDB_LIB_IO_POSIX",
"-DTBB",
],
),
(
"macos",
["-DOS_MACOSX"],
[
"-DOS_MACOSX",
"-DROCKSDB_PLATFORM_POSIX",
"-DROCKSDB_LIB_IO_POSIX",
"-DTBB",
],
),
(
"windows",
[ "-DOS_WIN", "-DWIN32", "-D_MBCS", "-DWIN64", "-DNOMINMAX" ]
),
]

ROCKSDB_PREPROCESSOR_FLAGS = [
"-DROCKSDB_PLATFORM_POSIX",
"-DROCKSDB_LIB_IO_POSIX",
"-DROCKSDB_SUPPORT_THREAD_LOCAL",

# Flags to enable libs we include
Expand All @@ -70,7 +83,6 @@ ROCKSDB_PREPROCESSOR_FLAGS = [
"-DZSTD",
"-DZSTD_STATIC_LINKING_ONLY",
"-DGFLAGS=gflags",
"-DTBB",

# Added missing flags from output of build_detect_platform
"-DROCKSDB_BACKTRACE",
Expand Down Expand Up @@ -246,6 +258,12 @@ cpp_library(
"options/options_parser.cc",
"port/port_posix.cc",
"port/stack_trace.cc",
"port/win/env_default.cc",
"port/win/env_win.cc",
"port/win/io_win.cc",
"port/win/port_win.cc",
"port/win/win_logger.cc",
"port/win/win_thread.cc",
"table/adaptive/adaptive_table_factory.cc",
"table/block_based/binary_search_index_reader.cc",
"table/block_based/block.cc",
Expand Down
20 changes: 10 additions & 10 deletions buckifier/targets_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def pretty_list(lst, indent=8):
class TARGETSBuilder(object):
def __init__(self, path):
self.path = path
self.targets_file = open(path, 'w')
self.targets_file = open(path, 'wb')
header = targets_cfg.rocksdb_target_header_template
self.targets_file.write(header)
self.targets_file.write(header.encode("utf-8"))
self.total_lib = 0
self.total_bin = 0
self.total_test = 0
Expand All @@ -52,7 +52,7 @@ def add_library(self, name, srcs, deps=None, headers=None,
headers_attr_prefix=headers_attr_prefix,
headers=headers,
deps=pretty_list(deps),
extra_external_deps=extra_external_deps))
extra_external_deps=extra_external_deps).encode("utf-8"))
self.total_lib = self.total_lib + 1

def add_rocksdb_library(self, name, srcs, headers=None):
Expand All @@ -66,18 +66,18 @@ def add_rocksdb_library(self, name, srcs, headers=None):
name=name,
srcs=pretty_list(srcs),
headers_attr_prefix=headers_attr_prefix,
headers=headers))
headers=headers).encode("utf-8"))
self.total_lib = self.total_lib + 1

def add_binary(self, name, srcs, deps=None):
self.targets_file.write(targets_cfg.binary_template % (
name,
pretty_list(srcs),
pretty_list(deps)))
self.targets_file.write(targets_cfg.binary_template.format(
name=name,
srcs=pretty_list(srcs),
deps=pretty_list(deps)).encode("utf-8"))
self.total_bin = self.total_bin + 1

def add_c_test(self):
self.targets_file.write("""
self.targets_file.write(b"""
if not is_opt_mode:
cpp_binary(
name = "c_test_bin",
Expand Down Expand Up @@ -119,5 +119,5 @@ def register_test(self,
self.total_test = self.total_test + 1

def flush_tests(self):
self.targets_file.write(targets_cfg.unittests_template % self.tests_cfg)
self.targets_file.write(targets_cfg.unittests_template.format(tests=self.tests_cfg).encode("utf-8"))
self.tests_cfg = ""
32 changes: 22 additions & 10 deletions buckifier/targets_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@
("gflags", None, "gflags"),
("lz4", None, "lz4"),
("zstd", None),
("tbb", None),
]
ROCKSDB_OS_DEPS = [
(
"linux",
["third-party//numa:numa", "third-party//liburing:uring"],
["third-party//numa:numa", "third-party//liburing:uring", "third-party//tbb:tbb"],
),
(
"macos",
["third-party//tbb:tbb"],
),
]
Expand All @@ -56,17 +59,27 @@
"-DHAVE_SSE42",
"-DLIBURING",
"-DNUMA",
"-DROCKSDB_PLATFORM_POSIX",
"-DROCKSDB_LIB_IO_POSIX",
"-DTBB",
],
),
(
"macos",
["-DOS_MACOSX"],
[
"-DOS_MACOSX",
"-DROCKSDB_PLATFORM_POSIX",
"-DROCKSDB_LIB_IO_POSIX",
"-DTBB",
],
),
(
"windows",
[ "-DOS_WIN", "-DWIN32", "-D_MBCS", "-DWIN64", "-DNOMINMAX" ]
),
]
ROCKSDB_PREPROCESSOR_FLAGS = [
"-DROCKSDB_PLATFORM_POSIX",
"-DROCKSDB_LIB_IO_POSIX",
"-DROCKSDB_SUPPORT_THREAD_LOCAL",
# Flags to enable libs we include
Expand All @@ -77,7 +90,6 @@
"-DZSTD",
"-DZSTD_STATIC_LINKING_ONLY",
"-DGFLAGS=gflags",
"-DTBB",
# Added missing flags from output of build_detect_platform
"-DROCKSDB_BACKTRACE",
Expand Down Expand Up @@ -154,12 +166,12 @@

binary_template = """
cpp_binary(
name = "%s",
srcs = [%s],
name = "{name}",
srcs = [{srcs}],
arch_preprocessor_flags = ROCKSDB_ARCH_PREPROCESSOR_FLAGS,
compiler_flags = ROCKSDB_COMPILER_FLAGS,
preprocessor_flags = ROCKSDB_PREPROCESSOR_FLAGS,
deps = [%s],
deps = [{deps}],
external_deps = ROCKSDB_EXTERNAL_DEPS,
)
"""
Expand All @@ -176,7 +188,7 @@
unittests_template = """
# [test_name, test_src, test_type, extra_deps, extra_compiler_flags]
ROCKS_TESTS = [
%s]
{tests}]
# Generate a test rule for each entry in ROCKS_TESTS
# Do not build the tests in opt mode, since SyncPoint and other test code
Expand Down
5 changes: 5 additions & 0 deletions env/env_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors

#if !defined(OS_WIN)

#include <dirent.h>
#ifndef ROCKSDB_NO_DYNAMIC_EXTENSION
#include <dlfcn.h>
Expand Down Expand Up @@ -517,3 +520,5 @@ std::unique_ptr<Env> NewCompositeEnv(std::shared_ptr<FileSystem> fs) {
}

} // namespace ROCKSDB_NAMESPACE

#endif
5 changes: 5 additions & 0 deletions env/fs_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors

#if !defined(OS_WIN)

#include <dirent.h>
#ifndef ROCKSDB_NO_DYNAMIC_EXTENSION
#include <dlfcn.h>
Expand Down Expand Up @@ -1060,3 +1063,5 @@ static FactoryFunc<FileSystem> posix_filesystem_reg =
#endif

} // namespace ROCKSDB_NAMESPACE

#endif
4 changes: 4 additions & 0 deletions port/port_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#if !defined(OS_WIN)

#include "port/port_posix.h"

#include <assert.h>
Expand Down Expand Up @@ -262,3 +264,5 @@ void SetCpuPriority(ThreadId id, CpuPriority priority) {

} // namespace port
} // namespace ROCKSDB_NAMESPACE

#endif
5 changes: 3 additions & 2 deletions port/stack_trace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
//
#include "port/stack_trace.h"

#if defined(ROCKSDB_LITE) || !(defined(ROCKSDB_BACKTRACE) || defined(OS_MACOSX)) || \
defined(CYGWIN) || defined(OS_FREEBSD) || defined(OS_SOLARIS)
#if defined(ROCKSDB_LITE) || \
!(defined(ROCKSDB_BACKTRACE) || defined(OS_MACOSX)) || defined(CYGWIN) || \
defined(OS_FREEBSD) || defined(OS_SOLARIS) || defined(OS_WIN)

// noop

Expand Down
4 changes: 4 additions & 0 deletions port/win/env_default.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#if defined(OS_WIN)

#include <mutex>

#include <rocksdb/env.h>
Expand Down Expand Up @@ -39,3 +41,5 @@ Env* Env::Default() {
}

} // namespace ROCKSDB_NAMESPACE

#endif
4 changes: 4 additions & 0 deletions port/win/env_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#if defined(OS_WIN)

#include "port/win/env_win.h"
#include "port/win/win_thread.h"
#include <algorithm>
Expand Down Expand Up @@ -1549,3 +1551,5 @@ std::string Env::GenerateUniqueId() {
}

} // namespace ROCKSDB_NAMESPACE

#endif
2 changes: 1 addition & 1 deletion port/win/env_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class WinEnv : public Env {

void StartThread(void(*function)(void* arg), void* arg) override;

void WaitForJoin();
void WaitForJoin() override;

unsigned int GetThreadPoolQueueLen(Env::Priority pri) const override;

Expand Down
16 changes: 10 additions & 6 deletions port/win/io_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#if defined(OS_WIN)

#include "port/win/io_win.h"

#include "monitoring/iostats_context_imp.h"
Expand Down Expand Up @@ -642,8 +644,8 @@ Status WinSequentialFile::Skip(uint64_t n) {
BOOL ret = SetFilePointerEx(hFile_, li, NULL, FILE_CURRENT);
if (ret == FALSE) {
auto lastError = GetLastError();
return IOErrorFromWindowsError("Skip SetFilePointerEx():" + filename_,
lastError);
return IOErrorFromWindowsError("Skip SetFilePointerEx():" + filename_,
lastError);
}
return Status::OK();
}
Expand Down Expand Up @@ -767,8 +769,8 @@ Status WinWritableImpl::AppendImpl(const Slice& data) {
Status s;

if (data.size() > std::numeric_limits<DWORD>::max()) {
return Status::InvalidArgument("data is too long for a single write" +
file_data_->GetName());
return Status::InvalidArgument("data is too long for a single write" +
file_data_->GetName());
}

size_t bytes_written = 0; // out param
Expand Down Expand Up @@ -805,8 +807,8 @@ Status WinWritableImpl::AppendImpl(const Slice& data) {
// is sector aligned
next_write_offset_ += bytes_written;
} else {
s = Status::IOError("Failed to write all bytes: " +
file_data_->GetName());
s = Status::IOError("Failed to write all bytes: " +
file_data_->GetName());
}
}

Expand Down Expand Up @@ -1067,3 +1069,5 @@ WinFileLock::~WinFileLock() {

}
} // namespace ROCKSDB_NAMESPACE

#endif
2 changes: 1 addition & 1 deletion port/win/io_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ class WinRandomRWFile : private WinFileData,

virtual Status Sync() override;

virtual Status Fsync() { return Sync(); }
virtual Status Fsync() override { return Sync(); }

virtual Status Close() override;
};
Expand Down
6 changes: 3 additions & 3 deletions port/win/port_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#if !defined(OS_WIN) && !defined(WIN32) && !defined(_WIN32)
#error Windows Specific Code
#endif
#if defined(OS_WIN)

#include "port/win/port_win.h"

Expand Down Expand Up @@ -272,3 +270,5 @@ void SetCpuPriority(ThreadId id, CpuPriority priority) {

} // namespace port
} // namespace ROCKSDB_NAMESPACE

#endif
4 changes: 4 additions & 0 deletions port/win/win_jemalloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#if defined(OS_WIN)

#ifndef ROCKSDB_JEMALLOC
# error This file can only be part of jemalloc aware build
#endif
Expand Down Expand Up @@ -73,3 +75,5 @@ void operator delete[](void* p) {
je_free(p);
}
}

#endif
Loading

0 comments on commit 249f2b5

Please sign in to comment.