
# FwdMap is a learning based system which automatically builds assembly to IR
# translators using code generators of modern compilers.

# Copyright (C) 2014 - 2015 by Niranjan Hasabnis and R.Sekar in Secure Systems
# Lab, Stony Brook University, Stony Brook, NY 11794.

# 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 2 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, write to the Free Software 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

CC=gcc-4.6
RM=rm
CCPLUS=g++
ARCH=X86

PLUGIN_SOURCE_FILES= plugin.c
PLUGIN_OBJECT_FILES= $(patsubst %.c,%.o,$(PLUGIN_SOURCE_FILES))
MAPPER_SOURCE_FILES= PPrtl.cpp Target.cpp RTLTree.cpp AVR.cpp ARM.cpp X86.cpp MapTbl.cpp Utility.cpp
MAPPER_OBJECT_FILES= $(patsubst %.cpp,%.o,$(MAPPER_SOURCE_FILES))
GCCPLUGINS_DIR:= $(shell $(CC) -print-file-name=plugin)

GCC_TOP:=${LISC_TOP}/pprtl/gcc
GCCINC_DIR:=$(GCC_TOP)/gcc/
GCC45PLUGINS_DIR:=$(GCC_TOP)/installdir/lib/gcc/i686-pc-linux-gnu/4.5.1/plugin
GCC_OBJDIR_GCC:=$(GCC_TOP)/objdir/gcc/
GCC_OBJDIR:=$(GCC_TOP)/objdir/
GMP_DIR:=$(GCC_TOP)/gmp-4.3.2/installdir
MPFR_DIR:=$(GCC_TOP)/mpfr-3.0.0/installdir/
MPC_DIR:=$(GCC_TOP)/mpc-0.8.2/installdir/
PPL_DIR:=$(GCC_TOP)/ppl-0.10/installdir/
ZLIB_DIR:=$(GCC_TOP)/zlib/installdir/
LIBELF_DIR:=$(GCC_TOP)/libelf-0.8.12/installdir/
EXTRA_INCLUDE_DIR:= .

DEP_LIBS_ASM2RTL:=$(GCC_OBJDIR_GCC)/build/rtl.o $(GCC_OBJDIR_GCC)/build/read-rtl.o 
DEP_LIBS_ASM2RTL+=$(GCC_OBJDIR_GCC)/build/mygensupport.o $(GCC_OBJDIR_GCC)/build/vec.o $(GCC_OBJDIR_GCC)/genrtl.o 
DEP_LIBS_ASM2RTL+=$(GCC_OBJDIR_GCC)/c-lang.o $(GCC_OBJDIR_GCC)/stub-objc.o $(GCC_OBJDIR_GCC)/attribs.o 
DEP_LIBS_ASM2RTL+=$(GCC_OBJDIR_GCC)/c-errors.o $(GCC_OBJDIR_GCC)/c-lex.o $(GCC_OBJDIR_GCC)/c-pragma.o $(GCC_OBJDIR_GCC)/c-decl.o 
DEP_LIBS_ASM2RTL+=$(GCC_OBJDIR_GCC)/c-typeck.o $(GCC_OBJDIR_GCC)/c-convert.o $(GCC_OBJDIR_GCC)/c-aux-info.o 
DEP_LIBS_ASM2RTL+=$(GCC_OBJDIR_GCC)/c-common.o $(GCC_OBJDIR_GCC)/c-opts.o $(GCC_OBJDIR_GCC)/c-format.o 
DEP_LIBS_ASM2RTL+=$(GCC_OBJDIR_GCC)/c-semantics.o $(GCC_OBJDIR_GCC)/c-ppoutput.o $(GCC_OBJDIR_GCC)/c-cppbuiltin.o 
DEP_LIBS_ASM2RTL+=$(GCC_OBJDIR_GCC)/c-objc-common.o $(GCC_OBJDIR_GCC)/c-dump.o $(GCC_OBJDIR_GCC)/c-pch.o $(GCC_OBJDIR_GCC)/c-parser.o 
DEP_LIBS_ASM2RTL+=$(GCC_OBJDIR_GCC)/i386-c.o $(GCC_OBJDIR_GCC)/c-gimplify.o $(GCC_OBJDIR_GCC)/tree-mudflap.o 
DEP_LIBS_ASM2RTL+=$(GCC_OBJDIR_GCC)/c-pretty-print.o $(GCC_OBJDIR_GCC)/c-omp.o $(GCC_OBJDIR_GCC)/dummy-checksum.o 
DEP_LIBS_ASM2RTL+=$(GCC_OBJDIR_GCC)/toplev.o
DEP_LIBS_ASM2RTL+=$(GCC_OBJDIR_GCC)/libbackend.a $(GCC_OBJDIR)/libcpp/libcpp.a $(GCC_OBJDIR)/libdecnumber/libdecnumber.a 
DEP_LIBS_ASM2RTL+=$(GCC_OBJDIR)/libcpp/libcpp.a  $(GCC_OBJDIR)/build-i686-pc-linux-gnu/libiberty/libiberty.a 
DEP_LIBS_ASM2RTL+=$(GCC_OBJDIR)/libdecnumber/libdecnumber.a 
DEP_LIBS_ASM2RTL+=-Wl,-rpath=${ZLIB_DIR}/lib -L$(ZLIB_DIR)/lib -lz 
DEP_LIBS_ASM2RTL+=-Wl,-rpath=${PPL_DIR}/lib -L$(PPL_DIR)/lib -lppl_c -lppl 
DEP_LIBS_ASM2RTL+=-Wl,-rpath=${GMP_DIR}/lib -L$(GMP_DIR)/lib -lgmp
DEP_LIBS_ASM2RTL+=-Wl,-rpath=${MPFR_DIR}/lib -L$(MPFR_DIR)/lib -lmpfr
DEP_LIBS_ASM2RTL+=-Wl,-rpath=${MPC_DIR}/lib -L$(MPC_DIR)/lib -lmpc 
DEP_LIBS_ASM2RTL+=-rdynamic -ldl 
DEP_LIBS_ASM2RTL+=-Wl,-rpath=${LIBELF_DIR}/lib -L$(LIBELF_DIR)/lib -lelf 

CFLAGS+= -I $(GCCPLUGINS_DIR)/include -fPIC -g -c -w -std=c99 -DARCH_$(ARCH)
CXXFLAGS+= -std=c++0x -I . -g -c -Wall -DARCH_$(ARCH)

GCCI_CFLAGS+= -I $(GCC45PLUGINS_DIR)/include -I $(GMP_DIR)/include 
GCCI_CFLAGS+= -I $(MPFR_DIR)/include -I $(MPC_DIR)/include 
GCCI_CFLAGS+= -I $(EXTRA_INCLUDE_DIR) -I $(GCCINC_DIR) -I $(GCC_OBJDIR_GCC)
GCCI_CFLAGS+= -Wall -g -std=c99 -DARCH_${ARCH}

all: plugin_dump_mapping.so pprtl stor

plugin_dump_mapping.so: $(PLUGIN_OBJECT_FILES)
	$(CC) -shared $^ -o $@

pprtl: $(MAPPER_OBJECT_FILES) gcci.o
	$(CCPLUS) $^ -o $@ $(DEP_LIBS_ASM2RTL)

stor: StrtoRTL.o gcci.o
	$(CCPLUS) $^ -o $@ $(DEP_LIBS_ASM2RTL)

gcci.o: gcci.c
	$(CC) -c $(GCCI_CFLAGS) -o $@ $^

clean:
	$(RM) -f pprtl *.o plugin_dump_mapping.so stor a.out

