#
# --- GSMP-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the GSMP-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# GSMP: Rules
# General Sound Manipulation Program is Copyright (C) 2000 - 2003
#   Valentin Ziegler and Ren Rebe
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2. A copy of the GNU General
# Public License can be found at LICENSE.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
# ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
# 
# --- GSMP-COPYRIGHT-NOTE-END ---

# The Rules containing all the tricky rules - some details
# are inspired from the Linux 2.6 kbuild Makefiles ...

# Set the TOPDIR
ifeq ($(origin TOPDIR), undefined)
        TOPDIR = .
endif

# Set CCD (current compile dir) to an initial .
ifeq ($(origin CCD), undefined)
        CCD = .
endif

# Do not print "Entering directory ..."
MAKEFLAGS += --no-print-directory

# Beautify output
Q = @

HOSTCC		= gcc
HOSTCXX		= g++
HOSTCFLAGS		+= -pipe
override HOSTCFLAGS	+= -Wall -Wshadow
HOSTCXXFLAGS		+= -pipe
override HOSTCXXFLAGS	+= -Wall -Wshadow

# set CROSS_COMPILE to the arch you want to compile for ...

CC		= $(CROSS_COMPILE)gcc
CXX		= $(CROSS_COMPILE)g++
CFLAGS			+= -pipe
override CFLAGS		+= -Wall -ggdb
CXXFLAGS		+= -pipe
override CXXFLAGS	+= -Wall -ggdb

# some default rules

# enable pch if supported
ifeq ($(PCH),1)
  all: $(PCHS)
endif

# enable pch STL if supported
ifeq ($(PCSTL),1)
  override CXXFLAGS += -include bits/stdc++.h -Winvalid-pch
endif

.PHONY: subdirs $(SUBDIRS)
subdirs: $(SUBDIRS)

# do not remove intermediate object files
.PRECIOUS: %.o
# do not remove generated files
.PRECIOUS: $(GENERATED)

# a tiny help for manual use
define LINK
	@echo '  LINK      $@'
        $(Q)$(CXX) $(CXXFLAGS) $(INCLUDES) $(LIBS) $(LDFLAGS) -o $@ $^
endef

# prevent warning for dirs with subdirs only
ifneq ($(DEPS),)
    -include $(DEPS)
endif

$(SUBDIRS):
	@echo '  ENTERING  $(CCD)/$@ ...'
	$(Q)$(MAKE) -C $@ CCD="$(CCD)/$@" $(MAKECMDGOALS)
	@echo '  LEAVING   $(CCD)/$@ ...'

# Pre Compiled Headers
%.hh.gch: %.hh
	@echo '  PCH       $@'
	$(Q)$(CXX) $(CXXFLAGS) $(INCLUDES) $^ || true

%: %.o
	@echo '  LINK EXEC $@'
	$(Q)$(CXX) $(CXXFLAGS) $(INCLUDES) -o $@ $^ $(LIBS)

# the ugly sed code is needed for creation of empty targets - so removed
# dependencies are ignored - and the target rebuilds as expected
%.o: %.cc
	@echo '  DEP       $(@:.o=.d)'
	$(Q)$(CXX) -MM $(CXXFLAGS) $(INCLUDES) $< | sed '/^#.*/d' > $(@:.o=.d)
	$(Q)sed -e 's/^[^:]*: *//' -e 's/ \\$$//' \
	        -e 's/^ *//g' -e 's/ /:\n/g' -e 's/$$/:/' $(@:.o=.d) >> $(@:.o=.d)
	@echo '  CXX       $@'
	$(Q)$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

# the -Xlinker -rpath mangling is needed to find those libraries during
# linking for other objects
%.so: $(BUILD_OBJS)
	@echo '  LINK SO   $@'
	$(Q)$(CXX) -shared $(CXXFLAGS) $(LDFLAGS) $(LIBS) \
$(patsubst -L%, -Xlinker -rpath %, $(filter -L%, $(LIBS))) -o $@ $^

.PHONY: clean-deps
clean-deps: subdirs
	@rm -vf $(DEPS)

PHONY: clean-pch
clean-pch: subdirs
	@rm -vf $(PCHS)

.PHONY: clean
clean: subdirs
	@rm -vf $(BUILD_OBJS) $(BUILD_LIBS) $(BUILD_PROGS) $(GENERATED) $(DEPS)
	@rm -vf conftest.* config.{tmp,log}

.PHONY: distclean
distclean: subdirs clean clean-pch
	@rm -vf *~ *.d $(PCHS)
	@rm -vf config.{h,make}

