#
# $RCSfile: Makefile.lib,v $
#
# x-kernel v3.3
#
# Copyright (c) 1996,1993,1991,1990  Arizona Board of Regents
#
# $Log: Makefile.lib,v $
# Revision 1.5  1996/07/02 17:02:03  slm
# Fix for making COMS directories on linux.
#
# Revision 1.4  1996/06/03  18:34:19  slm
# Updated version & copyright.
#
# Revision 1.3  1996/05/23  18:02:41  slm
# Cosmetic changes.
#
# Revision 1.2  1995/10/13  22:22:24  slm
# Create .o files in OBJS directories, Makedep files in
# DEPS directories, and ComposeFiles in COMS directories.
#
# Revision 1.1  1995/07/28  21:38:46  slm
# Initial revision
#
# Revision 1.22.1.3  1995/03/20  22:19:53  davidm
# Fixed LOOP target to work when $(DIRS) is the empty string.
#
# Revision 1.22.1.2  1994/11/22  20:47:17  hkaram
# Changed pxk -> pd
# Added XK_SUB_DIR to awk command
#
# Revision 1.22.1.1  1994/10/20  19:13:14  hkaram
# New branch
#
# Revision 1.22  1994/10/10  18:14:54  menze
#   [ 1994/05/31          menze ]
#   Added support for general source-filename extensions
#
#   [ 1994/05/18          menze ]
#   OBJECTFILE name is now keyed to $(HOW), host and PID to avoid
#   conflicts with simultaneous library builds for different
#   configurations.
#
#   [ 1994/04/15          menze ]
#   Added check for empty DIRS variable
#
# Revision 1.21  1994/01/10  18:09:45  menze
#   [ 1994/01/04          menze ]
#   Added default OBJECTFILE declaration
#
# -----------------------------------------------------------------
# Libary Makefile -- this is the equivalent of Makefile.common but modified
# slightly for libraries

MAKEFILE = Makefile

# The only difference between FILES and EXTRA_FILES is that EXTRA_FILES
# don't show up on the list of composeFiles, so aren't linked in by default.
#
# This mess of text function manipulations is to allow specification of files
# both with and without extensions (those without extensions default to a .c
# extension).  If we can get all FILES specified with extensions, this can be
# cleaned up quite a bit.

SRC := $(FILES) $(EXTRA_FILES)
EXT_PAT := $(addprefix %, $(suffix $(SRC)))
FILES_WO_EXT := $(filter-out $(EXT_PAT), $(SRC))
FILES_W_EXT := $(filter $(EXT_PAT), $(SRC))
SRC := $(FILES_W_EXT) $(addsuffix .c, $(FILES_WO_EXT))
SRC +:= $(addsuffix .c, $(SIMPLE_PGMS))

ifeq ($(XK_NO_LIBRARIES),)
LIBPREFIX = $(LIB)(
LIBSUFFIX = .o)
else
LIBPREFIX = OBJS/$(HOW)/
LIBSUFFIX = .o
endif

OBJ = $(addprefix $(LIBPREFIX), $(addsuffix $(LIBSUFFIX), $(basename $(FILES))))
LIBS :=

ifeq ($(OBJECTFILE),)
OBJECTFILE := newObjectList.$(shell hostname).$(HOW).$(shell echo $$$$)
endif

.PHONY: pd depend clean

default: 
	@rm -f $(OBJECTFILE)
	$(MAKE) lib

lib: $(OBJ)
ifeq ($(DIRS),)
	@true
else
	@$(MAKE) LOOP OBJECTFILE=$(OBJECTFILE) TARGET=
endif
	@if [ -s $(OBJECTFILE) ] ; then                                     \
	    echo rebuilding library ;                                       \
	    umask 2 ;                                                       \
	    $(AR) ruv $(LIB) `/bin/cat $(OBJECTFILE)` || echo '***Errors' ; \
	    rm -f $(OBJECTFILE) ;                                           \
	    echo randomizing library || echo '***Errors' ;                  \
	    $(RANLIB) $(LIB) ;                                              \
	else                                                                \
	    $(AR) rc $(LIB) ;                                               \
	fi

#SRCLIST HDRLIST:
#	rm -f $(OBJECTFILE) ; 
#	@$(MAKE) LOOP TARGET=$@

clean: localclean
	rm -f $(LIB)
	$(MAKE) LOOP TARGET=$@ OBJECTFILE=$(OBJECTFILE)
	rm -f $(OBJECTFILE)

objFiles objFilesAndLibs SRCLIST HDRLIST depend dotodirs: %: local%
ifeq ($(DIRS),)
	@true
else
	@$(MAKE) LOOP TARGET=$@ OBJECTFILE=$(OBJECTFILE)
	@rm -f $(OBJECTFILE)
endif

# Forcing rule to regenerate the Compose files (which group files into
# protocol-sized modules).

.PHONY: libCompose compose

libCompose compose: localobjFilesAndLibs
	@if [ ! -d COMS ] ; then   \
	    umask 2 ; mkdir COMS ; \
	fi
ifeq ($(DIRS),)
	@true
else
	@echo generating ComposeFiles
	@$(MAKE) LOOP TARGET=objFilesAndLibs OBJECTFILE=$(OBJECTFILE)
	@awk -f $(XRT)/bin/genComposeFiles.awk XK_SUB_DIR=$(XK_SUB_DIR) \
	    TOP_LEV_DIR=$(TOP_LEV_DIR) $(OBJECTFILE) > COMS/ComposeFiles.$(HOW)
	@rm -f $(OBJECTFILE)	
endif

# Non-forcing rule for Compose files

COMS/ComposeFiles.$(HOW):
	@if [ ! -d COMS ] ; then   \
	    umask 2 ; mkdir COMS ; \
	fi
	@$(MAKE) libCompose

include $(MAKE_INCLUDE)/Makefile.local

# Generic loop target called by other targets.
# The assignment to shell-variable "dirs" is necessary because /bin/sh
# otherwise complains if DIRS is empty.  It works just fine if *shell*-variable
# $dirs evaluates to the empty string though...

LOOP:
	@dirs="$(DIRS)" ;                                   \
	for dir in $$dirs ; do                              \
	    echo $$dir ;                                    \
	    cd $$dir ;                                      \
	    $(MAKE) $(TARGET) XRT:=../$(XRT) LIB:=../$(LIB) \
	        OBJECTFILE:=../$(OBJECTFILE).$$$$ ;         \
	    cd .. ;                                         \
	    if [ -s $(OBJECTFILE).$$$$ ] ; then             \
	        cat -s $(OBJECTFILE).$$$$ |                 \
		    fixRelPaths $$dir >> $(OBJECTFILE) ;    \
	        rm -f $(OBJECTFILE).$$$$ ;                  \
	    fi ;                                            \
	done
	@true

include DEPS/Makedep.$(HOW)
