# Copyright (C) 1991 Free Software Foundation, Inc.
# This file is part of the GNU Hurd.
# 
# The GNU Hurd 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 1, or (at your option)
# any later version.
#
# The GNU Hurd 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 the GNU Hurd; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.


GCC_EXEC_PREFIX=/usr/local/lib/gcc-
MK_INCLUDES=/usr1/mk/release/i386_mach/latest/include
CC=gcc
MACHSA_LIB=/usr1/mk/release/i386_mach/latest/lib/libmach_sa.a
MIGCOM=/usr1/mk/release/i386_mach/latest/lib/migcom
CTHREAD_LIB=/usr1/mk/release/i386_mach/latest/lib/libthreads.a
CPPFLAGS=-nostdinc -I. -I.. -I${MK_INCLUDES} \
	 -D__USE_GNU -D__GNU_SYSTEM -DCOMPAT_44
CFLAGS=-Wall -O -g

VPATH=.:../machine:../hurd
.PHONY: clean

# Emulator sources written in C
CSOURCES = alix_descrip.c alix_fs.c alix_generic.c alix_ipc.c alix_mman.c \
	alix_procprot.c alix_sig.c alix_sysent.c alix_misc.c alix_machdep.c \
	miscServer.c

# Emulator sources written in assembler.
SSOURCES = alix_vector.S

# Emulator sources written in MiG, residing in ../hurd.
MIGSOURCES = auth.defs fs.defs io.defs name.defs process.defs \
	socket.defs

# C files which come from MiG sources
MIGCFILES = $(subst .defs,User.c,$(MIGSOURCES))

# Files read through CPP (and thus which need -M dependency generation)
SOURCES = $(CSOURCES) $(SSOURCES) alix_genassym.c $(MIGCFILES)

# Dependency files
DEPFILES = $(addprefix .dep_,$(notdir $(SOURCES)))
	
# Files that get linked into the final emulator image	
OFILES = $(notdir $(subst .c,.o,$(CSOURCES))) \
	$(notdir $(subst .S,.o,$(SSOURCES))) \
	$(notdir $(subst .c,.o,$(MIGCFILES)))

test: $(OFILES)
	$(GCC_EXEC_PREFIX)ld -o alix $(OFILES) \
	../lib/libhurd.a $(CTHREAD_LIB) $(MACHSA_LIB)

# How to build the assembler symbols file
assym.h: alix_genassym
	alix_genassym > assym.h

# How to build system call table
alix_sysent.h alix_sysent.c: alix_makesysent syscalls.list
	alix_makesysent

# We need this line because we don't want the CPPFLAGS above. 
alix_makesysent: alix_makesysent.c
	$(CC) -o alix_makesysent alix_makesysent.c

misc.h miscServer.c: misc.defs
	$(GCC_EXEC_PREFIX)cpp $(CPPFLAGS) $< \
	| $(MIGCOM) -header misc.h -user /dev/null -server miscServer.c

socket.h socketUser.c: socket.defs
	$(GCC_EXEC_PREFIX)cpp $(CPPFLAGS) "-DSOCKET_IMPORTS=userprefix rpc_;" \
	$< | $(MIGCOM) -header socket.h -user socketUser.c \
	  -server /dev/null

process.h processUser.c: process.defs
	$(GCC_EXEC_PREFIX)cpp $(CPPFLAGS) \
	 "-DPROCESS_IMPORTS=userprefix rpc_;" \
	$< | $(MIGCOM) -header process.h -user processUser.c \
	-server /dev/null

io.h ioUser.c: io.defs
	$(GCC_EXEC_PREFIX)cpp $(CPPFLAGS) "-DIO_IMPORTS=userprefix rpc_;" $< \
	| $(MIGCOM) -header io.h -user ioUser.c -server /dev/null

fs.h fsUser.c: fs.defs
	$(GCC_EXEC_PREFIX)cpp $(CPPFLAGS) "-DFILE_IMPORTS=userprefix rpc_;" $<\
	| $(MIGCOM) -header fs.h -user fsUser.c -server /dev/null

auth.h authUser.c: auth.defs
	$(GCC_EXEC_PREFIX)cpp $(CPPFLAGS) $< \
	| $(MIGCOM) -header auth.h -user authUser.c \
	  -server /dev/null

name.h nameUser.c: name.defs
	$(GCC_EXEC_PREFIX)cpp $(CPPFLAGS) $< \
	| $(MIGCOM) -header name.h -user nameUser.c \
	  -server /dev/null

#End XXX

#$(OFILES): force
force:

# Automatic dependency generation
include Makedeps
Makedeps: $(SOURCES)
	for i in $^; do $(GCC_EXEC_PREFIX)cpp -M $(CPPFLAGS) $$i; done>Makedeps

# Cleanliness
clean:
	rm -f *.o $(subst .defs,.h,$(MIGSOURCES)) $(MIGCFILES) \
	alix_genassym assym.h Makedeps
