##
## Copyright (c) 2002 Sun Microsystems, Inc.
##
## Modified by Richard Jones, 2005-6
## Darwin rules added by John Reppy
##
## See the file "license.terms" for information on usage and redistribution
## of this file, and for a DISCLAIMER OF ALL WARRANTIES.
##

##
## GCspy - Makefile for the C code
##

UNAME=$(shell uname -s)
ifeq ($(UNAME), SunOS)
	EXTRA_CC_FLAGS = -D_SPARC_
	EXTRA_LD_FLAGS = -lsocket
	DYNAMIC_FLAG = -G
	LIB_SUFFIX = so
	LD = gcc
#	CC = cc
	CC = gcc
endif

ifeq ($(UNAME), Linux)
	EXTRA_CC_FLAGS = -D_LINUX_
	EXTRA_LD_FLAGS = -lpthread
	DYNAMIC_FLAG = -shared
	LD = gcc
	LIB_SUFFIX = so
	CC = gcc
endif

ifeq ($(UNAME), Darwin)
	EXTRA_CC_FLAGS = -dynamic -D_LINUX_
	DYNAMIC_FLAG = -dynamiclib
	LIB_SUFFIX = dylib
	LD = cc
	CC = cc
	GCSPY_LIB = -Llib -lgcspy
endif

CC_FLAGS = $(EXTRA_CC_FLAGS)
LD_FLAGS = -L$(LIB_DIR) $(EXTRA_LD_FLAGS) -lgcspy -levmgcspy -lhsgcspy -mt

OBJ_DIR = obj
BIN_DIR = bin
LIB_DIR = lib

SRC_DIR = ./src
INCLUDE_DIR = ./include

CORE_LIB_NAME = libgcspy.$(LIB_SUFFIX)
EVM_LIB_NAME = libevmgcspy.$(LIB_SUFFIX)
HS_LIB_NAME = libhsgcspy.$(LIB_SUFFIX)

CORE_FILES.c = 	gcspy_array_input.c \
		gcspy_array_output.c \
		gcspy_buffered_input.c \
		gcspy_buffered_output.c \
		gcspy_color_db.c \
		gcspy_command_stream.c \
		gcspy_comm.c \
		gcspy_d_utils.c \
		gcspy_gc_driver.c \
		gcspy_gc_stream.c \
		gcspy_interpreter.c \
		gcspy_main_server.c \
		gcspy_shape_stream.c \
		gcspy_timer.c \
		gcspy_utils.c

EVM_FILES.c =	evm_cms_driver.c \
		evm_fl_driver.c \
		evm_mc_driver.c \
		evm_ms_driver.c \
		evm_ss_driver.c 

HS_FILES.c =	hs_class_driver.c \
		hs_mc_driver.c \
		hs_perm_driver.c \
		hs_ss_driver.c

CORE_FILES.h = 	gcspy_array_input.h \
		gcspy_array_output.h \
		gcspy_buffered_input.h \
		gcspy_buffered_output.h \
		gcspy_color_db.h \
		gcspy_command_stream.h \
		gcspy_comm.h \
		gcspy_d_utils.h \
		gcspy_gc_driver.h \
		gcspy_gc_stream.h \
		gcspy_interpreter.h \
		gcspy_main_server.h \
		gcspy_shape_stream.h \
		gcspy_timer.h \
		gcspy_utils.h

EVM_FILES.h =	evm_cms_driver.h \
		evm_fl_driver.h \
		evm_mc_driver.h \
		evm_ms_driver.h \
		evm_ss_driver.h 

HS_FILES.h =	hs_class_driver.h \
		hs_mc_driver.h \
		hs_perm_driver.h \
		hs_ss_driver.h

CORE_FILES.bin =gcspy_test_buffered_io.c \
		gcspy_test_server.c \
		gcspy_test_shape_comm.c \
		gcspy_test_shape_stream.c \
		gcspy_test_simple_comm.c

EVM_FILES.bin =	test_evm_mc_driver.c \
		test_evm_ms_driver.c \
		test_evm_ss_driver.c \
		test_evm_ss_ms_driver.c 

HS_FILES.bin =	test_hs_mc_driver.c \
		test_hs_perm_driver.c \
		test_hs_ss_driver.c

DIR_CORE_FILES.o = $(patsubst %.c, $(OBJ_DIR)/%.o, $(CORE_FILES.c))
DIR_EVM_FILES.o = $(patsubst %.c, $(OBJ_DIR)/%.o, $(EVM_FILES.c))
DIR_HS_FILES.o = $(patsubst %.c, $(OBJ_DIR)/%.o, $(HS_FILES.c))

DIR_CORE_FILES.h = $(patsubst %.h, $(INCLUDE_DIR)/%.h, $(CORE_FILES.h))
DIR_EVM_FILES.h = $(patsubst %.h, $(INCLUDE_DIR)/%.h, $(EVM_FILES.h))
DIR_HS_FILES.h = $(patsubst %.h, $(INCLUDE_DIR)/%.h, $(HS_FILES.h))

DIR_CORE_FILES.bin = $(patsubst %.c, $(BIN_DIR)/%, $(CORE_FILES.bin))
DIR_EVM_FILES.bin = $(patsubst %.c, $(BIN_DIR)/%, $(EVM_FILES.bin))
DIR_HS_FILES.bin = $(patsubst %.c, $(BIN_DIR)/%, $(HS_FILES.bin))

all: dirs core evm hs

##

dirs: 
	mkdir -p $(OBJ_DIR)
	mkdir -p $(LIB_DIR)

## Core

core: core_lib

core_lib: $(LIB_DIR)/$(CORE_LIB_NAME)

$(LIB_DIR)/$(CORE_LIB_NAME): $(DIR_CORE_FILES.o)
	$(LD) $(DYNAMIC_FLAG) -o $@ $(DIR_CORE_FILES.o)

## EVM

evm: evm_lib

evm_lib: $(LIB_DIR)/$(EVM_LIB_NAME)

$(LIB_DIR)/$(EVM_LIB_NAME): $(DIR_EVM_FILES.o)
	$(LD) $(DYNAMIC_FLAG) $(GCSPY_LIB) -o $@ $(DIR_EVM_FILES.o)

## HS

hs: hs_lib

hs_lib: $(LIB_DIR)/$(HS_LIB_NAME)

$(LIB_DIR)/$(HS_LIB_NAME): $(DIR_HS_FILES.o)
	$(LD) $(DYNAMIC_FLAG) $(GCSPY_LIB) -o $@ $(DIR_HS_FILES.o)

## BIN

bindir:
	mkdir -p $(BIN_DIR)

bin: bindir $(DIR_CORE_FILES.bin) $(DIR_EVM_FILES.bin) $(DIR_HS_FILES.bin)

## General rules

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(DIR_CORE_FILES.h) $(DIR_EVM_FILES.h) $(DIR_HS_FILES.h)
	$(CC) -c $(CC_FLAGS) -I$(INCLUDE_DIR) -o $@ $<

$(BIN_DIR)/%: $(OBJ_DIR)/%.o
	$(CC) $(CC_FLAGS) $(LD_FLAGS) -I$(INCLUDE_DIR) -o $@ $< 

## Cleaning up stuff

clean: uninstall 

install: all bin

uninstall:
	rm -rf $(OBJ_DIR)
	rm -rf $(BIN_DIR)
	rm -rf $(LIB_DIR)
