"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Omar Polo <op@omarpolo.com>
Subject:
portable: building template/ as subproject
To:
gameoftrees@openbsd.org
Date:
Fri, 06 Jan 2023 11:27:55 +0100

Download raw body.

Thread
template is only needed at compile-time to generate sources, much like
yacc, and so may causes issues with cross-compiling (for example void
linux builds packages for some arches this way.)

This moves the template/ as subproject.  A subproject means that it
has its own (small) configure.ac, but is still hooked in the top-level
Makefile.am and partecipate in the various build phases as normal
(build, clean, dist, ...).  The ad-hoc configure.ac however makes it
possible to further customize parts of the build that would be
difficult, if not impossible, using automake alone.

On a similar note, I think we may consider the same approach for
gotwebd too.  It could provide a way to re-build the libexec helpers
with different flags (e.g. -static) and install them in a separate
directory (chroot_DIR?)  Well, something to think on at least :)

The following diff survived several `make distclean && ./configure &&
make` and also a build from a distribution tarball.

diff refs/remotes/origin/linux refs/heads/linux
commit - 5518bc7b0d88e0fc9551e96e18fa2f0e93747ccf
commit + a469ff083536c437cdfe23409c4529a3b2aa339d
blob - /dev/null
blob + cb22b6f52cad3fa05f5724b548c6977192539497 (mode 644)
--- /dev/null
+++ compat/err.c
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
+ *
+ * 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 <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static void vwarn_impl(const char*, va_list);
+static void vwarnx_impl(const char*, va_list);
+
+static void
+vwarn_impl(const char *fmt, va_list ap)
+{
+	fprintf(stderr, "%s: ", getprogname());
+	vfprintf(stderr, fmt, ap);
+	fprintf(stderr, ": %s\n", strerror(errno));
+}
+
+static void
+vwarnx_impl(const char *fmt, va_list ap)
+{
+	fprintf(stderr, "%s: ", getprogname());
+	vfprintf(stderr, fmt, ap);
+	fprintf(stderr, "\n");
+}
+
+void
+err(int ret, const char *fmt, ...)
+{
+	va_list	ap;
+
+	va_start(ap, fmt);
+	vwarn_impl(fmt, ap);
+	va_end(ap);
+	exit(ret);
+}
+
+void
+errx(int ret, const char *fmt, ...)
+{
+	va_list	ap;
+
+	va_start(ap, fmt);
+	vwarnx_impl(fmt, ap);
+	va_end(ap);
+	exit(ret);
+}
+
+void
+warn(const char *fmt, ...)
+{
+	va_list	ap;
+
+	va_start(ap, fmt);
+	vwarn_impl(fmt, ap);
+	va_end(ap);
+}
+
+void
+warnx(const char *fmt, ...)
+{
+	va_list	ap;
+
+	va_start(ap, fmt);
+	vwarnx_impl(fmt, ap);
+	va_end(ap);
+}
blob - 5cbed704667c0074ac83e8335515f5a52f564c63
blob + fc6aa5bb292d5c65b34f1f624f92a5b7a13ee5ad
--- configure.ac
+++ configure.ac
@@ -15,6 +15,8 @@ AC_CANONICAL_HOST
 AC_USE_SYSTEM_EXTENSIONS
 AC_CANONICAL_HOST
 
+AC_CONFIG_SUBDIRS([template])
+
 # When CFLAGS isn't set at this stage and gcc is detected by the macro below,
 # autoconf will automatically use CFLAGS="-O2 -g". Prevent that by using an
 # empty default.
@@ -685,7 +687,6 @@ AC_CONFIG_FILES([Makefile
 		 got/Makefile
 		 gotadmin/Makefile
 		 gotwebd/Makefile
-		 template/Makefile
 		 tog/Makefile
 		 Makefile.common:Makefile.common.in])
 AC_OUTPUT
blob - 3044a18a6e2658e4cdeacda423f6eba7db986eba
blob + 7326e919140a80d6c9be0dd108007b438baf0915
--- template/Makefile.am
+++ template/Makefile.am
@@ -1,13 +1,7 @@
 noinst_PROGRAMS = template
 
-include $(top_builddir)/Makefile.common
-
 template_SOURCES = template.c \
-	parse.y
+	parse.y \
+	got_compat.h
 
-template_DEPENDENCIES = $(top_builddir)/compat/libopenbsd-compat.a
-
-LDADD = -L$(top_builddir)/compat -lopenbsd-compat -lpthread -lm
-if HOST_FREEBSD
-LDADD += -lmd
-endif
+LDADD =	$(LIBOBJS)
blob - /dev/null
blob + 2a53fd48a7cfe237a85e23235ab7739c274645d0 (mode 644)
--- /dev/null
+++ template/configure.ac
@@ -0,0 +1,26 @@
+AC_INIT([template], 0.1, [op@openbsd.org])
+AC_CONFIG_LIBOBJ_DIR(../compat)
+AM_INIT_AUTOMAKE([-Wall foreign subdir-objects])
+
+AC_ARG_VAR(HOSTCC, [The C compiler on the host.])
+AC_ARG_VAR(HOSTCFLAGS, [CFLAGS for the host compiler])
+
+test -n "$HOSTCC"	&& export CC="$HOSTCC"
+test -n "$HOSTCFLAGS"	&& export CFLAGS="$HOSTCFLAGS"
+
+AC_PROG_CC
+AC_PROG_YACC
+
+AC_REPLACE_FUNCS([
+	asprintf \
+	err \
+	getprogname \
+	reallocarray \
+])
+
+AC_CHECK_DECL([TAILQ_REMOVE], [],
+    [AC_MSG_ERROR("*** sys/queue.h is missing key defines ***")],
+    [#include <sys/queue.h>])
+
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
blob - /dev/null
blob + d04abb24cf5ec179d56e16eb8dc0c4b58e5d5dcb (mode 644)
--- /dev/null
+++ template/got_compat.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2022 Omar Polo <op@openbsd.org>
+ *
+ * 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.
+ */
+
+#ifndef COMPAT_H
+#define COMPAT_H
+
+#ifndef __OpenBSD__
+#define pledge(s, p) (0)
+#define unveil(s, p) (0)
+#endif
+
+#ifndef __dead
+#define __dead __attribute__((__noreturn__))
+#endif
+
+#ifndef HAVE_ASPRINTF
+int		 asprintf(char **, const char *, ...);
+#endif
+
+#ifndef HAVE_ERR
+__dead void	 err(int, const char *, ...);
+__dead void	 errx(int, const char *, ...);
+void		 warn(const char *, ...);
+void		 warnx(const char *, ...);
+#endif
+
+#ifndef HAVE_GETPROGNAME
+const char	*getprogname(void);
+#endif
+
+#ifndef HAVE_REALLOCARRAY
+void		*reallocarray(void *, size_t, size_t);
+#endif
+
+#endif
blob - 0c5031c03e0b09bc23862db57030475dcd114da4
blob + 5ef632aa276741156a37a4dee515535102bad542
--- template/template.c
+++ template/template.c
@@ -14,7 +14,6 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>