include $(__nmk_dir)msg.mk
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

ifeq ($(ARCH),x86)
        SYS-DEF		:= syscall_64.tbl
        SYS-ASM-COMMON	:= syscall-common-x86-64.S
        asflags-y	+= -fpie -Wstrict-prototypes -Wa,--noexecstack
else
        SYS-DEF		:= syscall_32.tbl
        SYS-ASM-COMMON	:= syscall-common-x86-32.S
        asflags-y	+= -fno-pic -Wstrict-prototypes -Wa,--noexecstack
        obj-y		+= syscalls/syscall32.o

$(obj)/syscalls/syscall32.o: $(obj)/$(SYS-CODES) $(obj)/$(SYS-PROTO)
endif

asflags-y		+= -D__ASSEMBLY__ -nostdlib -fomit-frame-pointer
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)
	$(call msg-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)
	$(call msg-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 "#ifndef CR_NOGLIBC"									>> $@
	$(Q) echo "# error This file should only be used in the parasite code"				>> $@
	$(Q) echo "#endif"										>> $@
	$(Q) echo "#include \"syscall-codes.h\""							>> $@
	$(Q) echo "#include \"syscall-types.h\""							>> $@
ifneq ($(ARCH),x86)
	$(Q) echo "#include \"asm/syscall32.h\""							>> $@
endif
	$(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)
	$(call msg-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)
	$(call msg-gen, $@)
	$(Q) echo "/* Autogenerated, don't edit */"							>  $@
	$(Q) cat $< | awk '/^__NR/{print "SYSCALL(", substr($$3, 5), ",", $$2, ")"}'			>> $@
mrproper-y		+= $(obj)/$(SYS-EXEC-TBL)
all-y			+= $(obj)/$(SYS-EXEC-TBL)
