# Makefile -- Copyright (c) 2001-2003 Eberhard Mattes
#
# This file is part of epocemx.
#
# epocemx 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, or (at your option)
# any later version.
#
# epocemx 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 epocemx; see the file COPYING.  If not, write to the
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#


ROOT=/usr/local/epocemx

.PHONY: test test-disasm clean epoc install install-sdk default sis sis-minor

default: emxobj emxdump emximage emxrsc emxaif emxfixhdr emxlink emxmknod \
	emxmap \
	eld emxsis disasm esh emake ebasename ecat edirname els eprintenv

sis:
	make -f Makefile.epoc sis

sis-minor:
	make -f Makefile.epoc sis-minor

epoc:
	make -f Makefile.epoc

LIBEMX=../lib

CFLAGS=-g -Wall -pedantic -Wno-long-long
CCP=g++ $(CFLAGS)
CC=gcc $(CFLAGS)

.SUFFIXES: .c .cc .o

.c.o:
	$(CC) -c $<

.cc.o:
	$(CCP) -c $<

BIN1=$(ROOT)/bin
BIN2=$(ROOT)/arm-epoc-pe/bin
LIBSRC=../SDK-lib
LIBDST=$(ROOT)/epocsdk/lib

INCSRC=../SDK-inc
INCDST=$(ROOT)/epocsdk/include

install: $(BIN1)/emxobj emximage emxdump emxrsc emxaif emxfixhdr emxmap \
		emxlink emxmknod eld esh emake emxsis emxrsc.rss emxrsc.hrh \
		emx-config-cross
	test -d $(BIN1)
	cp emxinstall emximage emxdump emxrsc emxaif emxmap $(BIN1)
	chmod u+w $(BIN1)/emxinstall
	cp emxdump emxsis emxfixhdr emxlink emxmknod esh emake $(BIN1)
	cp emx-config-cross $(BIN1)
	chmod u+w $(BIN1)/emx-config-cross
	cp eld $(BIN1)/arm-epoc-pe-eld
	cp eld $(BIN2)/eld
	cp emxrsc.rss $(ROOT)/lib/emxrsc.rss
	cp emxrsc.hrh $(ROOT)/lib/emxrsc.hrh
	chmod u+w $(ROOT)/lib/emxrsc.rss
	chmod u+w $(ROOT)/lib/emxrsc.hrh

$(BIN1)/emxobj: emxobj
	test -d $(BIN1)
	cp emxobj $(BIN1)

install-sdk: $(BIN1)/emxobj remangle.explicit epoc.imp \
		install-epocsdk-libs install-epocsdk-headers
	mkdir -p $(LIBDST)
	mkdir -p $(INCDST)
	./install-epocsdk-libs $(LIBSRC) $(LIBDST)
	./install-epocsdk-headers $(INCSRC) $(INCDST)
	cp epoc.imp $(LIBDST)

EMXOBJ_OBJ=emxobj.o remangle.o pecoff.o binfile.o \
	xstr.o xstrio.o xstrvec.o xnew.o error.o
emxobj: $(EMXOBJ_OBJ)
	$(CCP) -o emxobj $(EMXOBJ_OBJ)

EMXDUMP_OBJ=emxdump.o emxheader.o priority.o uid_crc.o crc16.o \
	xstr.o xstrio.o xstrvec.o xnew.o binfile.o chkbinfile.o \
	disasm.o error.o
emxdump: $(EMXDUMP_OBJ)
	$(CCP) -o emxdump $(EMXDUMP_OBJ)

EMXIMAGE_OBJ=emximage.o emxheader.o priority.o uid_crc.o crc16.o \
		binfile.o xstr.o xstrio.o xnew.o error.o pecoff.o
emximage: $(EMXIMAGE_OBJ)
	$(CCP) -o emximage $(EMXIMAGE_OBJ)

EMXRSC_OBJ=emxrsc.o xstr.o xstrio.o xnew.o spawnp.o tempfile.o binfile.o \
	error.o
emxrsc: $(EMXRSC_OBJ)
	$(CCP) -o emxrsc $(EMXRSC_OBJ)

EMXAIF_OBJ=emxaif.o xstr.o xnew.o uid_crc.o crc16.o binfile.o tempfile.o \
		spawnp.o error.o fname.o
emxaif: $(EMXAIF_OBJ)
	$(CCP) -o emxaif $(EMXAIF_OBJ)

EMXFIXHDR_OBJ=emxfixhdr.o error.o
emxfixhdr: $(EMXFIXHDR_OBJ)
	$(CC) -o emxfixhdr $(EMXFIXHDR_OBJ)

EMXLINK_OBJ=emxlink.o uid_crc.o crc16.o
emxlink: $(EMXLINK_OBJ)
	$(CC) -o emxlink $(EMXLINK_OBJ)

EMXMKNOD_OBJ=emxmknod.o uid_crc.o crc16.o
emxmknod: $(EMXMKNOD_OBJ)
	$(CC) -o emxmknod $(EMXMKNOD_OBJ)

EMXMAP_OBJ=emxmap.o xstr.o xstrio.o xnew.o error.o
emxmap: $(EMXMAP_OBJ)
	$(CCP) -o emxmap $(EMXMAP_OBJ)

ESH_OBJ=esh.o xstr.o xstrio.o xstrvec.o xnew.o spawnp.o uid_crc.o crc16.o \
	error.o
esh: $(ESH_OBJ)
	$(CCP) -o esh $(ESH_OBJ)

EMAKE_OBJ=emake.o xstr.o xstrio.o xstrvec.o xnew.o spawn.o spawnp.o error.o
emake: $(EMAKE_OBJ)
	$(CCP) -o emake $(EMAKE_OBJ)

disasm: disasm.cc disasm.h
	$(CCP) -DTEST -o disasm disasm.cc

ELD_OBJ=eld.o tempfile.o spawn.o xstr.o xstrio.o xstrvec.o xnew.o \
	error.o fname.o
eld: $(ELD_OBJ)
	$(CCP) -o eld $(ELD_OBJ)

EMXSIS_OBJ=emxsis.o uid_crc.o crc16.o language.o error.o xstr.o xstrio.o \
	xnew.o binfile.o chkbinfile.o fname.o
emxsis: $(EMXSIS_OBJ)
	$(CCP) -o emxsis $(EMXSIS_OBJ)

ebasename: basename.cc
	$(CCP) -o ebasename basename.cc

ecat: cat.c
	$(CC) -o ecat cat.c

edirname: dirname.cc
	$(CCP) -o edirname dirname.cc

eprintenv: printenv.c
	$(CC) -o eprintenv printenv.c

ELS_OBJ=ls.o xstr.o xnew.o uid_crc.o crc16.o error.o
els: $(ELS_OBJ)
	$(CCP) -o els $(ELS_OBJ)

emxdump.o: emxdump.cc priority.h uid_crc.h disasm.h xstr.h xstrio.h \
	xvec.h xvec.cc xstrvec.h xnew.h binfile.h chkbinfile.h error.h
emximage.o: emximage.cc priority.h uid_crc.h xstr.h xstrio.h xvec.h xvec.cc \
	xnew.h binfile.h error.h rc_ptr.h pecoff.h
emxrsc.o: emxrsc.cc emxrsc.hrh xstr.h xstrio.h xvec.h xvec.cc \
	xhash.h xhash.cc xnew.h tempfile.h binfile.h error.h
emxaif.o: emxaif.cc xstr.h xvec.h xvec.cc uid_crc.h binfile.h \
	tempfile.h xnew.h error.h fname.h
emxobj.o: emxobj.cc xstr.h xstrio.h xvec.h xvec.cc xhash.h xhash.cc xnew.h \
	binfile.h rc_ptr.h pecoff.h remangle.h error.h
emxfixhdr.o: emxfixhdr.c error.h
emxlink.o: emxlink.c uid_crc.h
emxmknod.o: emxmknod.c uid_crc.h
eld.o: eld.cc tempfile.h xstrvec.h xstr.h xstrio.h xvec.h xhash.h xhash.cc \
	error.h fname.h
esh.o: esh.cc esh-epoc.h xstrvec.h xstr.h xstrio.h xvec.h xvec.cc xnew.h \
	xhash.h xhash.cc uid_crc.h
emake.o: emake.cc xstrvec.h xstr.h xstrvec.h xvec.h xvec.cc xstrio.h \
	xhash.h xhash.cc xlist.h xlist.cc xnew.h
ls.o: ls.cc xstr.h xvec.h xvec.cc xnew.h uid_crc.h error.h
emxsis.o: emxsis.cc uid_crc.h crc16.h language.h error.h xstr.h xstrio.h \
	xvec.h xvec.cc xstrvec.h xhash.h xhash.cc binfile.h chkbinfile.h \
	fname.h
remangle.o: remangle.cc remangle.h xstr.h xvec.h xstrvec.h xhash.h xhash.cc \
	error.h
pecoff.o: pecoff.cc pecoff.h rc_ptr.h xstr.h xvec.h xvec.cc binfile.h \
	error.h
language.o: language.c language.h
priority.o: priority.c priority.h
uid_crc.o: uid_crc.c uid_crc.h crc16.h
crc16.o: crc16.c crc16.h
disasm.o: disasm.cc disasm.h
fname.o: fname.c fname.h
tempfile.o: tempfile.cc tempfile.h xstr.h
binfile.o: binfile.cc binfile.h error.h xstr.h
chkbinfile.o: chkbinfile.cc binfile.h chkbinfile.h error.h xstr.h xnew.h
emxheader.o: emxheader.cc xstr.h binfile.h
error.o: error.c error.h
spawn.o: spawn.c
spawnp.o: spawnp.c
xstr.o: xstr.cc xstr.h error.h
xstrio.o: xstrio.cc xstr.h xstrio.h
xstrvec.o: xstrvec.cc xvec.cc xstrvec.h xstr.h xvec.h xnew.h

epoc.imp: emxobj
	rm -f epoc.imp
	for x in $(LIBSRC)/*.[Ll][Ii][Bb] ;do ./emxobj -i -m1 $$x >>epoc.imp ;done

test: test-disasm

test-disasm: disasm
	./disasm <disasm.inp >disasm.out
	diff disasm.out disasm.exp

clean:
	rm -f *.o *.exe
	rm -f emxobj emxdump emximage emxrsc emxaif
	rm -f eld els esh emake emxsis emxfixhdr emxlink emxmknod
	rm -f disasm ebasename ecat edirname eprintenv
	rm -f epoc.imp disasm.out
