# Makefile template for configure for the eBPF simulator
# Copyright (C) 2020-2023 Free Software Foundation, Inc.
#
# This file is part of GDB, the GNU debugger.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

## COMMON_PRE_CONFIG_FRAG

CGEN_STD_OBJS = cgen-run.o cgen-scache.o cgen-trace.o cgen-utils.o
BPF_GEN_OBJS = arch.o cpu.o \
               decode-le.o decode-be.o \
               sem-le.o sem-be.o \
               mloop-le.o mloop-be.o
BPF_HAND_OBJS = bpf.o sim-if.o traps.o bpf-helpers.o

SIM_OBJS = \
	$(SIM_NEW_COMMON_OBJS) \
	$(CGEN_STD_OBJS) \
	$(BPF_GEN_OBJS) \
	$(BPF_HAND_OBJS)

SIM_EXTRA_CFLAGS = -DWITH_TARGET_WORD_BITSIZE=64

SIM_EXTRA_CLEAN = bpf-clean

## COMMON_POST_CONFIG_FRAG

# Dependencies for binaries from CGEN generated source

mloop-le.o: mloop-le.c
	$(COMPILE) -DWANT_ISA_EBPFLE mloop-le.c
	$(POSTCOMPILE)
mloop-be.o: mloop-be.c
	$(COMPILE) -DWANT_ISA_EBPFBE mloop-be.c
	$(POSTCOMPILE)

decode-le.o: decode-le.c
	$(COMPILE) -DWANT_ISA_EBPFLE $(srcdir)/decode-le.c
	$(POSTCOMPILE)
decode-be.o: decode-be.c
	$(COMPILE) -DWANT_ISA_EBPFBE $(srcdir)/decode-be.c
	$(POSTCOMPILE)

sem-le.o: sem-le.c
	$(COMPILE) -DWANT_ISA_EBPFLE $(srcdir)/sem-le.c
	$(POSTCOMPILE)
sem-be.o: sem-be.c
	$(COMPILE) -DWANT_ISA_EBPFBE $(srcdir)/sem-be.c
	$(POSTCOMPILE)

arch = bpf

CGEN_COMMON_DEPS = \
	$(CGEN_READ_SCM) \
	$(srcdir)/../../cpu/bpf.cpu \
	$(srcdir)/../../cpu/bpf.opc \
	Makefile

stamp-arch: $(CGEN_COMMON_DEPS) $(CGEN_ARCH_SCM)
	$(MAKE) cgen-arch $(CGEN_FLAGS_TO_PASS) \
		mach=bpf cpu=bpfbf \
		archfile=$(srcdir)/../../cpu/bpf.cpu \
		FLAGS="with-scache"
	$(SILENCE) touch $@
$(srcdir)/arch.h $(srcdir)/arch.c $(srcdir)/cpuall.h: $(CGEN_MAINT) stamp-arch
	@true

stamp-cpu: $(CGEN_COMMON_DEPS) $(CGEN_CPU_SCM)
	$(MAKE) cgen-cpu  $(CGEN_FLAGS_TO_PASS) \
		isa=ebpfle,ebpfbe cpu=bpfbf mach=bpf \
		archfile=$(srcdir)/../../cpu/bpf.cpu \
		FLAGS="with-multiple-isa with-scache"
	rm -f $(srcdir)/model.c
	$(SILENCE) touch $@
$(srcdir)/cpu.h $(srcdir)/cpu.c $(srcdir)/model.c: $(CGEN_MAINT) stamp-cpu
	@true

# We need to generate a group of files per ISA.
# For eBPF little-endian:
#    defs-le.h
#    sem-le.c, decode-le.c, decode-le.h
#    $(objdir)/mloop-le.c $(objdir)/eng-le.h
# For eBPF big-endian:
#    defs-be.h
#    sem-be.c, decode-be.c, decode-be.h
#    $(objdir)/mloop-be.c $(objdir)/eng-le.h
#
# The rules below take care of that.

stamp-defs-le: $(CGEN_COMMON_DEPS) $(CGEN_CPU_SCM)
	$(MAKE) cgen-defs $(CGEN_FLAGS_TO_PASS) \
		isa=ebpfle cpu=bpfbf mach=bpf \
		archfile=$(srcdir)/../../cpu/bpf.cpu \
		FLAGS="with-scache" \
                SUFFIX="-le"
	$(SILENCE) touch $@
$(srcdir)/defs-le.h: $(CGEN_MAINT) stamp-defs-le
	@true


stamp-defs-be: $(CGEN_COMMON_DEPS) $(CGEN_CPU_SCM)
	$(MAKE) cgen-defs $(CGEN_FLAGS_TO_PASS) \
		isa=ebpfbe cpu=bpfbf mach=bpf \
		archfile=$(srcdir)/../../cpu/bpf.cpu \
		FLAGS="with-scache" \
                SUFFIX="-be"
	$(SILENCE) touch $@
$(srcdir)/defs-be.h: $(CGEN_MAINT) stamp-defs-be
	@true

stamp-decode-le: $(CGEN_COMMON_DEPS) $(CGEN_CPU_SCM) $(GEN_DECODE_SCM)
	$(MAKE) cgen-decode $(CGEN_FLAGS_TO_PASS) \
		isa=ebpfle cpu=bpfbf mach=bpf \
		archfile=$(srcdir)/../../cpu/bpf.cpu \
		FLAGS="with-scache" \
                SUFFIX="-le" \
		EXTRAFILES="$(CGEN_CPU_SEM)"
	$(SILENCE) touch $@
$(srcdir)/sem-le.c $(srcdir)/decode-le.c $(srcdir)/decode-le.h: \
              $(CGEN_MAINT) stamp-decode-le
	@true


stamp-decode-be: $(CGEN_COMMON_DEPS) $(CGEN_CPU_SCM) $(GEN_DECODE_SCM)
	$(MAKE) cgen-decode $(CGEN_FLAGS_TO_PASS) \
		isa=ebpfbe cpu=bpfbf mach=bpf \
		archfile=$(srcdir)/../../cpu/bpf.cpu \
		FLAGS="with-scache" \
                SUFFIX="-be" \
		EXTRAFILES="$(CGEN_CPU_SEM)"
	$(SILENCE) touch $@
$(srcdir)/sem-be.c $(srcdir)/decode-be.c $(srcdir)/decode-be.h: \
              $(CGEN_MAINT) stamp-decode-be
	@true

.PHONY = bpf-clean

bpf-clean:
	rm -f stamp-arch stamp-cpu stamp-decode stamp-defs
