From 3771cea7e84e169c8ab1dc39a95403c293fd1361 Mon Sep 17 00:00:00 2001
From: Anders Blomdell <anders.blomdell@control.lth.se>
Date: Wed, 21 Aug 2013 11:13:25 +0200
Subject: [PATCH] Added fixes for Darwin. Exposed constructor for generated
 code in case automatic calling not supported on platform.

---
 compiler/C_CodeGen.jrag           | 12 ++++++++++++
 lib/c/Makefile                    | 25 +++++++++++++++++++------
 lib/c/labcomm_compat_osx.h        | 28 ++++++++++++++++++++++++++++
 lib/c/labcomm_pthread_scheduler.c |  4 ++++
 lib/c/liblabcomm.so               |  0
 5 files changed, 63 insertions(+), 6 deletions(-)
 create mode 100644 lib/c/labcomm_compat_osx.h
 mode change 100644 => 100755 lib/c/liblabcomm.so

diff --git a/compiler/C_CodeGen.jrag b/compiler/C_CodeGen.jrag
index 67152f6..576698f 100644
--- a/compiler/C_CodeGen.jrag
+++ b/compiler/C_CodeGen.jrag
@@ -197,6 +197,7 @@ aspect C_CodeGen {
       getDecl(i).C_emitSizeofDeclaration(env);
       env.println("");
     }
+    C_emitConstructorDeclaration(env);
     C_emitForAll(env);
   }
 
@@ -943,6 +944,17 @@ aspect C_Signature {
   }
 
 
+  public void ASTNode.C_emitConstructorDeclaration(C_env env) {
+    throw new Error(this.getClass().getName() + 
+		    ".C_emitConstructorDeclaration(C_env env)" + 
+		    " not declared");
+  }
+
+  public void Program.C_emitConstructorDeclaration(C_env env) {
+    env.println("LABCOMM_CONSTRUCTOR void init_" +
+		env.prefix + "_signatures(void);");
+  }
+
 }
 
 aspect C_Sizeof {
diff --git a/lib/c/Makefile b/lib/c/Makefile
index 113e4ac..77d108c 100644
--- a/lib/c/Makefile
+++ b/lib/c/Makefile
@@ -1,9 +1,21 @@
 ## Macros
-
-CC=gcc
-CFLAGS=-g -Wall -Werror -O3  -I. -Itest 
-LDFLAGS=-L.
-LDLIBS_TEST=-llabcomm -lrt
+UNAME_S=$(shell uname -s)
+ifeq ($(UNAME_S),Linux)
+  CC=gcc
+  CFLAGS=-g -Wall -Werror -O3  -I. -Itest 
+  LDFLAGS=-L.
+  LDLIBS=-llabcomm -lrt
+  MAKESHARED=gcc -o $1 -shared -Wl,-soname,$2 $3 -lc -lrt
+else ifeq ($(UNAME_S),Darwin)
+  CC=clang
+  CFLAGS=-g -Wall -Werror -O3  -I. -Itest 
+	 -DLABCOMM_COMPAT=\"labcomm_compat_osx.h\" \
+	 -Wno-tautological-compare -Wno-unused-function
+  LDLIBS=-llabcomm
+  MAKESHARED=clang -o $1 -shared -Wl,-install_name,$2 $3 -lc
+else
+  $(error Unknown system $(UNAME_S))
+endif
 
 OBJS=labcomm_memory.o \
      labcomm_error.o \
@@ -50,6 +62,7 @@ liblabcomm.a: $(OBJS)
 	ar -r $@ $^
 
 liblabcomm.so.1: $(OBJS:%.o=%.pic.o)
+	$(call MAKESHARED,$@,$@,$^)
 	gcc -shared -Wl,-soname,$@ -o $@ $^ -lc -lrt
 
 labcomm.o : labcomm.c labcomm.h  labcomm_private.h
@@ -72,7 +85,7 @@ $(TEST_DIR)/%.o: $(TEST_DIR)/%.c
 
 .PRECIOUS: $(TEST_DIR)/%
 $(TEST_DIR)/%: $(TEST_DIR)/%.o 
-	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) $(LDLIBS_TEST)
+	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) 
 
 $(TEST_DIR)/gen:
 	mkdir -p $@
diff --git a/lib/c/labcomm_compat_osx.h b/lib/c/labcomm_compat_osx.h
new file mode 100644
index 0000000..da41c50
--- /dev/null
+++ b/lib/c/labcomm_compat_osx.h
@@ -0,0 +1,28 @@
+#ifndef __APPLE__
+#error "__APPLE__" not defined
+#endif
+
+#ifndef LABCOMM_COMPAT_OSX
+#define LABCOMM_COMPAT_OSX
+
+#include <machine/endian.h>
+#include <stdio.h>
+#include <time.h>
+
+#include <mach/clock.h>
+#include <mach/mach.h>
+
+#define CLOCK_REALTIME 0
+static void clock_gettime(int garbage, struct timespec *ts)
+{
+  (void) garbage;
+  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;
+}
+
+#endif
diff --git a/lib/c/labcomm_pthread_scheduler.c b/lib/c/labcomm_pthread_scheduler.c
index 7bb809c..f4d82a5 100644
--- a/lib/c/labcomm_pthread_scheduler.c
+++ b/lib/c/labcomm_pthread_scheduler.c
@@ -27,6 +27,10 @@
 #include "labcomm_scheduler_private.h"
 #include "labcomm_pthread_scheduler.h"
 
+#ifdef LABCOMM_COMPAT
+  #include LABCOMM_COMPAT
+#endif 
+
 struct pthread_time {
   struct labcomm_time time;
   struct labcomm_memory *memory;
diff --git a/lib/c/liblabcomm.so b/lib/c/liblabcomm.so
old mode 100644
new mode 100755
-- 
GitLab