1# SPDX-License-Identifier: GPL-2.0 2include ../../scripts/Makefile.include 3 4LIBUNWIND_ARCHS:=aarch64 arm loongarch64 mips ppc32 ppc64 riscv s390x x86 x86_64 5 6FILES= \ 7 test-all.bin \ 8 test-backtrace.bin \ 9 test-bionic.bin \ 10 test-libdw.bin \ 11 test-eventfd.bin \ 12 test-fortify-source.bin \ 13 test-glibc.bin \ 14 test-gtk2.bin \ 15 test-gtk2-infobar.bin \ 16 test-hello.bin \ 17 test-libbfd.bin \ 18 test-libbfd-threadsafe.bin \ 19 test-disassembler-four-args.bin \ 20 test-disassembler-init-styled.bin \ 21 test-reallocarray.bin \ 22 test-libbfd-liberty.bin \ 23 test-libbfd-liberty-z.bin \ 24 test-cplus-demangle.bin \ 25 test-cxa-demangle.bin \ 26 test-libcap.bin \ 27 test-libelf.bin \ 28 test-libelf-getphdrnum.bin \ 29 test-libelf-gelf_getnote.bin \ 30 test-libelf-getshdrstrndx.bin \ 31 test-libelf-zstd.bin \ 32 test-libdebuginfod.bin \ 33 test-libnuma.bin \ 34 test-numa_num_possible_cpus.bin \ 35 test-libperl.bin \ 36 test-libpython.bin \ 37 test-libslang.bin \ 38 test-libtraceevent.bin \ 39 test-libcpupower.bin \ 40 test-libtracefs.bin \ 41 test-libunwind.bin \ 42 test-libunwind-debug-frame.bin \ 43 $(foreach arch,$(LIBUNWIND_ARCHS),test-libunwind-$(arch).bin test-libunwind-debug-frame-$(arch).bin) \ 44 test-pthread-attr-setaffinity-np.bin \ 45 test-pthread-barrier.bin \ 46 test-stackprotector-all.bin \ 47 test-timerfd.bin \ 48 test-babeltrace2-ctf-writer.bin \ 49 test-libcapstone.bin \ 50 test-libcheck.bin \ 51 test-compile-32.bin \ 52 test-compile-x32.bin \ 53 test-zlib.bin \ 54 test-lzma.bin \ 55 test-bpf.bin \ 56 test-libbpf.bin \ 57 test-sdt.bin \ 58 test-cxx.bin \ 59 test-gettid.bin \ 60 test-jvmti.bin \ 61 test-jvmti-cmlr.bin \ 62 test-scandirat.bin \ 63 test-sched_getcpu.bin \ 64 test-setns.bin \ 65 test-libopencsd.bin \ 66 test-clang.bin \ 67 test-llvm.bin \ 68 test-llvm-perf.bin \ 69 test-libaio.bin \ 70 test-libzstd.bin \ 71 test-clang-bpf-co-re.bin \ 72 test-file-handle.bin \ 73 test-libpfm4.bin \ 74 test-rust.bin \ 75 test-libopenssl.bin \ 76 test-bpftool-skeletons.bin 77 78FILES := $(addprefix $(OUTPUT),$(FILES)) 79 80# Some distros provide the command $(CROSS_COMPILE)pkg-config for 81# searching packges installed with Multiarch. Use it for cross 82# compilation if it is existed. 83ifneq (, $(shell which $(CROSS_COMPILE)pkg-config)) 84 PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config 85else 86 PKG_CONFIG ?= pkg-config 87 88 # PKG_CONFIG_PATH or PKG_CONFIG_LIBDIR, alongside PKG_CONFIG_SYSROOT_DIR 89 # for modified system root, are required for the cross compilation. 90 # If these PKG_CONFIG environment variables are not set, Multiarch library 91 # paths are used instead. 92 ifdef CROSS_COMPILE 93 ifeq ($(PKG_CONFIG_LIBDIR)$(PKG_CONFIG_PATH)$(PKG_CONFIG_SYSROOT_DIR),) 94 CROSS_ARCH = $(notdir $(CROSS_COMPILE:%-=%)) 95 PKG_CONFIG_LIBDIR := /usr/local/$(CROSS_ARCH)/lib/pkgconfig/ 96 PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/lib/$(CROSS_ARCH)/pkgconfig/ 97 PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/lib/$(CROSS_ARCH)/pkgconfig/ 98 PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/share/pkgconfig/ 99 PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/share/pkgconfig/ 100 export PKG_CONFIG_LIBDIR 101 endif 102 endif 103endif 104 105ifeq ($(findstring -static,${LDFLAGS}),-static) 106 PKG_CONFIG += --static 107endif 108 109all: $(FILES) 110 111__BUILD = $(CC) $(CFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.c,$(@F)) $(LDFLAGS) 112 BUILD = $(__BUILD) > $(@:.bin=.make.output) 2>&1 113 BUILD_BFD = $(BUILD) -DPACKAGE='"perf"' -lbfd -ldl 114 BUILD_ALL = $(BUILD) -fstack-protector-all -O2 -D_FORTIFY_SOURCE=2 -ldw -lelf -lnuma -lelf -lslang \ 115 $(FLAGS_PERL_EMBED) $(FLAGS_PYTHON_EMBED) -ldl -lz -llzma -lzstd \ 116 $(shell $(PKG_CONFIG) --libs --cflags openssl 2>/dev/null) 117 118__BUILDXX = $(CXX) $(CXXFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.cpp,$(@F)) $(LDFLAGS) 119 BUILDXX = $(__BUILDXX) > $(@:.bin=.make.output) 2>&1 120 121############################### 122 123$(OUTPUT)test-all.bin: 124 $(BUILD_ALL) 125 126$(OUTPUT)test-hello.bin: 127 $(BUILD) 128 129$(OUTPUT)test-pthread-attr-setaffinity-np.bin: 130 $(BUILD) -D_GNU_SOURCE -lpthread 131 132$(OUTPUT)test-pthread-barrier.bin: 133 $(BUILD) -lpthread 134 135$(OUTPUT)test-stackprotector-all.bin: 136 $(BUILD) -fstack-protector-all 137 138$(OUTPUT)test-fortify-source.bin: 139 $(BUILD) -O2 -D_FORTIFY_SOURCE=2 140 141$(OUTPUT)test-bionic.bin: 142 $(BUILD) 143 144$(OUTPUT)test-libcap.bin: 145 $(BUILD) -lcap 146 147$(OUTPUT)test-libelf.bin: 148 $(BUILD) -lelf 149 150$(OUTPUT)test-eventfd.bin: 151 $(BUILD) 152 153$(OUTPUT)test-glibc.bin: 154 $(BUILD) 155 156$(OUTPUT)test-scandirat.bin: 157 $(BUILD) 158 159$(OUTPUT)test-sched_getcpu.bin: 160 $(BUILD) 161 162$(OUTPUT)test-setns.bin: 163 $(BUILD) 164 165$(OUTPUT)test-libopencsd.bin: 166 $(BUILD) # -lopencsd_c_api -lopencsd provided by 167 # $(FEATURE_CHECK_LDFLAGS-libopencsd) 168 169DWLIBS := -ldw 170ifeq ($(findstring -static,${LDFLAGS}),-static) 171 DWLIBS += -lelf -lz -llzma -lbz2 -lzstd 172 173 LIBDW_VERSION := $(shell $(PKG_CONFIG) --modversion libdw).0.0 174 LIBDW_VERSION_1 := $(word 1, $(subst ., ,$(LIBDW_VERSION))) 175 LIBDW_VERSION_2 := $(word 2, $(subst ., ,$(LIBDW_VERSION))) 176 177 # Elfutils merged libebl.a into libdw.a starting from version 0.177, 178 # Link libebl.a only if libdw is older than this version. 179 ifeq ($(shell test $(LIBDW_VERSION_2) -lt 177; echo $$?),0) 180 DWLIBS += -lebl 181 endif 182 183 # Must put -ldl after -lebl for dependency 184 DWARFLIBS += -ldl 185endif 186 187$(OUTPUT)test-libdw.bin: 188 $(BUILD) $(DWLIBS) 189 190$(OUTPUT)test-libelf-getphdrnum.bin: 191 $(BUILD) -lelf 192 193$(OUTPUT)test-libelf-gelf_getnote.bin: 194 $(BUILD) -lelf 195 196$(OUTPUT)test-libelf-getshdrstrndx.bin: 197 $(BUILD) -lelf 198 199$(OUTPUT)test-libelf-zstd.bin: 200 $(BUILD) -lelf -lz -lzstd 201 202$(OUTPUT)test-libdebuginfod.bin: 203 $(BUILD) -ldebuginfod 204 205$(OUTPUT)test-libnuma.bin: 206 $(BUILD) -lnuma 207 208$(OUTPUT)test-numa_num_possible_cpus.bin: 209 $(BUILD) -lnuma 210 211$(OUTPUT)test-libunwind.bin: 212 $(BUILD) -include libunwind.h -lelf -llzma -lunwind 213 214$(OUTPUT)test-libunwind-debug-frame.bin: 215 $(BUILD) -include libunwind.h -lelf -llzma -lunwind 216 217define LIBUNWIND_RULE 218$$(OUTPUT)test-libunwind-$(1).bin: 219 $$(CC) $$(CFLAGS) -MD -Wall -Werror -include libunwind-$(1).h -o $$@ \ 220 test-libunwind.c $$(LDFLAGS) -lelf -llzma -lunwind-$(1) \ 221 > $$(@:.bin=.make.output) 2>&1 222 223$$(OUTPUT)test-libunwind-debug-frame-$(1).bin: 224 $$(CC) $$(CFLAGS) -MD -Wall -Werror -include libunwind-$(1).h -o $$@ \ 225 test-libunwind-debug-frame.c $$(LDFLAGS) -lelf -llzma -lunwind-$(1) \ 226 > $$(@:.bin=.make.output) 2>&1 227 228endef 229$(foreach arch,$(LIBUNWIND_ARCHS), \ 230 $(eval $(call LIBUNWIND_RULE,$(arch))) \ 231) 232 233$(OUTPUT)test-libslang.bin: 234 $(BUILD) -lslang 235 236$(OUTPUT)test-libtraceevent.bin: 237 $(BUILD) -ltraceevent 238 239$(OUTPUT)test-libcpupower.bin: 240 $(BUILD) -lcpupower 241 242$(OUTPUT)test-libtracefs.bin: 243 $(BUILD) $(shell $(PKG_CONFIG) --cflags libtracefs 2>/dev/null) -ltracefs 244 245$(OUTPUT)test-gtk2.bin: 246 $(BUILD) $(shell $(PKG_CONFIG) --libs --cflags gtk+-2.0 2>/dev/null) -Wno-deprecated-declarations 247 248$(OUTPUT)test-gtk2-infobar.bin: 249 $(BUILD) $(shell $(PKG_CONFIG) --libs --cflags gtk+-2.0 2>/dev/null) 250 251grep-libs = $(filter -l%,$(1)) 252strip-libs = $(filter-out -l%,$(1)) 253 254PERL_EMBED_LDOPTS = $(shell perl -MExtUtils::Embed -e ldopts 2>/dev/null) 255PERL_EMBED_LDFLAGS = $(call strip-libs,$(PERL_EMBED_LDOPTS)) 256PERL_EMBED_LIBADD = $(call grep-libs,$(PERL_EMBED_LDOPTS)) 257PERL_EMBED_CCOPTS = $(shell perl -MExtUtils::Embed -e ccopts 2>/dev/null) 258FLAGS_PERL_EMBED=$(PERL_EMBED_CCOPTS) $(PERL_EMBED_LDOPTS) 259 260ifeq ($(CC_NO_CLANG), 0) 261 PERL_EMBED_LDOPTS := $(filter-out -specs=%,$(PERL_EMBED_LDOPTS)) 262 PERL_EMBED_CCOPTS := $(filter-out -flto=auto -ffat-lto-objects, $(PERL_EMBED_CCOPTS)) 263 PERL_EMBED_CCOPTS := $(filter-out -specs=%,$(PERL_EMBED_CCOPTS)) 264 FLAGS_PERL_EMBED += -Wno-compound-token-split-by-macro 265endif 266 267$(OUTPUT)test-libperl.bin: 268 $(BUILD) $(FLAGS_PERL_EMBED) 269 270$(OUTPUT)test-libpython.bin: 271 $(BUILD) $(FLAGS_PYTHON_EMBED) 272 273$(OUTPUT)test-libbfd.bin: 274 $(BUILD_BFD) 275 276$(OUTPUT)test-libbfd-threadsafe.bin: 277 $(BUILD_BFD) || $(BUILD_BFD) -liberty || $(BUILD_BFD) -liberty -lz 278 279$(OUTPUT)test-disassembler-four-args.bin: 280 $(BUILD_BFD) -lopcodes || $(BUILD_BFD) -lopcodes -liberty || \ 281 $(BUILD_BFD) -lopcodes -liberty -lz 282 283$(OUTPUT)test-disassembler-init-styled.bin: 284 $(BUILD_BFD) -lopcodes || $(BUILD_BFD) -lopcodes -liberty || \ 285 $(BUILD_BFD) -lopcodes -liberty -lz 286 287$(OUTPUT)test-reallocarray.bin: 288 $(BUILD) 289 290$(OUTPUT)test-libbfd-liberty.bin: 291 $(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty 292 293$(OUTPUT)test-libbfd-liberty-z.bin: 294 $(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty -lz 295 296$(OUTPUT)test-cplus-demangle.bin: 297 $(BUILD) -liberty 298 299$(OUTPUT)test-cxa-demangle.bin: 300 $(BUILDXX) 301 302$(OUTPUT)test-backtrace.bin: 303 $(BUILD) 304 305$(OUTPUT)test-timerfd.bin: 306 $(BUILD) 307 308$(OUTPUT)test-babeltrace2-ctf-writer.bin: 309 $(BUILD) # -lbabeltrace2-ctf-writer provided by $(FEATURE_CHECK_LDFLAGS-babeltrace2-ctf-writer) 310 311$(OUTPUT)test-libcapstone.bin: 312 $(BUILD) # -lcapstone provided by $(FEATURE_CHECK_LDFLAGS-libcapstone) 313 314$(OUTPUT)test-libcheck.bin: 315 $(BUILD) # -lcheck is provided by $(FEATURE_CHECK_LDFLAGS-libcheck) 316 317$(OUTPUT)test-compile-32.bin: 318 $(CC) -m32 -Wall -Werror -o $@ test-compile.c 319 320$(OUTPUT)test-compile-x32.bin: 321 $(CC) -mx32 -Wall -Werror -o $@ test-compile.c 322 323$(OUTPUT)test-zlib.bin: 324 $(BUILD) -lz 325 326$(OUTPUT)test-lzma.bin: 327 $(BUILD) -llzma 328 329$(OUTPUT)test-bpf.bin: 330 $(BUILD) 331 332$(OUTPUT)test-libbpf.bin: 333 $(BUILD) -lbpf 334 335$(OUTPUT)test-sdt.bin: 336 $(BUILD) 337 338$(OUTPUT)test-cxx.bin: 339 $(BUILDXX) -std=gnu++11 340 341$(OUTPUT)test-gettid.bin: 342 $(BUILD) 343 344$(OUTPUT)test-jvmti.bin: 345 $(BUILD) 346 347$(OUTPUT)test-jvmti-cmlr.bin: 348 $(BUILD) 349 350$(OUTPUT)test-llvm.bin: 351 $(BUILDXX) -std=gnu++17 \ 352 -I$(shell $(LLVM_CONFIG) --includedir) \ 353 -L$(shell $(LLVM_CONFIG) --libdir) \ 354 $(shell $(LLVM_CONFIG) --libs Core BPF) \ 355 $(shell $(LLVM_CONFIG) --system-libs) \ 356 > $(@:.bin=.make.output) 2>&1 357 358$(OUTPUT)test-llvm-perf.bin: 359 $(BUILDXX) -std=gnu++17 \ 360 -I$(shell $(LLVM_CONFIG) --includedir) \ 361 -L$(shell $(LLVM_CONFIG) --libdir) \ 362 $(shell $(LLVM_CONFIG) --libs Core BPF) \ 363 $(shell $(LLVM_CONFIG) --system-libs) \ 364 > $(@:.bin=.make.output) 2>&1 365 366$(OUTPUT)test-clang.bin: 367 $(BUILDXX) -std=gnu++17 \ 368 -I$(shell $(LLVM_CONFIG) --includedir) \ 369 -L$(shell $(LLVM_CONFIG) --libdir) \ 370 -Wl,--start-group -lclang-cpp -Wl,--end-group \ 371 $(shell $(LLVM_CONFIG) --libs Core option) \ 372 $(shell $(LLVM_CONFIG) --system-libs) \ 373 > $(@:.bin=.make.output) 2>&1 374 375-include $(OUTPUT)*.d 376 377$(OUTPUT)test-libaio.bin: 378 $(BUILD) -lrt 379 380$(OUTPUT)test-libzstd.bin: 381 $(BUILD) -lzstd 382 383$(OUTPUT)test-clang-bpf-co-re.bin: test-clang-bpf-co-re.c 384 { $(CLANG) -S -g --target=bpf -o - $< | \ 385 grep BTF_KIND_VAR; } > $(@:.bin=.make.output) 2>&1 && touch $@ 386 387$(OUTPUT)test-file-handle.bin: 388 $(BUILD) 389 390$(OUTPUT)test-libpfm4.bin: 391 $(BUILD) -lpfm 392 393$(OUTPUT)test-libopenssl.bin: 394 $(BUILD) $(shell $(PKG_CONFIG) --libs --cflags openssl 2>/dev/null) 395 396$(OUTPUT)test-bpftool-skeletons.bin: 397 { $(SYSTEM_BPFTOOL) version | grep '^features:.*skeletons'; } \ 398 > $(@:.bin=.make.output) 2>&1 && touch $@ 399 400# Testing Rust is special: we don't compile anything, it's enough to check the 401# compiler presence. Compiling a test code for this purposes is problematic, 402# because Rust will emit a dependency file without any external references, 403# meaning that if rustc will be removed the build process will still think it's 404# there. 405$(OUTPUT)test-rust.bin: 406 $(RUSTC) --version > /dev/null 2>&1 407 408############################### 409 410clean: 411 rm -f $(FILES) $(OUTPUT)*.d $(FILES:.bin=.make.output) 412