###############################################################################
###############################################################################
#####################                                     #####################
#####################            AFHEL MAKEFILE           #####################
#####################                                     #####################
###############################################################################
###############################################################################
# 
# DESCRIPTION: Makefile for Afhel. Allows installation of this library as both
#			   static and shared library, greatly simplifying its usage: this 
#              library can now be linked when compiling a program using the 
#              flag [-lafhel]. In addition, header Afhel.h is installed in a 
#			   public directory. In order to use Afhel library in your own .cpp 
#			   code, use this formula:
#                        #include <Afhel.h>
#
#              This makefile also allows the compilation&linking of .cpp files. 
#              A file calledfname.cpp can be target if this recipe in order 
#			   to create an executable:
#                        make fname_x
#
#              Some configuration may be required in the Makefile Variables to 
#              accomodate the settings to a given OS.
#
# AUTHOR: Alberto Ibarrondo (github @ibarrond)
# DATE: 15/06/2018
#    
#    
# LICENSE: GNU GPL v3
#  
#  Pyfhel 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.
#
#  Pyfhel 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/>.
#



###############################################################################
#                           MAKEFILE VARIABLES                                #
###############################################################################

#........................... LIBRARY ATTRIBUTES ...............................
LIB_NAME = afhel
			# Prefix 'lib' and extension '.la' are added later on
VERSION = 2:0:0
			# Library version, independent from packet/repo version
PREFIX = /usr/local

LIB_DIR = $(PREFIX)/lib
			# Directory where library will be installed
INCLUDE_DIR = $(PREFIX)/include
			# Directory where headers will be installed
SRC_DIR = .

#............................. COMPILER & LINKER ..............................
CC = g++
CFLAGS = -O3 -std=c++17 -I$(SRC_DIR) -I$(INCLUDE_DIR) \
	        -Wfatal-errors -Wshadow -fmax-errors=2
LFLAGS = -L$(LIB_DIR) -lseal -pthread

#.................................. SOURCE FILES ..............................
SRC = Afseal.cpp
#SRC = $(wildcard $(SRC_DIR)/*.cpp)

#.................................. HEADER FILES ..............................
HEADER = Afhel.h Afseal.h
#HEADER = $(wildcard $(SRC_DIR)/*.h) 

#........................... LIBRARY INTERMEDIATE FILES .......................
LOBJ = Afseal.lo
#LOBJ = $(SRC:%.cpp=%.lo)

#.................................. TEST FILES ...... .........................
TESTS = Demo_Afseal_x


#.............................. LIBRARY FINAL FILES ...........................
LIB_LA = lib$(LIB_NAME).la


###############################################################################
#                               BUILD FHE LIBRARY                             #
###############################################################################

all: $(LIB_LA)

%.lo: %.cpp $(HEADER)
	# COMPILATION: Create $@ object from the $< source
	@libtool --mode=compile $(CC) $(CFLAGS) -shared -c $< 

$(LIB_LA): $(LOBJ)
	# LINKING: Using Libtool to create the library (all .lo into one)
	@libtool --mode=link $(CC) $(CFLAGS) -o $@ $^ -rpath $(LIB_DIR) $(LFLAGS)\
	 -version-info $(VERSION)


###############################################################################
#                             INSTALL THE LIBRARY                             #
###############################################################################

install: $(LIB_LA)
	
	# Installing $(LIB_NAME) library in $(LIB_DIR)
	@libtool --mode=install cp $< $(LIB_DIR)
	ldconfig
	# Creating folders for header files
	@-mkdir -p $(INCLUDE_DIR)/$(LIB_NAME)
	# Copying Header files to $(INCLUDE_DIR)
	@-cp -t $(INCLUDE_DIR)/$(LIB_NAME) $(HEADER)



###############################################################################
#                          CREATE BINARIES WITH AFHEL                         #
###############################################################################

%_x: %.cpp
	$(CC) $(CFLAGS) -o $@ $< $(LFLAGS) -l$(LIB_NAME)



###############################################################################
#                                TEST PROGRAMS                                #
###############################################################################

test: $(TESTS)
	cp $^ ../Demos_Tests


###############################################################################
#                               UNINSTALL & CLEAN                             #
###############################################################################

uninstall: 
	libtool --mode=uninstall rm -f $(LIB_DIR)/$(LIB_LA)
	ldconfig
	rm -rf $(INCLUDE_DIR)/$(LIB_NAME)


clean:
	libtool --mode=clean rm -f $(LIB_LA)
	rm -rf .libs/ .deps/
	rm -f *.o *_x *_x.exe *.a *.lo *.la *.so *.pycon *.pypk *.pysk *.pyrok



###############################################################################
#                                ADDITIONAL INFO                              #
###############################################################################
.PHONY: info

info: 
	$(info ----------------------- Afhel Makefile ----------------------------)
	$(info Afhel requires [HElib, SEAL, PALISADE])
	$(info For a list if all the available commands, run >make info)
	$(info Dependencies= info  Pyfhel -> Afhel ->  [HElib | SEAL | PALISADE])
	$(info Compilation flags are 'CFLAGS=$(CFLAGS)')
	$(info Linking flags are 'LFLAGS=$(LFLAGS)')
	$(info Commands Available:)
	$(info * make - compile & link the library)
	$(info * make install - library available in the host. Requires root )
	$(info * make fileName_x - Compile & Link binary filename.cpp with Afhel)
	$(info * make test - Compile & Link Demo_Afhel.cpp with Afhel)
	$(info * make clean - remove all library files from the folder)
	$(info * make uninstall - remove library from host. Requires root)
	                                        
