targets		+= syscalls
targets		+= crtools

SYS-ASM		:= syscalls.S

syscalls-asm-y	+= $(SYS-ASM:.S=).o
crtools-obj-y	+= crtools.o
crtools-obj-y	+= cpu.o
crtools-obj-y	+= prlimit.o

ifeq ($(ARCH),x86_64)
SYS-DEF		:= syscall_64.tbl
SYS-ASM-COMMON	:= syscall-common-x86-64.S
else
SYS-DEF		:= syscall_32.tbl
SYS-ASM-COMMON	:= syscall-common-x86-32.S
endif

SYS-TYPES	:= ../../include/syscall-types.h
SYS-CODES	:= ../../include/syscall-codes.h
SYS-PROTO	:= ../../include/syscall.h

SYS-EXEC-TBL	:= sys-exec-tbl.c

ifeq ($(ARCH),x86_64)
syscalls-asm-y-asmflags	:= -fpie -Wstrict-prototypes -Wa,--noexecstack
else
syscalls-asm-y-asmflags	:= -fno-pic -Wstrict-prototypes -Wa,--noexecstack
endif
syscalls-asm-y-asmflags += -nostdlib -fomit-frame-pointer -I$(obj)

ifneq ($(ARCH),x86_64)
syscalls-obj-y += syscalls/syscall32.o
$(obj)/syscalls/syscall32.o: $(obj)/$(SYS-CODES) $(obj)/$(SYS-PROTO)
endif
cleanup-y += $(obj)/syscalls/*.o $(obj)/syscalls/*.d

ASMFLAGS	+= -D__ASSEMBLY__

$(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/{print "#define", $$1, $$2}'						>> $@
	$(Q) echo "#endif /* __ASM_CR_SYSCALL_CODES_H__ */"						>> $@
_all += $(obj)/$(SYS-CODES)
cleanup-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\""							>> $@
ifneq ($(ARCH),x86_64)
	$(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__ */"						>> $@
_all += $(obj)/$(SYS-PROTO)
cleanup-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, ")"}'				>> $@
_all += $(obj)/$(SYS-ASM)
cleanup-y += $(obj)/$(SYS-ASM)

$(obj)/syscalls.o: $(obj)/$(SYS-ASM)

$(obj)/$(SYS-EXEC-TBL): $(obj)/syscalls/$(SYS-DEF) $(obj)/$(SYS-CODES) $(obj)/$(SYS-PROTO)
	$(E) "  GEN     " $@
	$(Q) echo "/* Autogenerated, don't edit */"							>  $@
	$(Q) cat $< | awk '/^__NR/{print "SYSCALL(", substr($$3, 5), ",", $$2, ")"}'			>> $@
_all += $(obj)/$(SYS-EXEC-TBL)
cleanup-y += $(obj)/$(SYS-EXEC-TBL)

$(obj)/crtools.built-in.o: | $(obj)/$(SYS-CODES) $(obj)/$(SYS-PROTO)

ifneq ($(MAKECMDGOALS),clean)
deps-after := $(obj)/$(SYS-ASM)
incdeps := y
endif
