################################################################################
#
#    Project: The SPD Image correction and azimuthal regrouping
#                               http://forge.epn-campus.eu/projects/show/azimuthal
#
#    Copyright (C) 2000-2010 European Synchrotron Radiation Facility
#                            Grenoble, France
#
#    Principal authors: P. Boesecke (boesecke@esrf.fr)
#                       R. Wilcke (wilcke@esrf.fr)                                              
#                       Jerome Kieffer (jerome.kieffer@esrf.fr)  
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    and the GNU Lesser General Public License  along with this program.  
#    If not, see <http://www.gnu.org/licenses/>.
################################################################################
#
# This Makefile creates the "lz4" library with all the routines needed to 
# read edf files (ESRF Data Format EDF).
#
# The string defined in the LZFVERSION macro will be passed on to the compiler
# and can be used by the programs to create a version identifier.
#
# This Makefile can create the library for HP-UX, SunOS, LINUX, MAC and the 
# CYGNUS user environment on Window-PCs (CYGWIN). The versions for the 
# different operating systems are stored in different subdirectories. 
# For details, see below.
#
# Possible targets of this Makefile are:
#
# - all          (default) to make the library
# - clean_obj    to remove all object files created by the Makefile
# - clean        to remove everything that was created by the Makefile
#
# Each target will create or remove files only for the operating system it
# is being run with, e.g. doing "make clean" on a SUN will not remove any
# files that are in the subdirectories for HP-UX.
#
# Update: 2017-09-15 P. Boesecke (boesecke@esrf.fr)
#                    (original makefile from edfpack)
################################################################################

LIBNAM = liblz4.a
CC = gcc
AR = ar ru
RANLIB = ranlib
MAKE = make

#
# The LZFVERSION string defined here will be passed on to the compiler with the
# "-D" option.
# The backslashes and quotes (\") are necessary to pass it as a string.
#
LZFVERSION := $(strip $(wildcard E*))

CFLAGS := -Wall -Wno-format-nonliteral -Wformat=2 -g3 -O

#
# Find out the operating system, and define the names of the directories
# for the object files and the libraries accordingly:
# - the object files will be in   obj/$(SYSNAM)
# - the libraries will be in      lib/$(SYSNAM)
#
# where $(SYSNAM) is
# - HPUX   on HP-UX systems
# - SUN    on SunOS systems
# - LINUX  on LINUX systems
# - LINUX64  on 64-bit LINUX systems
# - CYGWIN on PCs with the Cygnus user enviromnent
#

OPSYS := $(shell uname)

ifeq ($(OPSYS),Darwin)

LZFVERSION := $(LZFVERSION)_mac
SYSNAM = MAC
CFLAGS += -m32

else
ifeq ($(OPSYS),HP-UX)

LZFVERSION := $(LZFVERSION)_hpux10-20
SYSNAM = HPUX

else
ifeq ($(OPSYS),SunOS)

LZFVERSION := $(LZFVERSION)_sunos5-7
SYSNAM = SUN

else
ifeq ($(OPSYS),Linux)

ifeq ($(shell uname -m),x86_64)
SYSNAM = LINUX64
LZFVERSION := $(LZFVERSION)_linux64
else
SYSNAM = LINUX
LZFVERSION := $(LZFVERSION)_linux
endif
MAKE = make

else

ifeq ($(findstring CYGWIN,$(OPSYS)),CYGWIN)

ifeq ($(shell uname -m),x86_64)
SYSNAM = CYGWIN64
LZFVERSION := $(LZFVERSION)_cygwin64
else
SYSNAM = CYGWIN
LZFVERSION := $(LZFVERSION)_cygwin
endif
MAKE = make

else

LZFVERSION := $(LZFVERSION)_undefined
endif      # Cygwin
endif      # Linux
endif      # SunOS
endif      # HP-UX
endif      # Darwin

CFLAGS += -DLZFVERSION=\"$(LZFVERSION)\" -c -I./bitshuffle-master/. -I./bitshuffle-master/src -I./bitshuffle-master/lz4 -I./bitshuffle-master/lzf -I./bitshuffle-master/lzf/lzf

ifeq ($(IOALLOC),1)
  CFLAGS += -DIOALLOC=1
endif # IOALLOC

H5INC_DIR := ../../hdf5/$(SYSNAM)/hdf5/include
CFLAGS += -I$(H5INC_DIR) -DH5FILES=1

# for avoiding that src/bitshuffle_core.h redefines types 
ifndef __STDC_VERSION__
  $(info __STDC_VERSION__ is undefined")
  CFLAGS += -D__STDC_VERSION__=199900L
  $(info changed to 199900L)
endif

#
# Determine the source file names and the corresponding object file names for
# the library.
#
OBJ_DIR = bin/$(SYSNAM)
LIB_DIR = lib/$(SYSNAM)

INC_FILES = $(shell echo bitshuffle-master/lz4/*.h) $(shell echo bitshuffle-master/lzf/*.h) $(shell echo bitshuffle-master/lzf/lzf/*.h) $(shell echo bitshuffle-master/src/*.h)

SRC_FILES_LZ4 = $(shell echo bitshuffle-master/lz4/*.c)
LIB_OBJECTS_LZ4 := $(patsubst %.c, %.o, $(notdir $(SRC_FILES_LZ4)))
OBJ_FILES_LZ4 = $(addprefix $(OBJ_DIR)/, $(LIB_OBJECTS_LZ4))

SRC_FILES_LZF = $(filter-out bitshuffle-master/lzf/example.c,$(shell echo bitshuffle-master/lzf/*.c))
LIB_OBJECTS_LZF := $(patsubst %.c, %.o, $(notdir $(SRC_FILES_LZF)))
OBJ_FILES_LZF = $(addprefix $(OBJ_DIR)/, $(LIB_OBJECTS_LZF))

SRC_FILES_LZFLZF = $(shell echo bitshuffle-master/lzf/lzf/*.c)
LIB_OBJECTS_LZFLZF := $(patsubst %.c, %.o, $(notdir $(SRC_FILES_LZFLZF)))
OBJ_FILES_LZFLZF = $(addprefix $(OBJ_DIR)/, $(LIB_OBJECTS_LZFLZF))

#bshuf_h5plugin.c  lzf_h5plugin.c, not needed for static linking
#SRC_FILES_SRC = $(shell echo bitshuffle-master/src/*.c)
SRC_FILES_SRC = $(filter-out $(shell echo bitshuffle-master/src/*_h5plugin.c),$(shell echo bitshuffle-master/src/*.c))
LIB_OBJECTS_SRC := $(patsubst %.c, %.o, $(notdir $(SRC_FILES_SRC)))
OBJ_FILES_SRC = $(addprefix $(OBJ_DIR)/, $(LIB_OBJECTS_SRC))

SRC_FILES = $(SRC_FILES_LZ4) $(SRC_FILES_LZFLZF) $(SRC_FILES_LZF) $(SRC_FILES_SRC) 

LIB_OBJECTS := $(patsubst %.c, %.o, $(notdir $(SRC_FILES)))

#OBJ_FILES = $(addprefix $(OBJ_DIR)/, $(LIB_OBJECTS))

#
# Make the library. This is the default.
#
all: lz4 

lz4: $(LIB_DIR) $(LIB_DIR)/$(LIBNAM)

$(LIB_DIR)/$(LIBNAM): $(OBJ_DIR) $(SRC_FILES) $(OBJ_FILES_LZ4) $(OBJ_FILES_LZF) $(OBJ_FILES_LZFLZF) $(OBJ_FILES_SRC)
	$(AR) $(LIB_DIR)/$(LIBNAM) $(OBJ_FILES_LZ4) $(OBJ_FILES_LZF) $(OBJ_FILES_LZFLZF) $(OBJ_FILES_SRC)
	$(RANLIB) $(LIB_DIR)/$(LIBNAM)

#
# Make the library file directory if it does not yet exist.
#
$(LIB_DIR):
	mkdir -p $(LIB_DIR)

#
# Make the object file directory if it does not yet exist.
#
$(OBJ_DIR):
	mkdir -p $(OBJ_DIR)

#
# Make the required object files for the libraries.
#
$(info SRC_FILES="$(SRC_FILES)")
$(info INC_FILES="$(INC_FILES)")
$(info OBJ_FILES_LZ4="$(OBJ_FILES_LZ4)")
$(info OBJ_FILES_LZF="$(OBJ_FILES_LZF)")
$(info OBJ_FILES_LZFLZF="$(OBJ_FILES_LZFLZF)")
$(info OBJ_FILES_SRC="$(OBJ_FILES_SRC)")

$(OBJ_FILES_LZ4): $(OBJ_DIR)/%.o: ./bitshuffle-master/lz4/%.c $(INC_FILES)
	$(CC) $(CFLAGS) $< -o $@
$(OBJ_FILES_LZF): $(OBJ_DIR)/%.o: ./bitshuffle-master/lzf/%.c $(INC_FILES)
	$(CC) $(CFLAGS) $< -o $@
$(OBJ_FILES_LZFLZF): $(OBJ_DIR)/%.o: ./bitshuffle-master/lzf/lzf/%.c $(INC_FILES)
	$(CC) $(CFLAGS) $< -o $@
$(OBJ_FILES_SRC): $(OBJ_DIR)/%.o: ./bitshuffle-master/src/%.c $(INC_FILES)
	$(CC) $(CFLAGS) $< -o $@

#
# Clean up all files that can be made by "make". Several variations exist:
#
# - target "clean_obj" cleans all object files created by the Makefile;
# - target "clean" cleans all the files created by the Makefile (the object
#   files and the corresponding libraries).
# 
clean_obj:
	rm -rf $(OBJ_DIR)

clean:
	rm -rf $(OBJ_DIR) $(LIB_DIR)
