From 6ddefbbbb420d295832c570f13e1845371622737 Mon Sep 17 00:00:00 2001 From: Torrekie Gen Date: Sat, 13 Nov 2021 19:10:25 +0800 Subject: [PATCH] Allow building on Darwin/arm64 Changes: 1. `-soname` should be `-install_name` on Darwin 2. Version suffix should placed before extension suffix (libucontext.so.1 -> libucontext.1.dylib) 3. Check for architecture specific include directory, and include it while building 4. `EXPORT_UNPREFIXED` macro shouldn't be set since Darwin don't support aliases 5. Choose correct subdir for arm64 6. Use `-dynamiclib` instead of `-shared` on Darwin for adding version info (`-current_version` and `-compatibility_version`) --- Makefile | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index cce1686..29c180c 100644 --- a/Makefile +++ b/Makefile @@ -13,13 +13,21 @@ endif ifeq ($(ARCH),$(filter $(ARCH),armv7l)) override ARCH = arm endif +ifeq ($(ARCH),$(filter $(ARCH),arm64)) + override ARCH = aarch64 +endif LIBDIR := /lib INCLUDEDIR := /usr/include PKGCONFIGDIR := /usr/lib/pkgconfig CFLAGS := -ggdb3 -O2 -Wall CPPFLAGS := -Iinclude -Iarch/${ARCH} -Iarch/common -EXPORT_UNPREFIXED := yes +ifneq ($(shell uname),Darwin) + EXPORT_UNPREFIXED := yes +else + # Darwin does not support aliases + EXPORT_UNPREFIXED := no +endif FREESTANDING := no ifeq ($(FREESTANDING),yes) @@ -34,13 +42,30 @@ endif LIBUCONTEXT_C_SRC = $(wildcard arch/${ARCH}/*.c) LIBUCONTEXT_S_SRC = $(wildcard arch/${ARCH}/*.S) +ifeq ($(shell test -d arch/${ARCH}/include; echo $?),0) + CPPFLAGS += -Iarch/${ARCH}/include +endif + LIBUCONTEXT_VERSION := $(shell head -n 1 VERSION) LIBUCONTEXT_OBJ = ${LIBUCONTEXT_C_SRC:.c=.o} ${LIBUCONTEXT_S_SRC:.S=.o} LIBUCONTEXT_SOVERSION = 1 -LIBUCONTEXT_NAME = libucontext.so +ifeq ($(shell uname),Darwin) + LIBUCONTEXT_NAME = libucontext.dylib + LIBUCONTEXT_SONAME = libucontext.${LIBUCONTEXT_SOVERSION}.dylib + LIBUCONTEXT_POSIX_NAME = libucontext_posix.dylib + LIBUCONTEXT_POSIX_SONAME = libucontext_posix.${LIBUCONTEXT_SOVERSION}.dylib + LIBUCONTEXT_LINKER_FLAGS = -Wl,-dynamiclib,-install_name,${LIBUCONTEXT_SONAME},-current_version,${LIBUCONTEXT_SOVERSION},-compatibility_version,${LIBUCONTEXT_SOVERSION} + LIBUCONTEXT_POSIX_LINKER_FLAGS = -Wl,-dynamiclib,-install_name,${LIBUCONTEXT_POSIX_SONAME},-current_version,${LIBUCONTEXT_SOVERSION},-compatibility_version,${LIBUCONTEXT_SOVERSION} +else + LIBUCONTEXT_NAME = libucontext.so + LIBUCONTEXT_SONAME = libucontext.so.${LIBUCONTEXT_SOVERSION} + LIBUCONTEXT_POSIX_NAME = libucontext_posix.so + LIBUCONTEXT_POSIX_SONAME = libucontext_posix.so.${LIBUCONTEXT_SOVERSION} + LIBUCONTEXT_LINKER_FLAGS = -Wl,-shared,-soname,${LIBUCONTEXT_SONAME} + LIBUCONTEXT_POSIX_LINKER_FLAGS = -Wl,-shared,-soname,${LIBUCONTEXT_POSIX_SONAME} +endif LIBUCONTEXT_STATIC_NAME = libucontext.a LIBUCONTEXT_PC = libucontext.pc -LIBUCONTEXT_SONAME = libucontext.so.${LIBUCONTEXT_SOVERSION} LIBUCONTEXT_PATH = ${LIBDIR}/${LIBUCONTEXT_SONAME} LIBUCONTEXT_STATIC_PATH = ${LIBDIR}/${LIBUCONTEXT_STATIC_NAME} LIBUCONTEXT_HEADERS = \ @@ -48,11 +73,9 @@ LIBUCONTEXT_HEADERS = \ include/libucontext/bits.h LIBUCONTEXT_EXAMPLES = \ examples/cooperative_threading -LIBUCONTEXT_POSIX_NAME = libucontext_posix.so LIBUCONTEXT_POSIX_STATIC_NAME = libucontext_posix.a LIBUCONTEXT_POSIX_C_SRC = libucontext_posix.c LIBUCONTEXT_POSIX_OBJ = ${LIBUCONTEXT_POSIX_C_SRC:.c=.o} -LIBUCONTEXT_POSIX_SONAME = libucontext_posix.so.${LIBUCONTEXT_SOVERSION} LIBUCONTEXT_POSIX_PATH = ${LIBDIR}/${LIBUCONTEXT_POSIX_SONAME} LIBUCONTEXT_POSIX_STATIC_PATH = ${LIBDIR}/${LIBUCONTEXT_POSIX_STATIC_NAME} @@ -64,8 +87,7 @@ endif all: ${LIBUCONTEXT_SONAME} ${LIBUCONTEXT_STATIC_NAME} ${LIBUCONTEXT_POSIX_NAME} ${LIBUCONTEXT_POSIX_STATIC_NAME} ${LIBUCONTEXT_PC} ${LIBUCONTEXT_POSIX_NAME}: ${LIBUCONTEXT_NAME} ${LIBUCONTEXT_POSIX_OBJ} - $(CC) -fPIC -o ${LIBUCONTEXT_POSIX_NAME} -Wl,-soname,${LIBUCONTEXT_POSIX_SONAME} \ - -shared ${LIBUCONTEXT_POSIX_OBJ} ${LDFLAGS} + $(CC) -fPIC -o ${LIBUCONTEXT_POSIX_NAME} ${LIBUCONTEXT_POSIX_LINKER_FLAGS} ${LIBUCONTEXT_POSIX_OBJ} ${LDFLAGS} ${LIBUCONTEXT_POSIX_STATIC_NAME}: ${LIBUCONTEXT_STATIC_NAME} ${LIBUCONTEXT_POSIX_OBJ} $(AR) rcs ${LIBUCONTEXT_POSIX_STATIC_NAME} ${LIBUCONTEXT_POSIX_OBJ} @@ -77,8 +99,7 @@ ${LIBUCONTEXT_STATIC_NAME}: ${LIBUCONTEXT_HEADERS} ${LIBUCONTEXT_OBJ} $(AR) rcs ${LIBUCONTEXT_STATIC_NAME} ${LIBUCONTEXT_OBJ} ${LIBUCONTEXT_NAME}: ${LIBUCONTEXT_HEADERS} ${LIBUCONTEXT_OBJ} - $(CC) -fPIC -o ${LIBUCONTEXT_NAME} -Wl,-soname,${LIBUCONTEXT_SONAME} \ - -shared ${LIBUCONTEXT_OBJ} ${LDFLAGS} + $(CC) -fPIC -o ${LIBUCONTEXT_NAME} ${LIBUCONTEXT_LINKER_FLAGS} ${LIBUCONTEXT_OBJ} ${LDFLAGS} ${LIBUCONTEXT_SONAME}: ${LIBUCONTEXT_NAME} ln -sf ${LIBUCONTEXT_NAME} ${LIBUCONTEXT_SONAME} -- 2.41.0