#
# $RCSfile: Makefile.common,v $
#
# x-kernel v3.3
#
# Copyright (c) 1996,1993,1991,1990  Arizona Board of Regents
#
# $Log: Makefile.common,v $
# Revision 1.4  1996/06/03 18:36:31  slm
# Updated version & copyright.
#
# Revision 1.3  1996/01/26  22:40:43  slm
# OBJECTFILE name is now keyed to $(HOW), host and PID to avoid
# conflicts with simultaneous library builds for different configurations.
#
# 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.12.1.1.1.3  1995/03/20  22:19:53  davidm
# Fixed LOOP target to work when $(DIRS) is the empty string.
#
# Revision 1.12.1.1.1.2  1994/11/22  20:46:51  hkaram
# Changed pxk -> pd
#
# Revision 1.12.1.1.1.1  1994/10/20  19:12:33  hkaram
# New branch
#
# Revision 1.12.1.1  1994/10/14  22:03:03  menze
# Added install and localinstall targets for utilities
#
# Revision 1.12  1994/10/10  18:14:03  menze
#   [ 1994/05/31          menze ]
#   Added support for general source-filename extensions
#
# Revision 1.11  1994/05/23  20:22:40  ho
# Added kludge to remove core files.
#
# Revision 1.10  1994/01/27  16:38:21  menze
#   [ 1994/01/06          menze ]
#   Added EXTRA_FILES make variable to allow systematic description of
#   files that should be compiled but not placed in the default object
#   list.
#
#   [ 1994/01/04          menze ]
#   Added support for independently compiled programs
#
#   [ 1994/01/04          menze ]
#   Added default OBJECTFILE declaration
#
# -----------------------------------------------------------------
# This Makefile can just be included by most x-kernel Makefiles to get
# the compilation rules.  The makefiles themselves just have to define:
#	DIRS -- subdirectories in which to recursively run make
#	FILES -- Source files to be compiled

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) $(EXTRA_FILES))))
SIMPLE_PGM_OBJ := $(addprefix OBJS/$(HOW)/, $(SIMPLE_PGMS))
PGM_OBJ := $(addprefix OBJS/$(HOW)/, $(PGM))

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

.PHONY: pd depend clean

def: $(PGM_OBJ) $(OBJ) $(SIMPLE_PGM_OBJ)
ifeq ($(DIRS),)
	@true
else
	@$(MAKE) LOOP TARGET=
endif

objFiles objFilesAndLibs SRCLIST HDRLIST depend clean dotodirs install: %: local%
ifeq ($(DIRS),)
	@true
else
	@$(MAKE) LOOP TARGET=$@
endif

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
# $directories 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).$$$$ ;         \
	    if [ -s core ] ; then                           \
	        rm -f core ;                                \
	    fi ;                                            \
	    cd .. ;                                         \
	    if [ -s $(OBJECTFILE).$$$$ ] ; then             \
	        cat -s $(OBJECTFILE).$$$$ |                 \
		    fixRelPaths $$dir >> $(OBJECTFILE) ;    \
	        rm -f $(OBJECTFILE).$$$$ ;                  \
	    fi ;                                            \
	done
	@true

include DEPS/Makedep.$(HOW)
