Skip to content

Commit

Permalink
Makefile: implement a "silent rules" approach
Browse files Browse the repository at this point in the history
The old behavior is still available through `make V=1`. This is similar
to the Linux kernel and newer versions of automake.
  • Loading branch information
Ole André Vadla Ravnås committed May 1, 2014
1 parent c20b049 commit f536d40
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 16 deletions.
73 changes: 59 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
include config.mk
include pkgconfig.mk # package version

# Verbose output?
V ?= 0

ifeq ($(PKG_EXTRA),)
PKG_VERSION = $(PKG_MAJOR).$(PKG_MINOR)
else
PKG_VERSION = $(PKG_MAJOR).$(PKG_MINOR).$(PKG_EXTRA)
endif

ifeq ($(CROSS),)
CC ?= cc
AR ?= ar
Expand Down Expand Up @@ -259,7 +268,12 @@ endif
$(INSTALL_DATA) $(BLDIR)/lib$(LIBNAME).$(EXT) $(BLDIR)/tests/

$(LIBRARY): $(LIBOBJ)
$(CC) $(LDFLAGS) $(LIBOBJ) -o $(LIBRARY)
ifeq ($(V),0)
$(call log,CCLD,$(@:$(BLDIR)/%=%))
@$(create-library)
else
$(create-library)
endif

$(LIBOBJ): config.mk

Expand All @@ -272,23 +286,21 @@ $(LIBOBJ_SYSZ): $(DEP_SYSZ)
$(LIBOBJ_X86): $(DEP_X86)

$(ARCHIVE): $(LIBOBJ)
rm -f $(ARCHIVE)
$(AR) q $(ARCHIVE) $(LIBOBJ)
$(RANLIB) $(ARCHIVE)
@rm -f $(ARCHIVE)
ifeq ($(V),0)
$(call log,AR,$(@:$(BLDIR)/%=%))
@$(create-archive)
else
$(create-archive)
endif

$(PKGCFGF):
echo 'Name: capstone' > $(PKGCFGF)
echo 'Description: Capstone disassembly engine' >> $(PKGCFGF)
ifeq ($(PKG_EXTRA),)
echo 'Version: $(PKG_MAJOR).$(PKG_MINOR)' >> $(PKGCFGF)
ifeq ($(V),0)
$(call log,GEN,$(@:$(BLDIR)/%=%))
@$(generate-pkgcfg)
else
echo 'Version: $(PKG_MAJOR).$(PKG_MINOR).$(PKG_EXTRA)' >> $(PKGCFGF)
$(generate-pkgcfg)
endif
echo 'libdir=$(LIBDIR)' >> $(PKGCFGF)
echo 'includedir=$(PREFIX)/include/capstone' >> $(PKGCFGF)
echo 'archive=$${libdir}/libcapstone.a' >> $(PKGCFGF)
echo 'Libs: -L$${libdir} -lcapstone' >> $(PKGCFGF)
echo 'Cflags: -I$${includedir}' >> $(PKGCFGF)

install: $(PKGCFGF) $(ARCHIVE) $(LIBRARY)
mkdir -p $(LIBDIR)
Expand Down Expand Up @@ -340,4 +352,37 @@ dist:

$(OBJDIR)/%.o: %.c
@mkdir -p $(@D)
ifeq ($(V),0)
$(call log,CC,$(@:$(OBJDIR)/%=%))
@$(compile)
else
$(compile)
endif

define compile
$(CC) $(CFLAGS) -c $< -o $@
endef

define create-archive
$(AR) q $(ARCHIVE) $(LIBOBJ)
$(RANLIB) $(ARCHIVE)
endef

define create-library
$(CC) $(LDFLAGS) $(LIBOBJ) -o $(LIBRARY)
endef

define generate-pkgcfg
echo 'Name: capstone' > $(PKGCFGF)
echo 'Description: Capstone disassembly engine' >> $(PKGCFGF)
echo 'Version: $(PKG_VERSION)' >> $(PKGCFGF)
echo 'libdir=$(LIBDIR)' >> $(PKGCFGF)
echo 'includedir=$(PREFIX)/include/capstone' >> $(PKGCFGF)
echo 'archive=$${libdir}/libcapstone.a' >> $(PKGCFGF)
echo 'Libs: -L$${libdir} -lcapstone' >> $(PKGCFGF)
echo 'Cflags: -I$${includedir}' >> $(PKGCFGF)
endef

define log
@printf " %-7s %s\n" "$(1)" "$(2)"
endef
37 changes: 35 additions & 2 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

include ../config.mk

# Verbose output?
V ?= 0

INCDIR = ../include
ifndef BUILDDIR
TESTDIR = .
Expand Down Expand Up @@ -82,9 +85,39 @@ $(BINARY): $(OBJS)

$(TESTDIR)/%$(BIN_EXT): $(OBJDIR)/%.o
@mkdir -p $(@D)
${CC} $(CFLAGS) $(LDFLAGS) $< -O3 -Wall -l$(LIBNAME) -o $@
${CC} $(CFLAGS) $(LDFLAGS) $< -O3 -Wall $(LIBDIR)/lib$(LIBNAME).$(AR_EXT) -o $(subst $(BIN_EXT),,$@).static$(BIN_EXT)
ifeq ($(V),0)
$(call log,CCLD,$(notdir $@))
@$(link-dynamic)
$(call log,CCLD,$(notdir $(call staticname,$@)))
@$(link-static)
else
$(link-dynamic)
$(link-static)
endif

$(OBJDIR)/%.o: %.c
@mkdir -p $(@D)
ifeq ($(V),0)
$(call log,CC,$(@:$(OBJDIR)/%=%))
@$(compile)
else
$(compile)
endif

define compile
${CC} ${CFLAGS} -c $< -o $@
endef

define link-dynamic
${CC} $(CFLAGS) $(LDFLAGS) $< -O3 -Wall -l$(LIBNAME) -o $@
endef

define link-static
${CC} $(CFLAGS) $(LDFLAGS) $< -O3 -Wall $(LIBDIR)/lib$(LIBNAME).$(AR_EXT) -o $(call staticname,$@)
endef

define log
@printf " %-7s %s\n" "$(1)" "$(2)"
endef

staticname = $(subst $(BIN_EXT),,$(1)).static$(BIN_EXT)

0 comments on commit f536d40

Please sign in to comment.