1NAME	:=	rtla
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)
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/rtla/.
55ifneq ($(wildcard Documentation/.*),)
56DOCSRC	=	Documentation/
57else
58DOCSRC	=	$(SRCTREE)/../../../Documentation/tools/rtla/
59endif
60
61LIBTRACEEVENT_MIN_VERSION = 1.5
62LIBTRACEFS_MIN_VERSION = 1.3
63
64.PHONY:	all warnings show_warnings
65all:	warnings rtla
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
109rtla: $(OBJ)
110	$(CC) -o rtla $(LDFLAGS) $(OBJ) $(LIBS)
111
112static: $(OBJ)
113	$(CC) -o rtla-static $(LDFLAGS) --static $(OBJ) $(LIBS) -lpthread -ldl
114
115.PHONY: install
116install: doc_install
117	$(MKDIR) -p $(DESTDIR)$(BINDIR)
118	$(INSTALL) rtla -m 755 $(DESTDIR)$(BINDIR)
119	$(STRIP) $(DESTDIR)$(BINDIR)/rtla
120	@test ! -f $(DESTDIR)$(BINDIR)/osnoise || rm $(DESTDIR)$(BINDIR)/osnoise
121	ln -s rtla $(DESTDIR)$(BINDIR)/osnoise
122	@test ! -f $(DESTDIR)$(BINDIR)/timerlat || rm $(DESTDIR)$(BINDIR)/timerlat
123	ln -s rtla $(DESTDIR)$(BINDIR)/timerlat
124
125.PHONY: clean tarball
126clean: doc_clean
127	@test ! -f rtla || rm rtla
128	@test ! -f rtla-static || rm rtla-static
129	@test ! -f src/rtla.o || rm src/rtla.o
130	@test ! -f $(TARBALL) || rm -f $(TARBALL)
131	@rm -rf *~ $(OBJ) *.tar.$(CEXT)
132
133tarball: clean
134	rm -rf $(NAME)-$(VERSION) && mkdir $(NAME)-$(VERSION)
135	echo $(VERSION) > $(NAME)-$(VERSION)/VERSION
136	cp -r $(DIRS) $(FILES) $(NAME)-$(VERSION)
137	mkdir $(NAME)-$(VERSION)/Documentation/
138	cp -rp $(SRCTREE)/../../../Documentation/tools/rtla/* $(NAME)-$(VERSION)/Documentation/
139	tar $(TAROPTS) --exclude='*~' $(NAME)-$(VERSION)
140	rm -rf $(NAME)-$(VERSION)
141
142.PHONY: doc doc_clean doc_install
143doc:
144	$(MAKE) -C $(DOCSRC)
145
146doc_clean:
147	$(MAKE) -C $(DOCSRC) clean
148
149doc_install:
150	$(MAKE) -C $(DOCSRC) install
151