#
# Copyright (c) 1995-2000 University of Utah and the Flux Group.
# All rights reserved.
# 
# This file is part of the Flux OSKit.  The OSKit is free software, also known
# as "open source;" you can redistribute it and/or modify it under the terms
# of the GNU General Public License (GPL), version 2, as published by the Free
# Software Foundation (FSF).  To explore alternate licensing terms, contact
# the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
# 
# The OSKit 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 GPL for more details.  You should have
# received a copy of the GPL along with the OSKit; see the file COPYING.  If
# not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
#

ifndef _oskit_freebsd_libc_makerules_
_oskit_freebsd_libc_makerules_ = yes

TARGET	= liboskit_freebsd_c.a

all: $(TARGET)

SRCDIRS	+=	$(OSKIT_SRCDIR)/freebsd/libc/gmon			\
		$(OSKIT_SRCDIR)/freebsd/libc/glue			\
		$(OSKIT_SRCDIR)/freebsd/libc/malloc			\
		$(OSKIT_SRCDIR)/freebsd/libc/x86/glue			\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/db/btree	\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/db/db		\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/db/hash	\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/db/mpool	\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/db/recno	\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/i386/net	\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/i386/gen	\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/i386/stdlib	\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/i386/string	\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/net		\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/regex		\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/stdio		\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/stdlib		\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/stdtime	\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/locale		\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/quad		\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/string		\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/compat-43

#
# There are many oddballs in the gen directory which I do not want
# to deal with yet. Just build some of it.
#
MOSTLY_SRCDIRS := \
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/gen \
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/rpc

OBJFILES +=	_rand48.o alarm.o assert.o drand48.o \
		erand48.o err.o fnmatch.o fts.o isatty.o jrand48.o \
		lcong48.o lrand48.o mrand48.o nrand48.o seed48.o \
		signal.o srand48.o termios.o tcgetattr.o time.o \
		getcwd.o exec.o ctermid.o fstab.o getgrent.o getpwent.o \
		getpass.o valloc.o rcmd.o getnetgrent.o arc4random.o

INCDIRS +=	$(OSKIT_SRCDIR)/freebsd/libc/include			\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/include			\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/sys			\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/include	\
		$(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/i386		\
		objinclude

DEFINES +=	-DOSKIT -D__FreeBSD__=3 -DNOPOLL -DNO__GETCWD 		\
		-D__DBINTERFACE_PRIVATE -D__MPOOLINTERFACE_PRIVATE

HFILES  :=	$(patsubst $(OSKIT_SRCDIR)/freebsd/3.x/src/include/%,%,	     \
	    $(wildcard $(addprefix $(OSKIT_SRCDIR)/freebsd/3.x/src/include/, \
			*.h */*.h */*/*.h)))				     \
		$(patsubst $(OSKIT_SRCDIR)/freebsd/3.x/src/sys/%,%,	     \
	    $(wildcard $(addprefix $(OSKIT_SRCDIR)/freebsd/3.x/src/sys/net/, \
			*.h */*.h */*/*.h)))				     \
		$(patsubst $(OSKIT_SRCDIR)/freebsd/3.x/src/sys/%,%,	     \
	    $(wildcard $(addprefix $(OSKIT_SRCDIR)/freebsd/3.x/src/sys/netinet/,  \
			*.h */*.h */*/*.h)))				     \
		$(patsubst $(OSKIT_SRCDIR)/freebsd/3.x/src/sys/%,%,	     \
	    $(wildcard $(addprefix $(OSKIT_SRCDIR)/freebsd/3.x/src/sys/sys/, \
			*.h */*.h */*/*.h)))

EXTRAH  :=      $(patsubst $(OSKIT_SRCDIR)/freebsd/libc/include/%,%, 	      \
	    $(wildcard $(addprefix $(OSKIT_SRCDIR)/freebsd/libc/include/,     \
			*.h */*.h */*/*.h)))				      \
		$(patsubst $(OSKIT_SRCDIR)/freebsd/libc/include/%,%,	      \
	    $(wildcard $(addprefix $(OSKIT_SRCDIR)/freebsd/libc/include/sys,  \
			*.h */*.h */*/*.h)))				      \
		$(patsubst $(OSKIT_SRCDIR)/freebsd/libc/include/%,%, 	      \
	    $(wildcard $(addprefix $(OSKIT_SRCDIR)/freebsd/libc/include/i386, \
			*.h */*.h */*/*.h)))

# This file isn't meant to be compiled by itself.
OSKIT_EXCLUDE := $(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/regex/engine.c

# This is superceded by the version in the glue dirrectory.
OSKIT_EXCLUDE += $(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/string/strerror.c

# These contain references to things we cannot do yet,
OSKIT_EXCLUDE += $(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/net/rcmd.c \
		 $(OSKIT_SRCDIR)/freebsd/3.x/src/lib/libc/stdlib/system.c

### Include other appropriate make rules files ###
include $(OSKIT_SRCDIR)/GNUmakerules-lib

# Remove the -I- from the CPPFLAGS option string,
# because the db code needs the traditional C compiler behavior
# of searching for headers first in the source file's directory.
OSKIT_CPPFLAGS := $(filter-out -I-,$(OSKIT_CPPFLAGS))

# Create the objinclude/machine symlink.
# We have to kludge a little to get it to work right,
# because OSKIT_SRCDIR might be either an absolute or a relative path
# and it will only work reliably if interpreted from _this_ directory.
$(OBJFILES): objinclude/machine
CLEAN_FILES += objinclude objinclude_machine
objinclude/machine:
	$(OSKIT_QUIET_MAKE_INFORM) "Creating $@"
	$(OSKIT_QUIET_MAKE_HIDE) test -d objinclude || mkdir objinclude
	$(OSKIT_QUIET_MAKE_HIDE) ln -s ../objinclude_machine objinclude/machine
	$(OSKIT_QUIET_MAKE_HIDE) ln -s $(OSKIT_SRCDIR)/freebsd/libc/include/i386 objinclude_machine

mcount.po: $(OSKIT_SRCDIR)/freebsd/libc/gmon/mcount.c
	$(OSKIT_QUIET_MAKE_INFORM) "Compiling $@"
	$(OSKIT_QUIET_MAKE_HIDE) $(filter-out -pg, $(CC)) -c $(OSKIT_CFLAGS) $(CFLAGS) -DGPROF $(DEFINES)  \
		$< -o mcount.po

$(TARGET): $(OBJDIR)/lib/liboskit_posix.a $(OBJFILES)
	$(OSKIT_QUIET_MAKE_INFORM) "Creating library $@"
	$(OSKIT_QUIET_MAKE_HIDE) cp $(OBJDIR)/lib/liboskit_posix.a $@
	$(OSKIT_QUIET_MAKE_HIDE) $(AR) $(ARFLAGS) $@ $(OBJFILES)
	$(OSKIT_QUIET_MAKE_HIDE) $(RANLIB) $@

$(PTARGET): $(OBJDIR)/lib/liboskit_posix_p.a $(POBJFILES)
	$(OSKIT_QUIET_MAKE_INFORM) "Creating library $@"
	$(OSKIT_QUIET_MAKE_HIDE) cp $(OBJDIR)/lib/liboskit_posix_p.a $@
	$(OSKIT_QUIET_MAKE_HIDE) $(AR) $(ARFLAGS) $@ $(POBJFILES)
	$(OSKIT_QUIET_MAKE_HIDE) $(RANLIB) $@

#
# Install all the headers.
#
install: $(addprefix $(INSTALL_INCDIR)/oskit/freebsd/,$(EXTRAH)) \
	$(addprefix $(INSTALL_INCDIR)/oskit/freebsd/,$(HFILES)) \
	$(INSTALL_INCDIR)/oskit/freebsd/machine

FREEBSD_INSTALL := $(INSTALL_INCDIR)/oskit/freebsd

$(FREEBSD_INSTALL)/%.h:: $(OSKIT_SRCDIR)/freebsd/libc/include/%.h
	$(OSKIT_QUIET_MAKE_INFORM) "Installing $@"
	$(OSKIT_QUIET_MAKE_HIDE) -mkdir -p $(patsubst %/,%,$(dir $@))
	$(OSKIT_QUIET_MAKE_HIDE) $(INSTALL) -m 644 $< $@

$(FREEBSD_INSTALL)/%.h:: $(OSKIT_SRCDIR)/freebsd/3.x/src/include/%.h
	$(OSKIT_QUIET_MAKE_INFORM) "Installing $@"
	$(OSKIT_QUIET_MAKE_HIDE) -mkdir -p $(patsubst %/,%,$(dir $@))
	$(OSKIT_QUIET_MAKE_HIDE) $(INSTALL) -m 644 $< $@

$(FREEBSD_INSTALL)/sys/%.h:: $(OSKIT_SRCDIR)/freebsd/libc/include/sys/%.h
	$(OSKIT_QUIET_MAKE_INFORM) "Installing $@"
	$(OSKIT_QUIET_MAKE_HIDE) -mkdir -p $(patsubst %/,%,$(dir $@))
	$(OSKIT_QUIET_MAKE_HIDE) $(INSTALL) -m 644 $< $@

$(FREEBSD_INSTALL)/sys/%.h:: $(OSKIT_SRCDIR)/freebsd/3.x/src/sys/sys/%.h
	$(OSKIT_QUIET_MAKE_INFORM) "Installing $@"
	$(OSKIT_QUIET_MAKE_HIDE) -mkdir -p $(patsubst %/,%,$(dir $@))
	$(OSKIT_QUIET_MAKE_HIDE) $(INSTALL) -m 644 $< $@

$(FREEBSD_INSTALL)/i386/%.h: $(OSKIT_SRCDIR)/freebsd/libc/include/i386/%.h
	$(OSKIT_QUIET_MAKE_INFORM) "Installing $@"
	$(OSKIT_QUIET_MAKE_HIDE) -mkdir -p $(patsubst %/,%,$(dir $@))
	$(OSKIT_QUIET_MAKE_HIDE) $(INSTALL) -m 644 $< $@

$(FREEBSD_INSTALL)/net/%.h: $(OSKIT_SRCDIR)/freebsd/3.x/src/sys/net/%.h
	$(OSKIT_QUIET_MAKE_INFORM) "Installing $@"
	$(OSKIT_QUIET_MAKE_HIDE) -mkdir -p $(patsubst %/,%,$(dir $@))
	$(OSKIT_QUIET_MAKE_HIDE) $(INSTALL) -m 644 $< $@

$(FREEBSD_INSTALL)/netinet/%.h: $(OSKIT_SRCDIR)/freebsd/3.x/src/sys/netinet/%.h
	$(OSKIT_QUIET_MAKE_INFORM) "Installing $@"
	$(OSKIT_QUIET_MAKE_HIDE) -mkdir -p $(patsubst %/,%,$(dir $@))
	$(OSKIT_QUIET_MAKE_HIDE) $(INSTALL) -m 644 $< $@

# XXX i386
$(FREEBSD_INSTALL)/machine:
	$(OSKIT_QUIET_MAKE_INFORM) "Creating dir $@"
	$(OSKIT_QUIET_MAKE_HIDE) ln -s i386 $@
endif
