# 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;
################################################################################
+.SUFFIXES:
+.SUFFIXES: .c .h .o .d
+
ifeq ($(dstdir),)
dstdir = $(srcdir)/bin
endif
CFLAGS += -ggdb -O0
CXXFLAGS += -ggdb -O0
endif
-
+
# Default target
.PHONY: all
all:
$(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
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
# 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)
# 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)
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
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)
# 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
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
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
################################################################################
+.SUFFIXES:
+.SUFFIXES: .c .h .o .d
+
ifeq ($(dstdir),)
dstdir = $(srcdir)/bin
endif
$(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
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
--- /dev/null
+#
+# 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
+