Merge pull request #69 from hughbe/unlink-windows

Introduce and use PlatformUtility.unlink
diff --git a/include/llbuild/Basic/PlatformUtility.h b/include/llbuild/Basic/PlatformUtility.h
index 2b77476..fcb0c6d 100644
--- a/include/llbuild/Basic/PlatformUtility.h
+++ b/include/llbuild/Basic/PlatformUtility.h
@@ -22,6 +22,7 @@
 namespace basic {
 namespace sys {
   bool chdir(const char *fileName);
+  int unlink(const char *fileName);
 }
 }
 }
diff --git a/lib/Basic/PlatformUtility.cpp b/lib/Basic/PlatformUtility.cpp
index 496976a..d5c7035 100644
--- a/lib/Basic/PlatformUtility.cpp
+++ b/lib/Basic/PlatformUtility.cpp
@@ -11,6 +11,7 @@
 
 #if defined(_WIN32)
 #include "LeanWindows.h"
+#include <io.h>
 #else
 #include <unistd.h>
 #endif
@@ -25,3 +26,11 @@
   return ::chdir(fileName) == 0;
 #endif
 }
+
+int sys::unlink(const char *fileName) {
+#if defined(_WIN32)
+  return ::_unlink(fileName);
+#else
+  return ::unlink(fileName);
+#endif
+}
diff --git a/lib/BuildSystem/BuildSystem.cpp b/lib/BuildSystem/BuildSystem.cpp
index ea7aeaa..6d59bc3 100644
--- a/lib/BuildSystem/BuildSystem.cpp
+++ b/lib/BuildSystem/BuildSystem.cpp
@@ -17,6 +17,7 @@
 #include "llbuild/Basic/FileSystem.h"
 #include "llbuild/Basic/Hashing.h"
 #include "llbuild/Basic/LLVM.h"
+#include "llbuild/Basic/PlatformUtility.h"
 #include "llbuild/Basic/ShellUtility.h"
 #include "llbuild/Core/BuildDB.h"
 #include "llbuild/Core/BuildEngine.h"
@@ -1834,7 +1835,7 @@
       auto success = true;
       if (llvm::sys::fs::create_link(contents, output->getName())) {
         // On failure, we attempt to unlink the file and retry.
-        ::unlink(output->getName().str().c_str());
+        basic::sys::unlink(output->getName().str().c_str());
         
         if (llvm::sys::fs::create_link(contents, output->getName())) {
           getBuildSystem(bsci.getBuildEngine()).error(
diff --git a/lib/Core/SQLiteBuildDB.cpp b/lib/Core/SQLiteBuildDB.cpp
index c65e961..2ba487c 100644
--- a/lib/Core/SQLiteBuildDB.cpp
+++ b/lib/Core/SQLiteBuildDB.cpp
@@ -12,6 +12,7 @@
 
 #include "llbuild/Core/BuildDB.h"
 
+#include "llbuild/Basic/PlatformUtility.h"
 #include "llbuild/Core/BuildEngine.h"
 
 #include "llvm/ADT/STLExtras.h"
@@ -22,7 +23,6 @@
 #include <sstream>
 
 #include <sqlite3.h>
-#include <unistd.h>
 
 using namespace llbuild;
 using namespace llbuild::core;
@@ -102,7 +102,7 @@
       sqlite3_close(db);
 
       // Always recreate the database from scratch when the schema changes.
-      result = unlink(path.str().c_str());
+      result = basic::sys::unlink(path.str().c_str());
       if (result == -1) {
         if (errno != ENOENT) {
           *error_out = std::string("unable to unlink existing database: ") +
diff --git a/unittests/Core/CMakeLists.txt b/unittests/Core/CMakeLists.txt
index c772d1b..3eeaffe 100644
--- a/unittests/Core/CMakeLists.txt
+++ b/unittests/Core/CMakeLists.txt
@@ -5,7 +5,7 @@
   MakefileDepsParserTest.cpp
   )
 
-target_link_libraries(CoreTests llbuildCore llvmSupport sqlite3)
+target_link_libraries(CoreTests llbuildCore llbuildBasic llvmSupport sqlite3)
 
 if(NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
   target_link_libraries(CoreTests curses)