################################################################
####
#### GNUmakefile
####
####	$RCSfile: GNUmakefile,v $
####	$Author: buchio $
####	$Date: 2004/10/22 02:33:58 $
####	$Revision: 1.1 $
####
################################################################

#### ^[Qbg𖾎IɎw肷
#### w肵Ȃꍇ̓vWFNg[g̃fBNgɂȂ

TARGET = 

#### A[LeN`vtBbNX
#CPU_ARCH  = arm
#SYS_TYPE  = elf
#ARCH_PREFIX = $(CPU_ARCH)-$(SYS_TYPE)-

#### RpC̎w
AS     = $(ARCH_PREFIX)gcc
CC     = $(ARCH_PREFIX)gcc
CXX    = $(ARCH_PREFIX)g++
LK     = $(CXX)
AR     = $(ARCH_PREFIX)ar
RANLIB = $(ARCH_PREFIX)ranlib

#### ^[QbgɈˑRpCIvV̎w
EXTRAASFLAGS  = -DDEBUG_ON_PC
EXTRACFLAGS   = $(EXTRAASFLAGS)
EXTRACXXFLAGS = $(EXTRACFLAGS)
EXTRALFLAGS   = 

#### OCu̎w ( : LIBS = -lm -lX11 )
LIBS = 

#### ^[QbgɈˑȂIvV̎w
ASFLAGS  = -Wall -c -I $(BUILD_ROOT) $(EXTRAASFLAGS)
CFLAGS   = -Wstrict-prototypes -Winline -Wundef -Wall -O2 -c -I $(BUILD_ROOT) $(EXTRACFLAGS)
CXXFLAGS = -Wno-deprecated -Wstrict-prototypes -Winline -Wundef -Wall -O2 -c -I $(BUILD_ROOT) $(EXTRACXXFLAGS)
LFLAGS = $(EXTRALFLAGS)


############### ȉ͕ҏWȂĂ悢

#### $(ROOT_MARKER)̂fBNgTĂvWFNg[gƂ
#### ݂Ȃꍇ̓JgfBNgvWFNg[gƂ݂Ȃ
#### ړI͋ʂ̃CN[hpX邽߁B
ROOT_MARKER= 00_BUILD_ROOT
BUILD_ROOT = $(shell (CURRENT=$$(pwd); while [ ! -e $(ROOT_MARKER) ] && [ $$(pwd) != "/" ]; \
		do cd ..; done; if [ $$(pwd) = "/" ]; then echo $$CURRENT ; else echo $$(pwd) ; fi ) )

ifeq ($(strip $(TARGET)), )
 TARGET     = $(notdir $(shell pwd))
endif

.SUFFIXES: .tex .dvi .ps .obj .eps .idx .ind

DIREXCEPTION= CVS HTML test include doc

################################################################
################ Targets                        ################
################################################################

INC_FILES = depend.mak

################ Default targets
OBJS = $(foreach src, $(wildcard *.s) $(wildcard *.c) $(wildcard *.cpp) $(wildcard *.cxx), $(basename $(src)).o )
FIND_EXCEPTION=\( -not \( $(foreach DIR, $(DIREXCEPTION), -not -name $(DIR) ) \) -prune -o -print \)
SUBDIR= $(shell find * -maxdepth 0 -type d $(FIND_EXCEPTION))
ALLSUBDIR= $(shell find . -type d -not -name . $(FIND_EXCEPTION))
SUB_OBJS=$(foreach DIR, $(ALLSUBDIR), $(foreach src, $(wildcard $(DIR)/*.s) $(wildcard $(DIR)/*.c) $(wildcard $(DIR)/*.cpp) $(wildcard $(DIR)/*.cxx), $(basename $(src)).o ))
all: premake subcompiles $(INC_FILES) $(TARGET) postmake

premake:
postmake:

-include premake.mak
-include postmake.mak


test: $(INC_FILES) $(TSUBS)
	$(MAKE) CFLAGS="$(CFLAGS) -D__RUN_SUBMODULE_TEST__" TARGET="test" SUBS+=$(TSUBS)

#### ̃fBNg̃\[Xt@CSăRpC
compile: subcompiles $(INC_FILES) $(OBJS)

#### ̃fBNg̃IuWFNgCuɂ܂Ƃ߂
link: $(basename $(TARGET)).a
$(basename $(TARGET)).a: $(INC_FILES) $(OBJS) $(SUB_OBJS)
	$(AR) r $(basename $(TARGET)).a $(OBJS) $(SUB_OBJS)


#### ^[QbgN
$(TARGET): $(INC_FILES) $(OBJS) $(SUB_OBJS)
	$(LK) $(LFLAGS) -o $(TARGET) $(OBJS) $(SUB_OBJS) $(LIBS) 

################ Default rules

%.o: %.s
	$(CC) -c -o $*.o $(ASFLAGS) $<

%.o: %.c
	$(CC) -c -o $*.o $(CFLAGS) $<

%.o: %.cxx
	$(CXX) -c -o $*.o $(CXXFLAGS) $<

%.o: %.C
	$(CXX) -c -o $*.o $(CXXFLAGS) $<

%.o: %.cc
	$(CXX) -c -o $*.o $(CXXFLAGS) $<

%.subcompile:
	$(MAKE) -C $(@D) compile
SUB_COMPILE = $(foreach SUB, $(SUBDIR), $(SUB)/m.subcompile)
subcompiles: $(SUB_COMPILE)

################################################################
################ ˑ֌W̓ǂݍ
################################################################

-include depend.mak

################################################################
################ Automatic dependency file generation
################################################################

depend: depend.mak

depend.mak:
	@for i in $(wildcard *.c) $(wildcard *.cpp) $(wildcard *.cxx) ;do $(CC) $(CFLAGS) -M $$i; done > depend.mak

################################################################
################  TuW[ɎgRs[Makefile𐮂
################################################################

makefiles:
	for i in $(SUBDIR) ;do cp GNUmakefile $$i && $(MAKE) -C $$i makefiles ;done

################################################################
################ JgfBNgvWFNg[gɐݒ
################################################################
rootmarker:
	find ./ -type f -name $(ROOT_MARKER) -exec rm -vf {} \;
	touch $(ROOT_MARKER)

################################################################
################ clean
################################################################
%.cleansub:
	$(MAKE) -C $(@D) local-clean
CLEAN_SUB = $(foreach SUB, $(SUBDIR), $(SUB)/m.cleansub)

clean: $(CLEAN_SUB) local-clean
	@rm -f $(TARGET)
	@rm -f $(TARGET).a
	@rm -f $(TARGET).exe
	@rm -f $(TARGET).EXE

local-clean:
	@rm -f $(OBJS)
	@rm -f depend.mak
	@rm -f *.o
	@rm -f *.log
	@rm -f *~
	@rm -f \#*\#
	@rm -f *.map
	@rm -f *.BAK
	@rm -f *.bak
	@rm -f TAGS
	@rm -f GTAGS
	@rm -f GSYMS
	@rm -f GRTAGS
	@rm -rf HTML
	@rm -f TAGS

################################################################
################  test Makefile it self
################################################################

show:
	@echo "BUILD_ROOT    = " [$(BUILD_ROOT)]
	@echo "TARGET        = " [$(TARGET)]
	@echo "OBJS          = " [$(OBJS)]
	@echo "SUB_OBJS      = " [$(SUB_OBJS)]
	@echo "SUBDIR        = " [$(SUBDIR)]
	@echo "ALLSUBDIR     = " [$(ALLSUBDIR)]
	@echo "SUB_COMPILE   = " [$(SUB_COMPILE)]
	@echo "CLEAN_SUB     = " [$(CLEAN_SUB)]
	@echo "DIREXCEPTION  = " [$(DIREXCEPTION)]
	@echo "FIND_EXCEPTION= " [$(FIND_EXCEPTION)]
	@echo "CLEAN_SUB     = " [$(CLEAN_SUB)]

################################################################
################ Print help messages
################################################################

help:
	@echo "TARGETS:"
	@echo "	all		ftHg^[Qbg"
	@echo "	test		eXgprh"
	@echo "	compile		TufBNgŃRpCs"
	@echo "	depend		ˑ֌Wt@C쐬"
	@echo "	makefiles	TuW[MakefileRs["
	@echo "	rootmarker	JgfBNgBUILD_ROOTɐݒ肷"
	@echo "	show		Makefile̊eϐ\"
	@echo "	clean		N["
	@echo "	local-clean	JgfBNĝ݃N["
	@echo "	help		̃bZ[W\"


################ End of file ################
