1NAME	:=	rv
2# Follow the kernel version
3VERSION :=	$(shell cat VERSION 2> /dev/null || make -sC ../../.. kernelversion | grep -v make)
4
5# From libtracefs:
6# Makefiles suck: This macro sets a default value of $(2) for the
7# variable named by $(1), unless the variable has been set by
8# environment or command line. This is necessary for CC and AR
9# because make sets default values, so the simpler ?= approach
10# won't work as expected.
11define allow-override
12  $(if $(or $(findstring environment,$(origin $(1))),\
13            $(findstring command line,$(origin $(1)))),,\
14    $(eval $(1) = $(2)))
15endef
16
17# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
18$(call allow-override,CC,$(CROSS_COMPILE)gcc)
19$(call allow-override,AR,$(CROSS_COMPILE)ar)
20$(call allow-override,STRIP,$(CROSS_COMPILE)strip)
21$(call allow-override,PKG_CONFIG,pkg-config)
22$(call allow-override,LD_SO_CONF_PATH,/etc/ld.so.conf.d/)
23$(call allow-override,LDCONFIG,ldconfig)
24
25INSTALL	=	install
26MKDIR	=	mkdir
27FOPTS	:=	-flto=auto -ffat-lto-objects -fexceptions -fstack-protector-strong \
28		-fasynchronous-unwind-tables -fstack-clash-protection
29WOPTS	:= 	-Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -Wno-maybe-uninitialized
30
31TRACEFS_HEADERS	:= $$($(PKG_CONFIG) --cflags libtracefs)
32
33CFLAGS	:=	-O -g -DVERSION=\"$(VERSION)\" $(FOPTS) $(MOPTS) $(WOPTS) $(TRACEFS_HEADERS) $(EXTRA_CFLAGS) -I include
34LDFLAGS	:=	-ggdb $(EXTRA_LDFLAGS)
35LIBS	:=	$$($(PKG_CONFIG) --libs libtracefs)
36
37SRC	:=	$(wildcard src/*.c)
38HDR	:=	$(wildcard src/*.h)
39OBJ	:=	$(SRC:.c=.o)
40DIRS	:=	src
41FILES	:=	Makefile README.txt
42CEXT	:=	bz2
43TARBALL	:=	$(NAME)-$(VERSION).tar.$(CEXT)
44TAROPTS	:=	-cvjf $(TARBALL)
45BINDIR	:=	/usr/bin
46DATADIR	:=	/usr/share
47DOCDIR	:=	$(DATADIR)/doc
48MANDIR	:=	$(DATADIR)/man
49LICDIR	:=	$(DATADIR)/licenses
50SRCTREE	:=	$(or $(BUILD_SRC),$(CURDIR))
51
52# If running from the tarball, man pages are stored in the Documentation
53# dir. If running from the kernel source, man pages are stored in
54# Documentation/tools/rv/.
55ifneq ($(wildcard Documentation/.*),)
56DOCSRC	=	Documentation/
57else
58DOCSRC	=	$(SRCTREE)/../../../Documentation/tools/rv/
59endif
60
61LIBTRACEEVENT_MIN_VERSION = 1.5
62LIBTRACEFS_MIN_VERSION = 1.3
63
64.PHONY:	all warnings show_warnings
65all:	warnings rv
66
67TEST_LIBTRACEEVENT = $(shell sh -c "$(PKG_CONFIG) --atleast-version $(LIBTRACEEVENT_MIN_VERSION) libtraceevent > /dev/null 2>&1 || echo n")
68ifeq ("$(TEST_LIBTRACEEVENT)", "n")
69WARNINGS = show_warnings
70MISSING_LIBS += echo "**   libtraceevent version $(LIBTRACEEVENT_MIN_VERSION) or higher";
71MISSING_PACKAGES += "libtraceevent-devel"
72MISSING_SOURCE += echo "**  https://git.kernel.org/pub/scm/libs/libtrace/libtraceevent.git/ ";
73endif
74
75TEST_LIBTRACEFS = $(shell sh -c "$(PKG_CONFIG) --atleast-version $(LIBTRACEFS_MIN_VERSION) libtracefs > /dev/null 2>&1 || echo n")
76ifeq ("$(TEST_LIBTRACEFS)", "n")
77WARNINGS = show_warnings
78MISSING_LIBS += echo "**   libtracefs version $(LIBTRACEFS_MIN_VERSION) or higher";
79MISSING_PACKAGES += "libtracefs-devel"
80MISSING_SOURCE += echo "**  https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/ ";
81endif
82
83define show_dependencies
84	@echo "********************************************";				\
85	echo "** NOTICE: Failed build dependencies";					\
86	echo "**";									\
87	echo "** Required Libraries:";							\
88	$(MISSING_LIBS)									\
89	echo "**";									\
90	echo "** Consider installing the latest libtracefs from your";			\
91	echo "** distribution, e.g., 'dnf install $(MISSING_PACKAGES)' on Fedora,";	\
92	echo "** or from source:";							\
93	echo "**";									\
94	$(MISSING_SOURCE)								\
95	echo "**";									\
96	echo "********************************************"
97endef
98
99show_warnings:
100	$(call show_dependencies);
101
102ifneq ("$(WARNINGS)", "")
103ERROR_OUT = $(error Please add the necessary dependencies)
104
105warnings: $(WARNINGS)
106	$(ERROR_OUT)
107endif
108
109rv: $(OBJ)
110	$(CC) -o rv $(LDFLAGS) $(OBJ) $(LIBS)
111
112.PHONY: install
113install: doc_install
114	$(MKDIR) -p $(DESTDIR)$(BINDIR)
115	$(INSTALL) rv -m 755 $(DESTDIR)$(BINDIR)
116	$(STRIP) $(DESTDIR)$(BINDIR)/rv
117
118.PHONY: clean tarball
119clean: doc_clean
120	@test ! -f rv || rm rv
121	@test ! -f $(TARBALL) || rm -f $(TARBALL)
122	@rm -rf *~ $(OBJ) *.tar.$(CEXT)
123
124tarball: clean
125	rm -rf $(NAME)-$(VERSION) && mkdir $(NAME)-$(VERSION)
126	echo $(VERSION) > $(NAME)-$(VERSION)/VERSION
127	cp -r $(DIRS) $(FILES) $(NAME)-$(VERSION)
128	mkdir $(NAME)-$(VERSION)/Documentation/
129	cp -rp $(SRCTREE)/../../../Documentation/tools/rv/* $(NAME)-$(VERSION)/Documentation/
130	tar $(TAROPTS) --exclude='*~' $(NAME)-$(VERSION)
131	rm -rf $(NAME)-$(VERSION)
132
133.PHONY: doc doc_clean doc_install
134doc:
135	$(MAKE) -C $(DOCSRC)
136
137doc_clean:
138	$(MAKE) -C $(DOCSRC) clean
139
140doc_install:
141	$(MAKE) -C $(DOCSRC) install
142