static mode, still TBC
authorNiki <niki@nikiroo.be>
Sat, 5 Apr 2025 06:50:04 +0000 (08:50 +0200)
committerNiki <niki@nikiroo.be>
Sat, 5 Apr 2025 06:50:04 +0000 (08:50 +0200)
Makefile
makefile.check.d
makefile.d
makefile.net.d
makefile.opts.d [new file with mode: 0644]

index bfd13afe587ddd07122b128baa46b978375afc34..57ba3a6b16a33f6b3af787b42863847d88aebb6b 100755 (executable)
--- 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;
 
index bdd7db72b5b233e0c818f776691d28aff7c49c8e..8cbdfc7be641dfe8ed9afac274e39a8a1be73878 100755 (executable)
@@ -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
 
index de97c3c0c5390f22da7e6038d66e968e2673f59e..94dc8254da227fb4d474b6390556d762d0af373f 100755 (executable)
@@ -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
 
index 282f2df17ca9c9d787dc42cfd8984faebf17dc89..68da42d9c88a31820f1247bf825c5939861a3761 100755 (executable)
@@ -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 (file)
index 0000000..96715b5
--- /dev/null
@@ -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
+