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

# Copyright (C) 1991, 1992, 1993 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.
# 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

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

.SUFFIXES: .defs

define nl	# This is needed by *.ir.


endef
ifdef user-interfaces
interface-includes := $(foreach if,$(user-interfaces),$(if).ir)
ifndef no_deps
include $(interface-includes)
endif
generated := $(generated) $(interface-includes)
endif

# %.ir defines a variable `%-calls', which lists the RPCs defined by
# %.defs, and a rule to build $(%-calls:%=__$(%-userprefix)%.c) from
# %.defs, where $(%-userprefix) is the user prefix given in %.defs.  We use
# the kludgificacious method of defining a pattern rule to build files
# matching patterns we are pretty damn sure will only match the particular
# files we have in mind.
%.ir: %.uh
	(echo 'define $*-calls'					;\
	 awk '/^kern_return_t/ { print $$2 }' $<		;\
	 echo 'endef'						;\
	 echo '$*-calls := $$(subst $$(nl), ,$$($*-calls))'	;\
	 echo '$$($*-calls:%=_\%%.c): $*.defs'			;\
	 echo '	$$(generate-user-calls)'			 \
	) > $@-new
	mv $@-new $@
vpath Machrules ../mach	# Find ourselves.

define generate-user-calls
$(MIG) $< $(MIGFLAGS) \
       -prefix __ -i ./__ -server /dev/null -user /dev/null -header /dev/null
endef

__%_server.c __%_server.h: %.defs
	$(MIG) $< $(MIGFLAGS) -prefix __ \
	       -user /dev/null -header /dev/null \
	       -server $(@:.h=.c) -sheader $(@:.c=.h)
%_server.c %_server.h: %.defs
	$(MIG) $< $(MIGFLAGS) \
	       -user /dev/null -header /dev/null \
	       -server $(@:.h=.c) -sheader $(@:.c=.h)

# To get header files that declare both the straight and __ functions,
# we generate two files and paste them together.
%.uh: %.defs
	$(MIG) $< $(MIGFLAGS) \
	       -header $@ -server /dev/null -user /dev/null
%.__h: %.defs
	$(MIG) $< $(MIGFLAGS) -prefix __ \
	       -header $@ -server /dev/null -user /dev/null

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

if-calls.c := $(strip $(foreach if,$(user-interfaces),$($(if)-calls:%=%.c)))
ifdef if-calls.c
$(if-calls.c):
	(echo '#include <gnu-stabs.h>'; \
	 echo 'symbol_alias (__$*, $*)') > $@-new
	mv $@-new $@
endif

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

# Remove the generated user stub source and header files,
# and don't distribute them.
mach-generated := $(interface-routines:%=%.c) \
		  $(interface-headers:%=%.h %.uh %.__h)
generated := $(generated) $(mach-generated)

# These are needed to generate the dependencies.
before-compile := $(before-compile) $(interface-headers)
