#
# Copyright 2014, NICTA
#
# This software may be distributed and modified according to the terms of
# the BSD 2-Clause license. Note that NO WARRANTY is provided.
# See "LICENSE_BSD2.txt" for details.
#
# @TAG(NICTA_BSD)
#

# Tools
BF_GEN_PATH := ${SOURCE_DIR}/tools/bitfield_gen.py
SYCALL_ID_GEN_PATH := ${SOURCE_DIR}/tools/syscall_header_gen.py
INVOCATION_ID_GEN_PATH := ${SOURCE_DIR}/tools/invocation_header_gen.py
XMLLINT_PATH := ${SOURCE_DIR}/tools/xmllint.sh
CHANGED_PATH := ${SOURCE_DIR}/tools/changed.sh

# Targets
PRIORITY_TARGETS := include/interfaces/sel4_client.h include/sel4/types_gen.h include/sel4/syscall.h \
					include/sel4/invocation.h arch_include/${ARCH}/sel4/arch/invocation.h
TARGETS          := libsel4.a

# Source files required to build the target
CFILES := \
	$(patsubst $(SOURCE_DIR)/%,%,$(wildcard $(SOURCE_DIR)/src/*.c))

# Header files/directories this library provides
# Note: sel4_client.h may not have been built at the time this is evaluated.
HDRFILES := \
	$(wildcard $(SOURCE_DIR)/include/*) \
	$(wildcard $(SOURCE_DIR)/arch_include/$(ARCH)/*) \
	$(BUILD_DIR)/include/sel4 \
	$(BUILD_DIR)/include/interfaces  #TODO proper prefix instruction

.DEFAULT_GOAL := default

install-headers: ${PRIORITY_TARGETS}

include $(SEL4_COMMON)/common.mk

# For all the autogenerated headers we use the 'changed' script which will
# only update the timestamp of the target if the file actually changed
# This prevents anything that depends upon the target from being rebuilt
# if the file did not really change. This is neccessary because these
# targets get called forcively as a result of install-headers being a
# PHONY target. The downside to this 'trick' is if one of our prerequisites
# has its timestamp updated then make will get confused and always think
# this build step needs to be redone. But since it is phony and gets redone
# anyway, who cares!
include/sel4/invocation.h: ${SOURCE_DIR}/include/interfaces/sel4.xml
	@echo " [GEN] $@"
	@mkdir -p $(dir $@)
	@${CHANGED_PATH} $@ python ${INVOCATION_ID_GEN_PATH} \
		--xml $< --libsel4 --dest $@

arch_include/${ARCH}/sel4/arch/invocation.h: ${SOURCE_DIR}/arch_include/${ARCH}/interfaces/sel4arch.xml 
	@echo " [GEN] $@"
	@mkdir -p $(dir $@)
	@${CHANGED_PATH} ${SOURCE_DIR}/$@ python ${INVOCATION_ID_GEN_PATH} \
		--xml $< --libsel4 --arch --dest ${SOURCE_DIR}/$@

# Header generator
include/sel4/syscall.h: ${SOURCE_DIR}/include/api/syscall.xsd \
						${SOURCE_DIR}/include/api/syscall.xml 
	@echo " [GEN] $@"
	@mkdir -p $(dir $@)
	@${XMLLINT_PATH} --noout --schema $^
	@${CHANGED_PATH} $@ python ${SYCALL_ID_GEN_PATH} \
	--xml ${SOURCE_DIR}/include/api/syscall.xml \
	--libsel4_header $@

# Bitfield generator
include/sel4/types_gen.h: \
		${SOURCE_DIR}/include/sel4/types.bf \
		${BF_GEN_PATH}
	@echo " [GEN] $@"
	@mkdir -p $(dir $@)
	@${CHANGED_PATH} $@ python ${BF_GEN_PATH} --environment libsel4 "${SOURCE_DIR}/include/sel4/types.bf" include/sel4/types_gen.h

# Stub generator
include/interfaces/sel4_client.h: \
		${SOURCE_DIR}/arch_include/${ARCH}/interfaces/sel4arch.xml \
		${SOURCE_DIR}/include/interfaces/sel4.xml
	@echo " [GEN] $@"
	@mkdir -p $(dir $@)
	@${CHANGED_PATH} $@ \
        python ${SOURCE_DIR}/tools/syscall_stub_gen.py \
        $(if ${CONFIG_LIB_SEL4_STUBS_USE_IPC_BUFFER_ONLY},--buffer,) \
        -a $(ARCH) -o $@ $^
