From 1d17725592ee9b391ff4bc74ddf488118d208cc6 Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Wed, 28 Dec 2016 13:18:04 +0000 Subject: [PATCH] Add FreeBSD support At the moment the platform specific code supports only FreeBSD as OpenBSD and NetBSD does not support the full path retrieval for an executable, we intentionally stop the compilation for those platforms. --- CMakeLists.txt | 4 ++-- src/network/TcpSocket.cpp | 1 + src/platform/linux.c | 19 ++++++++++++++++++- src/platform/platform.h | 2 +- src/platform/posix.c | 2 +- src/version.h | 3 +++ 6 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6db7eb280c..6e9c8ff088 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -326,7 +326,7 @@ endif (NOT DISABLE_HTTP_TWITCH) PKG_CHECK_MODULES(SPEEX REQUIRED speexdsp) -if (UNIX) +if (UNIX AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "BSD") # Include libdl for dlopen set(DLLIB dl) endif (UNIX) @@ -384,7 +384,7 @@ TARGET_LINK_LIBRARIES(${PROJECT} ${GLLIBS}) # if creating a static binary, precede libraries with -static, then name all the libs TARGET_LINK_LIBRARIES(${PROJECT} ${STATIC_START} ${SDL2LIBS} ${HTTPLIBS} ${NETWORKLIBS} ${SPEEX_LIBRARIES} ${DLLIB} ${REQUIREDLIBS} ${BREAKPAD_LIBS}) -if (APPLE OR STATIC) +if (APPLE OR STATIC OR ${CMAKE_SYSTEM_NAME} MATCHES "BSD") FIND_LIBRARY(ICONV_LIBRARIES NAMES iconv libiconv libiconv-2 c) TARGET_LINK_LIBRARIES(${PROJECT} ${ICONV_LIBRARIES}) endif (APPLE OR STATIC) diff --git a/src/network/TcpSocket.cpp b/src/network/TcpSocket.cpp index 1c7da18fe9..717d7d849b 100644 --- a/src/network/TcpSocket.cpp +++ b/src/network/TcpSocket.cpp @@ -44,6 +44,7 @@ #include #include #include + #include #include #include typedef int SOCKET; diff --git a/src/platform/linux.c b/src/platform/linux.c index 7322783e6b..7fb4511864 100644 --- a/src/platform/linux.c +++ b/src/platform/linux.c @@ -16,7 +16,13 @@ #include "../common.h" -#ifdef __LINUX__ +// Despite the name, this file contains support for more OSs besides Linux, provided the necessary ifdefs remain small. +// Otherwise, they should be spun off into their own files. +#if defined(__LINUX__) || defined(__FREEBSD__) + +#ifdef __FREEBSD__ +#include +#endif #include #include @@ -49,12 +55,23 @@ typedef enum { DT_NONE, DT_KDIALOG, DT_ZENITY } dialog_type; void platform_get_exe_path(utf8 *outPath, size_t outSize) { char exePath[MAX_PATH]; +#ifdef __LINUX__ ssize_t bytesRead; bytesRead = readlink("/proc/self/exe", exePath, MAX_PATH); if (bytesRead == -1) { log_fatal("failed to read /proc/self/exe"); } exePath[bytesRead - 1] = '\0'; +#elif __FREEBSD__ + size_t exeLen = sizeof(exePath); + const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; + if (sysctl(mib, 4, exePath, &exeLen, NULL, 0) == -1) { + log_fatal("failed to get process path"); + + } +#else +#error "Platform does not support full path exe retrieval" +#endif char *exeDelimiter = strrchr(exePath, *PATH_SEPARATOR); if (exeDelimiter == NULL) { diff --git a/src/platform/platform.h b/src/platform/platform.h index 6d636bda19..776fd39aa7 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -229,7 +229,7 @@ void core_init(); __declspec(dllexport) int StartOpenRCT(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow); #endif // __WINDOWS__ -#if defined(__LINUX__) || defined(__MACOSX__) +#if defined(__LINUX__) || defined(__MACOSX__) || defined(__FREEBSD__) void platform_posix_sub_user_data_path(char *buffer, size_t size, const char *homedir); void platform_posix_sub_resolve_openrct_data_path(utf8 *out, size_t size); #endif diff --git a/src/platform/posix.c b/src/platform/posix.c index f74eea3457..f4befe18d1 100644 --- a/src/platform/posix.c +++ b/src/platform/posix.c @@ -14,7 +14,7 @@ *****************************************************************************/ #pragma endregion -#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) +#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) #include #include diff --git a/src/version.h b/src/version.h index 4a2c22c3bc..db1271077b 100644 --- a/src/version.h +++ b/src/version.h @@ -48,6 +48,9 @@ #ifdef __MACOSX__ #define OPENRCT2_PLATFORM "macOS" #endif +#ifdef __FREEBSD__ + #define OPENRCT2_PLATFORM "FreeBSD" +#endif #ifndef OPENRCT2_PLATFORM #error Unknown platform! #endif