builtin-name		:= syscalls.built-in.o

CFLAGS			:= $(filter-out -pg $(CFLAGS-GCOV),$(CFLAGS))

SYS-TYPES		:= ../../include/syscall-types.h
SYS-CODES		:= ../../include/syscall-codes.h
SYS-PROTO		:= ../../include/syscall.h
SYS-DEF			:= syscall-ppc64.tbl
SYS-ASM-COMMON		:= syscall-common-ppc64.S

asflags-y		+= -D__ASSEMBLY__ -nostdlib -fomit-frame-pointer
asflags-y		+= -fpie -Wstrict-prototypes -Wa,--noexecstack
asflags-y		+= -iquote $(obj) -iquote $(obj)/include -iquote $(SRC_DIR)/criu/include

SYS-ASM			:= syscalls.S
obj-y			+= $(SYS-ASM:.S=).o

$(obj)/$(SYS-CODES): $(obj)/syscalls/$(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__ */"						>> $@
mrproper-y		+= $(obj)/$(SYS-CODES)

$(obj)/$(SYS-PROTO): $(obj)/syscalls/$(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 \"syscall-codes.h\""							>> $@
	$(Q) echo "#include \"syscall-types.h\""							>> $@
	$(Q) cat $< | awk  '/^__NR/{print "extern long", $$3, substr($$0, index($$0,$$4)), ";"}'	>> $@
	$(Q) echo "#endif /* __ASM_CR_SYSCALL_PROTO_H__ */"						>> $@
mrproper-y		+= $(obj)/$(SYS-PROTO)

$(obj)/$(SYS-ASM): $(obj)/syscalls/$(SYS-DEF) $(obj)/syscalls/$(SYS-ASM-COMMON) $(obj)/$(SYS-CODES) $(obj)/$(SYS-PROTO)
	$(E) "  GEN     " $@
	$(Q) echo "/* Autogenerated, don't edit */"							>  $@
	$(Q) echo "#include \"syscall-codes.h\""							>> $@
	$(Q) echo "#include \"syscalls/$(SYS-ASM-COMMON)\""						>> $@
	$(Q) cat $< | awk '/^__NR/{print "SYSCALL(", $$3, ",", $$2, ")"}'				>> $@
mrproper-y		+= $(obj)/$(SYS-ASM)

SYS-EXEC-TBL		:= sys-exec-tbl.c
$(obj)/$(SYS-EXEC-TBL): $(obj)/syscalls/$(SYS-DEF) $(obj)/$(SYS-CODES) $(obj)/$(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 "};"					>>  $@
mrproper-y		+= $(obj)/$(SYS-EXEC-TBL)
all-y			+= $(obj)/$(SYS-EXEC-TBL)
