################################################################################
#
# This GNU Makefile creates the "fitpack" library.
#
#
# The string defined in the FVERSION 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: 03/01/2016 P. Boesecke (boesecke@esrf.fr)
#                    CYGWIN64 added
# Update: 28/06/2013 P. Boesecke (boesecke@esrf.fr) for fitpack c-routines
################################################################################

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

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

CFLAGS := -Wall -Wformat=2 -g3 -O -DFVERSION=\"$(FVERSION)\"

#
# 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
# - MAC      on Darwin Macintosh systems
# - HPUX     on HP-UX systems
# - SUN      on SunOS systems CFLAGS += -DUNDERSCORE
# - LINUX    on 32-bit LINUX systems CFLAGS += -DUNDERSCORE
# - LINUX64  on 64-bit LINUX systems CFLAGS += -DUNDERSCORE
# - CYGWIN on PCs with the Cygnus user enviromnent CFLAGS += -DUNDERSCORE


OPSYS := $(shell uname)

ifeq ($(OPSYS),Darwin)

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

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

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

else
ifeq ($(OPSYS),SunOS)

FVERSION := $(FVERSION)_sunos5-7
DIRNAM = SUN
CFLAGS += -DUNDERSCORE

else
ifeq ($(OPSYS),Linux)

FVERSION := $(FVERSION)_linux
ifeq ($(shell uname -m),x86_64)
DIRNAM = LINUX64
else
DIRNAM = LINUX
endif
CFLAGS += -DUNDERSCORE

else

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

ifeq ($(shell uname -m),x86_64)
DIRNAM = CYGWIN64
CFLAGS += -DUNDERSCORE
PVERSION := $(PVERSION)_cygwin64
else
DIRNAM = CYGWIN
CFLAGS += -DUNDERSCORE
PVERSION := $(PVERSION)_cygwin
endif

else
FVERSION := $(FVERSION)_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: fitpack 

fitpack: $(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)
