Add the Maker build system
authorKalle Vahlman <kalle.vahlman@movial.com>
Mon, 3 Nov 2008 13:03:59 +0000 (15:03 +0200)
committerKalle Vahlman <kalle.vahlman@movial.com>
Mon, 3 Nov 2008 13:03:59 +0000 (15:03 +0200)
build/README [new file with mode: 0644]
build/binary.mk [new file with mode: 0644]
build/c++.mk [new file with mode: 0644]
build/c.mk [new file with mode: 0644]
build/common.mk [new file with mode: 0644]
build/library.mk [new file with mode: 0644]
build/pkgconfig.mk [new file with mode: 0644]
build/plugin.mk [new file with mode: 0644]
build/project.mk [new file with mode: 0644]
build/target.mk [new file with mode: 0644]
build/test.mk [new file with mode: 0644]

diff --git a/build/README b/build/README
new file mode 100644 (file)
index 0000000..534bd76
--- /dev/null
@@ -0,0 +1,10 @@
+This is a simple C/C++ build system based on GNU Make.  Supports (at least) the
+GNU toolchain and ELF platforms.  Meant to be used in place of Automake and
+Libtool.  Can be used with Autoconf.
+
+URL: http://iki.fi/timo.savola/git/?p=maker.git
+
+Original version is copyright (c) 2008 Timo Savola.
+Distributable under the terms of the Boost Software License 1.0:
+    http://boost.org/LICENSE_1_0.txt
+    http://opensource.org/licenses/bsl1.0.html
diff --git a/build/binary.mk b/build/binary.mk
new file mode 100644 (file)
index 0000000..b91beca
--- /dev/null
@@ -0,0 +1,17 @@
+include build/common.mk
+include build/target.mk
+
+O_NAME         := $(O)/bin/$(NAME)
+
+build: $(O_NAME)
+
+$(O_NAME): $(OBJECTS) $(DEPENDS)
+       $(call echo,Link,$@)
+       $(QUIET) mkdir -p $(dir $@)
+       $(QUIET) $(LINKCOMMAND) -o $@ $(OBJECTS) $(LIBS)
+
+install::
+       mkdir -p $(DEST_BINDIR)
+       install $(O_NAME) $(DEST_BINDIR)/
+
+.PHONY: build
diff --git a/build/c++.mk b/build/c++.mk
new file mode 100644 (file)
index 0000000..64e3839
--- /dev/null
@@ -0,0 +1,24 @@
+CXXSUFFIX      := $(firstword $(suffix $(filter $(CXXPATTERNS),$(SOURCES))))
+
+ifneq ($(CXXSUFFIX),)
+
+CXXCOMMAND     := $(CXX) $(GENERICFLAGS) $(CXXFLAGS)
+LINKCOMMAND    := $(CXXLINKER) $(CFLAGS) $(CXXFLAGS) $(LDFLAGS)
+
+$(O)/obj/%.o: %$(CXXSUFFIX)
+       $(call echo,Compile,$@)
+       $(QUIET) mkdir -p $(dir $@)
+       $(QUIET) $(CXXCOMMAND) \
+               -MF $(O)/obj/$(patsubst %,%.d,$*$(CXXSUFFIX)) \
+               -MT $(O)/obj/$*.o -MT $(O)/obj/$*.os \
+               -o $@ $*$(CXXSUFFIX)
+
+$(O)/obj/%.os: %$(CXXSUFFIX)
+       $(call echo,Compile,$@)
+       $(QUIET) mkdir -p $(dir $@)
+       $(QUIET) $(CXXCOMMAND) $(PICFLAGS) \
+               -MF $(O)/obj/$(patsubst %,%.d,$*$(CXXSUFFIX)) \
+               -MT $(O)/obj/$*.o -MT $(O)/obj/$*.os \
+               -o $@ $*$(CXXSUFFIX)
+
+endif
diff --git a/build/c.mk b/build/c.mk
new file mode 100644 (file)
index 0000000..3ddd4a4
--- /dev/null
@@ -0,0 +1,18 @@
+CCCOMMAND      := $(CC) $(GENERICFLAGS) $(CCFLAGS)
+LINKCOMMAND    := $(CCLINKER) $(CFLAGS) $(CCFLAGS) $(LDFLAGS)
+
+$(O)/obj/%.o: %.c
+       $(call echo,Compile,$@)
+       $(QUIET) mkdir -p $(dir $@)
+       $(QUIET) $(CCCOMMAND) \
+               -MF $(O)/obj/$(patsubst %,%.d,$*.c) \
+               -MT $(O)/obj/$*.o -MT $(O)/obj/$*.os \
+               -o $@ $*.c
+
+$(O)/obj/%.os: %.c
+       $(call echo,Compile,$@)
+       $(QUIET) mkdir -p $(dir $@)
+       $(QUIET) $(CCCOMMAND) $(PICFLAGS) \
+               -MF $(O)/obj/$(patsubst %,%.d,$*.c) \
+               -MT $(O)/obj/$*.o -MT $(O)/obj/$*.os \
+               -o $@ $*.c
diff --git a/build/common.mk b/build/common.mk
new file mode 100644 (file)
index 0000000..99245e9
--- /dev/null
@@ -0,0 +1,15 @@
+define echo
+       @ printf "  %-8s  %s\n" "$(1)" "$(2)"
+endef
+
+QUIET          := $(if $(V),,@)
+
+lastword       = $(if $(1),$(word $(words $(1)),$(1)),)
+
+DEST_PREFIX    = $(DESTDIR)$(PREFIX)
+DEST_BINDIR    = $(DESTDIR)$(BINDIR)
+DEST_SBINDIR   = $(DESTDIR)$(SBINDIR)
+DEST_LIBDIR    = $(DESTDIR)$(LIBDIR)
+DEST_PLUGINDIR = $(DESTDIR)$(PLUGINDIR)
+DEST_DATADIR   = $(DESTDIR)$(DATADIR)
+DEST_SYSCONFDIR        = $(DESTDIR)$(SYSCONFDIR)
diff --git a/build/library.mk b/build/library.mk
new file mode 100644 (file)
index 0000000..a6530ca
--- /dev/null
@@ -0,0 +1,43 @@
+include build/common.mk
+include build/target.mk
+
+LIBRARY                := lib$(NAME).so$(if $(VERSION),.$(VERSION),)
+LIBRARY_LINK   := lib$(NAME).so
+ARCHIVE                := lib$(NAME).a
+
+O_LIBRARY      := $(O)/lib/$(LIBRARY)
+O_LIBRARY_LINK := $(O)/lib/$(LIBRARY_LINK)
+O_ARCHIVE      := $(O)/lib/$(ARCHIVE)
+
+build: build-shared build-static
+build-shared: $(O_LIBRARY)
+build-static: $(O_ARCHIVE)
+
+$(O_LIBRARY): $(PIC_OBJECTS) $(DEPENDS)
+       $(call echo,Link,$@)
+       $(QUIET) mkdir -p $(dir $@)
+       $(QUIET) $(LINKCOMMAND) -fPIC -shared -Wl,-soname,$(LIBRARY) \
+               -o $@ $(PIC_OBJECTS) $(LIBS)
+ifneq ($(LIBRARY),$(LIBRARY_LINK))
+       $(QUIET) ln -sf $(LIBRARY) $(O_LIBRARY_LINK)
+endif
+
+$(O_ARCHIVE): $(OBJECTS)
+       $(call echo,Archive,$@)
+       $(QUIET) mkdir -p $(dir $@)
+       $(QUIET) $(AR) crs $@ $(OBJECTS)
+
+install::
+ifneq ($(wildcard $(O_LIBRARY)),)
+       mkdir -p $(DEST_LIBDIR)
+       install $(O_LIBRARY) $(DEST_LIBDIR)/
+       ln -sf $(LIBRARY) $(DEST_LIBDIR)/$(LIBRARY_LINK)
+endif
+
+install::
+ifneq ($(wildcard $(O_ARCHIVE)),)
+       mkdir -p $(DEST_LIBDIR)
+       install $(O_ARCHIVE) $(DEST_LIBDIR)/
+endif
+
+.PHONY: build build-shared build-static
diff --git a/build/pkgconfig.mk b/build/pkgconfig.mk
new file mode 100644 (file)
index 0000000..75b28fe
--- /dev/null
@@ -0,0 +1,13 @@
+ifneq ($(PKGS),)
+
+ ifeq ($(shell $(PKG_CONFIG) --exists --print-errors $(PKGS) && echo ok),)
+  $(error PKGS not found)
+ endif
+
+CPPFLAGS       += $(shell $(PKG_CONFIG) --cflags-only-I $(PKGS))
+CFLAGS         += $(shell $(PKG_CONFIG) --cflags-only-other $(PKGS))
+LDFLAGS                += $(shell $(PKG_CONFIG) --libs-only-other $(PKGS))
+LIBS           += $(shell $(PKG_CONFIG) --libs-only-L $(PKGS)) \
+                  $(shell $(PKG_CONFIG) --libs-only-l $(PKGS))
+
+endif
diff --git a/build/plugin.mk b/build/plugin.mk
new file mode 100644 (file)
index 0000000..8f5f55e
--- /dev/null
@@ -0,0 +1,20 @@
+include build/common.mk
+include build/target.mk
+
+PLUGIN         := $(NAME).so
+O_PLUGIN       := $(O)/plugin/$(PLUGIN)
+
+build-plugin: $(O_PLUGIN)
+
+$(O_PLUGIN): $(PIC_OBJECTS) $(DEPENDS)
+       $(call echo,Link,$@)
+       $(QUIET) mkdir -p $(dir $@)
+       $(QUIET) $(LINKCOMMAND) -fPIC -shared -o $@ $(PIC_OBJECTS) $(LIBS)
+
+install::
+ifneq ($(wildcard $(O_PLUGIN)),)
+       mkdir -p $(DEST_PLUGINDIR)
+       install $(O_PLUGIN) $(DEST_PLUGINDIR)/
+endif
+
+.PHONY: build
diff --git a/build/project.mk b/build/project.mk
new file mode 100644 (file)
index 0000000..c0c36f2
--- /dev/null
@@ -0,0 +1,109 @@
+O              ?= debug
+# V
+
+# CONFIG
+
+# CROSS_COMPILE
+CCACHE         ?= $(if $(shell which ccache),ccache,)
+
+ifeq ($(origin CC),default)
+CC             := $(CCACHE) $(CROSS_COMPILE)gcc
+endif
+
+ifeq ($(origin CXX),default)
+CXX            := $(CCACHE) $(CROSS_COMPILE)g++
+endif
+
+ifeq ($(origin AR),default)
+AR             := $(CCACHE) $(CROSS_COMPILE)ar
+endif
+
+CCLINKER       ?= $(CC)
+CXXLINKER      ?= $(CXX)
+
+PKG_CONFIG     ?= pkg-config
+# PKG_CONFIG_PATH
+
+# CPPFLAGS
+CFLAGS         ?= -g -Wall
+# CCFLAGS
+# CXXFLAGS
+# LDFLAGS
+# LIBS
+
+CXXPATTERNS    ?= %.cc %.cp %.cxx %.cpp %.CPP %.c++ %.C
+
+PREFIX         ?= /usr/local
+BINDIR         ?= $(PREFIX)/bin
+SBINDIR                ?= $(PREFIX)/sbin
+LIBDIR         ?= $(PREFIX)/lib
+PLUGINDIR      ?= $(LIBDIR)
+DATADIR                ?= $(PREFIX)/share
+SYSCONFDIR     ?= /etc
+
+# DESTDIR
+
+# LD_LIBRARY_PATH
+
+include build/common.mk
+
+ifneq ($(wildcard $(O)/config.mk),)
+ include $(O)/config.mk
+else
+ -include $(O).mk
+endif
+
+export O V
+export CONFIG
+export CC CXX CCLINKER CXXLINKER AR
+export PKG_CONFIG PKG_CONFIG_PATH
+export CPPFLAGS CFLAGS CCFLAGS CXXFLAGS LDFLAGS LIBS
+export CXXPATTERNS
+export PREFIX BINDIR SBINDIR LIBDIR PLUGINDIR DATADIR SYSCONFDIR
+export DESTDIR
+export TEST_LIBRARY_PATH := $(LD_LIBRARY_PATH)
+
+DIST           ?= $(BINARIES) $(LIBRARIES) $(PLUGINS)
+DO_DIST                := $(filter-out $(NODIST),$(DIST))
+
+LIBRARY_TARGETS        := $(foreach L,$(LIBRARIES),$(L)-shared $(L)-static)
+PLUGIN_TARGETS := $(foreach L,$(PLUGINS),$(L)-plugin)
+TARGETS                := $(BINARIES) $(TESTS) $(LIBRARY_TARGETS) $(PLUGIN_TARGETS)
+CHECK_TARGETS  := $(foreach T,$(TESTS),check-$(T))
+INSTALL_TARGETS        := $(foreach I,$(DO_DIST),install-$(I))
+
+build:
+check: $(CHECK_TARGETS)
+all: build check
+install: $(INSTALL_TARGETS)
+
+clean:
+       $(QUIET) test "$(PWD)" && readlink -f "$(O)" | grep -q "^$(PWD)/."
+       $(call echo,Remove,$(O))
+       $(QUIET) if [ -d "$(O)" ]; then rm -r "$(O)"; fi
+
+makefile       = $(firstword $(wildcard $(1).mk) $(1)/build.mk)
+librarymakefile        = $(call makefile,$(patsubst %-static,%,$(patsubst %-shared,%,$(1))))
+librarytarget  = build-$(call lastword,$(subst -, ,$(1)))
+pluginmakefile = $(call makefile,$(patsubst %-plugin,%,$(1)))
+plugintarget   = build-$(call lastword,$(subst -, ,$(1)))
+testmakefile   = $(call makefile,$(patsubst check-%,%,$(1)))
+distmakefile   = $(call makefile,$(patsubst install-%,%,$(1)))
+
+$(BINARIES) $(TESTS):
+       $(QUIET) $(MAKE) --no-print-directory -f $(call makefile,$@) build
+
+$(PLUGIN_TARGETS):
+       $(QUIET) $(MAKE) --no-print-directory -f $(call pluginmakefile,$@) $(call plugintarget,$@)
+
+$(LIBRARY_TARGETS):
+       $(QUIET) $(MAKE) --no-print-directory -f $(call librarymakefile,$@) $(call librarytarget,$@)
+
+$(CHECK_TARGETS): $(TESTS)
+       $(QUIET) $(MAKE) --no-print-directory -f $(call testmakefile,$@) check
+
+$(INSTALL_TARGETS): $(DO_DIST)
+       $(QUIET) $(MAKE) --no-print-directory -f $(call distmakefile,$@) install
+
+.PHONY: build install all check clean
+.PHONY: $(TARGETS) $(CHECK_TARGETS) $(INSTALL_TARGETS) $(LIBRARIES) $(PLUGINS)
diff --git a/build/target.mk b/build/target.mk
new file mode 100644 (file)
index 0000000..40bc88f
--- /dev/null
@@ -0,0 +1,18 @@
+$(if $(NAME),,$(error NAME not specified for target))
+$(if $(SOURCES),,$(error SOURCES not specified for target))
+
+-include build/pkgconfig.mk
+
+CONFIGFLAGS    := $(if $(CONFIG),-include $(CONFIG),)
+GENERICFLAGS   := $(CPPFLAGS) $(CONFIGFLAGS) $(CFLAGS) -c -MD
+PICFLAGS       := -DPIC -fPIC
+
+-include build/c.mk
+-include build/c++.mk
+
+OBJECTS                := $(patsubst %,$(O)/obj/%.o,$(basename $(SOURCES)))
+PIC_OBJECTS    := $(patsubst %,$(O)/obj/%.os,$(basename $(SOURCES)))
+
+-include $(SOURCES:%=$(O)/obj/%.d)
+
+$(OBJECTS) $(PIC_OBJECTS): $(CONFIG)
diff --git a/build/test.mk b/build/test.mk
new file mode 100644 (file)
index 0000000..3fba503
--- /dev/null
@@ -0,0 +1,7 @@
+include build/binary.mk
+
+check: build
+       $(call echo,Check,$(O_NAME))
+       $(QUIET) LD_LIBRARY_PATH=$(O)/lib:$(TEST_LIBRARY_PATH) $(O_NAME)
+
+.PHONY: check