# Rules for MiG interfaces that want to go into the C library.

# Copyright (C) 1991, 1992 Free Software Foundation, Inc.
# This file is part of the GNU C Library.

# The GNU C Library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public License
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.

# The GNU C Library 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
# Library General Public License for more details.

# You should have received a copy of the GNU Library General Public
# License along with the GNU C Library; see the file COPYING.LIB.  If
# not, write to the Free Software Foundation, Inc., 675 Mass Ave,
# Cambridge, MA 02139, USA.

# Makefiles define these variable before including this file:
#	user-interfaces		Names of interfaces to put user stubs in for.
#	server-interfaces	Names of interfaces to put server stubs in for.
#	interface-header-prefix	Directory prefix for interface header files.
# This file sets:
#	interface-headers	Names of generated interface header files.
#	interface-routines	Names of generated interface routines.
# All user stubs are put in individual files, prefixed with __, and a
# symbol-alias to the un-__'d form generated; header for both is put in
# foo.h.  If a server interface is listed as __foo, then its routines are
# prefixed with __; server interfaces are written to foo_server.c and
# foo_server.h (or __foo_server.c and __foo_server.h).

# Includers can also add to or modify `migdefines' to set MiG flags.

all:

include ../Makeconfig

# Where to find interface definition files.
ifdef	MIG_DEFS_PATH
vpath %.defs $(MIG_DEFS_PATH)
endif

ifndef MIG
MIG = mig
endif
MIGFLAGS = -DMACH_IPC_COMPAT=0 -DSTANDALONE -DTypeCheck=0 \
	   $(includes) $(migdefines)

.SUFFIXES: .defs

define nl


endef
ifdef user-interfaces
include interface-rules
endif
interface-rules: Makefile Machrules
	(for interface in $(user-interfaces); do	\
	   echo "include .ir-$${interface}";		\
	 done) > $@-new
	mv $@-new $@
.ir-%: %.uh Machrules
	(echo 'define $*-calls'					;\
	 awk '/^kern_return_t/ { print $$2 }' $<		;\
	 echo 'endef'						;\
	 echo '$*-calls := $$(subst $$(nl), ,$$($*-calls))'	;\
	 echo '$$(patsubst %,foreign-%%c,$$($*-calls:%=__%)): $*.defs'	;\
	 echo '	$$(MIG) < $$< $$(MIGFLAGS) -subrprefix __ -prefix __ -i ./foreign-__')\
	> $@-new
	mv $@-new $@
vpath Machrules ../mach

%.c: foreign-%.c
	(echo '#include <ansidecl.h>'; cat $^) > $@

# MiG doesn't know how to make separate files for the server stubs.
foreign-__%_server.c %_server.h: %.defs
	$(MIG) < $< $(MIGFLAGS) -subrprefix __ -prefix __ \
	       -user /dev/null -header /dev/null \
	       -server $@ -sheader $(@:__%.c=%.h)
foreign-%_server.c %_server.h: %.defs
	$(MIG) < $< $(MIGFLAGS) -subrprefix __ \
	       -user /dev/null -header /dev/null \
	       -server $@ -sheader $(@:.c=.h)

%.uh: %.defs
	$(MIG) < $< $(MIGFLAGS) -subrprefix __ \
	       -header $@ -server /dev/null -user /dev/null
%.__h: %.defs
	$(MIG) < $< $(MIGFLAGS) -subrprefix __ -prefix __ \
	       -header $@ -server /dev/null -user /dev/null

$(interface-header-prefix)%.h: %.__h %.uh $(interface-header-prefix)
# The last line of foo.uh is "#endif _foo_user_".
# The first two lines of foo.__h are "#ifndef _foo_user_"/"#define _foo_user_".
	(sed '$$d' < $<; tail +2 $(word 2,$^)) > $@

%.c: __%.c
	(echo '#include <gnu-stabs.h>'; \
	 echo 'symbol_alias (__$*, $*)') > $@

interface-routines := $(foreach if,$(user-interfaces),		\
				$(calls-$(if))			\
				$(addprefix __,$(calls-$(if))))	\
		      $(server-interfaces:%=%_server)
interface-headers := $(addprefix $(interface-header-prefix),\
				 $(user-interfaces:%=%.h) \
				 $(server-interfaces:%=%_server.h))
