xref: /openbmc/linux/tools/tracing/rtla/Makefile (revision 8c61e3be)
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
31ifeq ($(CC),clang)
32  FOPTS := $(filter-out -ffat-lto-objects, $(FOPTS))
33  WOPTS := $(filter-out -Wno-maybe-uninitialized, $(WOPTS))
34endif
35
36TRACEFS_HEADERS	:= $$($(PKG_CONFIG) --cflags libtracefs)
37
38CFLAGS	:=	-O -g -DVERSION=\"$(VERSION)\" $(FOPTS) $(MOPTS) $(WOPTS) $(TRACEFS_HEADERS) $(EXTRA_CFLAGS)
39LDFLAGS	:=	-flto=auto -ggdb $(EXTRA_LDFLAGS)
40LIBS	:=	$$($(PKG_CONFIG) --libs libtracefs)
41
42SRC	:=	$(wildcard src/*.c)
43HDR	:=	$(wildcard src/*.h)
44OBJ	:=	$(SRC:.c=.o)
45DIRS	:=	src
46FILES	:=	Makefile README.txt
47CEXT	:=	bz2
48TARBALL	:=	$(NAME)-$(VERSION).tar.$(CEXT)
49TAROPTS	:=	-cvjf $(TARBALL)
50BINDIR	:=	/usr/bin
51DATADIR	:=	/usr/share
52DOCDIR	:=	$(DATADIR)/doc
53MANDIR	:=	$(DATADIR)/man
54LICDIR	:=	$(DATADIR)/licenses
55SRCTREE	:=	$(or $(BUILD_SRC),$(CURDIR))
56
57# If running from the tarball, man pages are stored in the Documentation
58# dir. If running from the kernel source, man pages are stored in
59# Documentation/tools/rtla/.
60ifneq ($(wildcard Documentation/.*),)
61DOCSRC	=	Documentation/
62else
63DOCSRC	=	$(SRCTREE)/../../../Documentation/tools/rtla/
64endif
65
66LIBTRACEEVENT_MIN_VERSION = 1.5
67LIBTRACEFS_MIN_VERSION = 1.3
68
69.PHONY:	all warnings show_warnings
70all:	warnings rtla
71
72TEST_LIBTRACEEVENT = $(shell sh -c "$(PKG_CONFIG) --atleast-version $(LIBTRACEEVENT_MIN_VERSION) libtraceevent > /dev/null 2>&1 || echo n")
73ifeq ("$(TEST_LIBTRACEEVENT)", "n")
74WARNINGS = show_warnings
75MISSING_LIBS += echo "**   libtraceevent version $(LIBTRACEEVENT_MIN_VERSION) or higher";
76MISSING_PACKAGES += "libtraceevent-devel"
77MISSING_SOURCE += echo "**  https://git.kernel.org/pub/scm/libs/libtrace/libtraceevent.git/ ";
78endif
79
80TEST_LIBTRACEFS = $(shell sh -c "$(PKG_CONFIG) --atleast-version $(LIBTRACEFS_MIN_VERSION) libtracefs > /dev/null 2>&1 || echo n")
81ifeq ("$(TEST_LIBTRACEFS)", "n")
82WARNINGS = show_warnings
83MISSING_LIBS += echo "**   libtracefs version $(LIBTRACEFS_MIN_VERSION) or higher";
84MISSING_PACKAGES += "libtracefs-devel"
85MISSING_SOURCE += echo "**  https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/ ";
86endif
87
88define show_dependencies
89	@echo "********************************************";				\
90	echo "** NOTICE: Failed build dependencies";					\
91	echo "**";									\
92	echo "** Required Libraries:";							\
93	$(MISSING_LIBS)									\
94	echo "**";									\
95	echo "** Consider installing the latest libtracefs from your";			\
96	echo "** distribution, e.g., 'dnf install $(MISSING_PACKAGES)' on Fedora,";	\
97	echo "** or from source:";							\
98	echo "**";									\
99	$(MISSING_SOURCE)								\
100	echo "**";									\
101	echo "********************************************"
102endef
103
104show_warnings:
105	$(call show_dependencies);
106
107ifneq ("$(WARNINGS)", "")
108ERROR_OUT = $(error Please add the necessary dependencies)
109
110warnings: $(WARNINGS)
111	$(ERROR_OUT)
112endif
113
114rtla: $(OBJ)
115	$(CC) -o rtla $(LDFLAGS) $(OBJ) $(LIBS)
116
117static: $(OBJ)
118	$(CC) -o rtla-static $(LDFLAGS) --static $(OBJ) $(LIBS) -lpthread -ldl
119
120.PHONY: install
121install: doc_install
122	$(MKDIR) -p $(DESTDIR)$(BINDIR)
123	$(INSTALL) rtla -m 755 $(DESTDIR)$(BINDIR)
124	$(STRIP) $(DESTDIR)$(BINDIR)/rtla
125	@test ! -f $(DESTDIR)$(BINDIR)/osnoise || rm $(DESTDIR)$(BINDIR)/osnoise
126	ln -s rtla $(DESTDIR)$(BINDIR)/osnoise
127	@test ! -f $(DESTDIR)$(BINDIR)/hwnoise || rm $(DESTDIR)$(BINDIR)/hwnoise
128	ln -s rtla $(DESTDIR)$(BINDIR)/hwnoise
129	@test ! -f $(DESTDIR)$(BINDIR)/timerlat || rm $(DESTDIR)$(BINDIR)/timerlat
130	ln -s rtla $(DESTDIR)$(BINDIR)/timerlat
131
132.PHONY: clean tarball
133clean: doc_clean
134	@test ! -f rtla || rm rtla
135	@test ! -f rtla-static || rm rtla-static
136	@test ! -f src/rtla.o || rm src/rtla.o
137	@test ! -f $(TARBALL) || rm -f $(TARBALL)
138	@rm -rf *~ $(OBJ) *.tar.$(CEXT)
139
140tarball: clean
141	rm -rf $(NAME)-$(VERSION) && mkdir $(NAME)-$(VERSION)
142	echo $(VERSION) > $(NAME)-$(VERSION)/VERSION
143	cp -r $(DIRS) $(FILES) $(NAME)-$(VERSION)
144	mkdir $(NAME)-$(VERSION)/Documentation/
145	cp -rp $(SRCTREE)/../../../Documentation/tools/rtla/* $(NAME)-$(VERSION)/Documentation/
146	tar $(TAROPTS) --exclude='*~' $(NAME)-$(VERSION)
147	rm -rf $(NAME)-$(VERSION)
148
149.PHONY: doc doc_clean doc_install
150doc:
151	$(MAKE) -C $(DOCSRC)
152
153doc_clean:
154	$(MAKE) -C $(DOCSRC) clean
155
156doc_install:
157	$(MAKE) -C $(DOCSRC) install
158