# $Id: makefile.src,v 1.6 1997/08/12 07:39:17 digulla Exp $
TOP=../..
CURDIR=rom/aros

ifeq ("$(SHARED_AROS)","yes")
SPECIAL_CFLAGS := $(SHARED_CFLAGS)
else
SPECIAL_CFLAGS :=
endif

include $(TOP)/config/make.cfg

# BEGIN_DESC{makefile}
# This is the makefile for aros.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 aros.library. Need this apart as an anchor for the link:
INIT_FILE := aros_init

FILES := aros_functable

# Guarantee that this will go at the end of the file:
END_FILE := aros_endtag

# ***** Not Native ***************
else

FILES := aros_init aros_functable aros_endtag

# ***** END ***************
endif

FUNCTIONS := \
	arosinquirea

# 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)/aros
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)/aros

# ***** Native ***************
ifeq ($(FLAVOUR),native)

# The options for the linker
LIBS := -L$(LIBDIR) -laros

# The filename of the linklib
LIB := $(LIBDIR)/libaros.a

# The names of the linklibs this depends on
LIBLIST := aros arosc

# The filenames of the linklibs this depends on
LIBDEPS := $(foreach f,$(LIBLIST),$(LIBDIR)/lib$(f).a)

# The object filenames
OBJS := $(foreach f,$(FILES) $(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)/aros.library

.PHONY : asm
asm : $(foreach f,$(FUNCTIONS) $(INIT_FILE) $(END_FILE) $(FILES),$(f).s) all

$(OSMODDIR)/aros.library : $(INITFUNC) $(ENDFUNC) $(OBJS) $(LIBDEPS)
	@echo "Building $(notdir $@) ..."
	@$(CC) $(ILDFLAGS) -Xlinker -M $(INITFUNC) $(LIBS) $(ENDFUNC) -o $@ 2>&1|tee aroslib.map
	@if test ! -s aroslib.map; then rm aroslib.map ; else true ; fi
	@strip $@

%.s : %.c
	@echo "Generating $@ ..."
	@$(CC) -S $(CFLAGS) $< -c -o $@

# Collect all (asm-)functions in a linklib for fast linking:
$(LIB) : $(OBJS)
	@echo "Adding functions to $(notdir $@) ..."
	@$(AR) $@ $?
	@$(RANLIB) $@

# ***** Not Native ***************
else

%prepare_shlib aros $(SHARED_AROS)

OBJS := $(foreach f,$(FILES) $(FUNCTIONS),$(OBJDIR)/$(f).o)

all : setup $(LIB)

$(LIB) : $(OBJS)
	@echo "Recreating $@..."
	@$(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)"

aros_functable.c : $(foreach f,$(FUNCTIONS),$(f).c) \
	    $(TOP)/scripts/makefunctable.awk
	%mkfunctable_q Aros

aros_endtag.c : $(TOP)/scripts/makeendtag.awk
	%mkendtag_q Aros

$(OBJDIR)/aros_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)
