changeset 1075:69b90a8d4d35

misc: fix build on macOS
author David Demelier <markand@malikania.fr>
date Wed, 14 Jul 2021 20:37:27 +0200
parents 4ed442cf25c0
children 99dccafade22
files GNUmakefile extern/libbsd/reallocarray.c lib/irccd/config.h.in lib/irccd/util.c
diffstat 4 files changed, 60 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/GNUmakefile	Tue Jul 13 20:55:52 2021 +0200
+++ b/GNUmakefile	Wed Jul 14 20:37:27 2021 +0200
@@ -146,6 +146,10 @@
 # Per system commands.
 OS:=            $(shell uname -s)
 
+ifeq (${OS},Darwin)
+LIB_SRCS+=      extern/libbsd/reallocarray.c
+endif
+
 # Compile flags.
 DEFS=           -D_BSD_SOURCE -DTOP=\"$(shell pwd)\" -fPIC
 
@@ -176,7 +180,8 @@
 endif
 
 ifeq (${SSL},1)
-LIBS+=          -lssl -lcrypto
+INCS+=          $(shell pkg-config --cflags libssl libcrypto)
+LIBS+=          $(shell pkg-config --libs libssl libcrypto)
 endif
 
 # For config.h file.
@@ -194,15 +199,18 @@
 
 ifeq (${OS},Darwin)
 SHFLAGS=        -undefined dynamic_lookup
+ALLFLAGS=       -Wl,-all_load
 else
 SHFLAGS=        -shared
+ALLFLAGS=       -Wl,--whole-archive
+NOALLFLAGS=     -Wl,--no-whole-archive
+EXFLAGS=        -Wl,-E
 endif
 
 CMD.cc=         ${CC} ${DEFS} ${INCS} ${CFLAGS} -MMD -c $< -o $@
 CMD.ccld=       ${CC} ${DEFS} ${INCS} ${CFLAGS} -o $@ $^ ${LIBS} ${LDFLAGS}
-CMD.cchost=     ${CC} -Wl,-E -o $@ ${DEFS} ${INCS} ${CFLAGS} \
-	-Wl,--whole-archive $^ -Wl,--no-whole-archive ${LIBS} ${LDFLAGS}
-CMD.ccplg=      ${CC} ${DEFS} ${INCS} ${CFLAGS} ${SHFLAGS} -o $@ $^ ${LIBS} ${LDFLAGS}
+CMD.cchost=     ${CC} -o $@ ${DEFS} ${INCS} ${CFLAGS} ${EXFLAGS} ${ALLFLAGS} $^ ${NOALLFLAGS} ${LIBS} ${LDFLAGS}
+CMD.ccplg=      ${CC} ${DEFS} ${INCS} ${CFLAGS} ${SHFLAGS} -o $@ $< ${LIBS} ${LDFLAGS}
 
 .SUFFIXES:
 .SUFFIXES: .c .o .js
@@ -318,7 +326,7 @@
 ${TESTS_OBJS}: ${IRCCD_OBJS} ${LIB_OBJS} | irccd/irccd tests/data/example-dl-plugin.so
 
 # Generic plugin build command.
-plugins/%.so: plugins/%.c | ${IRCCD_OBJS}
+plugins/%.so: plugins/%.c ${IRCCD_OBJS}
 	${CMD.ccplg}
 
 # Plugin `links` require libcurl.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extern/libbsd/reallocarray.c	Wed Jul 14 20:37:27 2021 +0200
@@ -0,0 +1,38 @@
+/*	$OpenBSD: reallocarray.c,v 1.3 2015/09/13 08:31:47 guenther Exp $	*/
+/*
+ * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+/*
+ * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
+ * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
+ */
+#define MUL_NO_OVERFLOW	((size_t)1 << (sizeof(size_t) * 4))
+
+void *
+reallocarray(void *optr, size_t nmemb, size_t size)
+{
+	if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
+	    nmemb > 0 && SIZE_MAX / nmemb < size) {
+		errno = ENOMEM;
+		return NULL;
+	}
+	return realloc(optr, size * nmemb);
+}
--- a/lib/irccd/config.h.in	Tue Jul 13 20:55:52 2021 +0200
+++ b/lib/irccd/config.h.in	Wed Jul 14 20:37:27 2021 +0200
@@ -32,4 +32,12 @@
 @define WITH_JS@
 @define WITH_SSL@
 
+#if defined(__APPLE__)
+#       include <stddef.h>
+
+void *
+reallocarray(void *, size_t, size_t);
+
+#endif
+
 #endif /* !IRCCD_CONFIG_H */
--- a/lib/irccd/util.c	Tue Jul 13 20:55:52 2021 +0200
+++ b/lib/irccd/util.c	Wed Jul 14 20:37:27 2021 +0200
@@ -25,6 +25,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "config.h"
 #include "util.h"
 
 void *