Skip to content

Commit 907f308

Browse files
gavinandresensipa
authored andcommitted
Port leveldb to MinGW32
Several changes to make the native windows leveldb code compile with mingw32 and run on 32-bit Windows: * Remove -std=c++0x dependency (modified code to use NULL instead of nullptr) * Link with -lshlwapi * Only #define snprintf/etc if compiling with Visual Studio * Do not link against DbgHelp.lib (wrote a CreateDir instead of using DbgHelp's MakeSureDirectoryPathExists * Define WINVER=0x0500 so MinGW32 can use the 64-bit-filesystem Windows api calls * Define __USE_MINGW_ANSI_STDIO=1 to use MinGW's printf (which supports %ll) I also cleaned up makefile.mingw, assuming that dependencies would be in the standard /usr/local/{include,lib} by default but allowing overriding with make DEPSDIR=... etc
1 parent 9def2bf commit 907f308

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

build_detect_platform

+2-4
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,9 @@ case "$TARGET_OS" in
133133
;;
134134
OS_WINDOWS_CROSSCOMPILE | NATIVE_WINDOWS)
135135
PLATFORM=OS_WINDOWS
136-
COMMON_FLAGS="-fno-builtin-memcmp -D_REENTRANT -DOS_WINDOWS -DLEVELDB_PLATFORM_WINDOWS"
137-
PLATFORM_SHARED_CFLAGS=""
136+
COMMON_FLAGS="-fno-builtin-memcmp -D_REENTRANT -DOS_WINDOWS -DLEVELDB_PLATFORM_WINDOWS -DWINVER=0x0500 -D__USE_MINGW_ANSI_STDIO=1"
138137
PLATFORM_SOURCES="util/env_win.cc"
139-
PLATFORM_CXXFLAGS="-std=c++0x"
140-
PLATFORM_LIBS="-lshlwapi -ldbghelp"
138+
PLATFORM_LIBS="-lshlwapi"
141139
PORT_FILE=port/port_win.cc
142140
CROSS_COMPILE=true
143141
;;

port/port_win.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace leveldb {
3737
namespace port {
3838

3939
Mutex::Mutex() :
40-
cs_(nullptr) {
40+
cs_(NULL) {
4141
assert(!cs_);
4242
cs_ = static_cast<void *>(new CRITICAL_SECTION());
4343
::InitializeCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
@@ -48,7 +48,7 @@ Mutex::~Mutex() {
4848
assert(cs_);
4949
::DeleteCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
5050
delete static_cast<CRITICAL_SECTION *>(cs_);
51-
cs_ = nullptr;
51+
cs_ = NULL;
5252
assert(!cs_);
5353
}
5454

@@ -128,7 +128,7 @@ void InitOnce(OnceType* once, void (*initializer)()) {
128128
}
129129

130130
void* AtomicPointer::Acquire_Load() const {
131-
void * p = nullptr;
131+
void * p = NULL;
132132
InterlockedExchangePointer(&p, rep_);
133133
return p;
134134
}

port/port_win.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
#ifndef STORAGE_LEVELDB_PORT_PORT_WIN_H_
3232
#define STORAGE_LEVELDB_PORT_PORT_WIN_H_
3333

34+
#ifdef _MSC_VER
3435
#define snprintf _snprintf
3536
#define close _close
3637
#define fread_unlocked _fread_nolock
38+
#endif
3739

3840
#include <string>
3941
#include <stdint.h>
@@ -120,7 +122,7 @@ class AtomicPointer {
120122
private:
121123
void * rep_;
122124
public:
123-
AtomicPointer() : rep_(nullptr) { }
125+
AtomicPointer() : rep_(NULL) { }
124126
explicit AtomicPointer(void* v);
125127
void* Acquire_Load() const;
126128

util/env_win.cc

+21-7
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
#include <stdio.h>
2121
#include <errno.h>
2222
#include <io.h>
23-
#include <dbghelp.h>
2423
#include <algorithm>
25-
#pragma comment(lib,"DbgHelp.lib")
2624

2725
#ifdef max
2826
#undef max
@@ -908,18 +906,34 @@ uint64_t Win32Env::NowMicros()
908906
return (uint64_t)(GetTickCount64()*1000);
909907
}
910908

911-
Status Win32Env::CreateDir( const std::string& dirname )
909+
static Status CreateDirInner( const std::string& dirname )
912910
{
913911
Status sRet;
912+
DWORD attr = ::GetFileAttributes(dirname.c_str());
913+
if (attr == INVALID_FILE_ATTRIBUTES) { // doesn't exist:
914+
std::size_t slash = dirname.find_last_of("\\");
915+
if (slash != std::string::npos){
916+
sRet = CreateDirInner(dirname.substr(0, slash));
917+
if (!sRet.ok()) return sRet;
918+
}
919+
BOOL result = ::CreateDirectory(dirname.c_str(), NULL);
920+
if (result == FALSE) {
921+
sRet = Status::IOError(dirname, "Could not create directory.");
922+
return sRet;
923+
}
924+
}
925+
return sRet;
926+
}
927+
928+
Status Win32Env::CreateDir( const std::string& dirname )
929+
{
914930
std::string path = dirname;
915931
if(path[path.length() - 1] != '\\'){
916932
path += '\\';
917933
}
918934
ModifyPath(path);
919-
if(!::MakeSureDirectoryPathExists( path.c_str() ) ){
920-
sRet = Status::IOError(dirname, "Could not create directory.");
921-
}
922-
return sRet;
935+
936+
return CreateDirInner(path);
923937
}
924938

925939
Status Win32Env::DeleteDir( const std::string& dirname )

0 commit comments

Comments
 (0)