1# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2# Most of this file is copied from tools/lib/bpf/Makefile
3
4LIBPERF_VERSION = 0
5LIBPERF_PATCHLEVEL = 0
6LIBPERF_EXTRAVERSION = 1
7
8MAKEFLAGS += --no-print-directory
9
10ifeq ($(srctree),)
11srctree := $(patsubst %/,%,$(dir $(CURDIR)))
12srctree := $(patsubst %/,%,$(dir $(srctree)))
13srctree := $(patsubst %/,%,$(dir $(srctree)))
14#$(info Determined 'srctree' to be $(srctree))
15endif
16
17INSTALL = install
18
19# Use DESTDIR for installing into a different root directory.
20# This is useful for building a package. The program will be
21# installed in this directory as if it was the root directory.
22# Then the build tool can move it later.
23DESTDIR ?=
24DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
25
26include $(srctree)/tools/scripts/Makefile.include
27include $(srctree)/tools/scripts/Makefile.arch
28
29ifeq ($(LP64), 1)
30  libdir_relative = lib64
31else
32  libdir_relative = lib
33endif
34
35prefix ?=
36libdir = $(prefix)/$(libdir_relative)
37
38# Shell quotes
39libdir_SQ = $(subst ','\'',$(libdir))
40libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
41
42ifeq ("$(origin V)", "command line")
43  VERBOSE = $(V)
44endif
45ifndef VERBOSE
46  VERBOSE = 0
47endif
48
49ifeq ($(VERBOSE),1)
50  Q =
51else
52  Q = @
53endif
54
55# Set compile option CFLAGS
56ifdef EXTRA_CFLAGS
57  CFLAGS := $(EXTRA_CFLAGS)
58else
59  CFLAGS := -g -Wall
60endif
61
62INCLUDES = \
63-I$(srctree)/tools/perf/lib/include \
64-I$(srctree)/tools/lib/ \
65-I$(srctree)/tools/include \
66-I$(srctree)/tools/arch/$(SRCARCH)/include/ \
67-I$(srctree)/tools/arch/$(SRCARCH)/include/uapi \
68-I$(srctree)/tools/include/uapi
69
70# Append required CFLAGS
71override CFLAGS += $(EXTRA_WARNINGS)
72override CFLAGS += -Werror -Wall
73override CFLAGS += -fPIC
74override CFLAGS += $(INCLUDES)
75override CFLAGS += -fvisibility=hidden
76
77all:
78
79export srctree OUTPUT CC LD CFLAGS V
80export DESTDIR DESTDIR_SQ
81
82include $(srctree)/tools/build/Makefile.include
83
84VERSION_SCRIPT := libperf.map
85
86PATCHLEVEL    = $(LIBPERF_PATCHLEVEL)
87EXTRAVERSION  = $(LIBPERF_EXTRAVERSION)
88VERSION       = $(LIBPERF_VERSION).$(LIBPERF_PATCHLEVEL).$(LIBPERF_EXTRAVERSION)
89
90LIBPERF_SO := $(OUTPUT)libperf.so.$(VERSION)
91LIBPERF_A  := $(OUTPUT)libperf.a
92LIBPERF_IN := $(OUTPUT)libperf-in.o
93LIBPERF_PC := $(OUTPUT)libperf.pc
94
95LIBPERF_ALL := $(LIBPERF_A) $(OUTPUT)libperf.so*
96
97LIB_DIR := $(srctree)/tools/lib/api/
98
99ifneq ($(OUTPUT),)
100ifneq ($(subdir),)
101  API_PATH=$(OUTPUT)/../lib/api/
102else
103  API_PATH=$(OUTPUT)
104endif
105else
106  API_PATH=$(LIB_DIR)
107endif
108
109LIBAPI = $(API_PATH)libapi.a
110
111$(LIBAPI): FORCE
112	$(Q)$(MAKE) -C $(LIB_DIR) O=$(OUTPUT) $(OUTPUT)libapi.a
113
114$(LIBAPI)-clean:
115	$(call QUIET_CLEAN, libapi)
116	$(Q)$(MAKE) -C $(LIB_DIR) O=$(OUTPUT) clean >/dev/null
117
118$(LIBPERF_IN): FORCE
119	$(Q)$(MAKE) $(build)=libperf
120
121$(LIBPERF_A): $(LIBPERF_IN)
122	$(QUIET_AR)$(RM) $@ && $(AR) rcs $@ $(LIBPERF_IN)
123
124$(LIBPERF_SO): $(LIBPERF_IN) $(LIBAPI)
125	$(QUIET_LINK)$(CC) --shared -Wl,-soname,libperf.so \
126                                    -Wl,--version-script=$(VERSION_SCRIPT) $^ -o $@
127	@ln -sf $(@F) $(OUTPUT)libperf.so
128	@ln -sf $(@F) $(OUTPUT)libperf.so.$(LIBPERF_VERSION)
129
130
131libs: $(LIBPERF_A) $(LIBPERF_SO) $(LIBPERF_PC)
132
133all: fixdep
134	$(Q)$(MAKE) libs
135
136clean: $(LIBAPI)-clean
137	$(call QUIET_CLEAN, libperf) $(RM) $(LIBPERF_A) \
138                *.o *~ *.a *.so *.so.$(VERSION) *.so.$(LIBPERF_VERSION) .*.d .*.cmd LIBPERF-CFLAGS $(LIBPERF_PC)
139	$(Q)$(MAKE) -C tests clean
140
141tests: libs
142	$(Q)$(MAKE) -C tests
143	$(Q)$(MAKE) -C tests run
144
145$(LIBPERF_PC):
146	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
147		-e "s|@LIBDIR@|$(libdir_SQ)|" \
148		-e "s|@VERSION@|$(VERSION)|" \
149		< libperf.pc.template > $@
150
151define do_install_mkdir
152	if [ ! -d '$(DESTDIR_SQ)$1' ]; then             \
153		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \
154	fi
155endef
156
157define do_install
158	if [ ! -d '$(DESTDIR_SQ)$2' ]; then             \
159		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
160	fi;                                             \
161	$(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2'
162endef
163
164install_lib: libs
165	$(call QUIET_INSTALL, $(LIBPERF_ALL)) \
166		$(call do_install_mkdir,$(libdir_SQ)); \
167		cp -fpR $(LIBPERF_ALL) $(DESTDIR)$(libdir_SQ)
168
169install_headers:
170	$(call QUIET_INSTALL, headers) \
171		$(call do_install,include/perf/core.h,$(prefix)/include/perf,644); \
172		$(call do_install,include/perf/cpumap.h,$(prefix)/include/perf,644); \
173		$(call do_install,include/perf/threadmap.h,$(prefix)/include/perf,644); \
174		$(call do_install,include/perf/evlist.h,$(prefix)/include/perf,644); \
175		$(call do_install,include/perf/evsel.h,$(prefix)/include/perf,644);
176		$(call do_install,include/perf/event.h,$(prefix)/include/perf,644);
177
178install_pkgconfig: $(LIBPERF_PC)
179	$(call QUIET_INSTALL, $(LIBPERF_PC)) \
180		$(call do_install,$(LIBPERF_PC),$(libdir_SQ)/pkgconfig,644)
181
182install: install_lib install_headers install_pkgconfig
183
184FORCE:
185
186.PHONY: all install clean tests FORCE
187