ccflags-y		+= -iquote $(PLUGIN_ARCH_DIR)/std/syscalls/
asflags-y		+= -iquote $(PLUGIN_ARCH_DIR)/std/syscalls/

sys-types		:= $(obj)/include/uapi/std/syscall-types.h
sys-codes		:= $(obj)/include/uapi/std/syscall-codes.h
sys-proto		:= $(obj)/include/uapi/std/syscall.h

sys-def			:= $(PLUGIN_ARCH_DIR)/std/syscalls/syscall-ppc64.tbl
sys-asm-common-name	:= std/syscalls/syscall-common-ppc64.S
sys-asm-common		:= $(PLUGIN_ARCH_DIR)/$(sys-asm-common-name)
sys-asm-types		:= $(obj)/include/uapi/std/asm/syscall-types.h
sys-exec-tbl		 = $(PLUGIN_ARCH_DIR)/std/sys-exec-tbl.c

sys-asm			:= ./$(PLUGIN_ARCH_DIR)/std/syscalls/syscalls.S
std-obj-y		+= $(sys-asm:.S=).o

$(sys-codes): $(sys-def)
	$(E) "  GEN     " $@
	$(Q) echo "/* Autogenerated, don't edit */"							>  $@
	$(Q) echo "#ifndef __ASM_CR_SYSCALL_CODES_H__"							>> $@
	$(Q) echo "#define __ASM_CR_SYSCALL_CODES_H__"							>> $@
	$(Q) cat $< | awk '/^__NR/{SYSN=$$1; sub("^__NR", "SYS", SYSN);'\
	'print "\n#ifndef ", $$1, "\n#define", $$1, $$2, "\n#endif";'\
	'print "#ifndef ", SYSN, "\n#define ", SYSN, $$1, "\n#endif"}'					>> $@
	$(Q) echo "#endif /* __ASM_CR_SYSCALL_CODES_H__ */"						>> $@

$(sys-proto): $(sys-def)
	$(E) "  GEN     " $@
	$(Q) echo "/* Autogenerated, don't edit */"							>  $@
	$(Q) echo "#ifndef __ASM_CR_SYSCALL_PROTO_H__"							>> $@
	$(Q) echo "#define __ASM_CR_SYSCALL_PROTO_H__"							>> $@
	$(Q) echo "#include \"uapi/std/syscall-codes.h\""						>> $@
	$(Q) echo "#include \"uapi/std/syscall-types.h\""						>> $@
	$(Q) cat $< | awk  '/^__NR/{print "extern long", $$3, substr($$0, index($$0,$$4)), ";"}'	>> $@
	$(Q) echo "#endif /* __ASM_CR_SYSCALL_PROTO_H__ */"						>> $@

$(sys-asm): $(sys-def) $(sys-asm-common) $(sys-codes) $(sys-proto)
	$(E) "  GEN     " $@
	$(Q) echo "/* Autogenerated, don't edit */"							>  $@
	$(Q) echo "#include \"uapi/std/syscall-codes.h\""						>> $@
	$(Q) echo "#include \"$(sys-asm-common-name)\""							>> $@
	$(Q) cat $< | awk '/^__NR/{print "SYSCALL(", $$3, ",", $$2, ")"}'				>> $@

$(sys-exec-tbl): $(sys-def) $(sys-codes) $(sys-proto)
	$(E) "  GEN     " $@
	$(Q) echo "/* Autogenerated, don't edit */"							>  $@
	$(Q) echo "static struct syscall_exec_desc sc_exec_table[] = {"					>> $@
	$(Q) cat $< | awk '/^__NR/{print "SYSCALL(", substr($$3, 5), ",", $$2, ")"}'			>> $@
	$(Q) echo "	{ }, /* terminator */"								>> $@
	$(Q) echo "};"											>> $@

$(sys-asm-types): $(PLUGIN_ARCH_DIR)/include/asm/syscall-types.h
	$(call msg-gen, $@)
	$(Q) ln -s ../../../../../../$(PLUGIN_ARCH_DIR)/include/asm/syscall-types.h $(sys-asm-types)

std-headers-deps	+= $(sys-asm) $(sys-codes) $(sys-proto) $(sys-asm-types)
mrproper-y		+= $(std-headers-deps)
