.PHONY: default o_objects all clean distclean depend

include ../Make.include

## the following gives us all files ending with *.cpp, BUT those 
## with whitespaces in it, and removes duplicates (e.g. because of whitespaces)
SOURCES=$(sort $(filter %.cpp, $(wildcard *.cpp)))

O_OBJECTS = $(SOURCES:.cpp=.o)

$(OBJDIR):
	mkdir $(OBJDIR)

o_objects: $(OBJDIR) $(O_OBJECTS)

## dependencies: as proposed by the GNU-make documentation
## see http://www.gnu.org/software/make/manual/html_node/make_47.html#SEC51
ifeq (,$(findstring clean, $(MAKECMDGOALS)))
## don't include when running clean
-include $(SOURCES:.cpp=.d)
endif

%.d: %.cpp
	@set -e; rm -f $@; \
	 $(CXX) $(MAKEDEP_FLAGS) $(GEM_EXTRA_CPPFLAGS) $(GEM_CXXFLAGS) $< > $@.$$$$; \
	 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	 rm -f $@.$$$$

clean:
	-rm -f $(O_OBJECTS:%=$(OBJDIR)%)
	-rm -f $(O_OBJECTS)
	-rm -f $(SOURCES:.cpp=.d) 
	-rm -f *~

distclean: clean
	-rm -f *.o *.d *~
	$(DOS2UNIX) *.cpp *.h

$(O_OBJECTS): %.o : %.cpp
	$(CXX) -c  $(GEM_EXTRA_CXXFLAGS) $(GEM_CXXFLAGS) $*.cpp -o $(OBJDIR)$*.o

depend: $(SOURCES:.cpp=.d)

