131435049SJiri Olsa# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
231435049SJiri Olsa# Most of this file is copied from tools/lib/bpf/Makefile
331435049SJiri Olsa
447f9bcccSJiri OlsaLIBPERF_VERSION = 0
547f9bcccSJiri OlsaLIBPERF_PATCHLEVEL = 0
647f9bcccSJiri OlsaLIBPERF_EXTRAVERSION = 1
747f9bcccSJiri Olsa
831435049SJiri OlsaMAKEFLAGS += --no-print-directory
931435049SJiri Olsa
1031435049SJiri Olsaifeq ($(srctree),)
1131435049SJiri Olsasrctree := $(patsubst %/,%,$(dir $(CURDIR)))
1231435049SJiri Olsasrctree := $(patsubst %/,%,$(dir $(srctree)))
1331435049SJiri Olsasrctree := $(patsubst %/,%,$(dir $(srctree)))
1431435049SJiri Olsa#$(info Determined 'srctree' to be $(srctree))
1531435049SJiri Olsaendif
1631435049SJiri Olsa
170a64d709SJiri OlsaINSTALL = install
180a64d709SJiri Olsa
190a64d709SJiri Olsa# Use DESTDIR for installing into a different root directory.
200a64d709SJiri Olsa# This is useful for building a package. The program will be
210a64d709SJiri Olsa# installed in this directory as if it was the root directory.
220a64d709SJiri Olsa# Then the build tool can move it later.
230a64d709SJiri OlsaDESTDIR ?=
240a64d709SJiri OlsaDESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
250a64d709SJiri Olsa
2631435049SJiri Olsainclude $(srctree)/tools/scripts/Makefile.include
2731435049SJiri Olsainclude $(srctree)/tools/scripts/Makefile.arch
2831435049SJiri Olsa
290a64d709SJiri Olsaifeq ($(LP64), 1)
300a64d709SJiri Olsa  libdir_relative = lib64
310a64d709SJiri Olsaelse
320a64d709SJiri Olsa  libdir_relative = lib
330a64d709SJiri Olsaendif
340a64d709SJiri Olsa
350a64d709SJiri Olsaprefix ?=
360a64d709SJiri Olsalibdir = $(prefix)/$(libdir_relative)
370a64d709SJiri Olsa
380a64d709SJiri Olsa# Shell quotes
390a64d709SJiri Olsalibdir_SQ = $(subst ','\'',$(libdir))
400a64d709SJiri Olsalibdir_relative_SQ = $(subst ','\'',$(libdir_relative))
410a64d709SJiri Olsa
4231435049SJiri Olsaifeq ("$(origin V)", "command line")
4331435049SJiri Olsa  VERBOSE = $(V)
4431435049SJiri Olsaendif
4531435049SJiri Olsaifndef VERBOSE
4631435049SJiri Olsa  VERBOSE = 0
4731435049SJiri Olsaendif
4831435049SJiri Olsa
4931435049SJiri Olsaifeq ($(VERBOSE),1)
5031435049SJiri Olsa  Q =
5131435049SJiri Olsaelse
5231435049SJiri Olsa  Q = @
5331435049SJiri Olsaendif
5431435049SJiri Olsa
553d970601SJiri OlsaTEST_ARGS := $(if $(V),-v)
563d970601SJiri Olsa
5731435049SJiri Olsa# Set compile option CFLAGS
5831435049SJiri Olsaifdef EXTRA_CFLAGS
5931435049SJiri Olsa  CFLAGS := $(EXTRA_CFLAGS)
6031435049SJiri Olsaelse
6131435049SJiri Olsa  CFLAGS := -g -Wall
6231435049SJiri Olsaendif
6331435049SJiri Olsa
64d80a5540SJiri OlsaINCLUDES = \
653ce311afSJiri Olsa-I$(srctree)/tools/lib/perf/include \
66d80a5540SJiri Olsa-I$(srctree)/tools/lib/ \
67d80a5540SJiri Olsa-I$(srctree)/tools/include \
68d80a5540SJiri Olsa-I$(srctree)/tools/arch/$(SRCARCH)/include/ \
69d80a5540SJiri Olsa-I$(srctree)/tools/arch/$(SRCARCH)/include/uapi \
70d80a5540SJiri Olsa-I$(srctree)/tools/include/uapi
7131435049SJiri Olsa
7231435049SJiri Olsa# Append required CFLAGS
7331435049SJiri Olsaoverride CFLAGS += $(EXTRA_WARNINGS)
7431435049SJiri Olsaoverride CFLAGS += -Werror -Wall
7531435049SJiri Olsaoverride CFLAGS += -fPIC
7631435049SJiri Olsaoverride CFLAGS += $(INCLUDES)
7731435049SJiri Olsaoverride CFLAGS += -fvisibility=hidden
7831435049SJiri Olsa
7931435049SJiri Olsaall:
8031435049SJiri Olsa
8131435049SJiri Olsaexport srctree OUTPUT CC LD CFLAGS V
820a64d709SJiri Olsaexport DESTDIR DESTDIR_SQ
830a64d709SJiri Olsa
8431435049SJiri Olsainclude $(srctree)/tools/build/Makefile.include
8531435049SJiri Olsa
8647f9bcccSJiri OlsaVERSION_SCRIPT := libperf.map
8747f9bcccSJiri Olsa
8847f9bcccSJiri OlsaPATCHLEVEL    = $(LIBPERF_PATCHLEVEL)
8947f9bcccSJiri OlsaEXTRAVERSION  = $(LIBPERF_EXTRAVERSION)
9047f9bcccSJiri OlsaVERSION       = $(LIBPERF_VERSION).$(LIBPERF_PATCHLEVEL).$(LIBPERF_EXTRAVERSION)
9147f9bcccSJiri Olsa
9247f9bcccSJiri OlsaLIBPERF_SO := $(OUTPUT)libperf.so.$(VERSION)
9331435049SJiri OlsaLIBPERF_A  := $(OUTPUT)libperf.a
9431435049SJiri OlsaLIBPERF_IN := $(OUTPUT)libperf-in.o
950a64d709SJiri OlsaLIBPERF_PC := $(OUTPUT)libperf.pc
960a64d709SJiri Olsa
970a64d709SJiri OlsaLIBPERF_ALL := $(LIBPERF_A) $(OUTPUT)libperf.so*
9831435049SJiri Olsa
99d80a5540SJiri OlsaLIB_DIR := $(srctree)/tools/lib/api/
100d80a5540SJiri Olsa
101d80a5540SJiri Olsaifneq ($(OUTPUT),)
102d80a5540SJiri Olsaifneq ($(subdir),)
103d80a5540SJiri Olsa  API_PATH=$(OUTPUT)/../lib/api/
104d80a5540SJiri Olsaelse
105d80a5540SJiri Olsa  API_PATH=$(OUTPUT)
106d80a5540SJiri Olsaendif
107d80a5540SJiri Olsaelse
108d80a5540SJiri Olsa  API_PATH=$(LIB_DIR)
109d80a5540SJiri Olsaendif
110d80a5540SJiri Olsa
111d80a5540SJiri OlsaLIBAPI = $(API_PATH)libapi.a
112395e62cdSJiri Olsaexport LIBAPI
113d80a5540SJiri Olsa
114d80a5540SJiri Olsa$(LIBAPI): FORCE
115d80a5540SJiri Olsa	$(Q)$(MAKE) -C $(LIB_DIR) O=$(OUTPUT) $(OUTPUT)libapi.a
116d80a5540SJiri Olsa
117d80a5540SJiri Olsa$(LIBAPI)-clean:
118d80a5540SJiri Olsa	$(call QUIET_CLEAN, libapi)
119d80a5540SJiri Olsa	$(Q)$(MAKE) -C $(LIB_DIR) O=$(OUTPUT) clean >/dev/null
120d80a5540SJiri Olsa
12131435049SJiri Olsa$(LIBPERF_IN): FORCE
12231435049SJiri Olsa	$(Q)$(MAKE) $(build)=libperf
12331435049SJiri Olsa
12431435049SJiri Olsa$(LIBPERF_A): $(LIBPERF_IN)
12531435049SJiri Olsa	$(QUIET_AR)$(RM) $@ && $(AR) rcs $@ $(LIBPERF_IN)
12631435049SJiri Olsa
127d80a5540SJiri Olsa$(LIBPERF_SO): $(LIBPERF_IN) $(LIBAPI)
12847f9bcccSJiri Olsa	$(QUIET_LINK)$(CC) --shared -Wl,-soname,libperf.so \
12947f9bcccSJiri Olsa                                    -Wl,--version-script=$(VERSION_SCRIPT) $^ -o $@
13047f9bcccSJiri Olsa	@ln -sf $(@F) $(OUTPUT)libperf.so
13147f9bcccSJiri Olsa	@ln -sf $(@F) $(OUTPUT)libperf.so.$(LIBPERF_VERSION)
13247f9bcccSJiri Olsa
13331435049SJiri Olsa
1340a64d709SJiri Olsalibs: $(LIBPERF_A) $(LIBPERF_SO) $(LIBPERF_PC)
13531435049SJiri Olsa
13631435049SJiri Olsaall: fixdep
13731435049SJiri Olsa	$(Q)$(MAKE) libs
13831435049SJiri Olsa
139d80a5540SJiri Olsaclean: $(LIBAPI)-clean
14031435049SJiri Olsa	$(call QUIET_CLEAN, libperf) $(RM) $(LIBPERF_A) \
1413d970601SJiri Olsa                *.o *~ *.a *.so *.so.$(VERSION) *.so.$(LIBPERF_VERSION) .*.d .*.cmd tests/*.o LIBPERF-CFLAGS $(LIBPERF_PC) \
1423d970601SJiri Olsa                $(TESTS_STATIC) $(TESTS_SHARED)
1433d970601SJiri Olsa
1443d970601SJiri OlsaTESTS_IN = tests-in.o
1453d970601SJiri Olsa
1463d970601SJiri OlsaTESTS_STATIC = $(OUTPUT)tests-static
1473d970601SJiri OlsaTESTS_SHARED = $(OUTPUT)tests-shared
1483d970601SJiri Olsa
1493d970601SJiri Olsa$(TESTS_IN): FORCE
1503d970601SJiri Olsa	$(Q)$(MAKE) $(build)=tests
1513d970601SJiri Olsa
1523d970601SJiri Olsa$(TESTS_STATIC): $(TESTS_IN) $(LIBPERF_A) $(LIBAPI)
1533d970601SJiri Olsa	$(QUIET_LINK)$(CC) -o $@ $^
1543d970601SJiri Olsa
1553d970601SJiri Olsa$(TESTS_SHARED): $(TESTS_IN) $(LIBAPI)
1563d970601SJiri Olsa	$(QUIET_LINK)$(CC) -o $@ -L$(if $(OUTPUT),$(OUTPUT),.) $^ -lperf
1573d970601SJiri Olsa
1583d970601SJiri Olsamake-tests: libs $(TESTS_SHARED) $(TESTS_STATIC)
1596a94b52aSJiri Olsa
1603d970601SJiri Olsatests: make-tests
1613d970601SJiri Olsa	@echo "running static:"
1623d970601SJiri Olsa	@./$(TESTS_STATIC) $(TEST_ARGS)
1633d970601SJiri Olsa	@echo "running dynamic:"
1643d970601SJiri Olsa	@LD_LIBRARY_PATH=. ./$(TESTS_SHARED) $(TEST_ARGS)
1650a64d709SJiri Olsa
1660a64d709SJiri Olsa$(LIBPERF_PC):
1670a64d709SJiri Olsa	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
1680a64d709SJiri Olsa		-e "s|@LIBDIR@|$(libdir_SQ)|" \
1690a64d709SJiri Olsa		-e "s|@VERSION@|$(VERSION)|" \
1700a64d709SJiri Olsa		< libperf.pc.template > $@
1710a64d709SJiri Olsa
1720a64d709SJiri Olsadefine do_install_mkdir
1730a64d709SJiri Olsa	if [ ! -d '$(DESTDIR_SQ)$1' ]; then             \
1740a64d709SJiri Olsa		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \
1750a64d709SJiri Olsa	fi
1760a64d709SJiri Olsaendef
1770a64d709SJiri Olsa
1780a64d709SJiri Olsadefine do_install
1790a64d709SJiri Olsa	if [ ! -d '$(DESTDIR_SQ)$2' ]; then             \
1800a64d709SJiri Olsa		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
1810a64d709SJiri Olsa	fi;                                             \
1820a64d709SJiri Olsa	$(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2'
1830a64d709SJiri Olsaendef
1840a64d709SJiri Olsa
1850a64d709SJiri Olsainstall_lib: libs
1860a64d709SJiri Olsa	$(call QUIET_INSTALL, $(LIBPERF_ALL)) \
1870a64d709SJiri Olsa		$(call do_install_mkdir,$(libdir_SQ)); \
1880a64d709SJiri Olsa		cp -fpR $(LIBPERF_ALL) $(DESTDIR)$(libdir_SQ)
1890a64d709SJiri Olsa
1900a64d709SJiri Olsainstall_headers:
1910a64d709SJiri Olsa	$(call QUIET_INSTALL, headers) \
1920a64d709SJiri Olsa		$(call do_install,include/perf/core.h,$(prefix)/include/perf,644); \
1930a64d709SJiri Olsa		$(call do_install,include/perf/cpumap.h,$(prefix)/include/perf,644); \
1940a64d709SJiri Olsa		$(call do_install,include/perf/threadmap.h,$(prefix)/include/perf,644); \
1950a64d709SJiri Olsa		$(call do_install,include/perf/evlist.h,$(prefix)/include/perf,644); \
1967728fa0cSJiri Olsa		$(call do_install,include/perf/evsel.h,$(prefix)/include/perf,644); \
1977728fa0cSJiri Olsa		$(call do_install,include/perf/event.h,$(prefix)/include/perf,644); \
1987728fa0cSJiri Olsa		$(call do_install,include/perf/mmap.h,$(prefix)/include/perf,644);
1990a64d709SJiri Olsa
2000a64d709SJiri Olsainstall_pkgconfig: $(LIBPERF_PC)
2010a64d709SJiri Olsa	$(call QUIET_INSTALL, $(LIBPERF_PC)) \
2020a64d709SJiri Olsa		$(call do_install,$(LIBPERF_PC),$(libdir_SQ)/pkgconfig,644)
2030a64d709SJiri Olsa
20481de3bf3SJiri Olsainstall_doc:
20581de3bf3SJiri Olsa	$(Q)$(MAKE) -C Documentation install-man install-html install-examples
20681de3bf3SJiri Olsa
20781de3bf3SJiri Olsainstall: install_lib install_headers install_pkgconfig install_doc
20831435049SJiri Olsa
20931435049SJiri OlsaFORCE:
21031435049SJiri Olsa
2116a94b52aSJiri Olsa.PHONY: all install clean tests FORCE
212