Welcome to little lamb

Code » comain » release » tree

[release] / common.mk

# Do *not* edit this file
# Project-specific changes/customization should go into project.mk

# init some variables
DEBUG =
CPPFLAGS =
CFLAGS =
LDFLAGS =

# basic flags, create .d dependency files
COMMON_CPPFLAGS         = -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700
COMMON_CPPFLAGS        += -iquote src/include
COMMON_CFLAGS           = -pipe
COMMON_CFLAGS          += -MD -MP
COMMON_LDFLAGS          =
COMMON_CPPFLAGS_SHARED  =
COMMON_CFLAGS_SHARED    = -shared
COMMON_LDFLAGS_SHARED   =

BLD_DOCS =
BLD_SHARED_LIBS =
BLD_STATIC_LIBS =
BLD_BINS =
# this isn't ideal, since it's limited to a depth of 2. However, that'll be
# enough (for now), as 1 is binary/library level, 2 is header within library,
# and we don't expect to go any deeper than that.
DEPS = $(wildcard build/*/*.d) $(wildcard build/*/*/*.d)
LIBS =
BINS =
PRIV_LIBS =
PRIV_STATIC_LIBS = $(PRIV_LIBS:%=lib%.a)
PRIV_SHARED_LIBS = $(PRIV_LIBS:%=lib%.so)
PRIV_BINS =
DOCS = README COPYING
DATA =

# to be removed upon `make clean`
CLEAN = $(BLD_STATIC_LIBS) $(BLD_SHARED_LIBS) $(BLD_BINS)

# to be removed upon `make distclean`
DISTCLEAN =

build: comain
	$(_DIR) comain/mkbuild

build/doc/%.md: src/doc/%.md | build
	$(_DOC) comain/mkdoc $< $@

build/%.o: src/%.c Makefile common.mk config.mk project.mk | build
	$(_SAY) Building $@ :
	$(_CC) \
		$(COMMON_CPPFLAGS) \
		$(CPPFLAGS_$(word 2, $(subst /, ,$<))) \
		$(CPPFLAGS) \
		$(COMMON_CFLAGS) \
		$(CFLAGS_$(word 2, $(subst /, ,$<))) \
		$(CFLAGS_$(<:src/%.c=%)) \
		$(CFLAGS) \
		-o $@ \
		-c $<
	@test -e $(@:%.o=%.d) && mv $(@:%.o=%.d) $(@:%.o=%.o.d)

build/%.o: src/%.S Makefile common.mk config.mk project.mk | build
	$(_SAY) \n Building $@ :
	$(_CC) \
		$(COMMON_CPPFLAGS) \
		$(CPPFLAGS_$(word 2, $(subst /, ,$<))) \
		$(CPPFLAGS) \
		$(COMMON_CFLAGS) \
		$(CFLAGS_$(word 2, $(subst /, ,$<))) \
		$(CFLAGS_$(<:src/%.c=%)) \
		$(CFLAGS) \
		-o $@ \
		-c $<
	@test -e $(@:%.o=%.d) && mv $(@:%.o=%.d) $(@:%.o=%.o.d)

build/%.lo: src/%.c Makefile common.mk config.mk project.mk | build
	$(_SAY) Building $@ :
	$(_CC) \
		$(COMMON_CPPFLAGS) \
		$(COMMON_CPPFLAGS_SHARED) \
		$(CPPFLAGS_$(word 2, $(subst /, ,$<))) \
		$(CPPFLAGS) \
		$(COMMON_CFLAGS) \
		$(COMMON_CFLAGS_SHARED) \
		$(CFLAGS_$(word 2, $(subst /, ,$<))) \
		$(CFLAGS_$(<:src/%.c=%)) \
		$(CFLAGS) \
		-o $@ \
		-c $<
	@test -e $(@:%.lo=%.d) && mv $(@:%.lo=%.d) $(@:%.lo=%.lo.d)

build/comain/%: | build
	$(_SAY) Making comain confdir :
	$(_CMN) comain/mkcmn $@


lib%.a:
	$(_SAY) Linking $@ :
	$(_AR) $(AR) rc $@ $^
	$(_RANLIB) $(RANLIB) $@

lib%.so:
	$(_SAY) Linking $@ :
	$(_CC) \
		$(COMMON_CFLAGS) \
		$(COMMON_CFLAGS_SHARED) \
		$(CFLAGS_$(@:%.so=%)) \
		$(CFLAGS) \
		$(COMMON_LDFLAGS) \
		$(COMMON_LDFLAGS_SHARED) \
		$(LDFLAGS_$(@:%.so=%)) \
		$(LDFLAGS) \
		-Wl,-soname,$@.0 \
		-o $@ \
		$^

DESTMD = $(DESTDIR)$(SHAREDIR)/xpln
INSTMD = $(_INST) if test -h $<; then cp -P $< $@; else install -D -m 644 $< $@; fi

DESTDOC = $(DESTDIR)$(SHAREDIR)/doc/$(PROJECT_NAME)
INSTDOC = $(_INST) install -D -m 644 $< $@

INSTHDR = $(_INST) install -D -m 644 $< $@

# to be generated by ./configure
include config.mk

# te be manually edited
include project.mk

# created upon compiling
-include $(DEPS)

# binary-specific deps
DEPS += $(BLD_BINS:%=%.d)

ifeq ($(NOSTATIC),0)
STATIC_LIBS = $(LIBS:%=lib%.a)
endif
ifeq ($(NOSHARED),0)
SHARED_LIBS = $(LIBS:%=lib%.so)
endif

# install binaries
$(DESTDIR)$(BINDIR)/%: %
	$(_INST) install -D -m 755 $< $@

# install comain confdir
$(DESTDIR)$(LIBDIR)/comain: build/comain
	$(_DIR) mkdir -p $@

$(DESTDIR)$(LIBDIR)/comain/%: build/comain/% | $(DESTDIR)$(LIBDIR)/comain
	$(_INST) cp -aR $< $@

# install shared libs
$(DESTDIR)$(LIBDIR)/%.so: %.so
	$(_INST) install -D -m 755 $< $@

# install static libs
$(DESTDIR)$(LIBDIR)/%.a: %.a
	$(_INST) install -D -m 644 $< $@

# install "private" binaries
$(DESTDIR)$(LIBDIR)/$(PROJECT_NAME)/%: %
	$(_INST) install -D -m 755 $< $@

# install README
$(DESTDIR)$(SHAREDIR)/doc/$(PROJECT_NAME)/README:
	$(_INST) install -D -m 644 /dev/null $@ ; comain/mkreadme > $@

# install documentation
$(DESTDOC)/%: % | dummy%
	$(INSTDOC)

# install *.md documentation
$(DESTDIR)$(SHAREDIR)/doc/$(PROJECT_NAME)/md/%.md: build/doc/%.md | dummy%
	$(_INST) install -D -m 644 $< $@

# dummy rule, so "install doc" takes precedence over "install *.md doc" whenver
# there is an *.md file in the root. E.g. when it has been /generated from/ the
# doc/*.md source file
dummy%:
	@:

# install data
$(DESTDIR)$(SHAREDIR)/$(PROJECT_NAME)/%:
	$(_INST) install -D -m 644 $(@:$(DESTDIR)$(SHAREDIR)/$(PROJECT_NAME)/%=build/%) $@


install-bins: $(BINS:%=$(DESTDIR)$(BINDIR)/%)

install-libs: $(LIBS:%=$(DESTDIR)$(LIBDIR)/comain/%) \
	$(STATIC_LIBS:%=$(DESTDIR)$(LIBDIR)/%) \
	$(SHARED_LIBS:%=$(DESTDIR)$(LIBDIR)/%) \
	$(HEADERS:%=$(DESTDIR)$(INCDIR)/%)

install-priv: $(PRIV_STATIC_LIBS:%=$(DESTDIR)$(LIBDIR)/$(PROJECT_NAME)/%) \
	$(PRIV_SHARED_LIBS:%=$(DESTDIR)$(LIBDIR)/$(PROJECT_NAME)/%) \
	$(PRIV_BINS:%=$(DESTDIR)$(LIBDIR)/$(PROJECT_NAME)/%)

install-docs: $(DOCS:%=$(DESTDIR)$(SHAREDIR)/doc/$(PROJECT_NAME)/%) \
	$(BLD_DOCS:%=$(DESTMD)/%)

install-data: $(DATA) $(DATA:build/%=$(DESTDIR)$(SHAREDIR)/$(PROJECT_NAME)/%)

install: install-bins install-libs install-priv install-docs install-data

clean:
	$(_CLEAN) rm -rf build ; rm -f $(CLEAN)

distclean: clean
	$(_CLEAN) rm -f config.mk src/include/config.h $(DISTCLEAN)
	@rm -rf config.tmp

repoclean: distclean
	$(_CLEAN) rm -f configure common.mk

_SAY = $(if $(V),@printf \\n;echo,@true)
_DIR = $(if $(V),,$(call say," DIR  ")$@;)
_DOC = $(if $(V),,$(call say," DOC  ")$@;)
_CP = $(if $(V),,$(call say,"  CP  ")$@;)
_CC = $(if $(V),$(CC),$(call say,"  CC  ")$@;$(CC))
_GEN = $(if $(V),,$(call say," GEN  ")$@;)
_AR = $(if $(V),,$(call say,"  AR  ")$@;)
_RANLIB = $(if $(V),,$(call say,"RANLIB")$@;)
_CLEAN = $(if $(V),,$(call say,"CLEAN ");)
_MAKE = $(if $(V),,$(call say," MAKE ")$@;)
_INST = $(if $(V),,$(call say," INST ")$@;)
_CMN = $(if $(V),,$(call say," CMN  ")$@;)


all: doc $(BLD_STATIC_LIBS) $(BLD_SHARED_LIBS) $(BLD_BINS) $(LIBS:%=build/comain/%)

doc: $(BLD_MD0) $(BLD_MD1) $(BLD_MD2) $(BLD_MD3) $(BLD_MD4) $(BLD_MD5) \
	$(BLD_MD6) $(BLD_MD7) $(BLD_MD8) $(BLD_MD9)

$(BLD_BINS):
	$(_SAY) Linking $@ :
	$(_CC) \
		$(COMMON_CFLAGS) \
		$(CFLAGS_$(@)) \
		$(CFLAGS) \
		$(COMMON_LDFLAGS) \
		$(LDFLAGS_$(@)) \
		$(LDFLAGS) \
		-o $@ \
		$^

.PHONY: install-bins install-libs install-priv install-docs install-data install \
	clean distclean repoclean