From 3925f4ff63d05b2c4cf2c1543152c8ecfa0beb10 Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Tue, 11 Oct 2016 07:06:55 +0000
Subject: [PATCH 01/14] hooks: convert pre-commit to POSIX syntax

---
 hooks/pre-commit | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/hooks/pre-commit b/hooks/pre-commit
index ec2b5b77e..04fdaf8ec 100755
--- a/hooks/pre-commit
+++ b/hooks/pre-commit
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 
 # Enforce citra's whitespace policy
 git config --local core.whitespace tab-in-indent,trailing-space
@@ -32,7 +32,7 @@ for f in $(git diff --name-only --diff-filter=ACMRTUXB --cached); do
     if ! echo "$f" | egrep -q "^src/"; then
         continue
     fi
-    d=$(diff -u "$f" <(clang-format "$f"))
+    d=$(clang-format "$f" | diff -u "$f" -)
     if ! [ -z "$d" ]; then
         echo "!!! $f not compliant to coding style, here is the fix:"
         echo "$d"
@@ -40,4 +40,4 @@ for f in $(git diff --name-only --diff-filter=ACMRTUXB --cached); do
     fi
 done
 
-exit "$fail"
+exit "${fail-0}"

From 1410bd3bd03f057a96348c9167bb7f5c0cb3b0de Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Mon, 10 Oct 2016 02:25:19 +0000
Subject: [PATCH 02/14] common: define routines to set thread name on more BSDs

src/common/thread.cpp:123:5: error: use of undeclared identifier 'pthread_setname_np'
    pthread_setname_np(pthread_self(), szThreadName);
    ^
1 error generated.
---
 src/common/thread.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/common/thread.cpp b/src/common/thread.cpp
index 6e7b39b9a..a4f5fa336 100644
--- a/src/common/thread.cpp
+++ b/src/common/thread.cpp
@@ -8,7 +8,7 @@
 #elif defined(_WIN32)
 #include <Windows.h>
 #else
-#if defined(BSD4_4) || defined(__OpenBSD__)
+#if defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
 #include <pthread_np.h>
 #else
 #include <pthread.h>
@@ -117,8 +117,10 @@ void SwitchCurrentThread() {
 void SetCurrentThreadName(const char* szThreadName) {
 #ifdef __APPLE__
     pthread_setname_np(szThreadName);
-#elif defined(__OpenBSD__)
+#elif defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
     pthread_set_name_np(pthread_self(), szThreadName);
+#elif defined(__NetBSD__)
+    pthread_setname_np(pthread_self(), "%s", (void*)szThreadName);
 #else
     pthread_setname_np(pthread_self(), szThreadName);
 #endif

From 8ce1ec7ffa6b912dfa776d6923a7af18a7761f67 Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Mon, 10 Oct 2016 02:33:41 +0000
Subject: [PATCH 03/14] common: only FreeBSD has thread affinity compatible
 with Linux

src/common/thread.cpp:90:5: error: unknown type name 'cpu_set_t'; did you mean 'cpuset_t'?
    cpu_set_t cpu_set;
    ^~~~~~~~~
    cpuset_t
/usr/include/sys/_cpuset.h:48:24: note: 'cpuset_t' declared here
typedef struct _cpuset cpuset_t;
                       ^
1 error generated.
---
 src/common/thread.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/common/thread.cpp b/src/common/thread.cpp
index a4f5fa336..9bb2f4e1d 100644
--- a/src/common/thread.cpp
+++ b/src/common/thread.cpp
@@ -19,6 +19,10 @@
 #include <unistd.h>
 #endif
 
+#ifdef __FreeBSD__
+#define cpu_set_t cpuset_t
+#endif
+
 namespace Common {
 
 int CurrentThreadId() {
@@ -86,7 +90,7 @@ void SetCurrentThreadName(const char* szThreadName) {
 void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) {
 #ifdef __APPLE__
     thread_policy_set(pthread_mach_thread_np(thread), THREAD_AFFINITY_POLICY, (integer_t*)&mask, 1);
-#elif (defined __linux__ || defined BSD4_4) && !(defined ANDROID)
+#elif (defined __linux__ || defined __FreeBSD__) && !(defined ANDROID)
     cpu_set_t cpu_set;
     CPU_ZERO(&cpu_set);
 

From 51f92f0e4e688104daf9abdddbd1fd2d6bcef30d Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Mon, 10 Oct 2016 02:47:42 +0000
Subject: [PATCH 04/14] common: stat64 is non-standard, hide on a random Unix

src/common/file_util.cpp:79:19: error: variable has incomplete type 'struct stat64'
    struct stat64 file_info;
                  ^
src/common/file_util.cpp:79:12: note: forward declaration of 'stat64'
    struct stat64 file_info;
           ^
src/common/file_util.cpp:99:19: error: variable has incomplete type 'struct stat64'
    struct stat64 file_info;
                  ^
src/common/file_util.cpp:99:12: note: forward declaration of 'stat64'
    struct stat64 file_info;
           ^
src/common/file_util.cpp:342:19: error: variable has incomplete type 'struct stat64'
    struct stat64 buf;
                  ^
src/common/file_util.cpp:342:12: note: forward declaration of 'stat64'
    struct stat64 buf;
           ^
src/common/file_util.cpp:359:19: error: variable has incomplete type 'struct stat64'
    struct stat64 buf;
                  ^
src/common/file_util.cpp:359:12: note: forward declaration of 'stat64'
    struct stat64 buf;
           ^
4 errors generated.
---
 src/common/file_util.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 14cbcac6b..c1838efb9 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -52,7 +52,7 @@
 #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
 #endif
 
-#ifdef BSD4_4
+#if !defined(_WIN32) && !defined(__APPLE__) && !defined(__GLIBC__) && !defined(__UCLIBC__)
 #define stat64 stat
 #define fstat64 fstat
 #endif

From 26af2b644ce634c383f27b66b9d291a34d80ab30 Mon Sep 17 00:00:00 2001
From: "Anthony J. Bentley" <anthony@anjbe.name>
Date: Sun, 17 Jul 2016 04:30:00 -0600
Subject: [PATCH 05/14] common: convert to standard stat()/fstat() interfaces

Most modern Unix environments use 64-bit off_t by default: OpenBSD,
FreeBSD, OS X, and Linux libc implementations such as Musl.

glibc is the lone exception; it can default to 32 bits but this is
configurable by setting _FILE_OFFSET_BITS.

Avoiding the stat64()/fstat64() interfaces is desirable because they
are nonstandard and not implemented on many systems (including
OpenBSD and FreeBSD), and using 64 bits for stat()/fstat() is either
the default or trivial to set up.
---
 CMakeLists.txt           |  9 +++++++++
 src/common/file_util.cpp | 25 ++++++++++---------------
 2 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c7a24f04f..df7fe0cba 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -105,6 +105,15 @@ else()
     set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/DEBUG /INCREMENTAL:NO /OPT:REF,ICF" CACHE STRING "" FORCE)
 endif()
 
+# Set file offset size to 64 bits.
+#
+# On modern Unixes, this is typically already the case. The lone exception is
+# glibc, which may default to 32 bits. glibc allows this to be configured
+# by setting _FILE_OFFSET_BITS.
+if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+    add_definitions(-D_FILE_OFFSET_BITS=64)
+endif()
+
 add_definitions(-DSINGLETHREADED)
 # CMake seems to only define _DEBUG on Windows
 set_property(DIRECTORY APPEND PROPERTY
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index c1838efb9..407ed047a 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -23,8 +23,8 @@
 #define fseeko _fseeki64
 #define ftello _ftelli64
 #define atoll _atoi64
-#define stat64 _stat64
-#define fstat64 _fstat64
+#define stat _stat64
+#define fstat _fstat64
 #define fileno _fileno
 #else
 #ifdef __APPLE__
@@ -52,11 +52,6 @@
 #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
 #endif
 
-#if !defined(_WIN32) && !defined(__APPLE__) && !defined(__GLIBC__) && !defined(__UCLIBC__)
-#define stat64 stat
-#define fstat64 fstat
-#endif
-
 // This namespace has various generic functions related to files and paths.
 // The code still needs a ton of cleanup.
 // REMEMBER: strdup considered harmful!
@@ -76,7 +71,7 @@ static void StripTailDirSlashes(std::string& fname) {
 
 // Returns true if file filename exists
 bool Exists(const std::string& filename) {
-    struct stat64 file_info;
+    struct stat file_info;
 
     std::string copy(filename);
     StripTailDirSlashes(copy);
@@ -88,7 +83,7 @@ bool Exists(const std::string& filename) {
 
     int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info);
 #else
-    int result = stat64(copy.c_str(), &file_info);
+    int result = stat(copy.c_str(), &file_info);
 #endif
 
     return (result == 0);
@@ -96,7 +91,7 @@ bool Exists(const std::string& filename) {
 
 // Returns true if filename is a directory
 bool IsDirectory(const std::string& filename) {
-    struct stat64 file_info;
+    struct stat file_info;
 
     std::string copy(filename);
     StripTailDirSlashes(copy);
@@ -108,7 +103,7 @@ bool IsDirectory(const std::string& filename) {
 
     int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info);
 #else
-    int result = stat64(copy.c_str(), &file_info);
+    int result = stat(copy.c_str(), &file_info);
 #endif
 
     if (result < 0) {
@@ -339,11 +334,11 @@ u64 GetSize(const std::string& filename) {
         return 0;
     }
 
-    struct stat64 buf;
+    struct stat buf;
 #ifdef _WIN32
     if (_wstat64(Common::UTF8ToUTF16W(filename).c_str(), &buf) == 0)
 #else
-    if (stat64(filename.c_str(), &buf) == 0)
+    if (stat(filename.c_str(), &buf) == 0)
 #endif
     {
         LOG_TRACE(Common_Filesystem, "%s: %lld", filename.c_str(), (long long)buf.st_size);
@@ -356,8 +351,8 @@ u64 GetSize(const std::string& filename) {
 
 // Overloaded GetSize, accepts file descriptor
 u64 GetSize(const int fd) {
-    struct stat64 buf;
-    if (fstat64(fd, &buf) != 0) {
+    struct stat buf;
+    if (fstat(fd, &buf) != 0) {
         LOG_ERROR(Common_Filesystem, "GetSize: stat failed %i: %s", fd, GetLastErrorMsg());
         return 0;
     }

From 3d801be97d82b69ff800f939dd92d97ff012a868 Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Mon, 10 Oct 2016 03:26:27 +0000
Subject: [PATCH 06/14] common: some FreeBSD headers are incomplete to avoid
 namespace pollution

In file included from src/common/x64/cpu_detect.cpp:16:
/usr/include/machine/cpufunc.h:66:17: error: unknown type name 'u_int'
static __inline u_int
                ^
/usr/include/machine/cpufunc.h:67:6: error: unknown type name 'u_int'
bsfl(u_int mask)
     ^
/usr/include/machine/cpufunc.h:69:2: error: unknown type name 'u_int'
        u_int   result;
        ^
/usr/include/machine/cpufunc.h:75:17: error: unknown type name 'u_long'; did you mean 'long'?
static __inline u_long
                ^
/usr/include/machine/cpufunc.h:76:6: error: unknown type name 'u_long'; did you mean 'long'?
bsfq(u_long mask)
     ^
/usr/include/machine/cpufunc.h:78:2: error: use of undeclared identifier 'u_long'; did you mean
      'long'?
        u_long  result;
        ^
[...]
---
 src/common/x64/cpu_detect.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp
index 6ddf9b70c..4abbdb96a 100644
--- a/src/common/x64/cpu_detect.cpp
+++ b/src/common/x64/cpu_detect.cpp
@@ -13,8 +13,10 @@ namespace Common {
 #ifndef _MSC_VER
 
 #ifdef __FreeBSD__
-#include <machine/cpufunc.h>
+// clang-format off
 #include <sys/types.h>
+#include <machine/cpufunc.h>
+// clang-format on
 #endif
 
 static inline void __cpuidex(int info[4], int function_id, int subfunction_id) {

From ddd8709e14ba5f954ee4855d4bb55e4b31b0c3ec Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Mon, 10 Oct 2016 16:01:55 +0000
Subject: [PATCH 07/14] common: use system CPUID routine on DragonFly as well

---
 src/common/x64/cpu_detect.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp
index 4abbdb96a..370ae2c80 100644
--- a/src/common/x64/cpu_detect.cpp
+++ b/src/common/x64/cpu_detect.cpp
@@ -12,7 +12,7 @@ namespace Common {
 
 #ifndef _MSC_VER
 
-#ifdef __FreeBSD__
+#if defined(__DragonFly__) || defined(__FreeBSD__)
 // clang-format off
 #include <sys/types.h>
 #include <machine/cpufunc.h>
@@ -20,7 +20,7 @@ namespace Common {
 #endif
 
 static inline void __cpuidex(int info[4], int function_id, int subfunction_id) {
-#ifdef __FreeBSD__
+#if defined(__DragonFly__) || defined(__FreeBSD__)
     // Despite the name, this is just do_cpuid() with ECX as second input.
     cpuid_count((u_int)function_id, (u_int)subfunction_id, (u_int*)info);
 #else

From 94d23b480e0343869582f13ae3ef27a74a60465b Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Mon, 10 Oct 2016 06:14:58 +0000
Subject: [PATCH 08/14] common: use system bswap* functions on more BSDs

---
 src/common/swap.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/common/swap.h b/src/common/swap.h
index e241c9f73..d94cbe6b2 100644
--- a/src/common/swap.h
+++ b/src/common/swap.h
@@ -21,7 +21,8 @@
 #include <cstdlib>
 #elif defined(__linux__)
 #include <byteswap.h>
-#elif defined(__FreeBSD__)
+#elif defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) ||                     \
+    defined(__NetBSD__) || defined(__OpenBSD__)
 #include <sys/endian.h>
 #endif
 #include <cstring>
@@ -101,7 +102,9 @@ inline __attribute__((always_inline)) u32 swap32(u32 _data) {
 inline __attribute__((always_inline)) u64 swap64(u64 _data) {
     return __builtin_bswap64(_data);
 }
-#elif __FreeBSD__
+#elif defined(__Bitrig__) || defined(__OpenBSD__)
+// swap16, swap32, swap64 are left as is
+#elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__)
 inline u16 swap16(u16 _data) {
     return bswap16(_data);
 }

From 702439b5191589c7dbbc74afa41a79a85c3664ef Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Mon, 10 Oct 2016 16:36:55 +0000
Subject: [PATCH 09/14] core: some errno values are uncommon on Unix

src/core/hle/service/soc_u.cpp:107:6: error: 'ENODATA' was not declared in this scope
     {ENODATA, 43},
      ^
src/core/hle/service/soc_u.cpp:117:6: error: 'ENOSR' was not declared in this scope
     {ENOSR, 53},
      ^
src/core/hle/service/soc_u.cpp:118:6: error: 'ENOSTR' was not declared in this scope
     {ENOSTR, 54},
      ^
src/core/hle/service/soc_u.cpp:139:6: error: 'ETIME' was not declared in this scope
     {ETIME, 75},
      ^
---
 src/core/hle/service/soc_u.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/core/hle/service/soc_u.cpp b/src/core/hle/service/soc_u.cpp
index 4279b67fb..46b75db25 100644
--- a/src/core/hle/service/soc_u.cpp
+++ b/src/core/hle/service/soc_u.cpp
@@ -104,7 +104,9 @@ static const std::unordered_map<int, int> error_map = {{
     {ERRNO(ENETUNREACH), 40},
     {ENFILE, 41},
     {ERRNO(ENOBUFS), 42},
+#ifdef ENODATA
     {ENODATA, 43},
+#endif
     {ENODEV, 44},
     {ENOENT, 45},
     {ENOEXEC, 46},
@@ -114,8 +116,12 @@ static const std::unordered_map<int, int> error_map = {{
     {ENOMSG, 50},
     {ERRNO(ENOPROTOOPT), 51},
     {ENOSPC, 52},
+#ifdef ENOSR
     {ENOSR, 53},
+#endif
+#ifdef ENOSTR
     {ENOSTR, 54},
+#endif
     {ENOSYS, 55},
     {ERRNO(ENOTCONN), 56},
     {ENOTDIR, 57},
@@ -136,7 +142,9 @@ static const std::unordered_map<int, int> error_map = {{
     {ESPIPE, 72},
     {ESRCH, 73},
     {ERRNO(ESTALE), 74},
+#ifdef ETIME
     {ETIME, 75},
+#endif
     {ERRNO(ETIMEDOUT), 76},
 }};
 

From 50ce19b3ff64f26194009df73c456b9035d903c6 Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Mon, 10 Oct 2016 03:07:53 +0000
Subject: [PATCH 10/14] microprofile: unbreak on POSIX systems

In file included from src/common/microprofile.cpp:7:
In file included from src/./common/microprofile.h:23:
externals/microprofile/microprofile.h:830:5: error: use of undeclared identifier 'MP_BREAK'
    MP_ASSERT(t == nBegin);
    ^
externals/microprofile/microprofile.h:238:34: note: expanded from macro 'MP_ASSERT'
                                 ^
externals/microprofile/microprofile.h:831:5: error: use of undeclared identifier 'MP_BREAK'
    MP_ASSERT(nTimerIndex == (nToken&0x3fff));
    ^
externals/microprofile/microprofile.h:238:34: note: expanded from macro 'MP_ASSERT'
                                 ^
[...]
---
 externals/microprofile/microprofile.h   | 8 ++++----
 externals/microprofile/microprofileui.h | 1 +
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/externals/microprofile/microprofile.h b/externals/microprofile/microprofile.h
index d1ae0c1c2..30613b3b0 100644
--- a/externals/microprofile/microprofile.h
+++ b/externals/microprofile/microprofile.h
@@ -206,7 +206,7 @@ int64_t MicroProfileGetTick();
 #define MP_GETCURRENTTHREADID() GetCurrentThreadId()
 typedef uint32_t ThreadIdType;
 
-#elif defined(__linux__)
+#elif !defined(_WIN32)
 #include <unistd.h>
 #include <time.h>
 inline int64_t MicroProfileTicksPerSecondCpu()
@@ -510,7 +510,7 @@ typedef int MpSocket;
 #endif
 
 
-#if defined(__APPLE__) || defined(__linux__)
+#ifndef _WIN32
 typedef pthread_t MicroProfileThread;
 #elif defined(_WIN32)
 typedef HANDLE MicroProfileThread;
@@ -907,7 +907,7 @@ int64_t MicroProfileGetTick()
 
 typedef void* (*MicroProfileThreadFunc)(void*);
 
-#if defined(__APPLE__) || defined(__linux__)
+#ifndef _WIN32
 typedef pthread_t MicroProfileThread;
 void MicroProfileThreadStart(MicroProfileThread* pThread, MicroProfileThreadFunc Func)
 {
@@ -959,7 +959,7 @@ inline void MicroProfileThreadJoin(MicroProfileThread* pThread)
 #define MP_INVALID_SOCKET(f) (f == INVALID_SOCKET)
 #endif
 
-#if defined(__APPLE__)
+#ifndef _WIN32
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <fcntl.h>
diff --git a/externals/microprofile/microprofileui.h b/externals/microprofile/microprofileui.h
index 45bec8af6..66a73abc5 100644
--- a/externals/microprofile/microprofileui.h
+++ b/externals/microprofile/microprofileui.h
@@ -172,6 +172,7 @@ MICROPROFILEUI_API void MicroProfileCustomGroupAddTimer(const char* pCustomName,
 #ifdef _WIN32
 #define snprintf _snprintf
 #endif
+#include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <math.h>

From 48b6c98d31c176134a72fe59ff596afa1eef0d11 Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Mon, 10 Oct 2016 17:35:23 +0000
Subject: [PATCH 11/14] build: libc may not provide iconv() on Unix

/usr/bin/ld: ../common/libcommon.a(string_util.cpp.o): undefined reference to symbol 'libiconv_open'
---
 CMakeLists.txt | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index df7fe0cba..7e3f2ab8f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -169,14 +169,13 @@ endif()
 
 IF (APPLE)
     FIND_LIBRARY(COCOA_LIBRARY Cocoa)           # Umbrella framework for everything GUI-related
-    set(PLATFORM_LIBRARIES iconv ${COCOA_LIBRARY} ${IOKIT_LIBRARY} ${COREVIDEO_LIBRARY})
+    set(PLATFORM_LIBRARIES ${COCOA_LIBRARY} ${IOKIT_LIBRARY} ${COREVIDEO_LIBRARY})
 
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")
 ELSEIF(MINGW)
-    # GCC does not support codecvt, so use iconv instead
     # PSAPI is the Process Status API
-    set(PLATFORM_LIBRARIES winmm ws2_32 psapi iconv)
+    set(PLATFORM_LIBRARIES winmm ws2_32 psapi)
 
     # WSAPoll functionality doesn't exist before WinNT 6.x (Vista and up)
     add_definitions(-D_WIN32_WINNT=0x0600)
@@ -186,6 +185,14 @@ ELSE()
     set(PLATFORM_LIBRARIES rt)
 ENDIF (APPLE)
 
+# MINGW: GCC does not support codecvt, so use iconv instead
+if (UNIX OR MINGW)
+    find_library(ICONV_LIBRARY NAMES iconv)
+    if (ICONV_LIBRARY)
+        list(APPEND PLATFORM_LIBRARIES ${ICONV_LIBRARY})
+    endif()
+endif()
+
 if (ENABLE_QT)
     if (CITRA_USE_BUNDLED_QT)
         if (MSVC14 AND ARCHITECTURE_x86_64)

From 52da9de5c4855f0a89ae1019803327cba799b9e5 Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Mon, 10 Oct 2016 06:38:39 +0000
Subject: [PATCH 12/14] build: clock_gettime() is in libc on BSDs

---
 CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7e3f2ab8f..9fc4f5675 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -181,7 +181,7 @@ ELSEIF(MINGW)
     add_definitions(-D_WIN32_WINNT=0x0600)
 ELSEIF(WIN32)
     set(PLATFORM_LIBRARIES winmm ws2_32)
-ELSE()
+ELSEIF(CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU|SunOS)$")
     set(PLATFORM_LIBRARIES rt)
 ENDIF (APPLE)
 

From 2240cb2eb0178d16c7713b6084a7013e283e268b Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Mon, 10 Oct 2016 05:58:05 +0000
Subject: [PATCH 13/14] build: add default install for DragonFly, Solaris, etc.

---
 CMakeLists.txt              | 2 +-
 src/citra/CMakeLists.txt    | 2 +-
 src/citra_qt/CMakeLists.txt | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9fc4f5675..c2fbfefb8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -269,7 +269,7 @@ add_subdirectory(src)
 # http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
 # http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
 # http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html
-if(${CMAKE_SYSTEM_NAME} MATCHES "Linux|FreeBSD|OpenBSD|NetBSD")
+if(UNIX AND NOT APPLE)
     install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.desktop"
             DESTINATION "${CMAKE_INSTALL_PREFIX}/share/applications")
     install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.svg"
diff --git a/src/citra/CMakeLists.txt b/src/citra/CMakeLists.txt
index 43fa06b4e..f9c488a1a 100644
--- a/src/citra/CMakeLists.txt
+++ b/src/citra/CMakeLists.txt
@@ -23,7 +23,7 @@ if (MSVC)
 endif()
 target_link_libraries(citra ${PLATFORM_LIBRARIES} Threads::Threads)
 
-if(${CMAKE_SYSTEM_NAME} MATCHES "Linux|FreeBSD|OpenBSD|NetBSD")
+if(UNIX AND NOT APPLE)
     install(TARGETS citra RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
 endif()
 
diff --git a/src/citra_qt/CMakeLists.txt b/src/citra_qt/CMakeLists.txt
index b3c01ddd8..384875450 100644
--- a/src/citra_qt/CMakeLists.txt
+++ b/src/citra_qt/CMakeLists.txt
@@ -104,7 +104,7 @@ target_link_libraries(citra-qt core video_core audio_core common qhexedit)
 target_link_libraries(citra-qt ${OPENGL_gl_LIBRARY} ${CITRA_QT_LIBS})
 target_link_libraries(citra-qt ${PLATFORM_LIBRARIES} Threads::Threads)
 
-if(${CMAKE_SYSTEM_NAME} MATCHES "Linux|FreeBSD|OpenBSD|NetBSD")
+if(UNIX AND NOT APPLE)
     install(TARGETS citra-qt RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
 endif()
 

From 8b833d3a97712de3d547123bd4399ba8b530e2ac Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Mon, 10 Oct 2016 05:58:05 +0000
Subject: [PATCH 14/14] build: don't install freedesktop.org metadata for
 SDL2-only builds

Citra SDL2 doesn't have a launcher, and citra.desktop tries to execute
citra-qt which is N/A unless built with ENABLE_QT. Limiting installed
files to one of the options also makes it easier to split them into
separate non-conflicting packages downstream.
---
 CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c2fbfefb8..26dec8f86 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -269,7 +269,7 @@ add_subdirectory(src)
 # http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
 # http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
 # http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html
-if(UNIX AND NOT APPLE)
+if(ENABLE_QT AND UNIX AND NOT APPLE)
     install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.desktop"
             DESTINATION "${CMAKE_INSTALL_PREFIX}/share/applications")
     install(FILES "${CMAKE_SOURCE_DIR}/dist/citra.svg"