Skip to content

Commit

Permalink
Port leveldb to MinGW32
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gavinandresen authored and sipa committed Aug 17, 2013
1 parent 9def2bf commit 907f308
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
6 changes: 2 additions & 4 deletions build_detect_platform
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,9 @@ case "$TARGET_OS" in
;;
OS_WINDOWS_CROSSCOMPILE | NATIVE_WINDOWS)
PLATFORM=OS_WINDOWS
COMMON_FLAGS="-fno-builtin-memcmp -D_REENTRANT -DOS_WINDOWS -DLEVELDB_PLATFORM_WINDOWS"
PLATFORM_SHARED_CFLAGS=""
COMMON_FLAGS="-fno-builtin-memcmp -D_REENTRANT -DOS_WINDOWS -DLEVELDB_PLATFORM_WINDOWS -DWINVER=0x0500 -D__USE_MINGW_ANSI_STDIO=1"
PLATFORM_SOURCES="util/env_win.cc"
PLATFORM_CXXFLAGS="-std=c++0x"
PLATFORM_LIBS="-lshlwapi -ldbghelp"
PLATFORM_LIBS="-lshlwapi"
PORT_FILE=port/port_win.cc
CROSS_COMPILE=true
;;
Expand Down
6 changes: 3 additions & 3 deletions port/port_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace leveldb {
namespace port {

Mutex::Mutex() :
cs_(nullptr) {
cs_(NULL) {
assert(!cs_);
cs_ = static_cast<void *>(new CRITICAL_SECTION());
::InitializeCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
Expand All @@ -48,7 +48,7 @@ Mutex::~Mutex() {
assert(cs_);
::DeleteCriticalSection(static_cast<CRITICAL_SECTION *>(cs_));
delete static_cast<CRITICAL_SECTION *>(cs_);
cs_ = nullptr;
cs_ = NULL;
assert(!cs_);
}

Expand Down Expand Up @@ -128,7 +128,7 @@ void InitOnce(OnceType* once, void (*initializer)()) {
}

void* AtomicPointer::Acquire_Load() const {
void * p = nullptr;
void * p = NULL;
InterlockedExchangePointer(&p, rep_);
return p;
}
Expand Down
4 changes: 3 additions & 1 deletion port/port_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
#ifndef STORAGE_LEVELDB_PORT_PORT_WIN_H_
#define STORAGE_LEVELDB_PORT_PORT_WIN_H_

#ifdef _MSC_VER
#define snprintf _snprintf
#define close _close
#define fread_unlocked _fread_nolock
#endif

#include <string>
#include <stdint.h>
Expand Down Expand Up @@ -120,7 +122,7 @@ class AtomicPointer {
private:
void * rep_;
public:
AtomicPointer() : rep_(nullptr) { }
AtomicPointer() : rep_(NULL) { }
explicit AtomicPointer(void* v);
void* Acquire_Load() const;

Expand Down
28 changes: 21 additions & 7 deletions util/env_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
#include <stdio.h>
#include <errno.h>
#include <io.h>
#include <dbghelp.h>
#include <algorithm>
#pragma comment(lib,"DbgHelp.lib")

#ifdef max
#undef max
Expand Down Expand Up @@ -908,18 +906,34 @@ uint64_t Win32Env::NowMicros()
return (uint64_t)(GetTickCount64()*1000);
}

Status Win32Env::CreateDir( const std::string& dirname )
static Status CreateDirInner( const std::string& dirname )
{
Status sRet;
DWORD attr = ::GetFileAttributes(dirname.c_str());
if (attr == INVALID_FILE_ATTRIBUTES) { // doesn't exist:
std::size_t slash = dirname.find_last_of("\\");
if (slash != std::string::npos){
sRet = CreateDirInner(dirname.substr(0, slash));
if (!sRet.ok()) return sRet;
}
BOOL result = ::CreateDirectory(dirname.c_str(), NULL);
if (result == FALSE) {
sRet = Status::IOError(dirname, "Could not create directory.");
return sRet;
}
}
return sRet;
}

Status Win32Env::CreateDir( const std::string& dirname )
{
std::string path = dirname;
if(path[path.length() - 1] != '\\'){
path += '\\';
}
ModifyPath(path);
if(!::MakeSureDirectoryPathExists( path.c_str() ) ){
sRet = Status::IOError(dirname, "Could not create directory.");
}
return sRet;

return CreateDirInner(path);
}

Status Win32Env::DeleteDir( const std::string& dirname )
Expand Down

0 comments on commit 907f308

Please sign in to comment.