# 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

ifdef HAVE_LIBSYSTEMD
dtprobed_CFLAGS += $(shell pkg-config --cflags libsystemd)
dtprobed_LIBS += $(shell pkg-config --libs libsystemd)
endif

ifndef HAVE_FUSE_LOG
dtprobed_SOURCES += rpl_fuse_log.c
endif

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

install::
	mkdir -p $(INSTSBINDIR)
	$(call describe-install-target,$(INSTSBINDIR),dtprobed)
	install -m 755 $(objdir)/dtprobed $(INSTSBINDIR)
	mkdir -p $(INSTUDEVDIR)
	$(call describe-install-target,$(INSTUDEVDIR),60-dtprobed.rules)
	install -m 644 $(dtprobed_DIR)60-dtprobed.rules $(INSTUDEVDIR)
ifdef HAVE_LIBSYSTEMD
	mkdir -p $(INSTSYSTEMDUNITDIR) $(INSTSYSTEMDPRESETDIR)
	$(call describe-install-target,$(INSTSYSTEMDUNITDIR),dtprobed.service)
	install -m 644 $(dtprobed_DIR)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
