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

# Temporarily suppress dtprobed builds before configury is done: we don't know
# which fuse is safe to even look up with pkg-config without ugly messages.
ifdef CONFIGURED
CMDS += dtprobed

# Support FUSE 2 as well as FUSE 3: use if explicitly requested or if
# FUSE 3 is not found.
ifdef WANTS_LIBFUSE2
LIBFUSE := fuse
else
ifndef HAVE_LIBFUSE3
LIBFUSE := fuse
else
LIBFUSE := fuse3
endif
endif

dtprobed_DIR := $(current-dir)
dtprobed_TARGET = dtprobed
dtprobed_CPPFLAGS := -I. -Idtprobed -Ilibproc -Ilibcommon -Ilibport
dtprobed_CFLAGS := $(shell pkg-config --cflags $(LIBFUSE))
dtprobed_LIBS := -lcommon -lproc -lcommon -lport -lelf $(shell pkg-config --libs $(LIBFUSE))
dtprobed_DEPS := libproc.a libcommon.a libport.a
dtprobed_SOURCES := dtprobed.c dof_stash.c seccomp-assistance.c
dtprobed_LIBSOURCES := libproc libcommon

ifndef HAVE_FUSE_LOG
dtprobed_SOURCES += rpl_fuse_log.c
endif

dtprobed.c_CFLAGS := -Wno-pedantic
seccomp-assistance.c_CFLAGS := -fno-lto
endif

install-dtprobed-autostart::
ifneq ($(WITH_SYSTEMD),)
	mkdir -p $(INSTSYSTEMDUNITDIR) $(INSTSYSTEMDPRESETDIR)
	$(call describe-install-target,$(INSTSYSTEMDUNITDIR),dtprobed.service)
	sed 's,@SBINDIR@,$(SBINDIR),' < $(dtprobed_DIR)dtprobed.service.in > $(objdir)/dtprobed.service
	install -m 644 $(objdir)/dtprobed.service $(INSTSYSTEMDUNITDIR)
	$(call describe-install-target,$(INSTSYSTEMDUNITDIR),dtrace-usdt.target)
	install -m 644 $(dtprobed_DIR)dtrace-usdt.target $(INSTSYSTEMDUNITDIR)
	$(call describe-install-target,$(INSTSYSTEMDPRESETDIR),50-dtprobed.preset)
	install -m 644 $(dtprobed_DIR)50-dtprobed.preset $(INSTSYSTEMDPRESETDIR)
	if [[ -z $RPM_BUILD_ROOT ]]; then \
		systemctl enable dtprobed.service dtrace-usdt.target; \
	fi
endif

install::
	mkdir -p $(INSTSBINDIR)
	$(call describe-install-target,$(INSTSBINDIR),dtprobed)
	install -m 755 $(objdir)/dtprobed $(INSTSBINDIR)
	mkdir -p $(INSTMANDIR)/man8
	$(call describe-install-target,$(INSTMANDIR),dtprobed.8)
	install -m 644 $(dtprobed_DIR)/dtprobed.8 $(INSTMANDIR)/man8
	mkdir -p $(INSTUDEVDIR)
	$(call describe-install-target,$(INSTUDEVDIR),60-dtprobed.rules)
	install -m 644 $(dtprobed_DIR)60-dtprobed.rules $(INSTUDEVDIR)
	if [[ "$(SYSTEMDUNITDIR)" = /usr/lib/systemd/system ]] || \
	   [[ "$(SYSTEMDUNITDIR)" = /usr/local/lib/systemd/system ]] || \
	   [[ -n "$(filter SYSTEMDUNITDIR,$(CONFIGURED_VARS))" ]] ; then \
		$(MAKE) install-dtprobed-autostart; \
	else \
		echo " === WARNING WARNING WARNING === " >&2; \
		echo "systemd service files for dtprobed not installed." >&2; \
		echo "USDT probes (and the testsuite) will not work unless this is" >&2; \
		echo "done.  Try make install-dtprobed-autostart, but this may not" >&2; \
		echo "be enough: you may need to manually move the systemd service" >&2; \
		echo "files somewhere udev and systemd will find them." >&2; \
		echo "If there is already a running dtprobed, it may or may not work" >&2; \
		echo "in conjunction with this DTrace." >&2; \
		echo "Configure with --systemd-unit-dir to override this decision." >&2; \
		echo " === WARNING WARNING WARNING === " >&2; \
	fi
