#############################################################################
#  Written 1994++ by Peter Boesecke                                         #
#  Copyright (C) 2011 European Synchrotron Radiation Facility               #
#                        Grenoble, France                                   #
#                                                                           #
#     Principal authors: Peter Boesecke  (boesecke@esrf.eu)                 #
#                                                                           #
#     This program is free software: you can redistribute it and/or modify  #
#     it under the terms of the GNU 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 General Public License for more details.                          #
#                                                                           #
#     You should have received a copy of the GNU General Public License     #
#     along with this program.  If not, see <http://www.gnu.org/licenses/>. #
#############################################################################
################################################################################
#
# This Makefile creates the "rocapack" library 
#
# The string defined in the RVERSION 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.
#
################################################################################

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

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

CFLAGS := -g3 -O -DRVERSION=\"$(RVERSION)\"

#
# 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)

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

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

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

else
ifeq ($(OPSYS),SunOS)

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

else
ifeq ($(OPSYS),Linux)

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

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

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

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

CFLAGS += -Wall -c -I. -I../edfpack

#
# 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: rocapack 

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