From 21f709644699dca4014691d050ff08990023b6d3 Mon Sep 17 00:00:00 2001
From: q66 <daniel@octaforge.org>
Date: Fri, 19 Jun 2020 16:42:24 +0200
Subject: [PATCH] Add posix-generic which uses gcc builtins instead of
 arch-specific assembly. We can then use this for all our archs.

Also make sure our CFLAGS/LDFLAGS are respected.
---
 Make.unix                   | 11 +++++------
 posix-generic/Makefile      | 15 +++++++++++++++
 posix-generic/getcallerpc.c |  8 ++++++++
 posix-generic/tas.c         | 18 ++++++++++++++++++
 4 files changed, 46 insertions(+), 6 deletions(-)
 create mode 100644 posix-generic/Makefile
 create mode 100644 posix-generic/getcallerpc.c
 create mode 100644 posix-generic/tas.c

diff --git a/Make.unix b/Make.unix
index f61c516..3a536b3 100644
--- a/Make.unix
+++ b/Make.unix
@@ -4,14 +4,14 @@ PTHREAD=-pthread
 AR=ar
 AS=as
 RANLIB=ranlib
-X11=/usr/X11R6
+override X11 := $(XBPS_CROSS_BASE)/usr/include/X11
 CC=gcc
-CFLAGS=-Wall -Wno-missing-braces -ggdb -I$(ROOT) -I$(ROOT)/include -I$(ROOT)/kern -c -I$(X11)/include -D_THREAD_SAFE $(PTHREAD) -O2
+override CFLAGS := -fno-strict-aliasing -Wall -Wno-missing-braces -Wno-parentheses -I$(ROOT) -I$(ROOT)/include -I$(ROOT)/kern -c -I$(X11)/include -D_THREAD_SAFE $(PTHREAD) $(CFLAGS)
 O=o
 OS=posix
 GUI=x11
-LDADD=-L$(X11)/lib64 -L$(X11)/lib -lX11 -ggdb -lm
-LDFLAGS=$(PTHREAD)
+LDADD=-L$(X11)/lib64 -L$(X11)/lib -lX11 -lm
+override LDFLAGS := $(PTHREAD) $(LDFLAGS)
 TARG=drawterm
 # AUDIO=none
 AUDIO=unix
@@ -19,5 +19,4 @@ AUDIO=unix
 all: default
 
 libmachdep.a:
-	arch=`uname -m|sed 's/i.86/386/;s/Power Macintosh/power/; s/x86_64/amd64/; s/armv[567].*/arm/; s/aarch64/arm64/'`; \
-	(cd posix-$$arch &&  make)
+	cd posix-generic && make
diff --git a/posix-generic/Makefile b/posix-generic/Makefile
new file mode 100644
index 0000000..970204e
--- /dev/null
+++ b/posix-generic/Makefile
@@ -0,0 +1,15 @@
+ROOT=..
+include ../Make.config
+LIB=../libmachdep.a
+
+OFILES=\
+	getcallerpc.$O\
+	tas.$O
+
+default: $(LIB)
+$(LIB): $(OFILES)
+	$(AR) r $(LIB) $(OFILES)
+	$(RANLIB) $(LIB)
+
+%.$O: %.c
+	$(CC) $(CFLAGS) $*.c
diff --git a/posix-generic/getcallerpc.c b/posix-generic/getcallerpc.c
new file mode 100644
index 0000000..3614c59
--- /dev/null
+++ b/posix-generic/getcallerpc.c
@@ -0,0 +1,8 @@
+#include "u.h"
+#include "libc.h"
+
+uintptr
+getcallerpc(void *a)
+{
+	return ((uintptr*)a)[-1];
+}
diff --git a/posix-generic/tas.c b/posix-generic/tas.c
new file mode 100644
index 0000000..289f747
--- /dev/null
+++ b/posix-generic/tas.c
@@ -0,0 +1,18 @@
+#include "u.h"
+#include "libc.h"
+
+int
+tas(int *x)
+{
+	/* use a gcc __atomic builtin */
+	int v = __atomic_exchange_n(x, 1, __ATOMIC_SEQ_CST);
+	switch(v) {
+	case 0:
+	case 1:
+		return v;
+	default:
+		print("canlock: corrupted 0x%lux\n", v);
+		return 1;
+	}
+}
+
-- 
2.27.0

