#***********************************************************************
#
# Licensed Materials - Property of IBM
#
# (C) Copyright IBM Corp. 2010
# All Rights Reserved
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#***********************************************************************

CC		= gcc
BITS		= -m64
CFLAGS		= -Wall -I. -I../src -I ../../clu -I../../common -O3 $(BITS) $(EXTRA_FLAGS)
LDFLAGS		= -lOpenCL $(BITS) -lm -lstdc++

vpath	%.c	../src ../../clu ../../common
vpath 	%.h 	../src ../../clu ../../common

PROGS           = perlin perlin_gl perlin_remote
KERNELS		= perlin_kernel.cl
OBJS		= perlin.o perlin_host.o clock.o clu.o
GL_OBJS 	= perlin_gl.o gl_viewer.o 
REMOTE_OBJS	= perlin_remote.o remote_viewer.o network_utils.o
LINKED_SRC	= perlin_gl.c perlin_remote.c

GLUT_H		= /usr/include/GL/glut.h

DEPS    	= $(OBJS:.o=.d) $(GL_OBJS:.o=.d) $(REMOTE_OBJS:.o=.d)


$(GL_OBJS)     $(GL_OBJS:.o=.d):     EXTRA_FLAGS := -DGL_VIEWER
$(REMOTE_OBJS) $(REMOTE_OBJS:.o=.d): EXTRA_FLAGS := -DREMOTE_VIEWER


%.d: %.c
	@$(CC) -M $(CFLAGS) $< | sed 's/$*.o/& $@/g' > $@


.PHONY: all 
all:	$(PROGS) $(KERNELS)


perlin: $(OBJS)
	$(CC) -o $@ $^ $(LDFLAGS)

ifeq ($(wildcard $(GLUT_H)), $(GLUT_H))
perlin_gl:  $(GL_OBJS) clock.o clu.o
	$(CC) -o $@ $^ $(LDFLAGS) -lglut

perlin_remote: $(REMOTE_OBJS) clock.o clu.o
	$(CC) -o $@ $^ $(LDFLAGS)
else
perlin_gl:
	@echo "FYI: Not building perlin_gl because glut is not installed."

perlin_remote:
	@echo "FYI: Not building perlin_remote because glut is not installed."
endif

# Linked source code
perlin_gl.c:
	@ln -s -f ../src/perlin.c $@

perlin_remote.c:
	@ln -s -f ../src/perlin.c $@

perlin_kernel.cl:
	ln -s -f ../src/$@

.PHONY: clean
clean:
	@rm -f $(PROGS) *.ocl_bin $(OBJS) $(GL_OBJS) $(REMOTE_OBJS) $(KERNELS) $(LINKED_SRC) $(DEPS)

-include $(DEPS)
