##############################################################################################
##############################################################################################
###########################                                     ##############################
###########################    Afhel Binary Creator Makefile    ##############################
###########################                                     ##############################
##############################################################################################
##############################################################################################
# 
# DESCRIPTION: Makefile for creation of Binaries based on Afhel.
#                In order to use Afhel library in your own .cpp code, use this formula:
#                        #include <Afhel.h>
#
#                This makefile allows the compilation&linking of .cpp files. A file called
#                fname.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: 16/10/2017
#    
#    
# LICENSE: GNU GPL v3
#  
#  This file is part of Afhel
#
#  Afhel 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.
#
#  Afhel 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/>.
#


$(info ------------------AFHEL BIN COMPILER MAKEFILE--------------------------)
$(info Afhel requires HElib, NTL version 10.0.0 or higher and GMP)
$(info If you get compilation errors, try to add/remove -std=c++11 in Makefile)
$(info For a list if all the available commands, run >make info)



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


#.................................. COMPILER & LINKER ........................................
CC = g++
CFLAGS = -g -O2 -std=c++11 -I/usr/local/include \
	        -pthread -DFHE_THREADS -DFHE_DCRT_THREADS -DFHE_BOOT_THREADS \
	     -Wfatal-errors -Wshadow -Wall -fmax-errors=2
	     

# useful flags:
#   -std=c++11
#   -DNO_HALF_SIZE_PRIME  tells helib to not use the half size prime
#                  in the prime chain
#
#   -DFHE_THREADS  tells helib to enable generic multithreading capabilities;
#                  must be used with a thread-enabled NTL and the -pthread
#                  flag should be passed to gcc
#
#   -DFHE_DCRT_THREADS  tells HElib to use a multithreading strategy at the
#                  DoubleCRT level; requires -DFHE_THREADS (see above)
#
#   -DFHE_BOOT_THREADS  tells helib to use a multithreading strategy for
#                  bootstrapping; requires -DFHE_THREADS (see above)
#
#   -DEVALMAP_CACHED=1 Cache some constants for bootstrapping
#
#         * EVALMAP_CACHED=0 caches these constants as ZZX'es, this takes
#                   less space but it means that they still need to be converted
#                  to DoubleCRT's with every recryption operation. 
#
#        * EVALMAP_CACHED=1 caches these constants directly as DoubleCRT's,
#                   this may take a significant amount of memeory.
#
#  If you get compilation errors, you may need to add -std=c++11 or -std=c++0x


#.................................. LIBRARY ATTRIBUTES .......................................
GMP=-lgmp
NTL=-lntl
HELIB=-lfhe
AFHEL=-lafhel
LDLIBS = $(NTL) $(GMP) $(HELIB) $(AFHEL) -lm


#...................................... HEADER FILES .........................................
HEADER = Afhel.h



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

%_x: %.cpp
	$(CC) $(CFLAGS) -o $@ $< $(LDLIBS)



##############################################################################################
#                                       CLEAN DIRECTORY                                      #
##############################################################################################

clean:
	rm -f *.o *_x *_x.exe *.a *.lo *.la *.so *.aenv



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


info:
	: Afhel requires HElib, NTL version 10.0.0 or higher and GMP
	: Compilation flags are 'CFLAGS=$(CFLAGS)'
	: Commands Available:
	: * make fileName_x - Compile & Link binary filename.cpp with Afhel and its dependencies 
	: * make clean - remove all library files from the folder
	: If errors occur, try adding/removing '-std=c++11' in Makefile
	                                        
