From: Niki Date: Sat, 5 Apr 2025 06:50:04 +0000 (+0200) Subject: static mode, still TBC X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=12f4f577b800067e54c8e91c41a616cb86f20274;p=cutils.git static mode, still TBC --- diff --git a/Makefile b/Makefile index bfd13af..57ba3a6 100755 --- a/Makefile +++ b/Makefile @@ -1,10 +1,13 @@ # Simply pass everything to makefile.d, but calling from "../" +.SUFFIXES: .PHONY: default $(MAKECMDGOALS) default $(MAKECMDGOALS): @for mk in makefile.d makefile.check.d makefile.net.d; do \ $(MAKE) --no-print-directory -C ../ -f "$(CURDIR)/$$mk" \ - $(MAKECMDGOALS); \ + $(MAKECMDGOALS) \ + STATIC=$(STATIC) MUSL=$(MUSL) DEBUG=$(DEBUG) \ + ; \ done; diff --git a/makefile.check.d b/makefile.check.d index bdd7db7..8cbdfc7 100755 --- a/makefile.check.d +++ b/makefile.check.d @@ -25,6 +25,9 @@ PREFIX = /usr/local ################################################################################ +.SUFFIXES: +.SUFFIXES: .c .h .o .d + ifeq ($(dstdir),) dstdir = $(srcdir)/bin endif @@ -33,7 +36,7 @@ ifdef DEBUG CFLAGS += -ggdb -O0 CXXFLAGS += -ggdb -O0 endif - + # Default target .PHONY: all all: @@ -66,21 +69,21 @@ test run run-test run-test-more: $(dstdir)/lib$(NAME).a: $(OBJECTS) mkdir -p $(dstdir) - ## OLD: #note: -r = --relocatable, but former also works with Clang - ## OLD: $(LD) -r $(OBJECTS) -o $@ $(LDFLAGS) + @## OLD: #note: -r = --relocatable, but former also works with Clang + @## OLD: $(LD) -r $(OBJECTS) -o $@ $(LDFLAGS) $(AR) rcs $@ $(OBJECTS) clean: $(foreach lib,$(LIBS),$(MAKE) --no-print-directory \ -C $(lib)/ $@ dstdir=$(dstdir)) - rm -f $(OBJECTS) - rm -f $(DEPENDS) + $(RM) $(OBJECTS) + $(RM) $(DEPENDS) mrproper: mrpropre mrpropre: clean $(foreach lib,$(LIBS),$(MAKE) --no-print-directory \ -C $(lib)/ $@ dstdir=$(dstdir)) - rm -f $(dstdir)/lib$(NAME).a + $(RM) $(dstdir)/lib$(NAME).a rmdir $(dstdir) 2>/dev/null || true install: build @@ -89,9 +92,9 @@ install: build cp "$(ssrcdir)"/*.h "$(PREFIX)/include/$(srcdir)/" uninstall: - rm "$(PREFIX)/lib/lib$(NAME).a" - rmdir "$(PREFIX)/lib" 2>/dev/null - rm "$(PREFIX)/include/$(srcdir)/"*.h - rmdir "$(PREFIX)/include/$(srcdir)" 2>/dev/null - rmdir "$(PREFIX)/include" 2>/dev/null + $(RM) "$(PREFIX)/lib/lib$(NAME).a" + rmdir "$(PREFIX)/lib" 2>/dev/null + $(RM) "$(PREFIX)/include/$(srcdir)/"*.h + rmdir "$(PREFIX)/include/$(srcdir)" 2>/dev/null + rmdir "$(PREFIX)/include" 2>/dev/null diff --git a/makefile.d b/makefile.d index de97c3c..94dc825 100755 --- a/makefile.d +++ b/makefile.d @@ -2,6 +2,7 @@ # Makefile for C libraries # > NAME : the name of the main program (if programs, make a single .d file # per program, link them up in Makfile and use a $ssrcdir) +# > NOSTATIC : reason why STATIC will not work, if any # > srcdir : the source directory # > ssrcdir: the sub-sources directory (defaults to $srcdir) # > dstdir : the destination directory (defaults to $srcdir/bin) @@ -9,7 +10,11 @@ # Environment variables: # > PREFIX: where to (un)install (defaults to /usr/local) # > DEBUG: define it to build with all debug symbols +# > MUSL : can be used to use musl libc instead of glibc +# can be either the path to the spec gcc file or 'auto' +# > STATIC : enable static mode (except glibc -- but see MUSL option) # + NAME = cutils srcdir = $(NAME) ssrcdir = $(srcdir) @@ -20,11 +25,17 @@ CFLAGS += -Wall -pedantic -I./ -std=gnu99 CXXFLAGS += -Wall -pedantic -I./ PREFIX = /usr/local +# If you require dynamic libraries and STATIC will not work: reason why +# NOSTATIC = "libcheck is linked to m, rt and glicb" + # Required *locally compiled* libraries if any: # LIBS = cutils ################################################################################ +.SUFFIXES: +.SUFFIXES: .c .h .o .d + ifeq ($(dstdir),) dstdir = $(srcdir)/bin endif @@ -33,13 +44,13 @@ ifdef DEBUG CFLAGS += -ggdb -O0 CXXFLAGS += -ggdb -O0 endif - + # Default target .PHONY: all all: .PHONY: build rebuild install uninstall clean mrpropre mrpropre \ - $(NAME) test run run-test run-test-more + $(NAME) test run run-test run-test-more opts SOURCES=$(wildcard $(ssrcdir)/*.c) OBJECTS=$(SOURCES:%.c=%.o) @@ -47,8 +58,14 @@ DEPENDS =$(SOURCES:%.c=%.d) # Autogenerate dependencies from code -include $(DEPENDS) -%.o: %.c - $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ +%.o: %.c $(ssrcdir)/makefile.opts + $(CC) $(CFLAGS) -MMD -MP -c $< `cat $(ssrcdir)/makefile.opts` -o $@ + +# Static and MUSL support +opts: + @# updates the .opts file (but only if STATIC or MUSL changed) + $(MAKE) --no-print-directory -C $(ssrcdir) -f makefile.opts.d \ + MUSL=$(MUSL) STATIC=$(STATIC) NOSTATIC=$(NOSTATIC) # Main targets @@ -58,29 +75,30 @@ build: $(NAME) rebuild: clean build -$(NAME): $(dstdir)/lib$(NAME).a +$(NAME): opts $(dstdir)/lib$(NAME).a # Library, so no test and no run test run run-test run-test-more: @echo $(NAME) is a library, look at the tests instead -$(dstdir)/lib$(NAME).a: $(OBJECTS) +$(dstdir)/lib$(NAME).a: $(OBJECTS) $(ssrcdir)/makefile.opts mkdir -p $(dstdir) - ## OLD: #note: -r = --relocatable, but former also works with Clang - ## OLD: $(LD) -r $(OBJECTS) -o $@ $(LDFLAGS) + @## OLD: #note: -r = --relocatable, but former also works with Clang + @## OLD: $(LD) -r $(OBJECTS) -o $@ $(LDFLAGS) $(AR) rcs $@ $(OBJECTS) clean: $(foreach lib,$(LIBS),$(MAKE) --no-print-directory \ -C $(lib)/ $@ dstdir=$(dstdir)) - rm -f $(OBJECTS) - rm -f $(DEPENDS) + $(RM) $(OBJECTS) + $(RM) $(DEPENDS) + $(RM) $(ssrcdir)/makefile.opts mrproper: mrpropre mrpropre: clean $(foreach lib,$(LIBS),$(MAKE) --no-print-directory \ -C $(lib)/ $@ dstdir=$(dstdir)) - rm -f $(dstdir)/lib$(NAME).a + $(RM) $(dstdir)/lib$(NAME).a rmdir $(dstdir) 2>/dev/null || true install: build @@ -89,9 +107,9 @@ install: build cp "$(ssrcdir)"/*.h "$(PREFIX)/include/$(srcdir)/" uninstall: - rm "$(PREFIX)/lib/lib$(NAME).a" + $(RM) "$(PREFIX)/lib/lib$(NAME).a" rmdir "$(PREFIX)/lib" 2>/dev/null - rm "$(PREFIX)/include/$(srcdir)/"*.h + $(RM) "$(PREFIX)/include/$(srcdir)/"*.h rmdir "$(PREFIX)/include/$(srcdir)" 2>/dev/null rmdir "$(PREFIX)/include" 2>/dev/null diff --git a/makefile.net.d b/makefile.net.d index 282f2df..68da42d 100755 --- a/makefile.net.d +++ b/makefile.net.d @@ -25,6 +25,9 @@ PREFIX = /usr/local ################################################################################ +.SUFFIXES: +.SUFFIXES: .c .h .o .d + ifeq ($(dstdir),) dstdir = $(srcdir)/bin endif @@ -66,21 +69,21 @@ test run run-test run-test-more: $(dstdir)/lib$(NAME).a: $(OBJECTS) mkdir -p $(dstdir) - ## OLD: #note: -r = --relocatable, but former also works with Clang - ## OLD: $(LD) -r $(OBJECTS) -o $@ $(LDFLAGS) + @## OLD: #note: -r = --relocatable, but former also works with Clang + @## OLD: $(LD) -r $(OBJECTS) -o $@ $(LDFLAGS) $(AR) rcs $@ $(OBJECTS) clean: $(foreach lib,$(LIBS),$(MAKE) --no-print-directory \ -C $(lib)/ $@ dstdir=$(dstdir)) - rm -f $(OBJECTS) - rm -f $(DEPENDS) + $(RM) $(OBJECTS) + $(RM) $(DEPENDS) mrproper: mrpropre mrpropre: clean $(foreach lib,$(LIBS),$(MAKE) --no-print-directory \ -C $(lib)/ $@ dstdir=$(dstdir)) - rm -f $(dstdir)/lib$(NAME).a + $(RM) $(dstdir)/lib$(NAME).a rmdir $(dstdir) 2>/dev/null || true install: build @@ -89,9 +92,9 @@ install: build cp "$(ssrcdir)"/*.h "$(PREFIX)/include/$(srcdir)/" uninstall: - rm "$(PREFIX)/lib/lib$(NAME).a" + $(RM) "$(PREFIX)/lib/lib$(NAME).a" rmdir "$(PREFIX)/lib" 2>/dev/null - rm "$(PREFIX)/include/$(srcdir)/"*.h + $(RM) "$(PREFIX)/include/$(srcdir)/"*.h rmdir "$(PREFIX)/include/$(srcdir)" 2>/dev/null rmdir "$(PREFIX)/include" 2>/dev/null diff --git a/makefile.opts.d b/makefile.opts.d new file mode 100644 index 0000000..96715b5 --- /dev/null +++ b/makefile.opts.d @@ -0,0 +1,71 @@ +# +# Makefile for makefile.opts +# +# Variables: +# > MUSL : can be used to use musl libc instead of glibc +# can be either the path to the spec gcc file or 'auto' +# > STATIC : enable static mode (except glibc -- but see MUSL option) +# > NOSTATIC : reason why STATIC will not work, if any +# + +################################################################################ + +OPTS = makefile.opts + +.SUFFIXES: + +# Default target +.PHONY: all + +all: + @if [ -e $(OPTS) ]; then \ + mv $(OPTS) $(OPTS).bkp; \ + fi + + @$(RM) $(OPTS) + + @if [ "$(STATIC)" = 1 -a "$(NOSTATIC)" != "" ]; then \ + echo >&2; \ + echo This project cannot be STATIC: "$(NOSTATIC)" >&2; \ + echo >&2; \ + false ;\ + fi + + @spmusl=; \ + if [ "$(MUSL)" = auto ]; then \ + for try in \ + "/usr/local/musl/lib/musl-gcc.specs" \ + "/usr/musl/lib/musl-gcc.specs" \ + ; do \ + if [ -e "$$try" ]; then \ + spmusl="$$try"; \ + fi; \ + done;\ + if [ "$$spmusl" = "" ]; then \ + echo musl libc not found >&2; \ + exit 1; \ + fi;\ + /bin/echo -n " -specs $$spmusl " >> $(OPTS); \ + elif [ "$(MUSL)" != "" ]; then \ + /bin/echo -n " -specs $(MUSL) " >> $(OPTS); \ + fi + + @static=; \ + if [ "$(STATIC)" = 1 ]; then \ + /bin/echo -n " -static " >> $(OPTS); \ + fi + + @touch $(OPTS) + + @if [ -e "$(OPTS).bkp" ]; then \ + if diff "$(OPTS)" "$(OPTS).bkp" >/dev/null; then \ + echo "$(OPTS)": no changes; \ + mv "$(OPTS).bkp" "$(OPTS)"; \ + else \ + echo "$(OPTS)": updated; \ + $(RM) "$(OPTS).bkp"; \ + fi; \ + else \ + echo "$(OPTS)": new; \ + fi +