From cd3c3a48055ab4085d83f149ff4b4feba40b40cb Mon Sep 17 00:00:00 2001 From: Chloe Kudryavtsev Date: Mon, 12 Apr 2021 16:30:57 -0400 Subject: [PATCH] Add support for building static executables This fixes the following issues: * linked objects do not use LDFLAGS * pkg-config is not queried appropriately depending on the desired linkage * there's no way to configure/deconfigure static linking (except by setting LDFLAGS, which won't work due to the prior two) There are many ways of fixing these, but I went for the minimally intrusive approach: * any linked objects have `$(LDFLAGS)` in front of `$(LIBS)` * configure has a --static and --dynamic flag * --static is added to ldflags only if selected --- Makefile | 4 ++-- configure | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index b81981b..ad9ada2 100644 --- a/Makefile +++ b/Makefile @@ -58,11 +58,11 @@ $(OUTDIR)/mrsh.pc: mrsh: $(OUTDIR)/libmrsh.a $(mrsh_objects) @printf 'CCLD\t$@\n' - @$(CC) -o $@ $(LIBS) $(mrsh_objects) -L$(OUTDIR) -lmrsh + @$(CC) -o $@ $(LDFLAGS) $(LIBS) $(mrsh_objects) -L$(OUTDIR) -lmrsh highlight: $(OUTDIR)/libmrsh.a $(highlight_objects) @printf 'CCLD\t$@\n' - @$(CC) -o $@ $(LIBS) $(highlight_objects) -L$(OUTDIR) -lmrsh + @$(CC) -o $@ $(LDFLAGS) $(LIBS) $(highlight_objects) -L$(OUTDIR) -lmrsh check: mrsh $(tests) @for t in $(tests); do \ diff --git a/configure b/configure index 5241e27..c0e918e 100755 --- a/configure +++ b/configure @@ -10,6 +10,8 @@ LIBS= use_readline=-1 readline=readline +static= + for arg do case "$arg" in @@ -23,6 +25,12 @@ do use_readline=1 readline=${arg#*=} ;; + --static) + static=$arg + ;; + --dynamic) + static= + ;; esac done @@ -181,6 +189,11 @@ test_ldflags() { mkdir -p "$outdir" +if [ -n "$static" ] +then + test_ldflags $static +fi + for flag in \ -g -std=c99 -pedantic -Werror -Wundef -Wlogical-op \ -Wmissing-include-dirs -Wold-style-definition -Wpointer-arith -Winit-self \ @@ -259,8 +272,8 @@ fi if [ $use_readline -eq 1 ] then - append_cflags $($pkg_config --cflags-only-I $readline) - append_libs $($pkg_config --libs $readline) + append_cflags $($pkg_config $static --cflags-only-I $readline) + append_libs $($pkg_config $static --libs $readline) fi printf "Creating %s/config.mk... " "$outdir" -- 2.45.2