ifneq (yes, $(INCLUDING_CHILD_MAKEFILES))
COMMANDS := all install clean distclean test depclean osx-bundle osx-dmg test-automation
.PHONY: $(COMMANDS)
.DEFAULT_GOAL := all

# Would be nice to move this somewhere else (Makefile.inc?)
ifeq (yes, $(BUILD_DARWIN))
CFLAGS += -mmacosx-version-min=10.7 -gfull
CXXFLAGS += -mmacosx-version-min=10.7 -gfull
LDFLAGS += -mmacosx-version-min=10.7 -Wl,-dead_strip -pagezero_size 10000 -image_base 100000000
LIB_SHARED_LINK = $(LIB_SHARED_LINK_OSX)
endif

LIB_TARGETS := $(addprefix $(TOP)lib/,$(LIB:%=lib%.a))

# Handle per-target flags in the form foo_CFLAGS := -w by mapping them to all
# of the objects a target depends on. This has potentially dumb results if
# multiple targets use a single object file, so don't do that.
define set_target_flags
  OBJ += $($1_OBJ)

  $($1_OBJ): CPPFLAGS := $(CPPFLAGS) $($1_CPPFLAGS)
  $($1_OBJ): CXXFLAGS := $(CXXFLAGS) $($1_CXXFLAGS)
  $($1_OBJ): OBJCXXFLAGS := $(CXXFLAGS) $($1_CXXFLAGS)

  ifeq (yes, $(PRECOMPILED_HEADER))
    ifdef $1_PCH
      CLEANFILES += $($1_PCH).gch
      $($1_OBJ): CXXFLAGS += -include $($1_PCH) $(PCHFLAGS)
      $($1_OBJ): $($1_PCH).gch
      $($1_PCH).gch: $($1_PCH)
      $($1_PCH).gch: CPPFLAGS := $(CPPFLAGS) $($1_CPPFLAGS)
      $($1_PCH).gch: CXXFLAGS := $(CXXFLAGS) $($1_CXXFLAGS)
    else
      $($1_OBJ): CXXFLAGS += -include acconf.h
    endif
  else
    $($1_OBJ): CXXFLAGS += -include acconf.h
  endif
endef
$(foreach target,$(LIB),$(eval $(call set_target_flags,$(target),$(TOP)lib/lib$(target).a)))
$(foreach target,$(PROGRAM),$(eval $(call set_target_flags,$(notdir $(target)),$(target))))

# Create the build and install targets for programs
# Not done with a pattern rule since the pattern would be just %: and doing that
# leads to make trying to use gcc to make any nonexistent targets rather than
# erroring
define create_program_targets
  ifdef $1_INSTALLNAME
install: $(DESTDIR)$(P_BINDIR)/$($1_INSTALLNAME)
$(DESTDIR)$(P_BINDIR)/$($1_INSTALLNAME): $(dir $2)$($1_INSTALLNAME)
	@$(BIN_MKDIR) -p $(DESTDIR)$(P_BINDIR)
	@$(BIN_INSTALL) $(dir $2)$($1_INSTALLNAME) $(DESTDIR)$(P_BINDIR)/$($1_INSTALLNAME)
$(dir $2)$($1_INSTALLNAME): $($1_OBJ)
	$(BIN_CXX) -o $(dir $2)$($1_INSTALLNAME) $(LDFLAGS) $($1_OBJ) $(LIBS) $($1_LIBS)
all: $(dir $2)$($1_INSTALLNAME)
  else
$2: $($1_OBJ)
	$(BIN_CXX) -o $2 $(LDFLAGS) $($1_OBJ) $(LIBS) $($1_LIBS)
all: $2
  endif
  $(eval CLEANFILES += $(filter-out %/,$2 $(dir $2)$($1_INSTALLNAME)))
endef
$(foreach target,$(PROGRAM),$(eval $(call create_program_targets,$(notdir $(target)),$(target))))

# The dependency files which will be automatically generated by gcc.
# The filter is due to that libraries also appear in OBJ
DEP := $(filter %.d,$(OBJ:%.o=%.d))

# If the goal is a relative path to a file, convert it to an absolute path
ifneq ($(filter-out $(COMMANDS),$(MAKECMDGOALS)),)
ABSGOAL := $(abspath $(MAKECMDGOALS))
ifneq ($(MAKECMDGOALS),$(ABSGOAL))
$(MAKECMDGOALS): $(ABSGOAL)	;
endif
endif

# Primary build targets
all: $(LIB_TARGETS)

clean:
	$(BIN_RM) -f $(OBJ) $(CLEANFILES) $(LIB_TARGETS)

distclean: clean
	$(BIN_RM) -rf $(DISTCLEANFILES) $(DEP)

depclean: clean
	$(BIN_RM) -rf $(DEP)

install:

# The actual build rules
.SUFFIXES:

CXX_CMD = $(CXX_ENV) $(BIN_CXX) $(CFLAGS_DEP) $(CPPFLAGS)
CC_CMD  = $(CXX_ENV) $(BIN_CC) $(CFLAGS_DEP) $(CPPFLAGS)
POST_FLAGS = $($@_FLAGS) -c -o $@ $<

%.o: %.c	; $(CC_CMD) $(CFLAGS) $(POST_FLAGS)
%.o: %.cpp	; $(CXX_CMD) $(CXXFLAGS) $(POST_FLAGS)
%.o: %.cxx	; $(CXX_CMD) $(CXXFLAGS) $(POST_FLAGS)
%.o: %.cc	; $(CXX_CMD) $(CXXFLAGS) $(POST_FLAGS)
%.o: %.mm	; $(CXX_CMD) $(OBJCXXFLAGS) $(POST_FLAGS)
%.o: %.m	; $(CC_CMD) $(CFLAGS) -fmodules $(POST_FLAGS)

%.gch: %
	@$(BIN_RM) -f $@
	$(CXX_ENV) $(BIN_CXX) $(CPPFLAGS) $(CXXFLAGS) -x c++-header $<

.SECONDEXPANSION:

# Libraries contain all object files they depend on (but they may depend on other files)
# Not using libtool on OS X because it has an unsilenceable warning about a
# compatibility issue with BSD 4.3 (wtf)
lib%.a: $$($$*_OBJ)
	@$(BIN_MKDIR_P) $(dir $@)
	$(BIN_AR) cru $@ $(filter %.o,$^)
	$(BIN_RANLIB) $@

-include $(DEP)
endif
