1From 3157e53522e3fa5232c56fdbba378ebdf2f3ef4f Mon Sep 17 00:00:00 2001 2From: Andrew Hsieh <andrewhsieh@google.com> 3Date: Tue, 19 Mar 2013 10:38:57 -0700 4Subject: [PATCH 3/5] Fix mingw build 5 6Change-Id: I8d1669bf1d89cad60e8b49a946feee52734c4a23 7--- 8 include/mcld/Support/Path.h | 6 ++++-- 9 lib/Support/Directory.cpp | 2 +- 10 lib/Support/Path.cpp | 4 ++-- 11 lib/Support/Windows/FileSystem.inc | 19 +++++++++++++++++++ 12 lib/Support/Windows/PathV3.inc | 15 +++++++++++++-- 13 lib/Support/Windows/System.inc | 5 +++++ 14 lib/Support/raw_ostream.cpp | 2 +- 15 lib/Target/GNULDBackend.cpp | 4 ++++ 16 lib/Target/Mips/MipsLDBackend.cpp | 4 ++++ 17 tools/llvm-mcld/llvm-mcld.cpp | 13 +++++++++++++ 18 10 files changed, 66 insertions(+), 8 deletions(-) 19 20diff --git a/include/mcld/Support/Path.h b/include/mcld/Support/Path.h 21index 399c56d..b84d55a 100644 22--- a/include/mcld/Support/Path.h 23+++ b/include/mcld/Support/Path.h 24@@ -28,7 +28,7 @@ namespace mcld { 25 namespace sys { 26 namespace fs { 27 28-#if defined(MCLD_ON_WIN32) 29+#if defined(MCLD_ON_WIN32) && !defined(__MINGW32__) 30 const wchar_t preferred_separator = L'\\'; 31 const wchar_t separator = L'\\'; 32 const wchar_t colon = L':'; 33@@ -50,7 +50,7 @@ const char dot = L'.'; 34 class Path 35 { 36 public: 37-#if defined(MCLD_ON_WIN32) 38+#if defined(MCLD_ON_WIN32) && !defined(__MINGW32__) 39 typedef wchar_t ValueType; 40 typedef std::wstring StringType; 41 #else 42@@ -93,7 +93,9 @@ public: 43 { return m_PathName.c_str(); } 44 45 std::string string() const; 46+#if !defined(__MINGW32__) 47 std::wstring wstring() const; 48+#endif 49 50 // ----- decomposition ----- // 51 Path parent_path() const; 52diff --git a/lib/Support/Directory.cpp b/lib/Support/Directory.cpp 53index 827df59..d19d6a0 100644 54--- a/lib/Support/Directory.cpp 55+++ b/lib/Support/Directory.cpp 56@@ -24,7 +24,7 @@ bool is_symlink(FileStatus f) 57 return f.type() == SymlinkFile; 58 } 59 60-#if defined(MCLD_ON_WIN32) 61+#if defined(MCLD_ON_WIN32) && !defined(__MINGW32__) 62 const Path dot_path(L"."); 63 const Path dot_dot_path(L".."); 64 65diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp 66index 9989c1a..4a2e28e 100644 67--- a/lib/Support/Path.cpp 68+++ b/lib/Support/Path.cpp 69@@ -23,7 +23,7 @@ using namespace mcld::sys::fs; 70 // Helper 71 //===--------------------------------------------------------------------===// 72 namespace { 73-#if defined(MCLD_ON_WIN32) 74+#if defined(MCLD_ON_WIN32) && !defined(__MINGW32__) 75 bool is_separator(wchar_t value) 76 { 77 return (value == separator || value == preferred_separator); 78@@ -104,7 +104,7 @@ Path& Path::append(const Path& pPath) 79 unsigned int new_size = old_size + pPath.native().size(); 80 81 m_PathName.resize(new_size); 82-#if defined(MCLD_ON_WIN32) 83+#if defined(MCLD_ON_WIN32) && !defined(__MINGW32__) 84 wcscpy(const_cast<ValueType*>(m_PathName.data()+old_size), pPath.native().data()); 85 #else 86 strcpy(const_cast<ValueType*>(m_PathName.data()+old_size), pPath.native().data()); 87diff --git a/lib/Support/Windows/FileSystem.inc b/lib/Support/Windows/FileSystem.inc 88index 7ccb958..0708aff 100644 89--- a/lib/Support/Windows/FileSystem.inc 90+++ b/lib/Support/Windows/FileSystem.inc 91@@ -8,8 +8,10 @@ 92 //===----------------------------------------------------------------------===// 93 #include <string> 94 #include <io.h> 95+#include <stdio.h> 96 #include <fcntl.h> 97 #include <cstdlib> 98+#include <sys/stat.h> 99 #include <mcld/Support/FileHandle.h> 100 101 #ifndef STDIN_FILENO 102@@ -27,16 +29,29 @@ namespace sys{ 103 namespace fs{ 104 namespace detail{ 105 106+#if !defined(__MINGW32__) 107 Path::StringType static_library_extension = L".lib"; 108 Path::StringType shared_library_extension = L".dll"; 109 Path::StringType executable_extension = L".exe"; 110 Path::StringType relocatable_extension = L".obj"; 111 Path::StringType assembly_extension = L".s"; 112 Path::StringType bitcode_extension = L".bc"; 113+#else 114+Path::StringType static_library_extension = ".lib"; 115+Path::StringType shared_library_extension = ".dll"; 116+Path::StringType executable_extension = ".exe"; 117+Path::StringType relocatable_extension = ".obj"; 118+Path::StringType assembly_extension = ".s"; 119+Path::StringType bitcode_extension = ".bc"; 120+#endif 121 122 int open(const Path& pPath, int pOFlag) 123 { 124+#if !defined(__MINGW32__) 125 return ::_wopen(pPath.native().c_str(), pOFlag); 126+#else 127+ return ::open(pPath.native().c_str(), pOFlag); 128+#endif 129 } 130 131 int open(const Path& pPath, int pOFlag, int pPerm) 132@@ -52,7 +67,11 @@ int open(const Path& pPath, int pOFlag, int pPerm) 133 pPerm & FileHandle::WriteOther) 134 perm |= _S_IWRITE; 135 136+#if !defined(__MINGW32__) 137 return ::_wopen(pPath.native().c_str(), pOFlag, perm); 138+#else 139+ return ::open(pPath.native().c_str(), pOFlag, perm); 140+#endif 141 } 142 143 ssize_t pread(int pFD, void* pBuf, size_t pCount, size_t pOffset) 144diff --git a/lib/Support/Windows/PathV3.inc b/lib/Support/Windows/PathV3.inc 145index db577c0..ed0e712 100644 146--- a/lib/Support/Windows/PathV3.inc 147+++ b/lib/Support/Windows/PathV3.inc 148@@ -8,7 +8,7 @@ 149 //===----------------------------------------------------------------------===// 150 #include <mcld/Support/Path.h> 151 #include <mcld/Support/Directory.h> 152-#include <Windows.h> 153+#include <windows.h> 154 #include <sys/types.h> 155 #include <sys/stat.h> 156 #include <limits.h> 157@@ -30,10 +30,12 @@ std::string Path::string() const 158 return result; 159 } 160 161+#if !defined(__MINGW32__) 162 std::wstring Path::wstring() const 163 { 164 return m_PathName; 165 } 166+#endif 167 168 //===----------------------------------------------------------------------===// 169 // mcld::sys::fs::detail 170@@ -150,7 +152,11 @@ bool not_found_error(int perrno) 171 void status(const Path& p, FileStatus& pFileStatus) 172 { 173 struct ::_stat path_stat; 174+#if !defined(__MINGW32__) 175 if(::_wstat(p.c_str(), &path_stat)!= 0) 176+#else 177+ if(::_stat(p.c_str(), &path_stat)!= 0) 178+#endif 179 { 180 if(not_found_error(errno)) 181 { 182@@ -200,8 +206,13 @@ void close_dir(Directory& pDir) 183 184 void get_pwd(Path& pPWD) 185 { 186- wchar_t* pwd = (wchar_t*)malloc(PATH_MAX); 187+#if !defined(__MINGW32__) 188+ wchar_t* pwd = (wchar_t*)malloc(PATH_MAX*sizeof(wchar_t)); 189 pPWD.assign(_wgetcwd(pwd, PATH_MAX)); 190+#else 191+ char* pwd = (char*)malloc(PATH_MAX); 192+ pPWD.assign(pwd); 193+#endif 194 free(pwd); 195 } 196 197diff --git a/lib/Support/Windows/System.inc b/lib/Support/Windows/System.inc 198index 8b64c40..ba91105 100644 199--- a/lib/Support/Windows/System.inc 200+++ b/lib/Support/Windows/System.inc 201@@ -16,6 +16,11 @@ 202 namespace mcld{ 203 namespace sys{ 204 205+char *strerror(int errnum) 206+{ 207+ return strerror(errnum); 208+} 209+ 210 std::string getDefaultTargetTriple() 211 { 212 return MCLD_DEFAULT_TARGET_TRIPLE; 213diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp 214index 57213ed..4797245 100644 215--- a/lib/Support/raw_ostream.cpp 216+++ b/lib/Support/raw_ostream.cpp 217@@ -17,7 +17,7 @@ 218 #include <io.h> 219 #endif 220 221-#if defined(_MSC_VER) 222+#if defined(_MSC_VER) || defined(__MINGW32__) 223 #include <io.h> 224 #ifndef STDIN_FILENO 225 # define STDIN_FILENO 0 226diff --git a/lib/Target/GNULDBackend.cpp b/lib/Target/GNULDBackend.cpp 227index 12a2100..3e00c12 100644 228--- a/lib/Target/GNULDBackend.cpp 229+++ b/lib/Target/GNULDBackend.cpp 230@@ -37,6 +37,10 @@ 231 #include <mcld/LD/StubFactory.h> 232 #include <mcld/Object/ObjectBuilder.h> 233 234+#if defined(_WIN32) 235+ #define bzero(b,l) (memset((b), 0, (l)), (void)0) 236+#endif 237+ 238 using namespace mcld; 239 240 //===--------------------------------------------------------------------===// 241diff --git a/lib/Target/Mips/MipsLDBackend.cpp b/lib/Target/Mips/MipsLDBackend.cpp 242index 3db1c47..ae34f50 100644 243--- a/lib/Target/Mips/MipsLDBackend.cpp 244+++ b/lib/Target/Mips/MipsLDBackend.cpp 245@@ -27,6 +27,10 @@ 246 #include <mcld/Target/OutputRelocSection.h> 247 #include <mcld/Object/ObjectBuilder.h> 248 249+#if defined(_WIN32) 250+ #define bzero(b,l) (memset((b), 0, (l)), (void)0) 251+#endif 252+ 253 using namespace mcld; 254 255 //===----------------------------------------------------------------------===// 256diff --git a/tools/llvm-mcld/llvm-mcld.cpp b/tools/llvm-mcld/llvm-mcld.cpp 257index 98bdcad..81a7f55 100644 258--- a/tools/llvm-mcld/llvm-mcld.cpp 259+++ b/tools/llvm-mcld/llvm-mcld.cpp 260@@ -47,6 +47,19 @@ 261 # include <unistd.h> 262 #endif 263 264+#if defined(_MSC_VER) || defined(__MINGW32__) 265+#include <io.h> 266+#ifndef STDIN_FILENO 267+# define STDIN_FILENO 0 268+#endif 269+#ifndef STDOUT_FILENO 270+# define STDOUT_FILENO 1 271+#endif 272+#ifndef STDERR_FILENO 273+# define STDERR_FILENO 2 274+#endif 275+#endif 276+ 277 using namespace llvm; 278 279 #ifdef ENABLE_UNITTEST 280-- 2811.8.1.3 282 283