Add configure script and Makefile v2 PROPOSED

Oh, you even figured out how to put all artifacts in a separate
directory, nice!

I've rebased the patch to work with the latest commit, however I have
trouble running the Makefile with BSD make. It just does nothing when
running `bmake all`. `bmake clean` works properly. `bmake
.build/parser/parser.o` does nothing. `bmake .build/libmrsh.a` fails:

    AR	.build/libmrsh.a
    ar: .build/arithm.o: No such file or directory
    *** Error code 1

    Stop.
    bmake: stopped in /home/simon/src/mrsh

I'm not sure what's up.

Additionally, I don't think % rules are POSIX.

Any idea what's going wrong?
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/mrsh-dev/%3C20190705022642.8063-1-sir%40cmpwn.com%3E/mbox | git am -3
Learn more about email & git

[PATCH v2] Add configure script and Makefile Export this patch

The configure script is POSIX sh and the Makefile is POSIX make.
---
 .gitignore |   4 ++
 Makefile   | 142 ++++++++++++++++++++++++++++++++++++++++
 configure  | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 mkpc       |  12 ++++
 4 files changed, 344 insertions(+)
 create mode 100644 Makefile
 create mode 100755 configure
 create mode 100755 mkpc

diff --git a/.gitignore b/.gitignore
index 4c07ad0..32fe6d5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -53,3 +53,7 @@ dkms.conf
 
 /build
 /build-*
+
+.build
+highlight
+mrsh
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..c8522a7
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,142 @@
+.POSIX:
+OUTDIR=.build
+include $(OUTDIR)/config.mk
+
+INCLUDE=-Iinclude
+
+.DEFAULT_GOAL=all
+
+libmrsh_objects=\
+		$(OUTDIR)/arithm.o \
+		$(OUTDIR)/array.o \
+		$(OUTDIR)/ast_print.o \
+		$(OUTDIR)/ast.o \
+		$(OUTDIR)/buffer.o \
+		$(OUTDIR)/builtin/alias.o \
+		$(OUTDIR)/builtin/bg.o \
+		$(OUTDIR)/builtin/break.o \
+		$(OUTDIR)/builtin/builtin.o \
+		$(OUTDIR)/builtin/cd.o \
+		$(OUTDIR)/builtin/colon.o \
+		$(OUTDIR)/builtin/command.o \
+		$(OUTDIR)/builtin/dot.o \
+		$(OUTDIR)/builtin/eval.o \
+		$(OUTDIR)/builtin/exit.o \
+		$(OUTDIR)/builtin/export.o \
+		$(OUTDIR)/builtin/false.o \
+		$(OUTDIR)/builtin/fg.o \
+		$(OUTDIR)/builtin/getopts.o \
+		$(OUTDIR)/builtin/pwd.o \
+		$(OUTDIR)/builtin/read.o \
+		$(OUTDIR)/builtin/set.o \
+		$(OUTDIR)/builtin/shift.o \
+		$(OUTDIR)/builtin/times.o \
+		$(OUTDIR)/builtin/true.o \
+		$(OUTDIR)/builtin/type.o \
+		$(OUTDIR)/builtin/ulimit.o \
+		$(OUTDIR)/builtin/umask.o \
+		$(OUTDIR)/builtin/unalias.o \
+		$(OUTDIR)/builtin/unset.o \
+		$(OUTDIR)/builtin/unspecified.o \
+		$(OUTDIR)/builtin/wait.o \
+		$(OUTDIR)/getopt.o \
+		$(OUTDIR)/hashtable.o \
+		$(OUTDIR)/parser/arithm.o \
+		$(OUTDIR)/parser/parser.o \
+		$(OUTDIR)/parser/program.o \
+		$(OUTDIR)/parser/word.o \
+		$(OUTDIR)/shell/arithm.o \
+		$(OUTDIR)/shell/entry.o \
+		$(OUTDIR)/shell/job.o \
+		$(OUTDIR)/shell/path.o \
+		$(OUTDIR)/shell/process.o \
+		$(OUTDIR)/shell/redir.o \
+		$(OUTDIR)/shell/shell.o \
+		$(OUTDIR)/shell/task/assignment.o \
+		$(OUTDIR)/shell/task/ast.o \
+		$(OUTDIR)/shell/task/async.o \
+		$(OUTDIR)/shell/task/binop.o \
+		$(OUTDIR)/shell/task/case_clause.o \
+		$(OUTDIR)/shell/task/command_builtin.o \
+		$(OUTDIR)/shell/task/command_function.o \
+		$(OUTDIR)/shell/task/command_process.o \
+		$(OUTDIR)/shell/task/command.o \
+		$(OUTDIR)/shell/task/for_clause.o \
+		$(OUTDIR)/shell/task/function_definition.o \
+		$(OUTDIR)/shell/task/if_clause.o \
+		$(OUTDIR)/shell/task/list.o \
+		$(OUTDIR)/shell/task/loop_clause.o \
+		$(OUTDIR)/shell/task/pipeline.o \
+		$(OUTDIR)/shell/task/subshell.o \
+		$(OUTDIR)/shell/task/task.o \
+		$(OUTDIR)/shell/task/word.o \
+		$(OUTDIR)/shell/word.o
+
+mrsh_objects=\
+		$(OUTDIR)/main.o \
+		$(mrsh_frontend)
+
+highlight_objects=\
+		$(OUTDIR)/highlight.o
+
+public_includes=\
+		include/mrsh/arithm.h \
+		include/mrsh/array.h \
+		include/mrsh/ast.h \
+		include/mrsh/buffer.h \
+		include/mrsh/builtin.h \
+		include/mrsh/entry.h \
+		include/mrsh/getopt.h \
+		include/mrsh/hashtable.h \
+		include/mrsh/parser.h \
+		include/mrsh/shell.h
+
+include $(OUTDIR)/cppcache
+
+$(OUTDIR)/libmrsh.a: $(libmrsh_objects)
+	@printf 'AR\t$@\n'
+	@$(AR) -csr $@ $(libmrsh_objects)
+
+$(OUTDIR)/%.o: $(SRCDIR)/%.c
+	@mkdir -p $$(dirname "$@")
+	@printf 'CC\t$@\n'
+	@touch $(OUTDIR)/cppcache
+	@grep $< $(OUTDIR)/cppcache >/dev/null || \
+		$(CPP) $(INCLUDE) -MM -MT $@ $< >> $(OUTDIR)/cppcache
+	@$(CC) -c $(CFLAGS) $(INCLUDE) -o $@ $<
+
+libmrsh.so.$(SOVERSION): $(OUTDIR)/libmrsh.a
+	@printf 'LD\t$@\n'
+	@$(CC) -shared $(LDFLAGS) -o $@ $<
+
+$(OUTDIR)/mrsh.pc:
+	@printf 'MKPC\t$@\n'
+	@PREFIX=$(PREFIX) ./mkpc $@
+
+mrsh: $(OUTDIR)/libmrsh.a $(mrsh_objects)
+	@printf 'CCLD\t$@\n'
+	@$(CC) -o $@ $(LIBS) $(mrsh_objects) -L$(OUTDIR) -lmrsh
+
+highlight: $(OUTDIR)/libmrsh.a $(highlight_objects)
+	@printf 'CCLD\t$@\n'
+	@$(CC) -o $@ $(LIBS) $(highlight_objects) -L$(OUTDIR) -lmrsh
+
+all: mrsh highlight libmrsh.so.$(SOVERSION) $(OUTDIR)/mrsh.pc
+
+install: mrsh libmrsh.so.$(SOVERSION) $(OUTDIR)/mrsh.pc
+	mkdir -p $(BINDIR) $(LIBDIR) $(INCDIR)/mrsh $(PCDIR)
+	install -m755 mrsh $(BINDIR)/mrsh
+	install -m755 libmrsh.so.$(SOVERSION) $(LIBDIR)/libmrsh.so.$(SOVERSION)
+	for inc in $(public_includes); do \
+		install -m644 $$inc $(INCDIR)/mrsh/$$(basename $$inc); \
+	done
+	install -m644 $(OUTDIR)/mrsh.pc $(PCDIR)/mrsh.pc
+
+clean:
+	rm -rf \
+		$(libmrsh_objects) \
+		$(mrsh_objects) \
+		$(highlight_objects) \
+		mrsh higlight libmrsh.so.$(SOVERSION) $(OUTDIR)/mrsh.pc
+
+.PHONY: all install clean
diff --git a/configure b/configure
new file mode 100755
index 0000000..7cb2ba6
--- /dev/null
+++ b/configure
@@ -0,0 +1,186 @@
+#!/bin/sh -e
+SOVERSION=0.0.0
+
+pkg_config=${PKG_CONFIG:-pkg-config}
+outdir=${OUTDIR:-.build}
+srcdir=${SRCDIR:-$(dirname "$0")}
+CC=${CC:-cc}
+LIBS=
+
+use_readline=-1
+readline=readline
+
+for arg
+do
+	case "$arg" in
+		--prefix=*)
+			PREFIX=${arg#*=}
+			;;
+		--with-readline=*)
+			use_readline=1
+			readline=${arg#*=}
+			;;
+	esac
+done
+
+append_cflags() {
+	for flag
+	do
+		CFLAGS="$(printf '%s \\\n\t%s' "$CFLAGS" "$flag")"
+	done
+}
+
+append_ldflags() {
+	for flag
+	do
+		LDFLAGS="$(printf '%s \\\n\t%s' "$LDFLAGS" "$flag")"
+	done
+}
+
+append_libs() {
+	for flag
+	do
+		LIBS="$(printf '%s \\\n\t%s' "$LIBS" "$flag")"
+	done
+}
+
+test_cflags() {
+	[ ! -e "$outdir"/check.c ] && cat <<-EOF > "$outdir"/check.c
+	int main(void) { return 0; }
+	EOF
+	if $CC "$@" -o /dev/null "$outdir"/check.c >/dev/null 2>&1
+	then
+		append_cflags "$@"
+	else
+		return 1
+	fi
+}
+
+test_ldflags() {
+	[ ! -e "$outdir"/check.c ] && cat <<-EOF > "$outdir"/check.c
+	int main(void) { return 0; }
+	EOF
+	if $CC "$@" -o /dev/null "$outdir"/check.c >/dev/null 2>&1
+	then
+		append_ldflags "$@"
+	else
+		return 1
+	fi
+}
+
+mkdir -p $outdir
+
+for flag in \
+	-g -std=c99 -pedantic -Werror -Wundef -Wlogical-op \
+	-Wmissing-include-dirs -Wold-style-definition -Wpointer-arith -Winit-self \
+	-Wfloat-equal -Wstrict-prototypes -Wredundant-decls \
+	-Wimplicit-fallthrough=2 -Wendif-labels -Wstrict-aliasing=2 -Woverflow \
+	-Wformat=2 -Wno-missing-braces -Wno-missing-field-initializers \
+	-Wno-unused-parameter
+do
+	printf "Checking for $flag... "
+	if test_cflags "$flag"
+	then
+		echo yes
+	else
+		echo no
+	fi
+done
+
+for flag in -fPIC -Wl,--no-undefined -Wl,--as-needed
+do
+	test_ldflags "$flag"
+done
+
+soname=libmrsh.so.$(echo "$SOVERSION" | cut -d. -f1)
+printf "Checking for specifying soname for shared lib... "
+if ! \
+	test_ldflags -Wl,-soname,$soname || \
+	test_ldflags -Wl,-install_name,$soname
+then
+	echo no
+	echo "Unable to specify soname (is $(uname) supported?)" >&2
+	exit 1
+else
+	echo yes
+fi
+
+printf "Checking for exported symbol restrictions... "
+if ! \
+	test_ldflags -Wl,--version-script="libmrsh.gnu.sym" || \
+	test_ldflags -Wl,-exported_symbols_list,"libmrsh.darwin.sym"
+then
+	echo no
+	echo "Unable to specify exported symbols (is $(uname) supported?)" >&2
+	exit 1
+else
+	echo yes
+fi
+
+if [ $use_readline -eq -1 ]
+then
+	printf "Checking for readline... "
+	if $pkg_config readline
+	then
+		readline=readline
+		use_readline=1
+		append_cflags -DHAVE_READLINE
+		echo yes
+	else
+		echo no
+	fi
+fi
+if [ $use_readline -eq -1 ]
+then
+	printf "Checking for libedit... "
+	if $pkg_config libedit
+	then
+		echo yes
+		readline=libedit
+		use_readline=1
+		append_cflags -DHAVE_EDITLINE
+	else
+		echo no
+	fi
+fi
+
+if [ $use_readline -eq 1 ]
+then
+	append_cflags $($pkg_config --cflags $readline)
+	append_libs $($pkg_config --libs $readline)
+	if [ "$readline" = "readline" ]
+	then
+		# Undo GNU_SOURCE
+		append_cflags -U_GNU_SOURCE
+	fi
+fi
+
+printf "Creating $outdir/config.mk... "
+cat <<EOF > "$outdir"/config.mk
+SOVERSION=$SOVERSION
+CC=$CC
+PREFIX=${PREFIX:-/usr/local}
+_INSTDIR=\$(DESTDIR)\$(PREFIX)
+BINDIR?=${BINDIR:-\$(_INSTDIR)/bin}
+LIBDIR?=${LIBDIR:-\$(_INSTDIR)/lib}
+INCDIR?=${INCDIR:-\$(_INSTDIR)/include}
+MANDIR?=${MANDIR:-\$(_INSTDIR)/share/man}
+PCDIR?=${PCDIR:-\$(_INSTDIR)/lib/pkgconfig}
+CFLAGS=${CFLAGS}
+LDFLAGS=${LDFLAGS}
+LIBS=${LIBS}
+SRCDIR=${srcdir}
+EOF
+if [ $use_readline -eq 1 ]
+then
+	cat <<-EOF >> "$outdir"/config.mk
+	mrsh_frontend=\$(OUTDIR)/frontend/readline.o
+	EOF
+else
+	cat <<-EOF >> "$outdir"/config.mk
+	mrsh_frontend=\$(OUTDIR)/frontend/basic.o
+	EOF
+fi
+echo done
+
+touch $outdir/cppcache
diff --git a/mkpc b/mkpc
new file mode 100755
index 0000000..9916e9b
--- /dev/null
+++ b/mkpc
@@ -0,0 +1,12 @@
+#!/bin/sh
+cat <<EOF > $1
+prefix=$PREFIX
+libdir=\${prefix}/lib
+includedir=\${prefix}/include
+
+Name: mrsh
+Description: POSIX shell library
+Version: 0.0.0
+Libs: -L\${libdir} -lmrsh
+Cflags: -I\${includedir}
+EOF
-- 
2.22.0
Oh, you even figured out how to put all artifacts in a separate
directory, nice!

I've rebased the patch to work with the latest commit, however I have
trouble running the Makefile with BSD make. It just does nothing when
running `bmake all`. `bmake clean` works properly. `bmake
.build/parser/parser.o` does nothing. `bmake .build/libmrsh.a` fails:

    AR	.build/libmrsh.a
    ar: .build/arithm.o: No such file or directory
    *** Error code 1

    Stop.
    bmake: stopped in /home/simon/src/mrsh

I'm not sure what's up.

Additionally, I don't think % rules are POSIX.

Any idea what's going wrong?
View this thread in the archives