# This is the correct place to edit the build version.
# All other places this is stored (eg. compile.h) should be autogenerated.
export XEN_VERSION       = 4
export XEN_SUBVERSION    = 0
export XEN_EXTRAVERSION ?= .1-rc3$(XEN_VENDORVERSION)
export XEN_FULLVERSION   = $(XEN_VERSION).$(XEN_SUBVERSION)$(XEN_EXTRAVERSION)
-include xen-version

export XEN_WHOAMI	?= $(USER)
export XEN_DOMAIN	?= $(shell ([ -x /bin/dnsdomainname ] && /bin/dnsdomainname) || ([ -x /bin/domainname ] && /bin/domainname || echo [unknown]))

export BASEDIR := $(CURDIR)

.PHONY: default
default: build

.PHONY: dist
dist: install

.PHONY: build install clean distclean cscope TAGS tags MAP
build install debug clean distclean cscope TAGS tags MAP::
	$(MAKE) -f Rules.mk _$@

.PHONY: _build
_build: $(TARGET).gz

.PHONY: _install
_install: $(TARGET).gz
	[ -d $(DESTDIR)/boot ] || $(INSTALL_DIR) $(DESTDIR)/boot
	$(INSTALL_DATA) $(TARGET).gz $(DESTDIR)/boot/$(notdir $(TARGET))-$(XEN_FULLVERSION).gz
	ln -f -s $(notdir $(TARGET))-$(XEN_FULLVERSION).gz $(DESTDIR)/boot/$(notdir $(TARGET))-$(XEN_VERSION).$(XEN_SUBVERSION).gz
	ln -f -s $(notdir $(TARGET))-$(XEN_FULLVERSION).gz $(DESTDIR)/boot/$(notdir $(TARGET))-$(XEN_VERSION).gz
	ln -f -s $(notdir $(TARGET))-$(XEN_FULLVERSION).gz $(DESTDIR)/boot/$(notdir $(TARGET)).gz
	$(INSTALL_DATA) $(TARGET)-syms $(DESTDIR)/boot/$(notdir $(TARGET))-syms-$(XEN_FULLVERSION)

.PHONY: _debug
_debug:
	objdump -D -S $(TARGET)-syms > $(TARGET).s

.PHONY: _clean
_clean: delete-unfresh-files
	$(MAKE) -C tools clean
	$(MAKE) -f $(BASEDIR)/Rules.mk -C include clean
	$(MAKE) -f $(BASEDIR)/Rules.mk -C common clean
	$(MAKE) -f $(BASEDIR)/Rules.mk -C drivers clean
	$(MAKE) -f $(BASEDIR)/Rules.mk -C xsm clean
	$(MAKE) -f $(BASEDIR)/Rules.mk -C crypto clean
	$(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) clean
	rm -f include/asm *.o $(TARGET)* *~ core
	rm -f include/asm-*/asm-offsets.h
	[ -d tools/figlet ] && rm -f .banner*

.PHONY: _distclean
_distclean: clean
	rm -f tags TAGS cscope.files cscope.in.out cscope.out cscope.po.out

$(TARGET).gz: $(TARGET)
	gzip -f -9 < $< > $@.new
	mv $@.new $@

$(TARGET): delete-unfresh-files
	$(MAKE) -C tools
	$(MAKE) -f $(BASEDIR)/Rules.mk include/xen/compile.h
	[ -e include/asm ] || ln -sf asm-$(TARGET_ARCH) include/asm
	$(MAKE) -f $(BASEDIR)/Rules.mk -C include
	$(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) asm-offsets.s
	$(MAKE) -f $(BASEDIR)/Rules.mk include/asm-$(TARGET_ARCH)/asm-offsets.h
	$(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) $(TARGET)

# drivers/char/console.o contains static banner/compile info. Blow it away.
# Don't refresh these files during e.g., 'sudo make install'
.PHONY: delete-unfresh-files
delete-unfresh-files:
	@if [ ! -r include/xen/compile.h -o -O include/xen/compile.h ]; then \
		rm -f include/xen/compile.h; \
	fi

.banner: Makefile
	$(MAKE) -C tools
	@tools/figlet/figlet -d tools/figlet Xen $(XEN_FULLVERSION) 2>$@2 >$@1
	@cat $@1 $@2 >$@
	@rm -f $@1 $@2

# compile.h contains dynamic build info. Rebuilt on every 'make' invocation.
include/xen/compile.h: include/xen/compile.h.in .banner
	@sed -e 's/@@date@@/$(shell LC_ALL=C date)/g' \
	    -e 's/@@time@@/$(shell LC_ALL=C date +%T)/g' \
	    -e 's/@@whoami@@/$(XEN_WHOAMI)/g' \
	    -e 's/@@domain@@/$(XEN_DOMAIN)/g' \
	    -e 's/@@hostname@@/$(shell hostname)/g' \
	    -e 's!@@compiler@@!$(shell $(CC) $(CFLAGS) -v 2>&1 | tail -1)!g' \
	    -e 's/@@version@@/$(XEN_VERSION)/g' \
	    -e 's/@@subversion@@/$(XEN_SUBVERSION)/g' \
	    -e 's/@@extraversion@@/$(XEN_EXTRAVERSION)/g' \
	    -e 's!@@changeset@@!$(shell ((hg parents --template "{date|date} {rev}:{node|short}" >/dev/null && hg parents --template "{date|date} {rev}:{node|short}") || echo "unavailable") 2>/dev/null)!g' \
	    < include/xen/compile.h.in > $@.new
	@grep \" .banner >> $@.new
	@grep -v \" .banner
	@mv -f $@.new $@

include/asm-$(TARGET_ARCH)/asm-offsets.h: arch/$(TARGET_ARCH)/asm-offsets.s
	@(set -e; \
	  echo "/*"; \
	  echo " * DO NOT MODIFY."; \
	  echo " *"; \
	  echo " * This file was auto-generated from $<"; \
	  echo " *"; \
	  echo " */"; \
	  echo ""; \
	  echo "#ifndef __ASM_OFFSETS_H__"; \
	  echo "#define __ASM_OFFSETS_H__"; \
	  echo ""; \
	  sed -ne "/^->/{s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; s:->::; p;}"; \
	  echo ""; \
	  echo "#endif") <$< >$@

SUBDIRS = xsm arch/$(TARGET_ARCH) common drivers
define all_sources
    ( find include/asm-$(TARGET_ARCH) -name '*.h' -print; \
      find include -name 'asm-*' -prune -o -name '*.h' -print; \
      find $(SUBDIRS) -name '*.[chS]' -print )
endef

define set_exuberant_flags
    exuberant_flags=`$1 --version 2>/dev/null | (grep -iq exuberant && \
	echo "-I __initdata,__exitdata,__acquires,__releases \
	    -I EXPORT_SYMBOL,EXPORT_SYMBOL_GPL \
	    --extra=+f --c-kinds=+px") || true` 
endef

.PHONY: _TAGS
_TAGS: 
	set -e; rm -f TAGS; \
	$(call set_exuberant_flags,etags); \
	$(all_sources) | xargs etags $$exuberant_flags -a

.PHONY: _tags
_tags: 
	set -e; rm -f tags; \
	$(call set_exuberant_flags,ctags); \
	$(all_sources) | xargs ctags $$exuberant_flags -a

.PHONY: _cscope
_cscope:
	$(all_sources) > cscope.files
	cscope -k -b -q

.PHONY: _MAP
_MAP:
	$(NM) -n $(TARGET)-syms | grep -v '\(compiled\)\|\(\.o$$\)\|\( [aUw] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)' > System.map

.PHONY: FORCE
FORCE:

%.o %.i: %.c FORCE
	$(MAKE) -f $(BASEDIR)/Rules.mk -C $(*D) $(@F)

%.o %.s: %.S FORCE
	$(MAKE) -f $(BASEDIR)/Rules.mk -C $(*D) $(@F)

%/: FORCE
	$(MAKE) -f $(BASEDIR)/Rules.mk -C $* built_in.o
