################################################################################
#
#    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 "edfpack" library with all the routines needed to 
# read edf files (ESRF Data Format EDF).
#
# The string defined in the EVERSION 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: 2007-02-14 P. Boesecke (boesecke@esrf.fr)
#                    (original makefile from R. Wilcke (wilcke@esrf.fr))
#                    first working version.
#         2009-11    P. Boesecke (boesecke@esrf.fr), R. Wilcke ((wilcke@esrf.fr)
#                    Apple Darwin (MAC) added, gmake -> make
################################################################################

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

#
# The EVERSION 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.
#
EVERSION := $(strip $(wildcard E*))

CFLAGS := -Wall -Wformat=2 -g3 -O -DEVERSION=\"$(EVERSION)\"

#
# 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/$(DIRNAM)
# - the libraries will be in      lib/$(DIRNAM)
#
# where $(DIRNAM) 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)

EVERSION := $(EVERSION)_mac
DIRNAM = MAC
CFLAGS += -m32

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

EVERSION := $(EVERSION)_hpux10-20
DIRNAM = HPUX

else
ifeq ($(OPSYS),SunOS)

EVERSION := $(EVERSION)_sunos5-7
DIRNAM = SUN

else
ifeq ($(OPSYS),Linux)

EVERSION := $(EVERSION)_linux
ifeq ($(shell uname -m),x86_64)
DIRNAM = LINUX64
else
DIRNAM = LINUX
endif
MAKE = make

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

EVERSION := $(EVERSION)_cygwin
DIRNAM = CYGWIN
MAKE = make

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

INCLUDE_PATH=.

CFLAGS += -c -I$(INCLUDE_PATH)

#
# Determine the source file names and the corresponding object file names for
# the library.
#
OBJ_DIR = bin/$(DIRNAM)
LIB_DIR = lib/$(DIRNAM)
LIB_OBJECTS := $(patsubst %.c, %.o, $(shell echo *.c))
OBJ_FILES = $(addprefix $(OBJ_DIR)/, $(LIB_OBJECTS))
SRC_FILES = $(shell echo *.c)
INC_FILES = $(shell echo *.h)

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

edfpack: $(LIB_DIR) $(LIB_DIR)/$(LIBNAM)

$(LIB_DIR)/$(LIBNAM): $(OBJ_DIR) $(SRC_FILES) $(OBJ_FILES)
	$(AR) $(LIB_DIR)/$(LIBNAM) $(OBJ_FILES)
	$(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.
#
$(OBJ_FILES): $(OBJ_DIR)/%.o: %.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)
