# Oracle Linux DTrace.
# Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at
# http://oss.oracle.com/licenses/upl.

$(objdir)/include/.dir.stamp:
	mkdir -p $(objdir)/include
	for d in /usr/include/${ARCH}-linux-gnu/asm /usr/include/asm; do \
		if [ -d $$d ]; then \
			ln -sf $$d $(objdir)/include; \
			break; \
		fi; \
	done
	ln -sf /usr/include/asm-generic $(objdir)/include
	ln -sf /usr/include/linux $(objdir)/include
	@touch $(objdir)/include/.dir.stamp

BPFLIBS += bpf_dlib
bpf_dlib_CPPFLAGS = -Ilibdtrace -Iinclude -I$(objdir)/include -idirafter /usr/include
bpf_dlib_TARGET = dlibs/bpf_dlib
bpf_dlib_DIR := $(current-dir)
bpf_dlib_SRCDEPS = $(objdir)/include/.dir.stamp $(objdir)/include/bpf_asm_helpers.h
bpf_dlib_SOURCES = \
	agg_lqbin.c agg_qbin.c \
	basename.S \
	bvar_execargs.S \
	cleanpath.S \
	dirname.S \
	get_agg.c \
	get_bvar.c \
	get_dvar.c \
	index.S \
	inet_ntoa.S \
	inet_ntoa6.S \
	link_ntop.S \
	lltostr.S \
	mutex_owned.S \
	mutex_owner.S \
	progenyof.S \
	probe_error.c \
	rindex.S \
	rw_iswriter.S \
	rw_read_held.S \
	rw_write_held.S \
	speculation.c \
	strchr.S \
	strcmp.S \
	strjoin.S \
	strlen.c \
	strrchr.S \
	strtok.S \
	substr.S

bpf-check: $(objdir)/include/.dir.stamp
	$(BPFC) $(BPFCPPFLAGS) $(bpf_dlib_CPPFLAGS) $(BPFCFLAGS) -S \
		-o - bpf/get_bvar.c | \
		awk '/dt_get_bvar:/ { \
			 getline; \
			 if(/stxdw/ && /%r6$$/) { \
			     print "ERROR: $(BPFC) is too old"; \
			     exit(1); \
			 } \
			 exit(0); \
		     }'

install::
	mkdir -p $(INSTLIBDIR)/dtrace
	$(call describe-install-target,$(INSTLIBDIR)/dtrace,bpf_dlib.o)
	install -m 644 $(objdir)/dlibs/bpf_dlib.o $(INSTLIBDIR)/dtrace
