###############################################################################
###############################################################################
#####################                                     #####################
#####################            SEAL MAKEFILE            #####################
#####################                                     #####################
###############################################################################
###############################################################################
#
# DESCRIPTION: Makefile for SEAL encryption library from Microsoft. Installs 
#              this library as a shared library, and simplifying its usage: 
#              the lib can now be linked when compiling a program using the 
#              flag [-lseal]. Plus,all header files (.h) are installed in a 
#              folder called 'seal/'.In order to use the library in your own
#              .cpp code, append at the top of the file:
#                       #include <seal/seal.h>
#
#              This makefile also allows the compilation&linking of .cpp files
#              . A file fname.cpp can be target of this recipe in order to 
#			   create an executable:
#                       make fname_x
#
#              Some configuration may be required in the Makefile variables to 
#              accomodate the settings of a given OS. Our default is Ubuntu.
#
# AUTHOR: Alberto Ibarrondo (github @ibarrond)
# DATE: 18/05/2018
#   
#   
# LICENSE: GNU GPL v3
#  
#  This file is part of Pyfhel project
#
#  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 = seal
	                  # Prefix 'lib' and extension '.la' are added later on
VERSION = 2:3:1
	                  # 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 = SEAL/seal

#.............................. LIBRARY DOWNLOAD  .............................
SEAL4PYFHEL_URL = "https://github.com/ibarrond/SEAL4Pyfhel"
	
#............................. COMPILER & LINKER ..............................
CC = g++
CFLAGS = -g -O2 -std=c++17 -march=native -I$(SRC_DIR) -I$(INCLUDE_DIR) \
	        -pthread -Wfatal-errors -Wshadow -fmax-errors=2
LFLAGS = -lm -L$(LIB_DIR) -L$(LIB_DIR)

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

TESTPROG = SEALExamples/main_x

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

#........................... LIBRARY INTERMEDIATE FILES .......................
LOBJ = $(patsubst $(SRC_DIR)/%,%, $(SRC:%.cpp=%.lo))

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





all: $(LIB_LA)

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

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

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



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

install: $(LIB_LA)
	# Installing library in $(LIB_DIR)
	@libtool --mode=install cp $(LIB_LA) $(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 SEAL                         #
###############################################################################

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


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

test: $(TESTPROG)
	mv $(TESTPROG) ../Demos_Tests/Demo_SEAL_x

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



###############################################################################
#                                ADDITIONAL INFO                              #
###############################################################################


info:
	: ---------------------- PYFHEL: SEAL MAKEFILE -----------------------)
	: libtDependencies:)
	:  Pyfhel -> Afhel -> Afhelib    -> HElib    -> NTL & GMP)
	:                     Afpalisade -> PALISADE)
	:                     Afseal     -> SEAL)
	: SEAL has no external dependencies
	: Compilation flags are 'CFLAGS=$(CFLAGS)'
	: Commands Available:	
	: * make download: download latest source using wget
	: * make fix_original: correct source code to have one single folder
	: * make - download, fix, compile & link the library
	: * make install - library available in the host. Requires root 
	: * make fileName_x - Compile & Link binary filename.cpp with SEAL
	: * make test - compile the tests for SEAL
	: * make clean - remove all library files from the folder
	: * make uninstall - remove library from host. Requires root
	
