Skip to content

Commit

Permalink
Fix build with MinGW
Browse files Browse the repository at this point in the history
Summary:
There still are many warnings (most of them about invalid printf format
for long long), but it builds if FAIL_ON_WARNINGS is disabled.
Closes facebook#2052

Differential Revision: D4807355

Pulled By: siying

fbshipit-source-id: ef03786
  • Loading branch information
orgads authored and facebook-github-bot committed Mar 30, 2017
1 parent 80fe5b3 commit 6401a8b
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 42 deletions.
26 changes: 16 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ configure_file(util/build_version.cc.in ${BUILD_VERSION_CC} @ONLY)
add_library(build_version OBJECT ${BUILD_VERSION_CC})
target_include_directories(build_version PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/util)
if(WIN32)
if(MSVC)
if (${USE_AVX2} EQUAL 1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi /nologo /arch:AVX2 /EHsc /GS /Gd /GR /GF /fp:precise /Zc:wchar_t /Zc:forScope /errorReport:queue")
else ()
Expand All @@ -126,6 +126,9 @@ if(WIN32)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W -Wextra -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-compare -Wshadow -Wno-unused-parameter -Wno-unused-variable -Woverloaded-virtual -Wnon-virtual-dtor -Wno-missing-field-initializers")
if(MINGW)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer")
Expand Down Expand Up @@ -187,7 +190,7 @@ else()
set(OPTIMIZE_DEBUG ${OPTIMIZE_DEBUG_DEFAULT})
endif()

if(WIN32)
if(MSVC)
if((${OPTIMIZE_DEBUG} EQUAL 1))
message(STATUS "Debug optimization is enabled")
set(CMAKE_CXX_FLAGS_DEBUG "/Oxt /MDd")
Expand Down Expand Up @@ -235,6 +238,9 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Android")
add_definitions(-DOS_ANDROID)
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
add_definitions(-DWIN32 -DOS_WIN -D_MBCS -DWIN64 -DNOMINMAX)
if(MINGW)
add_definitions(-D_WIN32_WINNT=_WIN32_WINNT_VISTA)
endif()
endif()

if(NOT WIN32)
Expand Down Expand Up @@ -514,18 +520,18 @@ add_library(${ROCKSDB_STATIC_LIB} STATIC ${SOURCES})
target_link_libraries(${ROCKSDB_STATIC_LIB}
${THIRDPARTY_LIBS} ${SYSTEM_LIBS})

if(WIN32)
set_target_properties(${ROCKSDB_STATIC_LIB} PROPERTIES
COMPILE_FLAGS "/Fd${CMAKE_CFG_INTDIR}/${ROCKSDB_STATIC_LIB}.pdb")
endif()

if(WIN32)
add_library(${ROCKSDB_IMPORT_LIB} SHARED ${SOURCES})
target_link_libraries(${ROCKSDB_IMPORT_LIB}
${THIRDPARTY_LIBS} ${SYSTEM_LIBS})
set_target_properties(${ROCKSDB_IMPORT_LIB} PROPERTIES
COMPILE_FLAGS "-DROCKSDB_DLL -DROCKSDB_LIBRARY_EXPORTS /Fd${CMAKE_CFG_INTDIR}/${ROCKSDB_IMPORT_LIB}.pdb")
else()
COMPILE_DEFINITIONS "ROCKSDB_DLL;ROCKSDB_LIBRARY_EXPORTS")
if(MSVC)
set_target_properties(${ROCKSDB_STATIC_LIB} PROPERTIES
COMPILE_FLAGS "/Fd${CMAKE_CFG_INTDIR}/${ROCKSDB_STATIC_LIB}.pdb")
set_target_properties(${ROCKSDB_IMPORT_LIB} PROPERTIES
COMPILE_FLAGS "/Fd${CMAKE_CFG_INTDIR}/${ROCKSDB_IMPORT_LIB}.pdb")
endif()
endif()

option(WITH_JNI "build with JNI" OFF)
Expand Down Expand Up @@ -692,7 +698,7 @@ enable_testing()
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND})
set(TESTUTILLIB testutillib${ARTIFACT_SUFFIX})
add_library(${TESTUTILLIB} STATIC ${TESTUTIL_SOURCE})
if(WIN32)
if(MSVC)
set_target_properties(${TESTUTILLIB} PROPERTIES COMPILE_FLAGS "/Fd${CMAKE_CFG_INTDIR}/testutillib${ARTIFACT_SUFFIX}.pdb")
endif()
set_target_properties(${TESTUTILLIB}
Expand Down
2 changes: 1 addition & 1 deletion db/write_batch_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ TEST_F(WriteBatchTest, DISABLED_LargeKeyValue) {
// Insert key and value of 3GB and push total batch size to 12GB.
static const size_t kKeyValueSize = 3221225472u;
std::string raw(kKeyValueSize, 'A');
WriteBatch batch(12884901888u + 1024u);
WriteBatch batch(size_t(12884901888ull + 1024u));
for (char i = 0; i < 2; i++) {
raw[0] = 'A' + i;
raw[raw.length() - 1] = 'A' - i;
Expand Down
2 changes: 1 addition & 1 deletion include/rocksdb/iostats_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct IOStatsContext {
};

#ifndef IOS_CROSS_COMPILE
# ifdef _WIN32
# ifdef _MSC_VER
extern __declspec(thread) IOStatsContext iostats_context;
# else
extern __thread IOStatsContext iostats_context;
Expand Down
2 changes: 1 addition & 1 deletion include/rocksdb/perf_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ struct PerfContext {

#if defined(NPERF_CONTEXT) || defined(IOS_CROSS_COMPILE)
extern PerfContext perf_context;
#elif _WIN32
#elif defined(_MSC_VER)
extern __declspec(thread) PerfContext perf_context;
#else
extern __thread PerfContext perf_context;
Expand Down
2 changes: 1 addition & 1 deletion port/win/env_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ uint64_t WinEnvIO::NowMicros() {
if (GetSystemTimePreciseAsFileTime_ != NULL) {
// all std::chrono clocks on windows proved to return
// values that may repeat that is not good enough for some uses.
const int64_t c_UnixEpochStartTicks = 116444736000000000i64;
const int64_t c_UnixEpochStartTicks = 116444736000000000LL;
const int64_t c_FtToMicroSec = 10;

// This interface needs to return system time and not
Expand Down
4 changes: 2 additions & 2 deletions port/win/port_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ DIR* opendir(const char* name) {
return nullptr;
}

strncpy_s(dir->entry_.d_name, dir->data_.name, strlen(dir->data_.name));
strcpy_s(dir->entry_.d_name, sizeof(dir->entry_.d_name), dir->data_.name);

return dir.release();
}
Expand All @@ -164,7 +164,7 @@ struct dirent* readdir(DIR* dirp) {
return nullptr;
}

strncpy_s(dirp->entry_.d_name, dirp->data_.name, strlen(dirp->data_.name));
strcpy_s(dirp->entry_.d_name, sizeof(dirp->entry_.d_name), dirp->data_.name);

return &dirp->entry_;
}
Expand Down
5 changes: 5 additions & 0 deletions port/win/port_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
#undef GetCurrentTime
#undef DeleteFile

#ifndef _SSIZE_T_DEFINED
typedef SSIZE_T ssize_t;
#endif

// size_t printf formatting named in the manner of C99 standard formatting
// strings such as PRIu64
Expand All @@ -56,6 +58,7 @@ typedef SSIZE_T ssize_t;
#define ROCKSDB_PRIszt "Iu"
#endif

#ifdef _MSC_VER
#define __attribute__(A)

// Thread local storage on Linux
Expand All @@ -64,6 +67,8 @@ typedef SSIZE_T ssize_t;
#define __thread __declspec(thread)
#endif

#endif

#ifndef PLATFORM_IS_LITTLE_ENDIAN
#define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
#endif
Expand Down
4 changes: 2 additions & 2 deletions port/win/win_logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ namespace port {
WinLogger::WinLogger(uint64_t (*gettid)(), Env* env, HANDLE file,
const InfoLogLevel log_level)
: Logger(log_level),
file_(file),
gettid_(gettid),
log_size_(0),
last_flush_micros_(0),
env_(env),
flush_pending_(false),
file_(file) {}
flush_pending_(false) {}

void WinLogger::DebugWriter(const char* str, int len) {
DWORD bytesWritten = 0;
Expand Down
1 change: 1 addition & 0 deletions port/win/win_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class WinLogger : public rocksdb::Logger {

void Flush() override;

using rocksdb::Logger::Logv;
void Logv(const char* format, va_list ap) override;

size_t GetLogFileSize() const override;
Expand Down
4 changes: 2 additions & 2 deletions third-party/gtest-1.7.0/fused-src/gtest/gtest-all.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8676,8 +8676,8 @@ void Notification::WaitForNotification() {
}

Mutex::Mutex()
: type_(kDynamic),
owner_thread_id_(0),
: owner_thread_id_(0),
type_(kDynamic),
critical_section_init_phase_(0),
critical_section_(new CRITICAL_SECTION) {
::InitializeCriticalSection(critical_section_);
Expand Down
2 changes: 1 addition & 1 deletion util/arena.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
namespace rocksdb {

// MSVC complains that it is already defined since it is static in the header.
#ifndef OS_WIN
#ifndef _MSC_VER
const size_t Arena::kInlineSize;
#endif

Expand Down
2 changes: 1 addition & 1 deletion util/iostats_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace rocksdb {

#ifndef IOS_CROSS_COMPILE
# ifdef _WIN32
# ifdef _MSC_VER
__declspec(thread) IOStatsContext iostats_context;
# else
__thread IOStatsContext iostats_context;
Expand Down
2 changes: 1 addition & 1 deletion util/perf_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace rocksdb {

#if defined(NPERF_CONTEXT) || defined(IOS_CROSS_COMPILE)
PerfContext perf_context;
#elif _WIN32
#elif defined(_MSC_VER)
__declspec(thread) PerfContext perf_context;
#else
__thread PerfContext perf_context;
Expand Down
29 changes: 16 additions & 13 deletions util/thread_local.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pthread_key_t thread_local_key = -1;
void NTAPI WinOnThreadExit(PVOID module, DWORD reason, PVOID reserved) {
// We decided to punt on PROCESS_EXIT
if (DLL_THREAD_DETACH == reason) {
if (thread_local_key != -1 && thread_local_inclass_routine != nullptr) {
if (thread_local_key != pthread_key_t(-1) && thread_local_inclass_routine != nullptr) {
void* tls = pthread_getspecific(thread_local_key);
if (tls != nullptr) {
thread_local_inclass_routine(tls);
Expand All @@ -191,22 +191,11 @@ void NTAPI WinOnThreadExit(PVOID module, DWORD reason, PVOID reserved) {

} // wintlscleanup

#ifdef _WIN64

#pragma comment(linker, "/include:_tls_used")
#pragma comment(linker, "/include:p_thread_callback_on_exit")

#else // _WIN64

#pragma comment(linker, "/INCLUDE:__tls_used")
#pragma comment(linker, "/INCLUDE:_p_thread_callback_on_exit")

#endif // _WIN64

// extern "C" suppresses C++ name mangling so we know the symbol name for the
// linker /INCLUDE:symbol pragma above.
extern "C" {

#ifdef _MSC_VER
// The linker must not discard thread_callback_on_exit. (We force a reference
// to this variable with a linker /include:symbol pragma to ensure that.) If
// this variable is discarded, the OnThreadExit function will never be called.
Expand All @@ -222,15 +211,29 @@ const PIMAGE_TLS_CALLBACK p_thread_callback_on_exit =
// Reset the default section.
#pragma const_seg()

#pragma comment(linker, "/include:_tls_used")
#pragma comment(linker, "/include:p_thread_callback_on_exit")

#else // _WIN64

#pragma data_seg(".CRT$XLB")
PIMAGE_TLS_CALLBACK p_thread_callback_on_exit = wintlscleanup::WinOnThreadExit;
// Reset the default section.
#pragma data_seg()

#pragma comment(linker, "/INCLUDE:__tls_used")
#pragma comment(linker, "/INCLUDE:_p_thread_callback_on_exit")

#endif // _WIN64

#else
// https://github.com/couchbase/gperftools/blob/master/src/windows/port.cc
BOOL WINAPI DllMain(HINSTANCE h, DWORD dwReason, PVOID pv) {
if (dwReason == DLL_THREAD_DETACH)
wintlscleanup::WinOnThreadExit(h, dwReason, pv);
return TRUE;
}
#endif
} // extern "C"

#endif // OS_WIN
Expand Down
12 changes: 6 additions & 6 deletions utilities/persistent_cache/persistent_cache_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -324,23 +324,23 @@ void PersistentCacheDBTest::RunTest(
BlockBasedTableOptions table_options;
table_options.cache_index_and_filter_blocks = true;

const uint64_t uint64_max = std::numeric_limits<uint64_t>::max();
const size_t size_max = std::numeric_limits<size_t>::max();

switch (iter) {
case 0:
// page cache, block cache, no-compressed cache
pcache = new_pcache(/*is_compressed=*/true);
table_options.persistent_cache = pcache;
table_options.block_cache = NewLRUCache(uint64_max);
table_options.block_cache = NewLRUCache(size_max);
table_options.block_cache_compressed = nullptr;
options.table_factory.reset(NewBlockBasedTableFactory(table_options));
break;
case 1:
// page cache, block cache, compressed cache
pcache = new_pcache(/*is_compressed=*/true);
table_options.persistent_cache = pcache;
table_options.block_cache = NewLRUCache(uint64_max);
table_options.block_cache_compressed = NewLRUCache(uint64_max);
table_options.block_cache = NewLRUCache(size_max);
table_options.block_cache_compressed = NewLRUCache(size_max);
options.table_factory.reset(NewBlockBasedTableFactory(table_options));
break;
case 2:
Expand All @@ -349,8 +349,8 @@ void PersistentCacheDBTest::RunTest(
// also, make block cache sizes bigger, to trigger block cache hits
pcache = new_pcache(/*is_compressed=*/true);
table_options.persistent_cache = pcache;
table_options.block_cache = NewLRUCache(uint64_max);
table_options.block_cache_compressed = NewLRUCache(uint64_max);
table_options.block_cache = NewLRUCache(size_max);
table_options.block_cache_compressed = NewLRUCache(size_max);
options.table_factory.reset(NewBlockBasedTableFactory(table_options));
options.compression = kNoCompression;
break;
Expand Down

0 comments on commit 6401a8b

Please sign in to comment.