Support for building V8 with MinGW
authorsgjesse@chromium.org <sgjesse@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 9 Feb 2009 15:06:07 +0000 (15:06 +0000)
committersgjesse@chromium.org <sgjesse@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 9 Feb 2009 15:06:07 +0000 (15:06 +0000)
V8 can now be build with MinGW. It still fails the following four tests in debug mode:

  mjsunit/parse-int-float
  mjsunit/mirror-array.js
  mjsunit/integer-to-string.js
  mjsunit/regress/regress-114.js

Building with MinGW has been tested with version 5.1.4 using GCC 3.4.5.

In addition to supporting MinGW this change also makes it more explicit which targets needs to link with which libraries.

BUG=64
Review URL: http://codereview.chromium.org/20177

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1240 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

SConstruct
src/cpu-ia32.cc
src/dtoa-config.c
src/globals.h
src/platform-win32.cc
src/platform.h
tools/visual_studio/d8.vcproj
tools/visual_studio/v8_cctest.vcproj
tools/visual_studio/v8_mksnapshot.vcproj
tools/visual_studio/v8_process_sample.vcproj
tools/visual_studio/v8_shell_sample.vcproj

index 40da821..1aefee8 100644 (file)
@@ -42,10 +42,8 @@ LIBRARY_FLAGS = {
   },
   'gcc': {
     'all': {
-      'DIALECTFLAGS': ['-ansi'],
       'CCFLAGS':      ['$DIALECTFLAGS', '$WARNINGFLAGS'],
       'CXXFLAGS':     ['$CCFLAGS', '-fno-rtti', '-fno-exceptions'],
-      'LIBS':         ['pthread']
     },
     'mode:debug': {
       'CCFLAGS':      ['-g', '-O0'],
@@ -54,8 +52,18 @@ LIBRARY_FLAGS = {
     'mode:release': {
       'CCFLAGS':      ['-O3', '-fomit-frame-pointer', '-fdata-sections', '-ffunction-sections']
     },
+    'os:linux': {
+      'CCFLAGS':      ['-ansi'],
+    },
+    'os:macos': {
+      'CCFLAGS':      ['-ansi'],
+    },
     'os:freebsd': {
-      'LIBS':         ['execinfo']
+      'CCFLAGS':      ['-ansi'],
+    },
+    'os:win32': {
+      'CCFLAGS':      ['-DWIN32'],
+      'CXXFLAGS':     ['-DWIN32'],
     },
     'wordsize:64': {
       'CCFLAGS':      ['-m32'],
@@ -109,6 +117,9 @@ V8_EXTRA_FLAGS = {
     'arch:arm': {
       'CPPDEFINES':   ['ARM']
     },
+    'os:win32': {
+      'WARNINGFLAGS': ['-Wno-long-long']
+    },
     'disassembler:on': {
       'CPPDEFINES':   ['ENABLE_DISASSEMBLER']
     }
@@ -175,6 +186,18 @@ CCTEST_EXTRA_FLAGS = {
     'all': {
       'LIBPATH': [abspath('.')]
     },
+    'os:linux': {
+      'LIBS':         ['pthread'],
+    },
+    'os:macos': {
+      'LIBS':         ['pthread'],
+    },
+    'os:freebsd': {
+      'LIBS':         ['execinfo', 'pthread']
+    },
+    'os:win32': {
+      'LIBS': ['winmm']
+    },
     'wordsize:64': {
       'CCFLAGS':      ['-m32'],
       'LINKFLAGS':    ['-m32']
@@ -182,7 +205,8 @@ CCTEST_EXTRA_FLAGS = {
   },
   'msvc': {
     'all': {
-      'CPPDEFINES': ['_HAS_EXCEPTIONS=0']
+      'CPPDEFINES': ['_HAS_EXCEPTIONS=0'],
+      'LIBS': ['winmm']
     },
     'library:shared': {
       'CPPDEFINES': ['USING_V8_SHARED']
@@ -198,11 +222,19 @@ SAMPLE_FLAGS = {
   },
   'gcc': {
     'all': {
-      'LIBS': ['pthread'],
       'LIBPATH': ['.']
     },
+    'os:linux': {
+      'LIBS':         ['pthread'],
+    },
+    'os:macos': {
+      'LIBS':         ['pthread'],
+    },
     'os:freebsd': {
-      'LIBS':         ['execinfo']
+      'LIBS':         ['execinfo', 'pthread']
+    },
+    'os:win32': {
+      'LIBS':         ['winmm']
     },
     'wordsize:64': {
       'CCFLAGS':      ['-m32'],
@@ -219,6 +251,7 @@ SAMPLE_FLAGS = {
     'all': {
       'CCFLAGS': ['/nologo'],
       'LINKFLAGS': ['/nologo'],
+      'LIBS': ['winmm']
     },
     'library:shared': {
       'CPPDEFINES': ['USING_V8_SHARED']
@@ -254,8 +287,25 @@ D8_FLAGS = {
   'gcc': {
     'console:readline': {
       'LIBS': ['readline']
-    }
+    },
+    'os:linux': {
+      'LIBS': ['pthread'],
+    },
+    'os:macos': {
+      'LIBS': ['pthread'],
+    },
+    'os:freebsd': {
+      'LIBS': ['pthread'],
+    },
+    'os:win32': {
+      'LIBS': ['winmm'],
+    },
   },
+  'msvc': {
+    'all': {
+      'LIBS': ['winmm']
+    }
+  }
 }
 
 
index d61a548..030dbde 100644 (file)
@@ -51,7 +51,7 @@ void CPU::FlushICache(void* start, size_t size) {
 
 
 void CPU::DebugBreak() {
-#ifdef WIN32
+#ifdef _MSC_VER
   // To avoid Visual Studio runtime support the following code can be used
   // instead
   // __asm { int 3 }
index eb2c955..9fcd0dd 100644 (file)
 #include "stdlib.h"
 #endif
 
+/* For MinGW, turn on __NO_ISOCEXT so that its strtod doesn't get added */
+#ifdef __MINGW32__
+#define __NO_ISOCEXT
+#endif  /* __MINGW32__ */
+
 /* Make sure we use the David M. Gay version of strtod(). On Linux, we
  * cannot use the same name (maybe the function does not have weak
  * linkage?). */
index 2b5f3ac..dc87ac8 100644 (file)
 
 // -----------------------------------------------------------------------------
 // Types
-// Windows is missing the stdint.h header file. Instead we define standard
-// integer types for Windows here.
+// Visual Studio C++ is missing the stdint.h header file. Instead we define
+// standard integer types for Windows here.
 
-#ifdef WIN32
+#ifdef _MSC_VER
 typedef signed char int8_t;
 typedef unsigned char uint8_t;
 typedef short int16_t;  // NOLINT
@@ -42,9 +42,9 @@ typedef int int32_t;
 typedef unsigned int uint32_t;
 typedef __int64 int64_t;
 typedef unsigned __int64 uint64_t;
-#else
+#else  // _MSC_VER
 #include <stdint.h>  // for intptr_t
-#endif
+#endif  // _MSC_VER
 
 
 namespace v8 { namespace internal {
@@ -464,8 +464,10 @@ F FUNCTION_CAST(Address addr) {
 #define TRACK_MEMORY(name)
 #endif
 
-// define used for helping GCC to make better inlining.
-#ifdef __GNUC__
+// define used for helping GCC to make better inlining. Don't bother for debug
+// builds. On GCC 3.4.5 using __attribute__((always_inline)) causes compilation
+// errors in debug build.
+#if defined(__GNUC__) && !defined(DEBUG)
 #if (__GNUC__ >= 4)
 #define INLINE(header) inline header  __attribute__((always_inline))
 #else
index 71ba8ee..599e661 100644 (file)
 
 #include <time.h>  // For LocalOffset() implementation.
 #include <mmsystem.h>  // For timeGetTime().
+#ifndef __MINGW32__
 #include <dbghelp.h>  // For SymLoadModule64 and al.
+#endif  // __MINGW32__
+#include <limits.h>  // For INT_MAX and al.
 #include <tlhelp32.h>  // For Module32First and al.
 
 // These additional WIN32 includes have to be right here as the #undef's below
@@ -67,8 +70,6 @@
 #include <process.h>  // for _beginthreadex()
 #include <stdlib.h>
 
-#pragma comment(lib, "winmm.lib")  // force linkage with winmm.
-
 #undef VOID
 #undef DELETE
 #undef IN
 
 #include "platform.h"
 
-// Extra POSIX/ANSI routines for Win32. Please refer to The Open Group Base
-// Specification for specification of the correct semantics for these
-// functions.
+// Extra POSIX/ANSI routines for Win32 when when using Visual Studio C++. Please
+// refer to The Open Group Base Specification for specification of the correct
+// semantics for these functions.
 // (http://www.opengroup.org/onlinepubs/000095399/)
+#ifdef _MSC_VER
 
-// Test for finite value - usually defined in math.h
 namespace v8 {
 namespace internal {
 
+// Test for finite value - usually defined in math.h
 int isfinite(double x) {
   return _finite(x);
 }
@@ -152,19 +154,61 @@ int signbit(double x) {
 }
 
 
-// Generate a pseudo-random number in the range 0-2^31-1. Usually
-// defined in stdlib.h
-int random() {
-  return rand();
-}
-
-
 // Case-insensitive bounded string comparisons. Use stricmp() on Win32. Usually
 // defined in strings.h.
 int strncasecmp(const char* s1, const char* s2, int n) {
   return _strnicmp(s1, s2, n);
 }
 
+#endif  // _MSC_VER
+
+
+// Extra functions for MinGW. Most of these are the _s functions which are in
+// the Microsoft Visual Studio C++ CRT.
+#ifdef __MINGW32__
+
+int localtime_s(tm* out_tm, const time_t* time) {
+  tm* posix_local_time_struct = localtime(time);
+  if (posix_local_time_struct == NULL) return 1;
+  *out_tm = *posix_local_time_struct;
+  return 0;
+}
+
+
+// Not sure this the correct interpretation of _mkgmtime
+time_t _mkgmtime(tm* timeptr) {
+  return mktime(timeptr);
+}
+
+
+int fopen_s(FILE** pFile, const char* filename, const char* mode) {
+  *pFile = fopen(filename, mode);
+  return *pFile != NULL ? 0 : 1;
+}
+
+
+int _vsnprintf_s(char* buffer, size_t sizeOfBuffer, size_t count,
+                 const char* format, va_list argptr) {
+  return _vsnprintf(buffer, sizeOfBuffer, format, argptr);
+}
+#define _TRUNCATE 0
+
+
+int strncpy_s(char* strDest, size_t numberOfElements,
+              const char* strSource, size_t count) {
+  strncpy(strDest, strSource, count);
+  return 0;
+}
+
+#endif  // __MINGW32__
+
+// Generate a pseudo-random number in the range 0-2^31-1. Usually
+// defined in stdlib.h. Missing in both Microsoft Visual Studio C++ and MinGW.
+int random() {
+  return rand();
+}
+
+
 namespace v8 { namespace internal {
 
 double ceiling(double x) {
@@ -206,7 +250,7 @@ class Time {
 
  private:
   // Constants for time conversion.
-  static const int64_t kTimeEpoc = 116444736000000000;
+  static const int64_t kTimeEpoc = 116444736000000000LL;
   static const int64_t kTimeScaler = 10000;
   static const int64_t kMsPerMinute = 60000;
 
@@ -807,9 +851,11 @@ void OS::Sleep(int milliseconds) {
 
 void OS::Abort() {
   if (!IsDebuggerPresent()) {
+#ifdef _MSC_VER
     // Make the MSVCRT do a silent abort.
     _set_abort_behavior(0, _WRITE_ABORT_MSG);
     _set_abort_behavior(0, _CALL_REPORTFAULT);
+#endif  // _MSC_VER
     abort();
   } else {
     DebugBreak();
@@ -818,7 +864,11 @@ void OS::Abort() {
 
 
 void OS::DebugBreak() {
+#ifdef _MSC_VER
   __debugbreak();
+#else
+  ::DebugBreak();
+#endif
 }
 
 
@@ -901,6 +951,8 @@ Win32MemoryMappedFile::~Win32MemoryMappedFile() {
 #define VOID void
 #endif
 
+// DbgHelp isn't supported on MinGW yet
+#ifndef __MINGW32__
 // DbgHelp.h functions.
 typedef BOOL (__stdcall *DLL_FUNC_TYPE(SymInitialize))(IN HANDLE hProcess,
                                                        IN PSTR UserSearchPath,
@@ -1223,10 +1275,19 @@ int OS::StackWalk(OS::StackFrame* frames, int frames_size) {
 // Restore warnings to previous settings.
 #pragma warning(pop)
 
+#else  // __MINGW32__
+void OS::LogSharedLibraryAddresses() { }
+int OS::StackWalk(OS::StackFrame* frames, int frames_size) { return 0; }
+#endif  // __MINGW32__
+
 
 double OS::nan_value() {
+#ifdef _MSC_VER
   static const __int64 nanval = 0xfff8000000000000;
   return *reinterpret_cast<const double*>(&nanval);
+#else  // _MSC_VER
+  return NAN;
+#endif  // _MSC_VER
 }
 
 
@@ -1267,7 +1328,7 @@ bool VirtualMemory::Commit(void* address, size_t size, bool executable) {
 
 bool VirtualMemory::Uncommit(void* address, size_t size) {
   ASSERT(IsReserved());
-  return VirtualFree(address, size, MEM_DECOMMIT) != NULL;
+  return VirtualFree(address, size, MEM_DECOMMIT) != FALSE;
 }
 
 
index 0e42ba6..6165bd9 100644 (file)
 #ifndef V8_PLATFORM_H_
 #define V8_PLATFORM_H_
 
+// Windows specific stuff.
 #ifdef WIN32
 
+// Microsoft Visual C++ specific stuff.
+#ifdef _MSC_VER
+
 enum {
   FP_NAN,
   FP_INFINITE,
@@ -66,11 +70,26 @@ int isgreater(double x, double y);
 int fpclassify(double x);
 int signbit(double x);
 
+int strncasecmp(const char* s1, const char* s2, int n);
+
+#endif  // _MSC_VER
+
+// MinGW specific stuff.
+#ifdef __MINGW32__
+
+// Needed for va_list.
+#include <stdarg.h>
+
+#endif  // __MINGW32__
+
+// Random is missing on both Visual Studio and MinGW.
 int random();
 
-int strncasecmp(const char* s1, const char* s2, int n);
+#endif  // WIN32
 
-#else
+// GCC specific stuff
+#ifdef __GNUC__
+#define __GNUC_VERSION__ (__GNUC__ * 10000 + __GNUC_MINOR__ * 100)
 
 // Unfortunately, the INFINITY macro cannot be used with the '-pedantic'
 // warning flag and certain versions of GCC due to a bug:
@@ -78,17 +97,13 @@ int strncasecmp(const char* s1, const char* s2, int n);
 // For now, we use the more involved template-based version from <limits>, but
 // only when compiling with GCC versions affected by the bug (2.96.x - 4.0.x)
 // __GNUC_PREREQ is not defined in GCC for Mac OS X, so we define our own macro
-#if defined(__GNUC__)
-#define __GNUC_VERSION__ (__GNUC__ * 10000 + __GNUC_MINOR__ * 100)
-#endif
-
 #if __GNUC_VERSION__ >= 29600 && __GNUC_VERSION__ < 40100
 #include <limits>
 #undef INFINITY
 #define INFINITY std::numeric_limits<double>::infinity()
 #endif
 
-#endif  // WIN32
+#endif  // __GNUC__
 
 namespace v8 { namespace internal {
 
index 7f91ba7..0f71e8e 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="Windows-1252"?>
 <VisualStudioProject
        ProjectType="Visual C++"
-       Version="8,00"
+       Version="8.00"
        Name="d8"
        ProjectGUID="{7E4C7D2D-A4B9-40B9-8192-22654E626F6C}"
        RootNamespace="d8"
@@ -49,6 +49,7 @@
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="winmm.lib"
                        />
                        <Tool
                                Name="VCALinkTool"
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="winmm.lib"
                        />
                        <Tool
                                Name="VCALinkTool"
index d62b887..e748dc0 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="Windows-1252"?>
 <VisualStudioProject
        ProjectType="Visual C++"
-       Version="8,00"
+       Version="8.00"
        Name="v8_cctest"
        ProjectGUID="{97ECC711-7430-4FC4-90FD-004DA880E72A}"
        RootNamespace="v8_cctest"
@@ -49,6 +49,7 @@
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="winmm.lib"
                        />
                        <Tool
                                Name="VCALinkTool"
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="winmm.lib"
                        />
                        <Tool
                                Name="VCALinkTool"
index daa73f7..7bc5eed 100644 (file)
@@ -49,6 +49,7 @@
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="winmm.lib"
                        />
                        <Tool
                                Name="VCALinkTool"
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="winmm.lib"
                        />
                        <Tool
                                Name="VCALinkTool"
index 219b0cd..f9722f8 100644 (file)
@@ -49,6 +49,7 @@
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="winmm.lib"
                        />
                        <Tool
                                Name="VCALinkTool"
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="winmm.lib"
                        />
                        <Tool
                                Name="VCALinkTool"
index 28249d8..49d4fd9 100644 (file)
@@ -49,6 +49,7 @@
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="winmm.lib"
                        />
                        <Tool
                                Name="VCALinkTool"
                        />
                        <Tool
                                Name="VCLinkerTool"
+                               AdditionalDependencies="winmm.lib"
                        />
                        <Tool
                                Name="VCALinkTool"