# $Id: makefile.src,v 1.3 1997/07/31 09:43:24 digulla Exp $
TOP=../..
CURDIR=rom/layers

include $(TOP)/config/make.cfg

# BEGIN_DESC{makefile}
# This is the makefile for layers.library. Use it if you want to compile
# only this part of AROS.
# END_DESC{makefile}

# BEGIN_DESC{localmakevar}
# \item{FILES} This is a list of all files (without the .c) which
#	contain internal functions of the library (eg. libinit(),
#	libopen(), libexpunge() and the function table).
#
# \item{FUNCTIONS} This is a list of all files (without the .c)
#	with public functions of the library. The makefile will
#	strip all files from this list for which a special
#	version for this CPU does exist.
#
# END_DESC{localmakevar}

# ***** Native ***************
ifeq ($(FLAVOUR),native)

# Main function for layers.library. Need this apart as an anchor for the link:
INIT_FILE = layersstrap_init

FILES = basicfuncs

# Asm files that have no C counterpart
AFILES = basicasmfuncs

# Guarantee that this will go at the end of the file:
END_FILE = layers_endtag

# ***** Not Native ***************
else

FILES = layers_init layers_functable layers_endtag basicfuncs

# ***** END ***************
endif

FUNCTIONS = \
	disposelayerinfo		\
	dohookcliprects 		\
	fattenlayerinfo 		\
	initlayers			\
	installclipregion		\
	installlayerhook		\
	installlayerinfohook		\
	locklayer			\
	locklayerinfo			\
	locklayers			\
	newlayerinfo			\
	sortlayercr			\
	swapbitsrastportcliprect	\
	thinlayerinfo			\
	unlocklayer			\
	unlocklayerinfo 		\
	unlocklayers			\
	upfrontlayer			\
	whichlayer

# BEGIN_DESC{localmakevar}
# \item{ASM_PATH} The path to the assembler sources
#
# \item{ASM_FILES_TRY} Create a list with all files that might be
#	found as special CPU versions in $(ASM_PATH) with full
#	path.
#
# \item{ASM_FILES_EXIST} Create a list of all files from $(ASM_FILES_TRY)
#	which do really exist in $(ASM_PATH).
#
# END_DESC{localmakevar}

ifeq ($(FLAVOUR),native)
ASM_PATH := $(TOP)/config/$(KERNEL)/layers
else
ASM_PATH := $(TOP)/config/$(KERNEL)
endif
ARCH_PATH := $(TOP)/config/$(ARCH)

ASM_FILES_TRY := $(foreach f,$(FUNCTIONS),$(ASM_PATH)/$(f).[cs] $(ARCH_PATH)/$(f).[cs])
ASM_FILES_EXIST := $(wildcard $(ASM_FILES_TRY))
ASM_FILES_EXIST := $(basename $(notdir $(ASM_FILES_EXIST)))

# Black magic: First create a negative list of the files (ie. only the
# ones which do *not* have a special assembler version for this CPU
# and then strip the path and extensions from all files in this list.
# Now the list contains the basenames of all files which must be taken
# from the local directory.
FUNCTIONS := $(filter-out $(ASM_FILES_EXIST),$(FUNCTIONS))

OBJDIR=$(OSGENDIR)/layers

# ***** Native ***************
ifeq ($(FLAVOUR),native)

# The options for the linker
LIBS = -L$(LIBDIR) -llayers -larossupport -larosc

# The filename of the linklib
LIB=$(LIBDIR)/liblayers.a

# The names of the linklibs this depends on
LIBLIST = layers arossupport arosc

# The filenames of the linklibs this depends on
LIBDEPS = $(foreach f,$(LIBLIST),$(LIBDIR)/lib$(f).a)

# The object filenames
OBJS = $(foreach f,$(FILES) $(AFILES) $(FUNCTIONS) $(ASM_FILES_EXIST),$(OBJDIR)/$(f).o)

# The init and endskip object filenames
INITFUNC = $(OBJDIR)/$(INIT_FILE).o
ENDFUNC = $(OBJDIR)/$(END_FILE).o

all: setup $(END_FILE).c $(OSMODDIR)/layers.strap

%asm_rule

$(OSMODDIR)/layers.strap: $(INITFUNC) $(ENDFUNC) $(OBJS) $(LIBDEPS)
	@echo "Building $(notdir $@) ..."
	@$(CC) $(ILDFLAGS) -Xlinker -M $(INITFUNC) $(LIBS) $(ENDFUNC) -o $@ 2>&1|tee layers.map
	@if test ! -s layers.map; then rm layers.map ; else true ; fi
	@strip $@

%ctoasm_q

# Collect all (asm-)functions in a linklib for fast linking:
$(LIB): $(OBJS)
	@echo "Adding functions to $(notdir $@) ..."
	@$(AR) $@ $?
	@$(RANLIB) $@

# ***** Not Native ***************
else

LIB=$(LIBDIR)/liblayers.so

OBJS = $(foreach f,$(FILES) $(FUNCTIONS),$(OBJDIR)/$(f).o)

all: setup $(LIB)

$(LIB) : $(OBJS)
	@echo "Recreating $@..."
	@$(SHARED_AR) $@ $(OBJS)

# ***** END ***************
endif

setup :
	%mkdirs_q $(OSGENDIR) $(OBJDIR)
	@echo "CFLAGS=$(CFLAGS)"

clean::
	-$(RM) $(OBJDIR) *.err $(LIB) *.s

$(OBJDIR)/%.o: %.c
	%compile_q opt="$(SHARED_CFLAGS) $(CFLAGS)"

layers_functable.c: $(foreach f,$(FUNCTIONS),$(f).c) \
	    $(TOP)/scripts/makefunctable.awk
	%mkfunctable_q Layers

layers_endtag.c: $(TOP)/scripts/makeendtag.awk
	%mkendtag_q Layers

$(OBJDIR)/layers_init.o: libdefs.h

%libdefs_rule

$(OBJDIR)/%.d: %.c
	%mkdepend_q

%common
%include_deps $(foreach f,$(INIT_FILE) $(FILES) $(FUNCTIONS) $(END_FILE),$(OBJDIR)/$(f).d)
