# -*- Mode: makefile -*-
# Makefile for QEMU.

GENERATED_HEADERS = config-host.h

ifneq ($(wildcard config-host.mak),)
# Put the all: rule here so that config-host.mak can contain dependencies.
all: build-all
include config-host.mak
include $(SRC_PATH)/rules.mak
config-host.mak: $(SRC_PATH)/configure-small
	@echo $@ is out-of-date, running configure
	@sed -n "/.*Configured with/s/[^:]*: //p" $@ | sh
else
config-host.mak:
	@echo "Please call configure before running make!"
	@exit 1
endif

# Don't try to regenerate Makefile or configure
# We don't generate any of them
Makefile-small: ;
configure-small: ;

.PHONY: all clean cscope distclean tar build-all

$(call set-vpath, $(SRC_PATH):$(SRC_PATH)/hw)

LIBS+=-lz

SUBDIR_MAKEFLAGS=$(if $(V),,--no-print-directory)

config-all-devices.mak: arm-softmmu/config-devices.mak
	$(call quiet-command,cat arm-softmmu/config-devices.mak | grep =y | sort -u > $@,"  GEN   $@")

arm-softmmu/config-devices.mak: default-configs/arm-softmmu-small.mak
	$(call quiet-command,cat $< > $@.tmp, "  GEN   $@")
	@if test -f $@; then \
	  if cmp -s $@.old $@ || cmp -s $@ $@.tmp; then \
	    mv $@.tmp $@; \
	    cp -p $@ $@.old; \
	  else \
	    if test -f $@.old; then \
	      echo "WARNING: $@ (user modified) out of date.";\
	    else \
	      echo "WARNING: $@ out of date.";\
	    fi; \
	    echo "Run \"make defconfig\" to regenerate."; \
	    rm $@.tmp; \
	  fi; \
	 else \
	  mv $@.tmp $@; \
	  cp -p $@ $@.old; \
	 fi

defconfig:
	rm -f config-all-devices.mak arm-softmmu/config-devices.mak

-include config-all-devices.mak

build-all: subdir-arm-softmmu

config-host.h: config-host.h-timestamp
config-host.h-timestamp: config-host.mak

SUBDIR_RULES=subdir-arm-softmmu

subdir-%: $(GENERATED_HEADERS)
	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C $* V="$(V)" TARGET_DIR="$*/" all,)

ifneq ($(wildcard config-host.mak),)
include $(SRC_PATH)/Makefile-small.objs
endif

$(common-obj-y): $(GENERATED_HEADERS)
subdir-arm-softmmu: $(common-obj-y)

######################################################################

$(block-obj-y): $(GENERATED_HEADERS)

clean:
	rm -f *.o *.d *.a TAGS cscope.* *~ */*~
	rm -f block/*.o block/*.d
	for d in arm-softmmu libhw32 libhw64; do \
	if test -d $$d; then $(MAKE) -C $$d $@ || exit 1; fi; \
        done

distclean: clean
	rm -f config-host.mak config-host.h* config-host.ld
	rm -f config-all-devices.mak
	for d in arm-softmmu libhw32 libhw64; do \
	rm -rf $$d || exit 1 ; \
        done

.PHONY: TAGS
TAGS:
	find "$(SRC_PATH)" -name '*.[hc]' -print0 | xargs -0 etags

cscope:
	rm -f ./cscope.*
	find . -name "*.[ch]" -print | sed 's,^\./,,' > ./cscope.files
	cscope -b

VERSION ?= $(shell cat VERSION)
FILE = qemu-$(VERSION)

# tar release (use 'make -k tar' on a checkouted tree)
tar:
	rm -rf /tmp/$(FILE)
	cp -r . /tmp/$(FILE)
	cd /tmp && tar zcvf ~/$(FILE).tar.gz $(FILE) --exclude CVS --exclude .git --exclude .svn
	rm -rf /tmp/$(FILE)

# Include automatically generated dependency files
-include $(wildcard *.d block/*.d)
