Permalink
Browse files

Refactor so that the Makefile includes the engine files.

Also add the dependency for configured executables
(i.e. 'make program-debug' works as well as 'make debug' now).
  • Loading branch information...
1 parent 8aafced commit cbcc476875662ab6d7cbabf14458ed63ecbb5e20 Scott Bronson committed May 23, 2007
Showing with 157 additions and 148 deletions.
  1. +14 −131 project/Makefile
  2. +0 −17 project/build.mk
  3. +122 −0 project/scripts/build.mk
  4. +21 −0 project/scripts/defaults.mk
View
@@ -1,137 +1,20 @@
-# Makefile example
-# Scott Bronson
-# 22 Apr 2007
-#
-# TODO: swap the sense. i.e. build.mk should be the Makefile, and then
-# at the end it should include build.mk to implement the mechanism.
-# TODO: need a lot of tmtest tests.
-#
-# This file demonstrates how to incrementally build files from anywhere
-# on your filesystem in a single object directory with full dependencies.
-#
-# To use a different object directory:
-# make OBJDIR=/tmp/obj
-#
-# To ignore the makefile when calculating dependencies (if, for example,
-# you're tweaking the build and there's no need to regenerate the objects
-# each time)
-# make IGNORE_MAKEFILE=1
+include scripts/defaults.mk
+# This describes all configurations
+CONFIGS += headless headless-debug
-# By default we have only a single configuration: the empty one.
-# You can add more configurations in your build.mak file.
-CONFIGS = release debug
-CUSTOMFILE = build.mk
+# All configurations share these settings
+CPPFLAGS += -I ../libs
+SOURCE += ./main.cpp \
+ ../libs/tv/a.c \
+ ../libs/tv/b.cpp \
-# default flag settings (feel free to override in your build.mk file):
-CFLAGS_release += -O2 -Wall
-CXXFLAGS_release += -O2 -Wall
-CFLAGS_debug += -O0 -g -Wall
-CXXFLAGS_debug += -O0 -g -Wall
-# compile the subversion revision into the executable
-CPPFLAGS += -DVERSION=r$(shell svnversion .)
+# And these settings are specific to the various configurations.
+CFLAGS_headless += $(CFLAGS_release) -DHEADLESS
+CXXFLAGS_headless += $(CXXFLAGS_release) -DHEADLESS
+CFLAGS_headless-debug += $(CFLAGS_debug) -DHEADLESS
+CXXFLAGS_headless-debug += $(CXXFLAGS_debug) -DHEADLESS
-# By default objects go into objCONF in current dir.
-# Pass a different OBJDIR to place them somewhere else.
-OBJNAME = obj
-PROGNAME = program
-
-
-# This file needs to define a single variable, SOURCE, that
-# tells the location of each file that we will compile. The
-# files can be from anywhere in the filesystem.
-include $(CUSTOMFILE)
-
-CONFIG ?= $(firstword $(CONFIGS))
-
-
-# 'make' with no args is equivalent to 'make release'
-ifeq ($(CONFIG), $(firstword $(CONFIGS)))
-EXECUTABLE := $(PROGNAME)
-OBJDIR := $(OBJNAME)
-CLEANFILES += $(EXECUTABLE) $(OBJDIR) $(addprefix $(PROGNAME)-, $(CONFIGS)) $(addprefix obj-, $(CONFIGS))
-else
-EXECUTABLE := $(PROGNAME)-$(CONFIG)
-OBJDIR := $(OBJNAME)-$(CONFIG)
-CLEANFILES := $(EXECUTABLE) $(OBJDIR)
-endif
-
-
-# Copy the config-specific settings into the global vars
-COPYVARS += SOURCE LDLIBS ARFLAGS ASFLAGS CFLAGS CXXFLAGS COFLAGS CPPFLAGS FFLAGS GFLAGS LDFLAGS LFLAGS YFLAGS PFLAGS RFLAGS LINTFLAGS
-$(foreach V,$(COPYVARS),$(eval $(V) += $$($(V)_$(CONFIG))))
-
-
-# done with configuration, time for action
-
-FILES := $(notdir $(SOURCE))
-OBJFILES := $(addprefix $(OBJDIR)/, $(addsuffix .o, $(FILES)))
-DEPFILES := $(addprefix $(OBJDIR)/, $(addsuffix .d, $(FILES)))
-
-# Set up an assoc array to find the original file from its dependency.
-$(foreach V,$(SOURCE),$(eval file_$(notdir $(V)) = $(V)))
-
-
-all: $(EXECUTABLE)
-.PHONY: all
-
--include $(DEPFILES)
-
-# Using directories as build targets is not reliable. Targets tend to
-# get built far more often than they should. Therefore, we'll use a random
-# file to get make to tell us when the directory needs to be recreated.
-$(OBJDIR)/build-tag:
- mkdir -p $(OBJDIR)
- touch $@
-
-$(EXECUTABLE): $(OBJDIR)/$(EXECUTABLE)
- cp $< $@
-
-.PHONY: clean
-clean:
- rm -rf $(CLEANFILES)
-
-
-
-# Add targets for each configuration. Right now we support
-# 'make target' and 'make target-clean' (where target is a name of
-# one of your configurations.
-
-define ADD_TARGET
-.PHONY: $(1)
-$(1):
- @$$(MAKE) --no-print-directory CONFIG=$(1)
-
-.PHONY: $(1)-clean
-$(1)-clean:
- @$$(MAKE) --no-print-directory CONFIG=$(1) clean
-endef
-
-$(foreach conf,$(CONFIGS),$(eval $(call ADD_TARGET,$(conf))))
-
-
-
-# ---- tool targets:
-
-# Every build target needs to depend on these
-COMMONDEPS=$(OBJDIR)/build-tag
-ifeq ($(IGNORE_MAKEFILE),)
-COMMONDEPS+=$(CUSTOMFILE) Makefile
-endif
-
-# C++ compiler
-$(OBJDIR)/%.cpp.o: $(COMMONDEPS)
- $(CXX) -c -MD -MP $(CPPFLAGS) $(CXXFLAGS) $(file_$(basename $(notdir $@))) -o $@
-
-# C compiler
-$(OBJDIR)/%.c.o: $(COMMONDEPS)
- $(CC) -c -MD -MP $(CPPFLAGS) $(CFLAGS) $(file_$(basename $(notdir $@))) -o $@
-
-# Linker
-# Always use CXX because CC can't do C linkage.
-# Pass CXX flags so linker knows the status of -O.
-$(OBJDIR)/$(EXECUTABLE): $(COMMONDEPS) $(OBJFILES)
- $(CXX) $(CXXFLAGS) $(OBJFILES) $(LDLIBS) -o $@
-
+include scripts/build.mk
View
@@ -1,17 +0,0 @@
-# This describes all configurations in addition to the default (empty) config.
-CONFIGS += headless headless-debug
-
-# All configurations share these settings
-CPPFLAGS += -I ../libs
-
-SOURCE += ./main.cpp \
- ../libs/tv/a.c \
- ../libs/tv/b.cpp \
-
-# And these settings are specific to the various configurations.
-CFLAGS_headless += $(CFLAGS_release) -DHEADLESS
-CXXFLAGS_headless += $(CXXFLAGS_release) -DHEADLESS
-
-CFLAGS_headless-debug += $(CFLAGS_debug) -DHEADLESS
-CXXFLAGS_headless-debug += $(CXXFLAGS_debug) -DHEADLESS
-
View
@@ -0,0 +1,122 @@
+# Makefile engine
+# Scott Bronson
+# 22 Apr 2007
+#
+# This file contains the engine to allow you to make files from anywhere
+# on your filesystem in a single object directory with full dependencies.
+#
+#
+# To use a different object directory:
+# make OBJDIR=/tmp/obj
+#
+# To ignore the makefile when calculating dependencies (if, for example,
+# you're tweaking the build and there's no need to regenerate the objects
+# each time)
+# make IGNORE_MAKEFILE_DEPS=1
+
+
+# Save list of included Makefiles for later before we include depfiles.
+# This tells us to rebuild everything if any of the makefiles changes,
+# see IGNORE_MAKEFILE_DEPS in the documentation.
+SAVED_MAKEFILE_LIST := $(MAKEFILE_LIST)
+
+# Use the default config if it hasn't been set already.
+CONFIG ?= $(firstword $(CONFIGS))
+
+ifeq ($(CONFIG), $(firstword $(CONFIGS)))
+# If building the default config, don't use the config as a suffix.
+EXECUTABLE := $(PROGNAME)
+OBJDIR := $(OBJNAME)
+CLEANFILES += $(EXECUTABLE) $(OBJDIR) $(addprefix $(PROGNAME)-, $(CONFIGS)) $(addprefix obj-, $(CONFIGS))
+else
+# Otherwise, if not the default config, everything gets a suffix.
+EXECUTABLE := $(PROGNAME)-$(CONFIG)
+OBJDIR := $(OBJNAME)-$(CONFIG)
+CLEANFILES := $(EXECUTABLE) $(OBJDIR)
+endif
+
+
+# Copy the config-specific settings into the global vars
+# If your Makefile has custom variables that you'd like to copy
+# as well, just add them to COPYVARS and they'll be copied too.
+COPYVARS += SOURCE LDLIBS ARFLAGS ASFLAGS CFLAGS CXXFLAGS COFLAGS CPPFLAGS FFLAGS GFLAGS LDFLAGS LFLAGS YFLAGS PFLAGS RFLAGS LINTFLAGS
+$(foreach V,$(COPYVARS),$(eval $(V) += $$($(V)_$(CONFIG))))
+
+
+# We're done with the configuration, time for action
+
+FILES := $(notdir $(SOURCE))
+OBJFILES := $(addprefix $(OBJDIR)/, $(addsuffix .o, $(FILES)))
+DEPFILES := $(addprefix $(OBJDIR)/, $(addsuffix .d, $(FILES)))
+
+# Set up an assoc array to find the original file from its dependency.
+$(foreach V,$(SOURCE),$(eval file_$(notdir $(V)) = $(V)))
+
+
+all: $(EXECUTABLE)
+.PHONY: all
+
+-include $(DEPFILES)
+
+# Using directories as build targets is not reliable. Targets tend to
+# get built far more often than they should. Therefore, we'll use a random
+# file to get make to tell us when the directory needs to be recreated.
+$(OBJDIR)/build-tag:
+ mkdir -p $(OBJDIR)
+ touch $@
+
+$(EXECUTABLE): $(OBJDIR)/$(EXECUTABLE)
+ cp $< $@
+
+.PHONY: clean
+clean:
+ rm -rf $(CLEANFILES)
+
+
+
+# Add targets for each configuration. Right now we support
+# 'make target' and 'make target-clean' (where target is a name of
+# one of your configurations. Of course, we also support the
+# executable-config dependency (i.e. program-debug).
+
+define ADD_TARGET
+.PHONY: $(1)
+$(1): $(EXECUTABLE)-$(1)
+
+$(EXECUTABLE)-$(1):
+ @$$(MAKE) --no-print-directory CONFIG=$(1)
+
+.PHONY: $(1)-clean
+$(1)-clean:
+ @$$(MAKE) --no-print-directory CONFIG=$(1) clean
+endef
+
+$(foreach conf,$(CONFIGS),$(eval $(call ADD_TARGET,$(conf))))
+
+
+
+# ---- tool targets:
+#
+# Each tool produces both its target (%.cpp.o) AND a dependencies file
+# that includes all dependencies for the target (%.cpp.d).
+
+# Every tool target needs to depend on COMMONDEPS
+COMMONDEPS+=$(OBJDIR)/build-tag
+ifeq ($(IGNORE_MAKEFILE_DEPS),)
+COMMONDEPS+=$(SAVED_MAKEFILE_LIST)
+endif
+
+# C++ compiler
+$(OBJDIR)/%.cpp.o: $(COMMONDEPS)
+ $(CXX) -c -MD -MP $(CPPFLAGS) $(CXXFLAGS) $(file_$(basename $(notdir $@))) -o $@
+
+# C compiler
+$(OBJDIR)/%.c.o: $(COMMONDEPS)
+ $(CC) -c -MD -MP $(CPPFLAGS) $(CFLAGS) $(file_$(basename $(notdir $@))) -o $@
+
+# Linker
+# Always use CXX because CC can't do C linkage.
+# Pass CXX flags so linker knows the status of -O.
+$(OBJDIR)/$(EXECUTABLE): $(COMMONDEPS) $(OBJFILES)
+ $(CXX) $(CXXFLAGS) $(OBJFILES) $(LDLIBS) -o $@
+
@@ -0,0 +1,21 @@
+# default values for the makefile engine.
+#
+# You will almost certainly want to override PROGNAME.
+# You may also want to add some configs of your own, and probably
+# also want o add -Werror to your debug configurations.
+
+PROGNAME = program
+OBJNAME = obj
+
+# Default configurations:
+CONFIGS += release debug
+
+# Settings for each configuration
+CFLAGS_release += -O2 -Wall
+CXXFLAGS_release += -O2 -Wall
+CFLAGS_debug += -O0 -g -Wall
+CXXFLAGS_debug += -O0 -g -Wall
+
+# And we always want the version number added
+CPPFLAGS += -DVERSION=r$(shell svnversion .)
+

0 comments on commit cbcc476

Please sign in to comment.