# /* LICENSE:
#   =========================================================================
#     CMPack'03 Source Code Release for OPEN-R SDK v1.0
#     Copyright (C) 2003 Multirobot Lab [Project Head: Manuela Veloso]
#     School of Computer Science, Carnegie Mellon University
#     All rights reserved.
#   ========================================================================= */

# Set the architecture if the cpu is detected
ifneq ($(shell grep -i "model name.*athlon" /proc/cpuinfo),)
  CPU := -march=athlon
endif
ifneq ($(shell grep -i "model name.*pentium" /proc/cpuinfo),)
  CPU := -march=pentium
endif

#======================================================================#
# Compilation options
#======================================================================#

CC := g++
CFLAGS := -Wall
CFLAGS += -ffast-math $(CPU)
CFLAGS += -O2
# CFLAGS += -g
# CFLAGS += -pg
# CFLAGS += -D_GNU_SOURCE
# CFLAGS += -DUSE_AMBIGUOUS

FIXDEP := ../bin/dep_process

DEF :=
BIN := .

# Extra libraries if required
LIBS := -lz
LIBS += -L/usr/X11R6/lib -lX11
#LIBS += -lpthread
LIBS += -lMagick++ -lMagick

# all non-local includes should be relative to top level
CFLAGS += -I. -I../shared -I../..
CFLAGS += -I/usr/include/X11

# Each module will add to this
ALL_SRC :=
ALL_TARGETS :=

all: all_real

#======================================================================#
# Target description
#======================================================================#

SRC := $(wildcard *.cc) ../shared/image.cc
TARGET := thresh

THRESH_OBJ := $(SRC:.cc=.o)

ALL_SRC += $(SRC)
ALL_TARGETS += $(TARGET)

# if BIN is ".", then we don't need the following dependency
# $(TARGET): $(BIN)/$(TARGET)

$(BIN)/$(TARGET): $(THRESH_OBJ)
	$(CC) -o $@ $(CFLAGS) $(THRESH_OBJ) $(LIBS)

#======================================================================#
# Compilation and rules
#======================================================================#

# Determine the object files
OBJ := \
	$(patsubst %.cc,%.o,$(filter %.cc,$(ALL_SRC))) \
	$(patsubst %.c,%.o,$(filter %.c,$(ALL_SRC)))

# Determine the target files
ALL_BIN_TARGETS := $(addprefix $(BIN)/,$(ALL_TARGETS))

# Include the dependencies
include $(OBJ:.o=.d)

.SUFFIXES:

%.o: %.cc
	$(CC) $(CFLAGS) $(DEF) $(INC) -c $< -o $@

# Calculate dependencies
%.d: %.cc
	$(CC) $(CFLAGS) -M $< | $(FIXDEP) $(dir $<) > $@

all_real: $(ALL_BIN_TARGETS)
	(cd ../bin;\rm -rf $(TARGET);ln -s ../thresh/$(TARGET))

clean:
	rm -f $(OBJ) $(OBJ:.o=.d) $(ALL_BIN_TARGETS) ../bin/$(TARGET)

strip:
	strip -v --strip-unneeded $(ALL_BIN_TARGETS)

# don't need to do anything extra since dependencies are built automatically
depend:
	rm -f $(OBJ:.o=.d)
