Merge: Fix lib/realtime on osx
authorJean Privat <jean@pryen.org>
Thu, 23 Jul 2015 17:32:50 +0000 (13:32 -0400)
committerJean Privat <jean@pryen.org>
Thu, 23 Jul 2015 17:32:50 +0000 (13:32 -0400)
A lot of googling to solve two related issues on Mac OS X

* `clock_gettime` and cie is not provided, and one should use the more complex clock_get_time (and cie)
* the POSIX `-lrt` is refused by the linker

close #868

Pull-Request: #1586
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>

lib/realtime.nit
src/compiler/abstract_compiler.nit

index dc24c99..ad64930 100644 (file)
@@ -21,6 +21,28 @@ in "C header" `{
 #include <time.h>
 `}
 
+in "C" `{
+
+#ifdef __MACH__
+/* OS X does not have clock_gettime, mascarade it and use clock_get_time
+ * cf http://stackoverflow.com/questions/11680461/monotonic-clock-on-osx
+*/
+#include <mach/clock.h>
+#include <mach/mach.h>
+#define CLOCK_REALTIME CALENDAR_CLOCK
+#define CLOCK_MONOTONIC SYSTEM_CLOCK
+void clock_gettime(clock_t clock_name, struct timespec *ts) {
+       clock_serv_t cclock;
+       mach_timespec_t mts;
+       host_get_clock_service(mach_host_self(), clock_name, &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;
+}
+#endif
+`}
+
 # Elapsed time representation.
 extern class Timespec `{struct timespec*`}
 
@@ -105,7 +127,18 @@ class Clock
        # Smallest time frame reported by clock
        fun resolution : Timespec `{
                struct timespec* tv = malloc( sizeof(struct timespec) );
+#ifdef __MACH__
+               clock_serv_t cclock;
+               int nsecs;
+               mach_msg_type_number_t count;
+               host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
+               clock_get_attributes(cclock, CLOCK_GET_TIME_RES, (clock_attr_t)&nsecs, &count);
+               mach_port_deallocate(mach_task_self(), cclock);
+               tv->tv_sec = 0;
+               tv->tv_nsec = nsecs;
+#else
                clock_getres( CLOCK_MONOTONIC, tv );
+#endif
                return tv;
        `}
 
index 43686ae..7f3e924 100644 (file)
@@ -389,6 +389,15 @@ endif
                        makefile.write("CFLAGS += -D NO_STACKTRACE\n\n")
                end
 
+               makefile.write """
+# Special configuration for Darwin
+ifeq ($(uname_S),Darwin)
+       # Remove POSIX flag -lrt
+       LDLIBS := $(filter-out -lrt,$(LDLIBS))
+endif
+
+"""
+
                makefile.write("all: {outpath}\n")
                if outpath != real_outpath then
                        makefile.write("\tcp -- {outpath.escape_to_sh} {real_outpath.escape_to_sh.replace("$","$$")}")