# Makefile -- Copyright (c) 2001-2002 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.
#

# cf. <emxuid.h>
KEmxDllUidValue=0x101f3dcc
KEmxLinkUidValue=0x101f5c6c
KEmxDeviceUidValue=0x101f5c6d

ROOT=/usr/local/epocemx
BIN=$(ROOT)/arm-epoc-pe/bin

GCC0=arm-epoc-pe-gcc -Wall -I./include -O2 -Darm32
STATIC_GCC=$(GCC0)
SHARED_GCC=$(GCC0) -shared
AS=arm-epoc-pe-as
AR=$(BIN)/ar
RANLIB=$(BIN)/ranlib

I=include
E=$(I)/emxdll

default: libemx.a emxexe.o emxdll.o emx.dll emxrun.exe \
	emxlink.aif emxlink.app \
	emxdevice.aif emxdevice.app \
	emx.imp libtermcap.a libm.a

.PHONY: clean default install install-all install-epoc sis \
	sis-minor sis-minor-helper

# .o is used for static objects, .obj for DLL objects
.SUFFIXES: .c .cc .o .obj .s

%.o: %.c
	$(STATIC_GCC) -c $<
%.obj: %.c
	$(SHARED_GCC) -c -o $@ $<
%.o: %.cc
	$(STATIC_GCC) -c $<
%.obj: %.cc
	$(SHARED_GCC) -c -o $@ $<
%.obj: fdlibm/%.c
	$(SHARED_GCC) -c -o $@ $<

.s.o:
	$(AS) -o $@ $<
.s.obj:
	$(AS) -o $@ $<

sis: emxdll.sis

install-all: install install-epoc

install: libemx.a emxexe.o emxdll.o emx.dll emxrun.exe emx.imp \
		libtermcap.a libm.a
	mkdir -p $(ROOT)/lib
	mkdir -p $(ROOT)/include/emxdll
	mkdir -p $(ROOT)/include/emx/sys
	mkdir -p $(ROOT)/include/sys
	cp libemx.a emxexe.o emxdll.o emx.imp libtermcap.a libm.a $(ROOT)/lib
	cp $(I)/*.h $(ROOT)/include
	cp $(E)/*.h $(ROOT)/include/emxdll
	cp $(I)/emx/*.h $(ROOT)/include/emx
	cp $(I)/emx/sys/*.h $(ROOT)/include/emx/sys
	cp $(I)/sys/*.h $(ROOT)/include/sys
	chmod u+w $(ROOT)/include/*.h
	chmod u+w $(ROOT)/include/emxdll/*.h
	chmod u+w $(ROOT)/include/emx/*.h
	chmod u+w $(ROOT)/include/emx/sys/*.h
	chmod u+w $(ROOT)/include/sys/*.h

install-epoc: libemx.a emxexe.o emxdll.o emx.dll emxrun.exe emx.imp \
		libtermcap.a libm.a
	emxinstall -copy libemx.a emxexe.o emxdll.o emx.imp \
		libtermcap.a libm.a usr/lib
	emxinstall -copy $(I)/*.h usr/include
	emxinstall -copy $(E)/*.h usr/include/emxdll
	emxinstall -copy emx.dll emxrun.exe system/libs

DLL2SVR_OBJ=dll2svr01.o dll2svr02.o dll2svr03.o dll2svr04.o dll2svr05.o \
	dll2svr06.o dll2svr07.o dll2svr08.o dll2svr09.o dll2svr10.o

STATIC_OBJECTS=$(DLL2SVR_OBJ) getopt.o main.o

DLL_OBJECTS=config.obj bsearch.obj ctype.obj dllinit.obj dtoa.obj e32math.obj \
	emxconsole.obj emxmain.obj emxtrap.obj environ.obj \
	fsclient.obj fsserver.obj gcvt.obj inet.obj input.obj io.obj \
	locale.obj malloc.obj mkstemp.obj \
	mktemp.obj moremath.obj output.obj pathconf.obj proto.obj pwdgrp.obj \
	qsort.obj setjmp.obj signal.obj \
	spawn.obj stdio-emx.obj stdio-estlib.obj stdlib.obj strftime.obj \
	string.obj sysconf.obj termcap.obj time.obj thread.obj utils.obj \
	s_ceil.obj s_copysign.obj s_copysignf.obj s_expm1.obj \
	s_finite.obj s_floor.obj s_frexp.obj s_isinf.obj s_isinff.obj \
	s_isnan.obj s_isnanf.obj s_ldexp.obj \
	s_modf.obj s_rint.obj s_scalbn.obj

# use -nostdlib to avoid linking against libemx.a...
emx.dll imp.a: $(DLL_OBJECTS) estlib.a emx.def emxdll.o dll.a
	$(SHARED_GCC) -o emx.dll -Zimplib imp.a -Zmap emx.map -nostdlib \
		emxdll.o $(DLL_OBJECTS) emx.def \
		-lapparc -lbafl -lcone -leikon -lefsrv -lestor -lesock \
		-linsock \
		dll.a estlib.a -lgccsub -leuser \
		-uid3 $(KEmxDllUidValue)

libemx.a: imp.a $(STATIC_OBJECTS)
	cp imp.a libemx.a
	$(AR) rs libemx.a $(STATIC_OBJECTS)

emxexe.o: emxexe.s

emxdll.o: emxdll.s

estlib.a: estlib.def dllname.sed
	arm-epoc-pe-dlltool --output-lib estlib.a --input-def estlib.def \
		--dllname "`head -1 estlib.def|sed -f dllname.sed`"

estlib.def: $(ROOT)/epocsdk/lib/libestlib.a estlib.sed
	emxobj -e $(ROOT)/epocsdk/lib/libestlib.a | \
		sed -n -f estlib.sed >estlib.def

estlib.sed: rename.sed estlib.rename
	echo '/^[;E]/p' >estlib.sed
	sed -n -f rename.sed estlib.rename >>estlib.sed

emx.imp: libemx.a
	emxobj -i libemx.a >emx.imp

# Static objects

DLL2SVR_S=$(DLL2SVR_OBJ:.o=.s)

$(DLL2SVR_S): make-dll2svr dll2svr.list dll2svr.template
	./make-dll2svr <dll2svr.list

dll.a: $(DLL2SVR_OBJ)
	$(AR) crs dll.a $(DLL2SVR_OBJ)

# DLL objects
bsearch.obj: bsearch.c
config.obj: config.cc $(I)/emxuid.h $(E)/config.h $(E)/global.h $(E)/thread.h
ctype.obj: ctype.cc
dllinit.obj: dllinit.cc $(I)/emx.h $(I)/emxconsole.h $(E)/config.h \
	$(E)/global.h $(E)/thread.h $(E)/fsclient.h $(E)/fsserver.h \
	$(E)/stdio.h $(E)/spawn.h $(E)/time.h $(E)/utils.h
dtoa.obj: dtoa.c
e32math.obj: e32math.cc
emxtrap.obj: emxtrap.cc $(E)/spawn.h
emxconsole.obj: emxconsole.cc $(I)/emx.h $(I)/emxconsole.h $(E)/emxconsole.h \
	$(E)/utils.h
emxmain.obj: emxmain.cc $(I)/emx.h $(I)/emxheader.h $(E)/config.h \
	$(E)/global.h $(E)/spawn.h $(E)/stdio.h $(E)/thread.h $(E)/utils.h \
	estlib.h
environ.obj: environ.cc $(E)/thread.h
fsclient.obj: fsclient.cc $(I)/emx.h $(E)/fsclient.h $(E)/fsserver.h
fsserver.obj: fsserver.cc $(I)/emx.h $(I)/emxconsole.h $(I)/emxdevices.h \
	$(I)/emxuid.h \
	$(E)/config.h $(E)/fsclient.h $(E)/fsserver.h $(E)/global.h \
	$(E)/spawn.h $(E)/time.h $(E)/utils.h
gcvt.obj: gcvt.c $(E)/float.h
inet.obj: inet.cc $(E)/thread.h
input.obj: input.c
io.obj: io.cc estlib.h $(I)/emx.h $(I)/utime.h $(E)/thread.h $(E)/fsclient.h \
	$(E)/fsserver.h $(E)/utils.h $(I)/emx/sys/ioctl.h
locale.obj: locale.c
malloc.obj: malloc.cc
mkstemp.obj: mkstemp.c
mktemp.obj: mktemp.c
moremath.obj: moremath.c fdlibm/math_private.h
qsort.obj: qsort.c
s_ceil.obj: fdlibm/s_ceil.c fdlibm/math_private.h
s_copysign.obj: fdlibm/s_copysign.c fdlibm/math_private.h
s_copysignf.obj: fdlibm/s_copysignf.c fdlibm/math_private.h
s_expm1.obj: fdlibm/s_expm1.c fdlibm/math_private.h
s_finite.obj: fdlibm/s_finite.c fdlibm/math_private.h
s_floor.obj: fdlibm/s_floor.c fdlibm/math_private.h
s_frexp.obj: fdlibm/s_frexp.c fdlibm/math_private.h
s_isinf.obj: fdlibm/s_isinf.c fdlibm/math_private.h
s_isinff.obj: fdlibm/s_isinff.c fdlibm/math_private.h
s_isnan.obj: fdlibm/s_isnan.c fdlibm/math_private.h
s_isnanf.obj: fdlibm/s_isnanf.c fdlibm/math_private.h
s_ldexp.obj: fdlibm/s_ldexp.c fdlibm/math_private.h
s_modf.obj: fdlibm/s_modf.c fdlibm/math_private.h
s_rint.obj: fdlibm/s_rint.c fdlibm/math_private.h
s_scalbn.obj: fdlibm/s_scalbn.c fdlibm/math_private.h
output.obj: output.c
pathconf.obj: pathconf.c
proto.obj: proto.c
pwdgrp.obj: pwdgrp.c $(I)/grp.h
setjmp.obj: setjmp.s
signal.obj: signal.c
spawn.obj: spawn.cc $(I)/emx.h $(I)/emxheader.h $(I)/process.h \
	$(E)/config.h $(E)/epocimage.h $(E)/fsclient.h $(E)/global.h \
	$(E)/spawn.h $(E)/thread.h $(E)/utils.h estlib.h
stdio-emx.obj: stdio-emx.c $(E)/thread.h
stdio-estlib.obj: stdio-estlib.cc estlib.h \
	$(E)/fsclient.h $(E)/stdio.h $(I)/process.h
stdlib.obj: stdlib.cc strtol.cc $(E)/thread.h
strftime.obj: strftime.c $(E)/utils.h
string.obj: string.cc $(E)/thread.h
sysconf.obj: sysconf.c
termcap.obj: termcap.c $(I)/termcap.h $(E)/thread.h
time.obj: time.cc $(E)/thread.h $(E)/time.h
thread.obj: thread.c $(E)/thread.h
utils.obj: utils.cc $(I)/emx.h $(I)/emxuid.h $(E)/utils.h

emxrun.exe: emxexe.o emxrun.o libemx.a
	$(STATIC_GCC) -nostdlib -o emxrun.exe emxexe.o emxrun.o libemx.a

emxrun.o: emxrun.s

emxlink.aif: emxlink-aif.rss emxlink.mbm
	emxaif -o emxlink.aif emxlink-aif.rss emxlink.mbm

emxlink.app: emxlink.cc emxlink.def $(I)/emxuid.h
	$(SHARED_GCC) -o emxlink.app emxlink.cc emxlink.def \
		-lapparc -leikon -uid3 $(KEmxLinkUidValue)

emxdevice.aif: emxdevice-aif.rss emxdevice.mbm
	emxaif -o emxdevice.aif emxdevice-aif.rss emxdevice.mbm

emxdevice.app: emxdevice.cc emxdevice.def $(I)/emxuid.h
	$(SHARED_GCC) -o emxdevice.app emxdevice.cc emxdevice.def \
		-lapparc -leikon -uid3 $(KEmxDeviceUidValue)

libtermcap.a:
	$(AR) crs libtermcap.a

libm.a:
	$(AR) crs libm.a

SISFILES=EM EMPTY emx.dll emxrun.exe emxlink.aif emxlink.app \
	emxdevice.aif emxdevice.app \
	../COPYING ../README c.lnk d.lnk e.lnk z.lnk usr.lnk \
	tty.dev console.dev null.dev zero.dev c.dev d.dev e.dev
emxdll.sis: emxdll.pkg $(SISFILES)
	emxsis -D KEmxDllUidValue=$(KEmxDllUidValue) emxdll.pkg emxdll.sis

c.lnk:
	emxlink c:/ c.lnk

d.lnk:
	emxlink d:/ d.lnk

e.lnk:
	emxlink e:/ e.lnk

z.lnk:
	emxlink z:/ z.lnk

usr.lnk:
	emxlink d:/emx usr.lnk

tty.dev:
	emxmknod tty.dev c 1 1

console.dev:
	emxmknod console.dev c 1 2

null.dev:
	emxmknod null.dev c 2 1

zero.dev:
	emxmknod zero.dev c 2 2

c.dev:
	emxmknod c.dev b 1 3

d.dev:
	emxmknod d.dev b 1 4

e.dev:
	emxmknod e.dev b 1 5

sis-minor: $(SISFILES) sis-minor-helper emxdll.sis

sis-minor-helper:
	emxsis -iv -D KEmxDllUidValue=$(KEmxDllUidValue) emxdll.pkg tmp.pkg
	mv -f tmp.pkg emxdll.pkg

clean:
	rm -f *.o *.obj *.lnk *.dev imp.a libemx.a libtermcap.a libm.a
	rm -f emxexe.o emxdll.o dll2svr??.s tmp.a
	rm -f emx.dll emx.imp emxrun.exe
	rm -f emxlink.aif emxlink.app emxdevice.aif emxdevice.app
	rm -f dll.a estlib.a estlib.def estlib.sed emxdll.sis tmp.pkg
	rm -f make-dll2svr.tmp
