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