162306a36Sopenharmony_ci# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
262306a36Sopenharmony_ci# Most of this file is copied from tools/lib/bpf/Makefile
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciLIBPERF_VERSION = 0
562306a36Sopenharmony_ciLIBPERF_PATCHLEVEL = 0
662306a36Sopenharmony_ciLIBPERF_EXTRAVERSION = 1
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciMAKEFLAGS += --no-print-directory
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciifeq ($(srctree),)
1162306a36Sopenharmony_cisrctree := $(patsubst %/,%,$(dir $(CURDIR)))
1262306a36Sopenharmony_cisrctree := $(patsubst %/,%,$(dir $(srctree)))
1362306a36Sopenharmony_cisrctree := $(patsubst %/,%,$(dir $(srctree)))
1462306a36Sopenharmony_ci#$(info Determined 'srctree' to be $(srctree))
1562306a36Sopenharmony_ciendif
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciINSTALL = install
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci# Use DESTDIR for installing into a different root directory.
2062306a36Sopenharmony_ci# This is useful for building a package. The program will be
2162306a36Sopenharmony_ci# installed in this directory as if it was the root directory.
2262306a36Sopenharmony_ci# Then the build tool can move it later.
2362306a36Sopenharmony_ciDESTDIR ?=
2462306a36Sopenharmony_ciDESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ciinclude $(srctree)/tools/scripts/Makefile.include
2762306a36Sopenharmony_ciinclude $(srctree)/tools/scripts/Makefile.arch
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ciifeq ($(LP64), 1)
3062306a36Sopenharmony_ci  libdir_relative = lib64
3162306a36Sopenharmony_cielse
3262306a36Sopenharmony_ci  libdir_relative = lib
3362306a36Sopenharmony_ciendif
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ciprefix ?=
3662306a36Sopenharmony_cilibdir = $(prefix)/$(libdir_relative)
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci# Shell quotes
3962306a36Sopenharmony_cilibdir_SQ = $(subst ','\'',$(libdir))
4062306a36Sopenharmony_cilibdir_relative_SQ = $(subst ','\'',$(libdir_relative))
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ciifeq ("$(origin V)", "command line")
4362306a36Sopenharmony_ci  VERBOSE = $(V)
4462306a36Sopenharmony_ciendif
4562306a36Sopenharmony_ciifndef VERBOSE
4662306a36Sopenharmony_ci  VERBOSE = 0
4762306a36Sopenharmony_ciendif
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ciifeq ($(VERBOSE),1)
5062306a36Sopenharmony_ci  Q =
5162306a36Sopenharmony_cielse
5262306a36Sopenharmony_ci  Q = @
5362306a36Sopenharmony_ciendif
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ciTEST_ARGS := $(if $(V),-v)
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci# Set compile option CFLAGS
5862306a36Sopenharmony_ciifdef EXTRA_CFLAGS
5962306a36Sopenharmony_ci  CFLAGS := $(EXTRA_CFLAGS)
6062306a36Sopenharmony_cielse
6162306a36Sopenharmony_ci  CFLAGS := -g -Wall
6262306a36Sopenharmony_ciendif
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ciINCLUDES = \
6562306a36Sopenharmony_ci-I$(srctree)/tools/lib/perf/include \
6662306a36Sopenharmony_ci-I$(srctree)/tools/lib/ \
6762306a36Sopenharmony_ci-I$(srctree)/tools/include \
6862306a36Sopenharmony_ci-I$(srctree)/tools/arch/$(SRCARCH)/include/ \
6962306a36Sopenharmony_ci-I$(srctree)/tools/arch/$(SRCARCH)/include/uapi \
7062306a36Sopenharmony_ci-I$(srctree)/tools/include/uapi
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci# Append required CFLAGS
7362306a36Sopenharmony_cioverride CFLAGS += $(EXTRA_WARNINGS)
7462306a36Sopenharmony_cioverride CFLAGS += -Werror -Wall
7562306a36Sopenharmony_cioverride CFLAGS += -fPIC
7662306a36Sopenharmony_cioverride CFLAGS += $(INCLUDES)
7762306a36Sopenharmony_cioverride CFLAGS += -fvisibility=hidden
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciall:
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciexport srctree OUTPUT CC LD CFLAGS V
8262306a36Sopenharmony_ciexport DESTDIR DESTDIR_SQ
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciinclude $(srctree)/tools/build/Makefile.include
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ciVERSION_SCRIPT := libperf.map
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciPATCHLEVEL    = $(LIBPERF_PATCHLEVEL)
8962306a36Sopenharmony_ciEXTRAVERSION  = $(LIBPERF_EXTRAVERSION)
9062306a36Sopenharmony_ciVERSION       = $(LIBPERF_VERSION).$(LIBPERF_PATCHLEVEL).$(LIBPERF_EXTRAVERSION)
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ciLIBPERF_SO := $(OUTPUT)libperf.so.$(VERSION)
9362306a36Sopenharmony_ciLIBPERF_A  := $(OUTPUT)libperf.a
9462306a36Sopenharmony_ciLIBPERF_IN := $(OUTPUT)libperf-in.o
9562306a36Sopenharmony_ciLIBPERF_PC := $(OUTPUT)libperf.pc
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciLIBPERF_ALL := $(LIBPERF_A) $(OUTPUT)libperf.so*
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ciLIB_DIR := $(srctree)/tools/lib/api/
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ciifneq ($(OUTPUT),)
10262306a36Sopenharmony_ciifneq ($(subdir),)
10362306a36Sopenharmony_ci  API_PATH=$(OUTPUT)/../lib/api/
10462306a36Sopenharmony_cielse
10562306a36Sopenharmony_ci  API_PATH=$(OUTPUT)
10662306a36Sopenharmony_ciendif
10762306a36Sopenharmony_cielse
10862306a36Sopenharmony_ci  API_PATH=$(LIB_DIR)
10962306a36Sopenharmony_ciendif
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ciLIBAPI = $(API_PATH)libapi.a
11262306a36Sopenharmony_ciexport LIBAPI
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci$(LIBAPI): FORCE
11562306a36Sopenharmony_ci	$(Q)$(MAKE) -C $(LIB_DIR) O=$(OUTPUT) $(OUTPUT)libapi.a
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci$(LIBAPI)-clean:
11862306a36Sopenharmony_ci	$(call QUIET_CLEAN, libapi)
11962306a36Sopenharmony_ci	$(Q)$(MAKE) -C $(LIB_DIR) O=$(OUTPUT) clean >/dev/null
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci$(LIBPERF_IN): FORCE
12262306a36Sopenharmony_ci	$(Q)$(MAKE) $(build)=libperf
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci$(LIBPERF_A): $(LIBPERF_IN)
12562306a36Sopenharmony_ci	$(QUIET_AR)$(RM) $@ && $(AR) rcs $@ $(LIBPERF_IN)
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci$(LIBPERF_SO): $(LIBPERF_IN) $(LIBAPI)
12862306a36Sopenharmony_ci	$(QUIET_LINK)$(CC) --shared -Wl,-soname,libperf.so \
12962306a36Sopenharmony_ci                                    -Wl,--version-script=$(VERSION_SCRIPT) $^ -o $@
13062306a36Sopenharmony_ci	@ln -sf $(@F) $(OUTPUT)libperf.so
13162306a36Sopenharmony_ci	@ln -sf $(@F) $(OUTPUT)libperf.so.$(LIBPERF_VERSION)
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_cilibs: $(LIBPERF_A) $(LIBPERF_SO) $(LIBPERF_PC)
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ciall: fixdep
13762306a36Sopenharmony_ci	$(Q)$(MAKE) libs
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ciclean: $(LIBAPI)-clean
14062306a36Sopenharmony_ci	$(call QUIET_CLEAN, libperf) $(RM) $(LIBPERF_A) \
14162306a36Sopenharmony_ci                *.o *~ *.a *.so *.so.$(VERSION) *.so.$(LIBPERF_VERSION) .*.d .*.cmd tests/*.o LIBPERF-CFLAGS $(LIBPERF_PC) \
14262306a36Sopenharmony_ci                $(TESTS_STATIC) $(TESTS_SHARED)
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ciTESTS_IN = tests-in.o
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ciTESTS_STATIC = $(OUTPUT)tests-static
14762306a36Sopenharmony_ciTESTS_SHARED = $(OUTPUT)tests-shared
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci$(TESTS_IN): FORCE
15062306a36Sopenharmony_ci	$(Q)$(MAKE) $(build)=tests
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci$(TESTS_STATIC): $(TESTS_IN) $(LIBPERF_A) $(LIBAPI)
15362306a36Sopenharmony_ci	$(QUIET_LINK)$(CC) -o $@ $^
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci$(TESTS_SHARED): $(TESTS_IN) $(LIBAPI)
15662306a36Sopenharmony_ci	$(QUIET_LINK)$(CC) -o $@ -L$(or $(OUTPUT),.) $^ -lperf
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_cimake-tests: libs $(TESTS_SHARED) $(TESTS_STATIC)
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_citests: make-tests
16162306a36Sopenharmony_ci	@echo "running static:"
16262306a36Sopenharmony_ci	@./$(TESTS_STATIC) $(TEST_ARGS)
16362306a36Sopenharmony_ci	@echo "running dynamic:"
16462306a36Sopenharmony_ci	@LD_LIBRARY_PATH=. ./$(TESTS_SHARED) $(TEST_ARGS)
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci$(LIBPERF_PC):
16762306a36Sopenharmony_ci	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
16862306a36Sopenharmony_ci		-e "s|@LIBDIR@|$(libdir_SQ)|" \
16962306a36Sopenharmony_ci		-e "s|@VERSION@|$(VERSION)|" \
17062306a36Sopenharmony_ci		< libperf.pc.template > $@
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_cidefine do_install_mkdir
17362306a36Sopenharmony_ci	if [ ! -d '$(DESTDIR_SQ)$1' ]; then             \
17462306a36Sopenharmony_ci		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \
17562306a36Sopenharmony_ci	fi
17662306a36Sopenharmony_ciendef
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_cidefine do_install
17962306a36Sopenharmony_ci	if [ ! -d '$2' ]; then             \
18062306a36Sopenharmony_ci		$(INSTALL) -d -m 755 '$2'; \
18162306a36Sopenharmony_ci	fi;                                \
18262306a36Sopenharmony_ci	$(INSTALL) $1 $(if $3,-m $3,) '$2'
18362306a36Sopenharmony_ciendef
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ciinstall_lib: libs
18662306a36Sopenharmony_ci	$(call QUIET_INSTALL, $(LIBPERF_ALL)) \
18762306a36Sopenharmony_ci		$(call do_install_mkdir,$(libdir_SQ)); \
18862306a36Sopenharmony_ci		cp -fpR $(LIBPERF_ALL) $(DESTDIR)$(libdir_SQ)
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ciHDRS := bpf_perf.h core.h cpumap.h threadmap.h evlist.h evsel.h event.h mmap.h
19162306a36Sopenharmony_ciINTERNAL_HDRS := cpumap.h evlist.h evsel.h lib.h mmap.h rc_check.h threadmap.h xyarray.h
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ciINSTALL_HDRS_PFX := $(DESTDIR)$(prefix)/include/perf
19462306a36Sopenharmony_ciINSTALL_HDRS := $(addprefix $(INSTALL_HDRS_PFX)/, $(HDRS))
19562306a36Sopenharmony_ciINSTALL_INTERNAL_HDRS_PFX := $(DESTDIR)$(prefix)/include/internal
19662306a36Sopenharmony_ciINSTALL_INTERNAL_HDRS := $(addprefix $(INSTALL_INTERNAL_HDRS_PFX)/, $(INTERNAL_HDRS))
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci$(INSTALL_HDRS): $(INSTALL_HDRS_PFX)/%.h: include/perf/%.h
19962306a36Sopenharmony_ci	$(call QUIET_INSTALL, $@) \
20062306a36Sopenharmony_ci		$(call do_install,$<,$(INSTALL_HDRS_PFX)/,644)
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci$(INSTALL_INTERNAL_HDRS): $(INSTALL_INTERNAL_HDRS_PFX)/%.h: include/internal/%.h
20362306a36Sopenharmony_ci	$(call QUIET_INSTALL, $@) \
20462306a36Sopenharmony_ci		$(call do_install,$<,$(INSTALL_INTERNAL_HDRS_PFX)/,644)
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ciinstall_headers: $(INSTALL_HDRS) $(INSTALL_INTERNAL_HDRS)
20762306a36Sopenharmony_ci	$(call QUIET_INSTALL, libperf_headers)
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ciinstall_pkgconfig: $(LIBPERF_PC)
21062306a36Sopenharmony_ci	$(call QUIET_INSTALL, $(LIBPERF_PC)) \
21162306a36Sopenharmony_ci		$(call do_install,$(LIBPERF_PC),$(DESTDIR_SQ)$(libdir_SQ)/pkgconfig,644)
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ciinstall_doc:
21462306a36Sopenharmony_ci	$(Q)$(MAKE) -C Documentation install-man install-html install-examples
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ciinstall: install_lib install_headers install_pkgconfig install_doc
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ciFORCE:
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci.PHONY: all install clean tests FORCE
221