#
# 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
SYSCALL_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

ifeq (${PYTHON},)
PYTHON = python
endif

ifeq (${KERNEL_32}, y)
TYPE_SUFFIX:=32
else
ifeq (${KERNEL_64}, y)
TYPE_SUFFIX:=64
else
$(error Cannot determine TYPE_SUFFIX)
endif
endif

# 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 \
					include/sel4/shared_types_gen.h sel4_arch_include/${SEL4_ARCH}/sel4/sel4_arch/invocation.h \
					sel4_arch_include/${SEL4_ARCH}/sel4/sel4_arch/types_gen.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)/*) \
	$(wildcard $(SOURCE_DIR)/sel4_arch_include/$(SEL4_ARCH)/*) \
	$(wildcard $(SOURCE_DIR)/sel4_plat_include/$(PLAT)/*) \
	$(BUILD_DIR)/include/sel4 \
	$(BUILD_DIR)/include/interfaces \
    $(BUILD_DIR)/sel4_arch_include/$(SEL4_ARCH)/*
#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 $@

sel4_arch_include/${SEL4_ARCH}/sel4/sel4_arch/invocation.h: ${SOURCE_DIR}/sel4_arch_include/${SEL4_ARCH}/interfaces/sel4arch.xml
	@echo " [GEN] $@"
	@mkdir -p $(dir $@)
	@${CHANGED_PATH} ${SOURCE_DIR}/$@ ${PYTHON} ${INVOCATION_ID_GEN_PATH} \
		--xml $< --libsel4 --sel4_arch --dest ${SOURCE_DIR}/$@

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} ${SYSCALL_ID_GEN_PATH} \
	--xml ${SOURCE_DIR}/include/api/syscall.xml \
	--libsel4_header $@

include/sel4/%.pbf: ${SOURCE_DIR}/include/sel4/%.bf $(srctree)/include/generated/autoconf.h
	@echo " [PBF_GEN] $@"
	@mkdir -p $(dir $@)
	$(Q)${CPP} ${CPPFLAGS} -P $< > $@

include/sel4/sel4_arch/%.pbf: ${SOURCE_DIR}/sel4_arch_include/${SEL4_ARCH}/sel4/sel4_arch/types.bf $(srctree)/include/generated/autoconf.h
	@echo " [PBF_GEN] $@"
	@mkdir -p $(dir $@)
	$(Q)${CPP} ${CPPFLAGS} -P $< > $@

# Bitfield generator
include/sel4/types_gen.h: \
		include/sel4/types_${TYPE_SUFFIX}.pbf \
		${BF_GEN_PATH}
	@echo " [GEN] $@"
	@mkdir -p $(dir $@)
	@${CHANGED_PATH} $@ ${PYTHON} ${BF_GEN_PATH} --environment libsel4 $< include/sel4/types_gen.h

include/sel4/shared_types_gen.h: \
		include/sel4/shared_types_${TYPE_SUFFIX}.pbf \
		${BF_GEN_PATH}
	@echo " [GEN] $@"
	@mkdir -p $(dir $@)
	@${CHANGED_PATH} $@ ${PYTHON} ${BF_GEN_PATH} --environment libsel4 $< include/sel4/shared_types_gen.h

sel4_arch_include/${SEL4_ARCH}/sel4/sel4_arch/types_gen.h: \
		include/sel4/sel4_arch/types.pbf \
		${BF_GEN_PATH}
	@echo " [GEN] $@"
	@mkdir -p $(dir $@)
	@${CHANGED_PATH} $@ python ${BF_GEN_PATH} --environment libsel4 $< sel4_arch_include/${SEL4_ARCH}/sel4/sel4_arch/types_gen.h

# Stub generator
include/interfaces/sel4_client.h: \
		${SOURCE_DIR}/sel4_arch_include/${SEL4_ARCH}/interfaces/sel4arch.xml \
        ${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 $(SEL4_ARCH) -c ${srctree}/.config -o $@ $^
