#
# Copyright (c) 2011, CESNET z.s.p.o
# Copyright (c) 2011, Silicon Genome, LLC.
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

# Shared/Static library?
SHARED_LIBRARY ?= 1
# Use constant memory for huffman coder tables?
HUFFMAN_CODER_TABLES_IN_CONSTANT ?= 1
# Flag if use OpenGL
USE_OPENGL ?= 0
# Debug
DEBUG ?= 0
# CUDA install path
CUDA_INSTALL_PATH ?= /usr/local/cuda
#Destination directory
DESTDIR ?= /usr/local


# Target executable
TARGET := libgpujpeg.a
ifeq ($(SHARED_LIBRARY),1)
    LIBRARY_NAME := libgpujpeg.so
    SONAME := $(LIBRARY_NAME).0
    REAL_NAME := $(SONAME).0.0
    TARGET := $(REAL_NAME)
endif

# C files
CFILES := \
    gpujpeg_common.c \
    gpujpeg_encoder.c \
    gpujpeg_decoder.c \
    gpujpeg_table.c \
    gpujpeg_dct_cpu.c \
    gpujpeg_huffman_cpu_encoder.c \
    gpujpeg_huffman_cpu_decoder.c \
    gpujpeg_writer.c \
    gpujpeg_reader.c
# CUDA files
CUFILES := \
    gpujpeg_dct_gpu.cu \
    gpujpeg_preprocessor.cu \
    gpujpeg_huffman_gpu_encoder.cu \
    gpujpeg_huffman_gpu_decoder.cu

# Compilers
CC := gcc -fPIC
LINK := g++ -fPIC
NVCC := $(CUDA_INSTALL_PATH)/bin/nvcc -Xcompiler -fPIC

# Debug
ifeq ($(DEBUG),1)
    COMMONFLAGS += -g -D_DEBUG -O0
    NVCCFLAGS += -G
else
    COMMONFLAGS += -O2
endif

# Common flags
COMMONFLAGS += -I. -I$(CUDA_INSTALL_PATH)/include
# C flags
CFLAGS += $(COMMONFLAGS) -std=c99
# CUDA flags
NVCCFLAGS += $(COMMONFLAGS) \
	--ptxas-options="-v" \
	-gencode arch=compute_20,code=sm_20 \
	-gencode arch=compute_11,code=sm_11 \
	-gencode arch=compute_10,code=sm_10
# Linker flags
LDFLAGS += -shared -Wl,-soname,$(SONAME)

# Other Flags
ifeq ($(HUFFMAN_CODER_TABLES_IN_CONSTANT),1)
    COMMONFLAGS += -DGPUJPEG_HUFFMAN_CODER_TABLES_IN_CONSTANT
endif
ifeq ($(USE_OPENGL),1)
    LDFLAGS += -lGLEW
    COMMONFLAGS += -DGPUJPEG_USE_OPENGL
endif 

# Do 32bit vs. 64bit setup
LBITS := $(shell getconf LONG_BIT)
ifeq ($(LBITS),64)
    # 64bit
    LDFLAGS += -L$(CUDA_INSTALL_PATH)/lib64
else
    # 32bit
    LDFLAGS += -L$(CUDA_INSTALL_PATH)/lib
endif
LDFLAGS += -lcudart

# Build
build: $(TARGET)
	ln -sf $(TARGET) $(LIBRARY_NAME)
	ln -sf $(TARGET) $(SONAME)

# Clean
clean:
	rm -f *.o $(TARGET) $(SONAME) $(LIBRARY_NAME)
	rm -f *.i *.ii 
	rm -f *.cudafe1.c *.cudafe1.cpp *.cudafe1.gpu *.cudafe1.stub.c
	rm -f *.cudafe2.c *.cudafe2.gpu *.cudafe2.stub.c
	rm -f *.fatbin *.fatbin.c *.ptx *.hash *.cubin *.cu.cpp

# Lists of object files
COBJS=$(CFILES:.c=.c.o)
CUOBJS=$(CUFILES:.cu=.cu.o)

# Build target
ifeq ($(SHARED_LIBRARY),1)
    $(TARGET): $(COBJS) $(CUOBJS)
		$(LINK) $(COBJS) $(CUOBJS) $(LDFLAGS) -o $(TARGET);    
else
    $(TARGET): $(COBJS) $(CUOBJS)
    
		ar rcs $(TARGET) $(COBJS) $(CUOBJS);
endif

# Set suffix for CUDA files
.SUFFIXES: .cu

# Pattern rule for compiling C files
%.c.o: %.c 
	$(CC) $(CFLAGS) -c $< -o $@

# Pattern rule for compiling CUDA files
%.cu.o: %.cu
	$(NVCC) $(NVCCFLAGS) -c $< -o $@;

install: $(TARGET)
	install -d -m 755 $(DESTDIR)/lib
	install -m 644 $(TARGET) $(DESTDIR)/lib
ifeq ($(SHARED_LIBRARY),1)
	ldconfig -n $(DESTDIR)/lib
	ln -sf $(DESTDIR)/lib/$(SONAME) $(DESTDIR)/lib/$(LIBRARY_NAME)
endif

uninstall: $(TARGET)
	rm $(DESTDIR)/lib/$(TARGET)
ifeq ($(SHARED_LIBRARY),1)
	rm $(DESTDIR)/lib/$(SONAME)
	rm $(DESTDIR)/lib/$(LIBRARY_NAME)
endif


# Set file dependencies
gpujpeg_common.c.o: gpujpeg_common.c gpujpeg_common.h
gpujpeg_encoder.c.o: gpujpeg_encoder.c gpujpeg_encoder.h
gpujpeg_decoder.c.o: gpujpeg_decoder.c gpujpeg_decoder.h
gpujpeg_table.c.o: gpujpeg_table.c gpujpeg_table.h
gpujpeg_preprocessor.cu.o: gpujpeg_preprocessor.cu gpujpeg_preprocessor.h gpujpeg_colorspace.h
gpujpeg_dct_cpu.c.o: gpujpeg_dct_cpu.c gpujpeg_dct_cpu.h
gpujpeg_dct_gpu.cu.o: gpujpeg_dct_gpu.cu gpujpeg_dct_gpu.h
gpujpeg_huffman_cpu_encoder.c.o: gpujpeg_huffman_cpu_encoder.c gpujpeg_huffman_cpu_encoder.h
gpujpeg_huffman_gpu_encoder.cu.o: gpujpeg_huffman_gpu_encoder.cu gpujpeg_huffman_gpu_encoder.h
gpujpeg_huffman_cpu_decoder.c.o: gpujpeg_huffman_cpu_decoder.c gpujpeg_huffman_cpu_decoder.h
gpujpeg_huffman_gpu_decoder.cu.o: gpujpeg_huffman_gpu_decoder.cu gpujpeg_huffman_gpu_decoder.h
gpujpeg_writer.c.o: gpujpeg_writer.c gpujpeg_writer.h
gpujpeg_reader.c.o: gpujpeg_reader.c gpujpeg_reader.h
