~emersion/mrsh-dev

Add configure script and Makefile v1 PROPOSED

Drew DeVault: 1
 Add configure script and Makefile

 3 files changed, 247 insertions(+), 0 deletions(-)
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/patches/6529/mbox | git am -3
Learn more about email & git

[PATCH] Add configure script and Makefile Export this patch

The configure script is POSIX sh and the Makefile is POSIX make.
---
Rationale: meson is non-POSIX

This is not quite done, but I'd like some feedback before putting a bow
on it. The remaining todo items are:

- Remove use of += to make the Makefile actually POSIX
- Test for linker features in ./configure
- Use the symbols files appropriate to the host platform
- Finish the install target
- Wire up tests

However, this is enough to give a basic proof-of-concept and should
flush out all of the unknowns. Features include:

- Detecting which flags the compiler supports
- Detects readline and uses it if present
- Creating header dependencies with cpp -MM
- Building in a dedicated directory instead of littering .o's around
- Pretty prints the things it's doing instead of endless cc commands

I also experimented with out-of-tree builds but decided it was too
complicated.

 .gitignore |   4 ++
 Makefile   | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure  | 111 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 247 insertions(+)
 create mode 100644 Makefile
 create mode 100755 configure

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..963d730
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,132 @@
+.POSIX:
+# ^ TODO: this is not true until we remove += usage in variables
+
+OUTDIR=.build
+include $(OUTDIR)/config.mk
+SOVERSION=0.0.0
+
+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
+
+highlight_objects+=\
+		$(OUTDIR)/highlight.o
+
+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 $@ $<
+
+# TODO: Test for these compiler features in ./configure
+# TODO: Use the symbols appropriate to the host platform
+libmrsh.so.$(SOVERSION): $(OUTDIR)/libmrsh.a
+	@printf 'LD\t$@\n'
+	@$(CC) \
+		-shared -fPIC \
+		-Wl,--no-undefined -Wl,--as-needed \
+		-Wl,-soname,libmrsh.so.$$(echo "$(SOVERSION)" | cut -d. -f1) \
+		-Wl,--version-script=$(SRCDIR)/libmrsh.gnu.sym \
+		-o $@ $<
+
+mrsh: $(OUTDIR)/libmrsh.a $(mrsh_objects)
+	@printf 'CCLD\t$@\n'
+	@$(CC) -o $@ $(LDFLAGS) $(mrsh_objects) -L$(OUTDIR) -lmrsh
+
+highlight: $(OUTDIR)/libmrsh.a $(highlight_objects)
+	@printf 'CCLD\t$@\n'
+	@$(CC) -o $@ $(LDFLAGS) $(highlight_objects) -L$(OUTDIR) -lmrsh
+
+all: mrsh highlight libmrsh.so.$(SOVERSION)
+
+install: mrsh libmrsh.so.$(SOVERSION)
+	mkdir -p $(BINDIR) $(LIBDIR) $(PCDIR)
+	install -m755 mrsh $(BINDIR)/mrsh
+	install -m755 libmrsh.so.$(SOVERSION) $(LIBDIR)/libmrsh.so.$(SOVERSION)
+	# TODO: install headers, pc
+
+clean:
+	rm -rf \
+		$(libmrsh_objects) \
+		$(mrsh_objects) \
+		$(highlight_objects) \
+		mrsh higlight libmrsh.so.$(SOVERSION)
+
+.PHONY: all install clean
diff --git a/configure b/configure
new file mode 100755
index 0000000..0c25343
--- /dev/null
+++ b/configure
@@ -0,0 +1,111 @@
+#!/bin/sh -e
+pkg_config=${PKG_CONFIG:-pkg-config}
+outdir=${OUTDIR:-.build}
+srcdir=${SRCDIR:-$(dirname "$0")}
+CC=${CC:-cc}
+
+use_readline=-1
+readline=readline
+
+for arg
+do
+	case "$arg" in
+		--prefix=*)
+			PREFIX=${arg#*=}
+			;;
+		--with-readline=*)
+			use_readline=1
+			readline=${arg#*=}
+			;;
+	esac
+done
+
+if [ $use_readline -eq -1 ]
+then
+	printf "Checking for readline... "
+	if $pkg_config readline
+	then
+		readline=readline
+		use_readline=1
+		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
+	else
+		echo no
+	fi
+fi
+
+mkdir -p $outdir
+
+test_cflags() {
+	[ ! -e "$outdir"/.test.c ] && cat <<-EOF > "$outdir"/.test.c
+	int main(void) { return 0; }
+	EOF
+	if $CC "$@" -o /dev/null "$outdir"/.test.c >/dev/null 2>&1
+	then
+		for flag
+		do
+			CFLAGS="$(printf '%s \\\n\t%s' "$CFLAGS" "$flag")"
+		done
+	else
+		return 1
+	fi
+}
+
+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
+
+printf "Creating $outdir/config.mk... "
+cat <<EOF > "$outdir"/config.mk
+CC=$CC
+PREFIX=${PREFIX:-/usr/local}
+_INSTDIR=\$(DESTDIR)\$(PREFIX)
+BINDIR?=${BINDIR:-\$(_INSTDIR)/bin}
+LIBDIR?=${LIBDIR:-\$(_INSTDIR)/lib}
+MANDIR?=${MANDIR:-\$(_INSTDIR)/share/man}
+PCDIR?=${PCDIR:-\$(_INSTDIR)/lib/pkgconfig}
+CFLAGS=${CFLAGS}
+SRCDIR=${srcdir}
+EOF
+if [ $use_readline -eq 1 ]
+then
+	cat <<-EOF >> "$outdir"/config.mk
+	CFLAGS+=$($pkg_config --cflags $readline) -DHAVE_READLINE -U_GNU_SOURCE
+	LDFLAGS+=$($pkg_config --libs $readline)
+
+	mrsh_objects+=\$(OUTDIR)/frontend/readline.o
+	EOF
+else
+	cat <<-EOF >> "$outdir"/config.mk
+	CFLAGS+=-DHAVE_EDITLINE
+
+	mrsh_objects+=\$(OUTDIR)/frontend/basic.o
+	EOF
+fi
+echo done
+
+touch $outdir/cppcache
-- 
2.22.0
View this thread in the archives