# Oracle Linux DTrace.
# Copyright (c) 2011, 2025, 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.

EXTERNAL_64BIT_TRIGGERS = testprobe readwholedir mmap bogus-ioctl open delaydie futex \
    periodic_output \
    pid-tst-args1 pid-tst-float pid-tst-fork pid-tst-gcc \
    pid-tst-ret1 pid-tst-ret2 pid-tst-vfork pid-tst-weak1 pid-tst-weak2 \
    proc-tst-sigwait proc-tst-omp proc-tst-pthread-exec profile-tst-ufuncsort \
    raise-tst-raise1 raise-tst-raise2 raise-tst-raise3 syscall-tst-args \
    ustack-tst-basic ustack-tst-bigstack ustack-tst-bigstack-spin \
    ustack-tst-spin ustack-tst-mtspin \
    visible-constructor visible-constructor-static visible-constructor-static-unstripped

EXTERNAL_64BIT_SDT_TRIGGERS = usdt-tst-argmap usdt-tst-args \
    usdt-tst-arg-const usdt-tst-arg-reg \
    usdt-tst-deref-decode usdt-tst-forker usdt-tst-defer \
    usdt-tst-multiprovider usdt-tst-multiprov-dupprobe usdt-tst-special
EXTERNAL_64BIT_TRIGGERS += $(EXTERNAL_64BIT_SDT_TRIGGERS)

EXTERNAL_32BIT_TRIGGERS := visible-constructor-32
EXTERNAL_TRIGGERS = $(EXTERNAL_64BIT_TRIGGERS) $(if $(NATIVE_BITNESS_ONLY),,$(EXTERNAL_32BIT_TRIGGERS))

INTERNAL_64BIT_TRIGGERS = libproc-pldd libproc-consistency libproc-sleeper \
    libproc-sleeper-pie libproc-dlmadopen libproc-lookup-by-name \
    libproc-lookup-victim libproc-execing-bkpts libproc-execing-bkpts-victim
INTERNAL_32BIT_TRIGGERS := libproc-sleeper-32 libproc-sleeper-pie-32
INTERNAL_TRIGGERS = $(INTERNAL_64BIT_TRIGGERS) $(if $(NATIVE_BITNESS_ONLY),,$(INTERNAL_32BIT_TRIGGERS))

SCRIPT_TRIGGERS = longsleep execloop doio.sh io-mount-local.sh io-mount-nfs.sh

TRIGGERS = $(EXTERNAL_TRIGGERS) $(INTERNAL_TRIGGERS)

TRIGGER_SOLIBS = libproc-dlmlib libproc-lookup-victim-lib

install-test::
	$(call describe-install-target,$(INSTTESTDIR)/test/triggers,$(SCRIPT_TRIGGERS))
	mkdir -p $(INSTTESTDIR)/test/triggers
	install -m 755 $(addprefix test/triggers/,$(SCRIPT_TRIGGERS)) $(INSTTESTDIR)/test/triggers

define trigger-template
CMDS += $(1)
$(1)_DIR := $(current-dir)
$(1)_TARGET = $(1)
$(1)_SOURCES = $(1).c
$(1)_POST := link-trigger
$(1)_NOCFLAGS := --coverage
$(1)_NOLDFLAGS := --coverage

clean::
	rm -f test/triggers/$(1)

install-test::
	$(call describe-install-target,$(INSTTESTDIR)/test/triggers,$(1))
	install -m 755 test/triggers/$(1) $(INSTTESTDIR)/test/triggers/$(1)
endef

define link-trigger
	rm -f test/triggers/$(notdir $(1))
	ln -s $(1) test/triggers/$(notdir $(1))
endef

define trigger-solib-template

BUILDLIBS += $(1)-build
SHLIBS += $(1)

$(1)-build_TARGET := $(1)
$(1)-build_DIR := $(current-dir)
$(1)-build_SOURCES := $(1).c

$(1)_DIR := $(current-dir)
$(1)_TARGET := $(1)
$(1)_SONAME := $(1).so.0
$(1)_VERSION := 0.0.0
$(1)_LIBSOURCES := $(1)-build
$(1)_POST := link-trigger-solib

clean::
	rm -f test/triggers/$(1).so*

install-test::
	$(call describe-install-target,$(INSTTESTDIR)/test/triggers,$(1).so)
	cp -a test/triggers/$(1).so* $(INSTTESTDIR)/test/triggers/
	chown root:root $(INSTTESTDIR)/test/triggers/$(1).so* 2>/dev/null || true
endef

define link-trigger-solib
	rm -f test/triggers/$(notdir $(1))
	cp -al $(1) test/triggers/$(notdir $(1))
	ln -s $(notdir $(1)) test/triggers/`echo $(notdir $(1)) | sed 's,\.[0-9]\+\.[0-9]\+$$$$,,'`
	ln -s $(notdir $(1)) test/triggers/`echo $(notdir $(1)) | sed 's,\.[0-9]\+\.[0-9]\+\.[0-9]\+$$$$,,'`
endef

# This must go above all the file-specific overrides below, since they can
# override things it defines.  (If it went below, it would blow all the
# overrides away.)

$(foreach trigger,$(TRIGGERS),$(eval $(call trigger-template,$(trigger))))
$(foreach trigger-solib,$(TRIGGER_SOLIBS),$(eval $(call trigger-solib-template,$(trigger-solib))))

proc-tst-sigwait_LIBS := -lrt
proc-tst-omp_CFLAGS := -fopenmp
proc-tst-omp_LDFLAGS := -fopenmp
proc-tst-pthread-exec_CFLAGS := -pthread
proc-tst-pthread-exec_LIBS := -pthread

# readwholedir is used for ustack() tests, so needs a symbol table and needs
# all its functions in it, even if they would be sibcalled out of existence.
readwholedir_CFLAGS := -fno-inline -fno-optimize-sibling-calls
readwholedir_LDFLAGS := -Wl,--export-dynamic

# visible-constructor-32 is 32-bit.
visible-constructor-32_CFLAGS := -m32

# visible-constructor-static and visible-constructor-static-unstripped
# are both statically linked; the latter has a symbol table.
# The others should never have one, even if building with debug=yes.
visible-constructor-static-unstripped_LDFLAGS := -static -Bdynamic
visible-constructor-static_LDFLAGS := -static -s
visible-constructor_LDFLAGS := -s
visible-constructor-32_LDFLAGS := -s

# We are using machinery from libproc directly, but libproc calls into
# nonpublic parts of libdtrace for various shared machinery (dt_list
# and so on).  Thus, we must link directly to the build libraries, since
# these have no symbol visibility constraints.

# (Technically libproc-* are not triggers, because they are run directly by the
# test rather than being invoked in parallel by the build machinery.  It seems
# easier to reuse that machinery than to introduce a whole new abstraction for
# 'testing things that are not dtrace' into the DTrace testsuite.  We do want
# test coverage analysis for this stuff, or at least for the libproc parts of
# it.)

libproc-pldd_CFLAGS := -Ilibproc -Ilibdtrace
libproc-pldd_NOCFLAGS :=
libproc-pldd_NOLDFLAGS :=
libproc-pldd_DEPS := build-libproc.a build-libdtrace.a libcommon.a libport.a
libproc-pldd_LIBS := $(objdir)/build-libproc.a $(objdir)/build-libdtrace.a $(objdir)/build-libcommon.a $(objdir)/build-libport.a $(libdtrace_LIBS)

# Technically libproc-dlmlib is not a dependency of libproc-consistency, but in
# practice the tests never call it with anything else, so it's needed whenever
# libproc-consistency is.
libproc-consistency_CFLAGS := -Ilibproc -Ilibdtrace
libproc-consistency_NOCFLAGS :=
libproc-consistency_NOLDFLAGS :=
libproc-consistency_DEPS := build-libproc.a build-libdtrace.a libcommon.a libport.a libproc-dlmlib.so
libproc-consistency_LIBS := $(objdir)/build-libproc.a $(objdir)/build-libdtrace.a $(objdir)/libcommon.a $(objdir)/build-libport.a $(libdtrace_LIBS)

# The lookup victim also needs to have an rpath baked into it, since when
# testing in --use-installed mode, there is no LD_LIBRARY_PATH pointing into
# build/ by default.

libproc-lookup-victim_LDFLAGS := -Wl,-rpath test/triggers
libproc-lookup-victim_DEPS := libproc-lookup-victim-lib.so
libproc-lookup-victim_LIBS := $(objdir)/libproc-lookup-victim-lib.so -ldl

libproc-lookup-by-name_CFLAGS := -Ilibproc -Ilibdtrace
libproc-lookup-by-name_LDFLAGS := -Bdynamic
libproc-lookup-by-name_NOCFLAGS :=
libproc-lookup-by-name_NOLDFLAGS :=
libproc-lookup-by-name_DEPS := build-libproc.a build-libdtrace.a libcommon.a libport.a
libproc-lookup-by-name_LIBS := $(objdir)/build-libproc.a $(objdir)/build-libdtrace.a $(objdir)/libcommon.a $(objdir)/build-libport.a $(libdtrace_LIBS)

libproc-execing-bkpts_CFLAGS := -Ilibproc -Ilibdtrace
libproc-execing-bkpts_LDFLAGS :=
libproc-execing-bkpts_NOCFLAGS :=
libproc-execing-bkpts_NOLDFLAGS :=
libproc-execing-bkpts_DEPS := build-libproc.a build-libdtrace.a libcommon.a libport.a
libproc-execing-bkpts_LIBS := $(objdir)/build-libproc.a $(objdir)/build-libdtrace.a $(objdir)/libcommon.a $(objdir)/build-libport.a $(libdtrace_LIBS)

# We need multiple versions of libproc-sleeper with different combinations
# of flags.
libproc-sleeper-32_CFLAGS := -m32
libproc-sleeper-pie_CFLAGS := -fPIE
libproc-sleeper-pie_LDFLAGS := -fPIE -pie
libproc-sleeper-pie-32_CFLAGS := -m32 -fPIE
libproc-sleeper-pie-32_LDFLAGS := -fPIE -pie

# Make dlmadopen PIE so that we can ensure that we can track execve()s
# in which the value of r_brk changes.  As with the libproc-lookup-victim,
# this needs an rpath, too.  It's also threaded (sometimes, anyway).
libproc-dlmadopen_LIBS := -ldl -lpthread
libproc-dlmadopen_CFLAGS := -fPIE
libproc-dlmadopen_LDFLAGS := -fPIE -pie -Wl,-rpath test/triggers

# Various pid-tst-* triggers need to be compiled without optimization
pid-tst-args1_CFLAGS := -O0
pid-tst-fork_CFLAGS := -O0
pid-tst-ret1_CFLAGS := -O0
pid-tst-ret2_CFLAGS := -O0
pid-tst-weak1_CFLAGS := -O0
pid-tst-weak2_CFLAGS := -O0
profile-tst-ufuncsort_CFLAGS := -O0

# usdt-tst-argmap calls USDT probes using sys/sdt.h
usdt-tst-argmap_CFLAGS := -Iuts/common
usdt-tst-argmap_PROV := usdt-tst-argmap-prov.d

# usdt-tst-deref-decode calls USDT probes using sys/sdt.h
usdt-tst-deref-decode_CFLAGS := -Iuts/common -O3
usdt-tst-deref-decode_SOURCES := usdt-tst-deref-decode.${ARCH}.c
usdt-tst-deref-decode_PROV := usdt-tst-deref-decode-prov.d

# usdt-tst-args calls USDT probes using sys/sdt.h
usdt-tst-args_CFLAGS := -Iuts/common
usdt-tst-args_PROV := usdt-tst-args-prov.d

# usdt-tst-args-const calls USDT probes based on dtrace -h
usdt-tst-arg-const_CFLAGS := -Iuts/common
usdt-tst-arg-const_PROV := usdt-tst-arg-const-prov.d

# usdt-tst-args-reg calls USDT probes based on dtrace -h
usdt-tst-arg-reg_CFLAGS := -Iuts/common -O0
usdt-tst-arg-reg_PROV := usdt-tst-arg-reg-prov.d

# usdt-tst-forker calls USDT probes based on dtrace -h
usdt-tst-forker_PROV := usdt-tst-forker-prov.d

# usdt-tst-defer calls USDT probes based on dtrace -h
usdt-tst-defer_PROV := usdt-tst-defer-prov.d

# usdt-tst-multiprovider calls USDT probes based on dtrace -h
usdt-tst-multiprovider_PROV := usdt-tst-multiprovider-prov.d

# usdt-tst-multiprov-dupprobe calls USDT probes based on dtrace -h
usdt-tst-multiprov-dupprobe_PROV := usdt-tst-multiprov-dupprobe-prov.d

# usdt-tst-special calls USDT probes based on dtrace -h
usdt-tst-special_CFLAGS := -fno-inline -O2
usdt-tst-special_PROV := usdt-tst-special-prov.d

# ustack-tst-mtspin uses threads
# ustack wants no optimization

ustack-tst-basic_CFLAGS := -O0 -fno-inline -fno-omit-frame-pointer
ustack-tst-bigstack_CFLAGS := -O0 -fno-inline -fno-omit-frame-pointer
ustack-tst-bigstack-spin_CFLAGS := -O0 -fno-inline -fno-omit-frame-pointer
ustack-tst-spin_CFLAGS := -O0
ustack-tst-mtspin_CFLAGS := -O0
ustack-tst-mtspin_LIBS := -lpthread
