1*1b76c13eSWang Nan# Most of this file is copied from tools/lib/traceevent/Makefile 2*1b76c13eSWang Nan 3*1b76c13eSWang NanBPF_VERSION = 0 4*1b76c13eSWang NanBPF_PATCHLEVEL = 0 5*1b76c13eSWang NanBPF_EXTRAVERSION = 1 6*1b76c13eSWang Nan 7*1b76c13eSWang NanMAKEFLAGS += --no-print-directory 8*1b76c13eSWang Nan 9*1b76c13eSWang Nan 10*1b76c13eSWang Nan# Makefiles suck: This macro sets a default value of $(2) for the 11*1b76c13eSWang Nan# variable named by $(1), unless the variable has been set by 12*1b76c13eSWang Nan# environment or command line. This is necessary for CC and AR 13*1b76c13eSWang Nan# because make sets default values, so the simpler ?= approach 14*1b76c13eSWang Nan# won't work as expected. 15*1b76c13eSWang Nandefine allow-override 16*1b76c13eSWang Nan $(if $(or $(findstring environment,$(origin $(1))),\ 17*1b76c13eSWang Nan $(findstring command line,$(origin $(1)))),,\ 18*1b76c13eSWang Nan $(eval $(1) = $(2))) 19*1b76c13eSWang Nanendef 20*1b76c13eSWang Nan 21*1b76c13eSWang Nan# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix. 22*1b76c13eSWang Nan$(call allow-override,CC,$(CROSS_COMPILE)gcc) 23*1b76c13eSWang Nan$(call allow-override,AR,$(CROSS_COMPILE)ar) 24*1b76c13eSWang Nan 25*1b76c13eSWang NanINSTALL = install 26*1b76c13eSWang Nan 27*1b76c13eSWang Nan# Use DESTDIR for installing into a different root directory. 28*1b76c13eSWang Nan# This is useful for building a package. The program will be 29*1b76c13eSWang Nan# installed in this directory as if it was the root directory. 30*1b76c13eSWang Nan# Then the build tool can move it later. 31*1b76c13eSWang NanDESTDIR ?= 32*1b76c13eSWang NanDESTDIR_SQ = '$(subst ','\'',$(DESTDIR))' 33*1b76c13eSWang Nan 34*1b76c13eSWang NanLP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1) 35*1b76c13eSWang Nanifeq ($(LP64), 1) 36*1b76c13eSWang Nan libdir_relative = lib64 37*1b76c13eSWang Nanelse 38*1b76c13eSWang Nan libdir_relative = lib 39*1b76c13eSWang Nanendif 40*1b76c13eSWang Nan 41*1b76c13eSWang Nanprefix ?= /usr/local 42*1b76c13eSWang Nanlibdir = $(prefix)/$(libdir_relative) 43*1b76c13eSWang Nanman_dir = $(prefix)/share/man 44*1b76c13eSWang Nanman_dir_SQ = '$(subst ','\'',$(man_dir))' 45*1b76c13eSWang Nan 46*1b76c13eSWang Nanexport man_dir man_dir_SQ INSTALL 47*1b76c13eSWang Nanexport DESTDIR DESTDIR_SQ 48*1b76c13eSWang Nan 49*1b76c13eSWang Naninclude ../../scripts/Makefile.include 50*1b76c13eSWang Nan 51*1b76c13eSWang Nan# copy a bit from Linux kbuild 52*1b76c13eSWang Nan 53*1b76c13eSWang Nanifeq ("$(origin V)", "command line") 54*1b76c13eSWang Nan VERBOSE = $(V) 55*1b76c13eSWang Nanendif 56*1b76c13eSWang Nanifndef VERBOSE 57*1b76c13eSWang Nan VERBOSE = 0 58*1b76c13eSWang Nanendif 59*1b76c13eSWang Nan 60*1b76c13eSWang Nanifeq ($(srctree),) 61*1b76c13eSWang Nansrctree := $(patsubst %/,%,$(dir $(shell pwd))) 62*1b76c13eSWang Nansrctree := $(patsubst %/,%,$(dir $(srctree))) 63*1b76c13eSWang Nansrctree := $(patsubst %/,%,$(dir $(srctree))) 64*1b76c13eSWang Nan#$(info Determined 'srctree' to be $(srctree)) 65*1b76c13eSWang Nanendif 66*1b76c13eSWang Nan 67*1b76c13eSWang NanFEATURE_DISPLAY = libelf libelf-getphdrnum libelf-mmap bpf 68*1b76c13eSWang NanFEATURE_TESTS = libelf bpf 69*1b76c13eSWang Nan 70*1b76c13eSWang NanINCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/arch/$(ARCH)/include/uapi -I$(srctree)/include/uapi 71*1b76c13eSWang NanFEATURE_CHECK_CFLAGS-bpf = $(INCLUDES) 72*1b76c13eSWang Nan 73*1b76c13eSWang Naninclude $(srctree)/tools/build/Makefile.feature 74*1b76c13eSWang Nan 75*1b76c13eSWang Nanexport prefix libdir src obj 76*1b76c13eSWang Nan 77*1b76c13eSWang Nan# Shell quotes 78*1b76c13eSWang Nanlibdir_SQ = $(subst ','\'',$(libdir)) 79*1b76c13eSWang Nanlibdir_relative_SQ = $(subst ','\'',$(libdir_relative)) 80*1b76c13eSWang Nanplugin_dir_SQ = $(subst ','\'',$(plugin_dir)) 81*1b76c13eSWang Nan 82*1b76c13eSWang NanLIB_FILE = libbpf.a libbpf.so 83*1b76c13eSWang Nan 84*1b76c13eSWang NanVERSION = $(BPF_VERSION) 85*1b76c13eSWang NanPATCHLEVEL = $(BPF_PATCHLEVEL) 86*1b76c13eSWang NanEXTRAVERSION = $(BPF_EXTRAVERSION) 87*1b76c13eSWang Nan 88*1b76c13eSWang NanOBJ = $@ 89*1b76c13eSWang NanN = 90*1b76c13eSWang Nan 91*1b76c13eSWang NanLIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION) 92*1b76c13eSWang Nan 93*1b76c13eSWang Nan# Set compile option CFLAGS 94*1b76c13eSWang Nanifdef EXTRA_CFLAGS 95*1b76c13eSWang Nan CFLAGS := $(EXTRA_CFLAGS) 96*1b76c13eSWang Nanelse 97*1b76c13eSWang Nan CFLAGS := -g -Wall 98*1b76c13eSWang Nanendif 99*1b76c13eSWang Nan 100*1b76c13eSWang Nanifeq ($(feature-libelf-mmap), 1) 101*1b76c13eSWang Nan override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT 102*1b76c13eSWang Nanendif 103*1b76c13eSWang Nan 104*1b76c13eSWang Nanifeq ($(feature-libelf-getphdrnum), 1) 105*1b76c13eSWang Nan override CFLAGS += -DHAVE_ELF_GETPHDRNUM_SUPPORT 106*1b76c13eSWang Nanendif 107*1b76c13eSWang Nan 108*1b76c13eSWang Nan# Append required CFLAGS 109*1b76c13eSWang Nanoverride CFLAGS += $(EXTRA_WARNINGS) 110*1b76c13eSWang Nanoverride CFLAGS += -Werror -Wall 111*1b76c13eSWang Nanoverride CFLAGS += -fPIC 112*1b76c13eSWang Nanoverride CFLAGS += $(INCLUDES) 113*1b76c13eSWang Nan 114*1b76c13eSWang Nanifeq ($(VERBOSE),1) 115*1b76c13eSWang Nan Q = 116*1b76c13eSWang Nanelse 117*1b76c13eSWang Nan Q = @ 118*1b76c13eSWang Nanendif 119*1b76c13eSWang Nan 120*1b76c13eSWang Nan# Disable command line variables (CFLAGS) overide from top 121*1b76c13eSWang Nan# level Makefile (perf), otherwise build Makefile will get 122*1b76c13eSWang Nan# the same command line setup. 123*1b76c13eSWang NanMAKEOVERRIDES= 124*1b76c13eSWang Nan 125*1b76c13eSWang Nanexport srctree OUTPUT CC LD CFLAGS V 126*1b76c13eSWang Nanbuild := -f $(srctree)/tools/build/Makefile.build dir=. obj 127*1b76c13eSWang Nan 128*1b76c13eSWang NanBPF_IN := $(OUTPUT)libbpf-in.o 129*1b76c13eSWang NanLIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE)) 130*1b76c13eSWang Nan 131*1b76c13eSWang NanCMD_TARGETS = $(LIB_FILE) 132*1b76c13eSWang Nan 133*1b76c13eSWang NanTARGETS = $(CMD_TARGETS) 134*1b76c13eSWang Nan 135*1b76c13eSWang Nanall: $(VERSION_FILES) all_cmd 136*1b76c13eSWang Nan 137*1b76c13eSWang Nanall_cmd: $(CMD_TARGETS) 138*1b76c13eSWang Nan 139*1b76c13eSWang Nan$(BPF_IN): force elfdep bpfdep 140*1b76c13eSWang Nan $(Q)$(MAKE) $(build)=libbpf 141*1b76c13eSWang Nan 142*1b76c13eSWang Nan$(OUTPUT)libbpf.so: $(BPF_IN) 143*1b76c13eSWang Nan $(QUIET_LINK)$(CC) --shared $^ -o $@ 144*1b76c13eSWang Nan 145*1b76c13eSWang Nan$(OUTPUT)libbpf.a: $(BPF_IN) 146*1b76c13eSWang Nan $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ 147*1b76c13eSWang Nan 148*1b76c13eSWang Nandefine update_dir 149*1b76c13eSWang Nan (echo $1 > $@.tmp; \ 150*1b76c13eSWang Nan if [ -r $@ ] && cmp -s $@ $@.tmp; then \ 151*1b76c13eSWang Nan rm -f $@.tmp; \ 152*1b76c13eSWang Nan else \ 153*1b76c13eSWang Nan echo ' UPDATE $@'; \ 154*1b76c13eSWang Nan mv -f $@.tmp $@; \ 155*1b76c13eSWang Nan fi); 156*1b76c13eSWang Nanendef 157*1b76c13eSWang Nan 158*1b76c13eSWang Nandefine do_install 159*1b76c13eSWang Nan if [ ! -d '$(DESTDIR_SQ)$2' ]; then \ 160*1b76c13eSWang Nan $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \ 161*1b76c13eSWang Nan fi; \ 162*1b76c13eSWang Nan $(INSTALL) $1 '$(DESTDIR_SQ)$2' 163*1b76c13eSWang Nanendef 164*1b76c13eSWang Nan 165*1b76c13eSWang Naninstall_lib: all_cmd 166*1b76c13eSWang Nan $(call QUIET_INSTALL, $(LIB_FILE)) \ 167*1b76c13eSWang Nan $(call do_install,$(LIB_FILE),$(libdir_SQ)) 168*1b76c13eSWang Nan 169*1b76c13eSWang Naninstall: install_lib 170*1b76c13eSWang Nan 171*1b76c13eSWang Nan### Cleaning rules 172*1b76c13eSWang Nan 173*1b76c13eSWang Nanconfig-clean: 174*1b76c13eSWang Nan $(call QUIET_CLEAN, config) 175*1b76c13eSWang Nan $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null 176*1b76c13eSWang Nan 177*1b76c13eSWang Nanclean: 178*1b76c13eSWang Nan $(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d \ 179*1b76c13eSWang Nan $(RM) LIBBPF-CFLAGS 180*1b76c13eSWang Nan $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP 181*1b76c13eSWang Nan 182*1b76c13eSWang Nan 183*1b76c13eSWang Nan 184*1b76c13eSWang NanPHONY += force elfdep bpfdep 185*1b76c13eSWang Nanforce: 186*1b76c13eSWang Nan 187*1b76c13eSWang Nanelfdep: 188*1b76c13eSWang Nan @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit -1 ; fi 189*1b76c13eSWang Nan 190*1b76c13eSWang Nanbpfdep: 191*1b76c13eSWang Nan @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit -1 ; fi 192*1b76c13eSWang Nan 193*1b76c13eSWang Nan# Declare the contents of the .PHONY variable as phony. We keep that 194*1b76c13eSWang Nan# information in a variable so we can use it in if_changed and friends. 195*1b76c13eSWang Nan.PHONY: $(PHONY) 196