Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 1 | # Most of this file is copied from tools/lib/traceevent/Makefile |
| 2 | |
| 3 | BPF_VERSION = 0 |
| 4 | BPF_PATCHLEVEL = 0 |
| 5 | BPF_EXTRAVERSION = 1 |
| 6 | |
| 7 | MAKEFLAGS += --no-print-directory |
| 8 | |
Naveen N. Rao | d5ef314 | 2016-01-11 13:48:00 +0000 | [diff] [blame] | 9 | ifeq ($(srctree),) |
Uwe Kleine-König | e19b7ce | 2016-11-22 09:30:26 +0100 | [diff] [blame] | 10 | srctree := $(patsubst %/,%,$(dir $(CURDIR))) |
Naveen N. Rao | d5ef314 | 2016-01-11 13:48:00 +0000 | [diff] [blame] | 11 | srctree := $(patsubst %/,%,$(dir $(srctree))) |
| 12 | srctree := $(patsubst %/,%,$(dir $(srctree))) |
| 13 | #$(info Determined 'srctree' to be $(srctree)) |
| 14 | endif |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 15 | |
| 16 | # Makefiles suck: This macro sets a default value of $(2) for the |
| 17 | # variable named by $(1), unless the variable has been set by |
| 18 | # environment or command line. This is necessary for CC and AR |
| 19 | # because make sets default values, so the simpler ?= approach |
| 20 | # won't work as expected. |
| 21 | define allow-override |
| 22 | $(if $(or $(findstring environment,$(origin $(1))),\ |
| 23 | $(findstring command line,$(origin $(1)))),,\ |
| 24 | $(eval $(1) = $(2))) |
| 25 | endef |
| 26 | |
| 27 | # Allow setting CC and AR, or setting CROSS_COMPILE as a prefix. |
| 28 | $(call allow-override,CC,$(CROSS_COMPILE)gcc) |
| 29 | $(call allow-override,AR,$(CROSS_COMPILE)ar) |
| 30 | |
| 31 | INSTALL = install |
| 32 | |
| 33 | # Use DESTDIR for installing into a different root directory. |
| 34 | # This is useful for building a package. The program will be |
| 35 | # installed in this directory as if it was the root directory. |
| 36 | # Then the build tool can move it later. |
| 37 | DESTDIR ?= |
| 38 | DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))' |
| 39 | |
Naveen N. Rao | d5ef314 | 2016-01-11 13:48:00 +0000 | [diff] [blame] | 40 | include $(srctree)/tools/scripts/Makefile.arch |
| 41 | |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 42 | ifeq ($(LP64), 1) |
| 43 | libdir_relative = lib64 |
| 44 | else |
| 45 | libdir_relative = lib |
| 46 | endif |
| 47 | |
| 48 | prefix ?= /usr/local |
| 49 | libdir = $(prefix)/$(libdir_relative) |
| 50 | man_dir = $(prefix)/share/man |
| 51 | man_dir_SQ = '$(subst ','\'',$(man_dir))' |
| 52 | |
| 53 | export man_dir man_dir_SQ INSTALL |
| 54 | export DESTDIR DESTDIR_SQ |
| 55 | |
| 56 | include ../../scripts/Makefile.include |
| 57 | |
| 58 | # copy a bit from Linux kbuild |
| 59 | |
| 60 | ifeq ("$(origin V)", "command line") |
| 61 | VERBOSE = $(V) |
| 62 | endif |
| 63 | ifndef VERBOSE |
| 64 | VERBOSE = 0 |
| 65 | endif |
| 66 | |
Arnaldo Carvalho de Melo | 65f041b | 2015-09-21 17:25:27 -0300 | [diff] [blame] | 67 | FEATURE_USER = .libbpf |
Arnaldo Carvalho de Melo | 20517cd | 2015-09-21 17:00:27 -0300 | [diff] [blame] | 68 | FEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf |
| 69 | FEATURE_DISPLAY = libelf bpf |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 70 | |
Arnaldo Carvalho de Melo | 971e827 | 2016-07-11 16:38:05 -0300 | [diff] [blame] | 71 | INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 72 | FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES) |
| 73 | |
Wang Nan | d8ad6a1 | 2015-11-24 13:36:07 +0000 | [diff] [blame] | 74 | check_feat := 1 |
| 75 | NON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help |
| 76 | ifdef MAKECMDGOALS |
| 77 | ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),) |
| 78 | check_feat := 0 |
| 79 | endif |
| 80 | endif |
| 81 | |
| 82 | ifeq ($(check_feat),1) |
Jiri Olsa | 5868360 | 2015-12-23 18:58:34 +0100 | [diff] [blame] | 83 | ifeq ($(FEATURES_DUMP),) |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 84 | include $(srctree)/tools/build/Makefile.feature |
Jiri Olsa | 5868360 | 2015-12-23 18:58:34 +0100 | [diff] [blame] | 85 | else |
| 86 | include $(FEATURES_DUMP) |
| 87 | endif |
Wang Nan | d8ad6a1 | 2015-11-24 13:36:07 +0000 | [diff] [blame] | 88 | endif |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 89 | |
| 90 | export prefix libdir src obj |
| 91 | |
| 92 | # Shell quotes |
| 93 | libdir_SQ = $(subst ','\'',$(libdir)) |
| 94 | libdir_relative_SQ = $(subst ','\'',$(libdir_relative)) |
| 95 | plugin_dir_SQ = $(subst ','\'',$(plugin_dir)) |
| 96 | |
| 97 | LIB_FILE = libbpf.a libbpf.so |
| 98 | |
| 99 | VERSION = $(BPF_VERSION) |
| 100 | PATCHLEVEL = $(BPF_PATCHLEVEL) |
| 101 | EXTRAVERSION = $(BPF_EXTRAVERSION) |
| 102 | |
| 103 | OBJ = $@ |
| 104 | N = |
| 105 | |
| 106 | LIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION) |
| 107 | |
| 108 | # Set compile option CFLAGS |
| 109 | ifdef EXTRA_CFLAGS |
| 110 | CFLAGS := $(EXTRA_CFLAGS) |
| 111 | else |
| 112 | CFLAGS := -g -Wall |
| 113 | endif |
| 114 | |
| 115 | ifeq ($(feature-libelf-mmap), 1) |
| 116 | override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT |
| 117 | endif |
| 118 | |
| 119 | ifeq ($(feature-libelf-getphdrnum), 1) |
| 120 | override CFLAGS += -DHAVE_ELF_GETPHDRNUM_SUPPORT |
| 121 | endif |
| 122 | |
| 123 | # Append required CFLAGS |
| 124 | override CFLAGS += $(EXTRA_WARNINGS) |
| 125 | override CFLAGS += -Werror -Wall |
| 126 | override CFLAGS += -fPIC |
| 127 | override CFLAGS += $(INCLUDES) |
| 128 | |
| 129 | ifeq ($(VERBOSE),1) |
| 130 | Q = |
| 131 | else |
| 132 | Q = @ |
| 133 | endif |
| 134 | |
Masahiro Yamada | 505d308 | 2017-03-09 16:16:33 -0800 | [diff] [blame] | 135 | # Disable command line variables (CFLAGS) override from top |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 136 | # level Makefile (perf), otherwise build Makefile will get |
| 137 | # the same command line setup. |
| 138 | MAKEOVERRIDES= |
| 139 | |
Jiri Olsa | 7c422f5 | 2015-09-23 12:34:02 +0200 | [diff] [blame] | 140 | all: |
| 141 | |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 142 | export srctree OUTPUT CC LD CFLAGS V |
Jiri Olsa | ab6201d | 2015-09-23 12:33:56 +0200 | [diff] [blame] | 143 | include $(srctree)/tools/build/Makefile.include |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 144 | |
| 145 | BPF_IN := $(OUTPUT)libbpf-in.o |
| 146 | LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE)) |
| 147 | |
| 148 | CMD_TARGETS = $(LIB_FILE) |
| 149 | |
| 150 | TARGETS = $(CMD_TARGETS) |
| 151 | |
Jiri Olsa | 7c422f5 | 2015-09-23 12:34:02 +0200 | [diff] [blame] | 152 | all: fixdep $(VERSION_FILES) all_cmd |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 153 | |
| 154 | all_cmd: $(CMD_TARGETS) |
| 155 | |
| 156 | $(BPF_IN): force elfdep bpfdep |
Daniel Díaz | f15d5e6 | 2017-08-15 11:33:30 -0500 | [diff] [blame] | 157 | @(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \ |
Arnaldo Carvalho de Melo | 971e827 | 2016-07-11 16:38:05 -0300 | [diff] [blame] | 158 | (diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \ |
Ingo Molnar | 8255e1e | 2017-07-30 11:51:30 +0200 | [diff] [blame] | 159 | echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true |
Daniel Díaz | f15d5e6 | 2017-08-15 11:33:30 -0500 | [diff] [blame] | 160 | @(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \ |
Arnaldo Carvalho de Melo | 971e827 | 2016-07-11 16:38:05 -0300 | [diff] [blame] | 161 | (diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \ |
Ingo Molnar | 8255e1e | 2017-07-30 11:51:30 +0200 | [diff] [blame] | 162 | echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf_common.h' differs from latest version at 'include/uapi/linux/bpf_common.h'" >&2 )) || true |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 163 | $(Q)$(MAKE) $(build)=libbpf |
| 164 | |
| 165 | $(OUTPUT)libbpf.so: $(BPF_IN) |
| 166 | $(QUIET_LINK)$(CC) --shared $^ -o $@ |
| 167 | |
| 168 | $(OUTPUT)libbpf.a: $(BPF_IN) |
| 169 | $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ |
| 170 | |
| 171 | define update_dir |
| 172 | (echo $1 > $@.tmp; \ |
| 173 | if [ -r $@ ] && cmp -s $@ $@.tmp; then \ |
| 174 | rm -f $@.tmp; \ |
| 175 | else \ |
| 176 | echo ' UPDATE $@'; \ |
| 177 | mv -f $@.tmp $@; \ |
| 178 | fi); |
| 179 | endef |
| 180 | |
| 181 | define do_install |
| 182 | if [ ! -d '$(DESTDIR_SQ)$2' ]; then \ |
| 183 | $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \ |
| 184 | fi; \ |
| 185 | $(INSTALL) $1 '$(DESTDIR_SQ)$2' |
| 186 | endef |
| 187 | |
| 188 | install_lib: all_cmd |
| 189 | $(call QUIET_INSTALL, $(LIB_FILE)) \ |
| 190 | $(call do_install,$(LIB_FILE),$(libdir_SQ)) |
| 191 | |
Jakub Kicinski | eb54e52 | 2017-07-25 11:17:11 -0700 | [diff] [blame] | 192 | install_headers: |
| 193 | $(call QUIET_INSTALL, headers) \ |
| 194 | $(call do_install,bpf.h,$(prefix)/include/bpf,644) |
| 195 | |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 196 | install: install_lib |
| 197 | |
| 198 | ### Cleaning rules |
| 199 | |
| 200 | config-clean: |
| 201 | $(call QUIET_CLEAN, config) |
| 202 | $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null |
| 203 | |
| 204 | clean: |
Jiri Olsa | 22992a3 | 2016-01-11 11:54:49 +0100 | [diff] [blame] | 205 | $(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d .*.cmd \ |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 206 | $(RM) LIBBPF-CFLAGS |
Yunlong Song | 0c77c04 | 2015-11-05 14:56:34 +0800 | [diff] [blame] | 207 | $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 208 | |
| 209 | |
| 210 | |
| 211 | PHONY += force elfdep bpfdep |
| 212 | force: |
| 213 | |
| 214 | elfdep: |
| 215 | @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit -1 ; fi |
| 216 | |
| 217 | bpfdep: |
| 218 | @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit -1 ; fi |
| 219 | |
| 220 | # Declare the contents of the .PHONY variable as phony. We keep that |
| 221 | # information in a variable so we can use it in if_changed and friends. |
| 222 | .PHONY: $(PHONY) |