Skip to content

Commit

Permalink
OS X compatibility fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdoege authored and sapier committed Jun 29, 2014
1 parent ee7af21 commit c410e91
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 31 deletions.
20 changes: 8 additions & 12 deletions CMakeLists.txt
Expand Up @@ -72,11 +72,9 @@ if(WIN32)
set(EXAMPLE_CONF_DIR ".") set(EXAMPLE_CONF_DIR ".")
set(LOCALEDIR "locale") set(LOCALEDIR "locale")
elseif(APPLE) elseif(APPLE)
# Random placeholders; this isn't usually used and may not work set(SHAREDIR ".")
# See https://github.com/toabi/minetest-mac/ set(BINDIR "./bin")
set(SHAREDIR "${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}") set(DOCDIR "./doc/${PROJECT_NAME}")
set(BINDIR "bin")
set(DOCDIR "share/doc/${PROJECT_NAME}")
set(EXAMPLE_CONF_DIR ${DOCDIR}) set(EXAMPLE_CONF_DIR ${DOCDIR})
set(LOCALEDIR "locale") set(LOCALEDIR "locale")
elseif(UNIX) # Linux, BSD etc elseif(UNIX) # Linux, BSD etc
Expand Down Expand Up @@ -171,7 +169,7 @@ install(FILES "doc/menu_lua_api.txt" DESTINATION "${DOCDIR}")
install(FILES "doc/mapformat.txt" DESTINATION "${DOCDIR}") install(FILES "doc/mapformat.txt" DESTINATION "${DOCDIR}")
install(FILES "minetest.conf.example" DESTINATION "${EXAMPLE_CONF_DIR}") install(FILES "minetest.conf.example" DESTINATION "${EXAMPLE_CONF_DIR}")


if(UNIX) if(UNIX AND NOT APPLE)
install(FILES "doc/minetest.6" "doc/minetestserver.6" DESTINATION "${MANDIR}/man6") install(FILES "doc/minetest.6" "doc/minetestserver.6" DESTINATION "${MANDIR}/man6")
install(FILES "misc/minetest.desktop" DESTINATION "${XDG_APPS_DIR}") install(FILES "misc/minetest.desktop" DESTINATION "${XDG_APPS_DIR}")
install(FILES "misc/minetest.appdata.xml" DESTINATION "${APPDATADIR}") install(FILES "misc/minetest.appdata.xml" DESTINATION "${APPDATADIR}")
Expand Down Expand Up @@ -211,15 +209,13 @@ if(WIN32)
# This might be needed for some installer # This might be needed for some installer
#set(CPACK_PACKAGE_EXECUTABLES bin/minetest.exe "Minetest" bin/minetestserver.exe "Minetest Server") #set(CPACK_PACKAGE_EXECUTABLES bin/minetest.exe "Minetest" bin/minetestserver.exe "Minetest Server")
elseif(APPLE) elseif(APPLE)
# TODO
# see http://cmake.org/Wiki/CMake:CPackPackageGenerators#Bundle_.28OSX_only.29 # see http://cmake.org/Wiki/CMake:CPackPackageGenerators#Bundle_.28OSX_only.29
#
set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${VERSION_STRING}-osx") set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${VERSION_STRING}-osx")
set(CPACK_PACKAGE_ICON "") set(CPACK_PACKAGE_ICON ${CMAKE_CURRENT_SOURCE_DIR}/misc/mac/minetest-icon.icns)
set(CPACK_BUNDLE_NAME ${PROJECT_NAME}) set(CPACK_BUNDLE_NAME ${PROJECT_NAME})
set(CPACK_BUNDLE_ICON "") set(CPACK_BUNDLE_ICON ${CPACK_PACKAGE_ICON})
set(CPACK_BUNDLE_PLIST "") set(CPACK_BUNDLE_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/misc/mac/Info.plist)
set(CPACK_BUNDLE_STARTUP_COMMAND "Contents/MacOS/${PROJECT_NAME}") set(CPACK_BUNDLE_STARTUP_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/misc/mac/minetest-mac.sh)
set(CPACK_GENERATOR "Bundle") set(CPACK_GENERATOR "Bundle")
else() else()
set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${VERSION_STRING}-linux") set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${VERSION_STRING}-linux")
Expand Down
12 changes: 12 additions & 0 deletions cmake/Modules/FindGettextLib.cmake
Expand Up @@ -16,6 +16,18 @@ FIND_PROGRAM(GETTEXT_MSGFMT
PATHS "${CUSTOM_GETTEXT_PATH}/bin" PATHS "${CUSTOM_GETTEXT_PATH}/bin"
DOC "path to msgfmt") DOC "path to msgfmt")


if(APPLE)
FIND_LIBRARY(GETTEXT_LIBRARY
NAMES libintl.a
PATHS "${CUSTOM_GETTEXT_PATH}/lib"
DOC "gettext *intl*.lib")

FIND_LIBRARY(ICONV_LIBRARY
NAMES libiconv.dylib
PATHS "/usr/lib"
DOC "iconv lib")
endif(APPLE)

# modern Linux, as well as Mac, seem to not need require special linking # modern Linux, as well as Mac, seem to not need require special linking
# they do not because gettext is part of glibc # they do not because gettext is part of glibc
# TODO check the requirements on other BSDs and older Linux # TODO check the requirements on other BSDs and older Linux
Expand Down
33 changes: 25 additions & 8 deletions src/CMakeLists.txt
Expand Up @@ -162,7 +162,12 @@ else()
endif(APPLE) endif(APPLE)
endif(BUILD_CLIENT) endif(BUILD_CLIENT)
find_package(ZLIB REQUIRED) find_package(ZLIB REQUIRED)
set(PLATFORM_LIBS -lpthread -lrt ${CMAKE_DL_LIBS}) set(PLATFORM_LIBS -lpthread ${CMAKE_DL_LIBS})
if(APPLE)
set(PLATFORM_LIBS "-framework CoreFoundation" ${PLATFORM_LIBS})
else()
set(PLATFORM_LIBS -lrt ${PLATFORM_LIBS})
endif(APPLE)
#set(CLIENT_PLATFORM_LIBS -lXxf86vm) #set(CLIENT_PLATFORM_LIBS -lXxf86vm)
# This way Xxf86vm is found on OpenBSD too # This way Xxf86vm is found on OpenBSD too
find_library(XXF86VM_LIBRARY Xxf86vm) find_library(XXF86VM_LIBRARY Xxf86vm)
Expand Down Expand Up @@ -504,7 +509,7 @@ set(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin")
if(BUILD_CLIENT) if(BUILD_CLIENT)
add_executable(${PROJECT_NAME} ${minetest_SRCS}) add_executable(${PROJECT_NAME} ${minetest_SRCS})
add_dependencies(${PROJECT_NAME} GenerateVersion) add_dependencies(${PROJECT_NAME} GenerateVersion)
target_link_libraries( set(minetest_LIBS
${PROJECT_NAME} ${PROJECT_NAME}
${ZLIB_LIBRARIES} ${ZLIB_LIBRARIES}
${IRRLICHT_LIBRARY} ${IRRLICHT_LIBRARY}
Expand All @@ -522,6 +527,16 @@ if(BUILD_CLIENT)
${PLATFORM_LIBS} ${PLATFORM_LIBS}
${CLIENT_PLATFORM_LIBS} ${CLIENT_PLATFORM_LIBS}
) )
if(APPLE)
target_link_libraries(
${minetest_LIBS}
${ICONV_LIBRARY}
)
else()
target_link_libraries(
${minetest_LIBS}
)
endif()
if(USE_CURL) if(USE_CURL)
target_link_libraries( target_link_libraries(
${PROJECT_NAME} ${PROJECT_NAME}
Expand Down Expand Up @@ -605,7 +620,9 @@ if(MSVC)


else() else()
# Probably GCC # Probably GCC

if(APPLE)
SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pagezero_size 10000 -image_base 100000000" )
endif()
if(WARN_ALL) if(WARN_ALL)
set(RELEASE_WARNING_FLAGS "-Wall") set(RELEASE_WARNING_FLAGS "-Wall")
else() else()
Expand All @@ -623,12 +640,12 @@ else()
set(OTHER_FLAGS "-mthreads -fexceptions") set(OTHER_FLAGS "-mthreads -fexceptions")
endif() endif()


set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG ${RELEASE_WARNING_FLAGS} ${WARNING_FLAGS} ${OTHER_FLAGS} -ffast-math -Wall -pipe -funroll-loops")
if(APPLE) if(APPLE)
set(CMAKE_OSX_ARCHITECTURES i386 CACHE STRING "do not build for 64-bit" FORCE) set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Os")
set(ARCH i386) else()
endif() set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -fomit-frame-pointer")

endif(APPLE)
set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG ${RELEASE_WARNING_FLAGS} ${WARNING_FLAGS} ${OTHER_FLAGS} -O3 -ffast-math -Wall -fomit-frame-pointer -pipe -funroll-loops")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O1 -Wall ${WARNING_FLAGS} ${OTHER_FLAGS}") set(CMAKE_CXX_FLAGS_DEBUG "-g -O1 -Wall ${WARNING_FLAGS} ${OTHER_FLAGS}")


if(USE_GPROF) if(USE_GPROF)
Expand Down
2 changes: 2 additions & 0 deletions src/cguittfont/irrUString.h
Expand Up @@ -45,6 +45,8 @@
#define __BYTE_ORDER 0 #define __BYTE_ORDER 0
#define __LITTLE_ENDIAN 0 #define __LITTLE_ENDIAN 0
#define __BIG_ENDIAN 1 #define __BIG_ENDIAN 1
#elif __MACH__
#include <machine/endian.h>
#else #else
#include <endian.h> #include <endian.h>
#endif #endif
Expand Down
7 changes: 7 additions & 0 deletions src/jthread/jevent.h
Expand Up @@ -30,6 +30,11 @@


#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#elif __MACH__
#include <mach/mach.h>
#include <mach/task.h>
#include <mach/semaphore.h>
#include <sys/semaphore.h>
#else #else
#include <semaphore.h> #include <semaphore.h>
#endif #endif
Expand All @@ -38,6 +43,8 @@
class Event { class Event {
#ifdef _WIN32 #ifdef _WIN32
HANDLE hEvent; HANDLE hEvent;
#elif __MACH__
semaphore_t sem;
#else #else
sem_t sem; sem_t sem;
#endif #endif
Expand Down
12 changes: 12 additions & 0 deletions src/jthread/jsemaphore.h
Expand Up @@ -24,6 +24,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <windows.h> #include <windows.h>
#include <assert.h> #include <assert.h>
#define MAX_SEMAPHORE_COUNT 1024 #define MAX_SEMAPHORE_COUNT 1024
#elif __MACH__
#include <pthread.h>
#include <mach/mach.h>
#include <mach/task.h>
#include <mach/semaphore.h>
#include <sys/semaphore.h>
#include <errno.h>
#include <time.h>
#else #else
#include <pthread.h> #include <pthread.h>
#include <semaphore.h> #include <semaphore.h>
Expand All @@ -44,9 +52,13 @@ class JSemaphore {
private: private:
#if defined(WIN32) #if defined(WIN32)
HANDLE m_hSemaphore; HANDLE m_hSemaphore;
#elif __MACH__
semaphore_t m_semaphore;
#else #else
sem_t m_semaphore; sem_t m_semaphore;
#endif #endif
int semcount;
}; };



#endif /* JSEMAPHORE_H_ */ #endif /* JSEMAPHORE_H_ */
13 changes: 13 additions & 0 deletions src/jthread/pthread/jevent.cpp
Expand Up @@ -29,6 +29,19 @@


#define UNUSED(expr) do { (void)(expr); } while (0) #define UNUSED(expr) do { (void)(expr); } while (0)


#ifdef __MACH__
#undef sem_t
#define sem_t semaphore_t
#undef sem_init
#define sem_init(s, p, c) semaphore_create(mach_task_self(), (s), 0, (c))
#undef sem_wait
#define sem_wait(s) semaphore_wait(*(s))
#undef sem_post
#define sem_post(s) semaphore_signal(*(s))
#undef sem_destroy
#define sem_destroy(s) semaphore_destroy(mach_task_self(), *(s))
#endif

Event::Event() { Event::Event() {
int sem_init_retval = sem_init(&sem, 0, 0); int sem_init_retval = sem_init(&sem, 0, 0);
assert(sem_init_retval == 0); assert(sem_init_retval == 0);
Expand Down
59 changes: 55 additions & 4 deletions src/jthread/pthread/jsemaphore.cpp
Expand Up @@ -20,13 +20,33 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <errno.h> #include <errno.h>
#include <sys/time.h> #include <sys/time.h>
#include "jthread/jsemaphore.h" #include "jthread/jsemaphore.h"
#ifdef __MACH__
#include <unistd.h>
#endif


#define UNUSED(expr) do { (void)(expr); } while (0) #define UNUSED(expr) do { (void)(expr); } while (0)


#ifdef __MACH__
#undef sem_t
#undef sem_init
#undef sem_wait
#undef sem_post
#undef sem_destroy
#define sem_t semaphore_t
#define sem_init(s, p, c) semaphore_create(mach_task_self(), (s), 0, (c))
#define sem_wait(s) semaphore_wait(*(s))
#define sem_post(s) semaphore_signal(*(s))
#define sem_destroy(s) semaphore_destroy(mach_task_self(), *(s))
pthread_mutex_t semcount_mutex;
#endif

JSemaphore::JSemaphore() { JSemaphore::JSemaphore() {
int sem_init_retval = sem_init(&m_semaphore,0,0); int sem_init_retval = sem_init(&m_semaphore,0,0);
assert(sem_init_retval == 0); assert(sem_init_retval == 0);
UNUSED(sem_init_retval); UNUSED(sem_init_retval);
#ifdef __MACH__
semcount = 0;
#endif
} }


JSemaphore::~JSemaphore() { JSemaphore::~JSemaphore() {
Expand All @@ -45,32 +65,59 @@ void JSemaphore::Post() {
int sem_post_retval = sem_post(&m_semaphore); int sem_post_retval = sem_post(&m_semaphore);
assert(sem_post_retval == 0); assert(sem_post_retval == 0);
UNUSED(sem_post_retval); UNUSED(sem_post_retval);
#ifdef __MACH__
pthread_mutex_lock(&semcount_mutex);
semcount++;
pthread_mutex_unlock(&semcount_mutex);
#endif
} }


void JSemaphore::Wait() { void JSemaphore::Wait() {
int sem_wait_retval = sem_wait(&m_semaphore); int sem_wait_retval = sem_wait(&m_semaphore);
assert(sem_wait_retval == 0); assert(sem_wait_retval == 0);
UNUSED(sem_wait_retval); UNUSED(sem_wait_retval);
#ifdef __MACH__
pthread_mutex_lock(&semcount_mutex);
semcount--;
pthread_mutex_unlock(&semcount_mutex);
#endif
} }


bool JSemaphore::Wait(unsigned int time_ms) { bool JSemaphore::Wait(unsigned int time_ms) {
#ifdef __MACH__
mach_timespec_t waittime;
waittime.tv_sec = time_ms / 1000;
waittime.tv_nsec = 1000000 * (time_ms % 1000);
#else
struct timespec waittime; struct timespec waittime;
#endif
struct timeval now; struct timeval now;


if (gettimeofday(&now, NULL) == -1) { if (gettimeofday(&now, NULL) == -1) {
assert("Unable to get time by clock_gettime!" == 0); assert("Unable to get time by clock_gettime!" == 0);
return false; return false;
} }


#ifndef __MACH__
waittime.tv_nsec = ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000); waittime.tv_nsec = ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000);
waittime.tv_sec = (time_ms / 1000) + (waittime.tv_nsec / (1000*1000*1000)) + now.tv_sec; waittime.tv_sec = (time_ms / 1000) + (waittime.tv_nsec / (1000*1000*1000)) + now.tv_sec;
waittime.tv_nsec %= 1000*1000*1000; waittime.tv_nsec %= 1000*1000*1000;
#endif


errno = 0; errno = 0;
int sem_wait_retval = sem_timedwait(&m_semaphore,&waittime); #ifdef __MACH__
int sem_wait_retval = semaphore_timedwait(m_semaphore, waittime);
#else
int sem_wait_retval = sem_timedwait(&m_semaphore, &waittime);
#endif


if (sem_wait_retval == 0) if (sem_wait_retval == 0)
{ {
#ifdef __MACH__
pthread_mutex_lock(&semcount_mutex);
semcount--;
pthread_mutex_unlock(&semcount_mutex);
#endif
return true; return true;
} }
else { else {
Expand All @@ -81,10 +128,14 @@ bool JSemaphore::Wait(unsigned int time_ms) {
} }


int JSemaphore::GetValue() { int JSemaphore::GetValue() {

int retval = 0; int retval = 0;
sem_getvalue(&m_semaphore,&retval); #ifdef __MACH__

pthread_mutex_lock(&semcount_mutex);
retval = semcount;
pthread_mutex_unlock(&semcount_mutex);
#else
sem_getvalue(&m_semaphore, &retval);
#endif
return retval; return retval;
} }


5 changes: 1 addition & 4 deletions src/porting.cpp
Expand Up @@ -25,10 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,


#include "porting.h" #include "porting.h"


#if defined(__APPLE__) #if defined(__FreeBSD__)
#include <mach-o/dyld.h>
#include "CoreFoundation/CoreFoundation.h"
#elif defined(__FreeBSD__)
#include <sys/types.h> #include <sys/types.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#elif defined(_WIN32) #elif defined(_WIN32)
Expand Down
26 changes: 23 additions & 3 deletions src/porting.h
Expand Up @@ -123,6 +123,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,


#define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1)) #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))


#if defined(__APPLE__)
#include <mach-o/dyld.h>
#include <CoreFoundation/CoreFoundation.h>
#endif

namespace porting namespace porting
{ {


Expand Down Expand Up @@ -220,9 +225,13 @@ void initIrrlicht(irr::IrrlichtDevice * );
} }


#else // Posix #else // Posix
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>

#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif

inline u32 getTimeS() inline u32 getTimeS()
{ {
struct timeval tv; struct timeval tv;
Expand All @@ -247,7 +256,18 @@ void initIrrlicht(irr::IrrlichtDevice * );
inline u32 getTimeNs() inline u32 getTimeNs()
{ {
struct timespec ts; struct timespec ts;
// from http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts.tv_sec = mts.tv_sec;
ts.tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_REALTIME, &ts); clock_gettime(CLOCK_REALTIME, &ts);
#endif
return ts.tv_sec * 1000000000 + ts.tv_nsec; return ts.tv_sec * 1000000000 + ts.tv_nsec;
} }


Expand Down

0 comments on commit c410e91

Please sign in to comment.