#***********************************************************************
#
# 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 -g -O3 $(BITS) -I ../../clu -I ../../common -I ../src $(EXTRA_FLAGS)
LDFLAGS		= -lOpenCL  $(BITS) -lm -lstdc++ 

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

PROGS		= fluid fluid_gl fluid_remote
KERNELS		= fluid_solver_lm_vec4.cl fluid_solver_vec4.cl
OBJS		= fluid.o clock.o solver_host.o solver_accl.o clu.o 
GL_OBJS 	= fluid_gl.o solver_accl_gl.o gl_viewer.o 
REMOTE_OBJS	= fluid_remote.o solver_accl_remote.o remote_viewer.o network_utils.o
LINKED_SRC	= fluid_gl.c solver_accl_gl.c fluid_remote.c solver_accl_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)


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

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

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

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

endif

# Linked source code
fluid_gl.c:
	@ln -s -f ../src/fluid.c $@
solver_accl_gl.c:
	@ln -s -f ../src/solver_accl.c $@
fluid_remote.c:
	@ln -s -f ../src/fluid.c $@
solver_accl_remote.c:
	@ln -s -f ../src/solver_accl.c $@
fluid_solver_vec4.cl:
	ln -s -f ../src/$@
fluid_solver_lm_vec4.cl:
	ln -s -f ../src/$@

-include $(DEPS)

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