xref: /linux/tools/build/feature/Makefile (revision 4456c404c6bb2e978769f5f27090deee3457f95c)
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-pthread-mutex-robust.bin		\
45         test-stackprotector-all.bin            \
46         test-timerfd.bin                       \
47         test-babeltrace2-ctf-writer.bin        \
48         test-libcapstone.bin			\
49         test-libcheck.bin			\
50         test-compile-32.bin                    \
51         test-compile-x32.bin                   \
52         test-zlib.bin                          \
53         test-lzma.bin                          \
54         test-bpf.bin                           \
55         test-libbpf.bin                        \
56         test-sdt.bin                           \
57         test-gettid.bin			\
58         test-jvmti.bin				\
59         test-jvmti-cmlr.bin			\
60         test-scandirat.bin			\
61         test-sched_getcpu.bin			\
62         test-setns.bin				\
63         test-libopencsd.bin			\
64         test-llvm.bin				\
65         test-llvm-perf.bin   			\
66         test-libaio.bin			\
67         test-libzstd.bin			\
68         test-clang-bpf-co-re.bin		\
69         test-file-handle.bin			\
70         test-libpfm4.bin			\
71         test-rust.bin				\
72         test-libopenssl.bin			\
73         test-bpftool-skeletons.bin
74
75FILES := $(addprefix $(OUTPUT),$(FILES))
76
77# Some distros provide the command $(CROSS_COMPILE)pkg-config for
78# searching packges installed with Multiarch. Use it for cross
79# compilation if it is existed.
80ifneq (, $(shell which $(CROSS_COMPILE)pkg-config))
81  PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
82else
83  PKG_CONFIG ?= pkg-config
84
85  # PKG_CONFIG_PATH or PKG_CONFIG_LIBDIR, alongside PKG_CONFIG_SYSROOT_DIR
86  # for modified system root, are required for the cross compilation.
87  # If these PKG_CONFIG environment variables are not set, Multiarch library
88  # paths are used instead.
89  ifdef CROSS_COMPILE
90    ifeq ($(PKG_CONFIG_LIBDIR)$(PKG_CONFIG_PATH)$(PKG_CONFIG_SYSROOT_DIR),)
91      CROSS_ARCH = $(notdir $(CROSS_COMPILE:%-=%))
92      PKG_CONFIG_LIBDIR := /usr/local/$(CROSS_ARCH)/lib/pkgconfig/
93      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/lib/$(CROSS_ARCH)/pkgconfig/
94      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/lib/$(CROSS_ARCH)/pkgconfig/
95      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/share/pkgconfig/
96      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/share/pkgconfig/
97      export PKG_CONFIG_LIBDIR
98    endif
99  endif
100endif
101
102ifeq ($(findstring -static,${LDFLAGS}),-static)
103  PKG_CONFIG += --static
104endif
105
106all: $(FILES)
107
108__BUILD = $(CC) $(CFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.c,$(@F)) $(LDFLAGS)
109  BUILD = $(__BUILD) > $(@:.bin=.make.output) 2>&1
110  BUILD_BFD = $(BUILD) -DPACKAGE='"perf"' -lbfd -ldl
111  BUILD_ALL = $(BUILD) -fstack-protector-all -O2 -D_FORTIFY_SOURCE=2 -ldw -lelf -lnuma -lelf -lslang \
112	      $(FLAGS_PERL_EMBED) $(FLAGS_PYTHON_EMBED) -ldl -lz -llzma -lzstd \
113	      $(shell $(PKG_CONFIG) --libs --cflags openssl 2>/dev/null)
114
115__BUILDXX = $(CXX) $(CXXFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.cpp,$(@F)) $(LDFLAGS)
116  BUILDXX = $(__BUILDXX) > $(@:.bin=.make.output) 2>&1
117
118###############################
119
120$(OUTPUT)test-all.bin:
121	$(BUILD_ALL)
122
123$(OUTPUT)test-hello.bin:
124	$(BUILD)
125
126$(OUTPUT)test-pthread-attr-setaffinity-np.bin:
127	$(BUILD) -D_GNU_SOURCE -lpthread
128
129$(OUTPUT)test-pthread-barrier.bin:
130	$(BUILD) -lpthread
131
132$(OUTPUT)test-pthread-mutex-robust.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-gtk4.bin:
246	$(BUILD) $(shell $(PKG_CONFIG) --libs --cflags gtk4 2>/dev/null)
247
248$(OUTPUT)test-python-module.bin:
249	$(BUILD) $(FLAGS_PYTHON_EMBED)
250
251$(OUTPUT)test-libbfd.bin:
252	$(BUILD_BFD) || $(BUILD_BFD) -lzstd
253
254$(OUTPUT)test-libbfd-threadsafe.bin:
255	$(BUILD_BFD) || $(BUILD_BFD) -lzstd || $(BUILD_BFD) -liberty || $(BUILD_BFD) -liberty -lz
256
257$(OUTPUT)test-disassembler-four-args.bin:
258	$(BUILD_BFD) -lopcodes || $(BUILD_BFD) -lopcodes -lzstd || $(BUILD_BFD) -lopcodes -liberty || \
259	$(BUILD_BFD) -lopcodes -liberty -lz
260
261$(OUTPUT)test-disassembler-init-styled.bin:
262	$(BUILD_BFD) -lopcodes ||  $(BUILD_BFD) -lopcodes -liberty || \
263	$(BUILD_BFD) -lopcodes -liberty -lz
264
265$(OUTPUT)test-reallocarray.bin:
266	$(BUILD)
267
268$(OUTPUT)test-libbfd-liberty.bin:
269	$(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty
270
271$(OUTPUT)test-libbfd-liberty-z.bin:
272	$(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty -lz
273
274$(OUTPUT)test-cplus-demangle.bin:
275	$(BUILD) -liberty
276
277$(OUTPUT)test-cxa-demangle.bin:
278	$(BUILDXX)
279
280$(OUTPUT)test-backtrace.bin:
281	$(BUILD)
282
283$(OUTPUT)test-timerfd.bin:
284	$(BUILD)
285
286$(OUTPUT)test-babeltrace2-ctf-writer.bin:
287	$(BUILD) # -lbabeltrace2-ctf-writer provided by $(FEATURE_CHECK_LDFLAGS-babeltrace2-ctf-writer)
288
289$(OUTPUT)test-libcapstone.bin:
290	$(BUILD) # -lcapstone provided by $(FEATURE_CHECK_LDFLAGS-libcapstone)
291
292$(OUTPUT)test-libcheck.bin:
293	$(BUILD) # -lcheck is provided by $(FEATURE_CHECK_LDFLAGS-libcheck)
294
295$(OUTPUT)test-compile-32.bin:
296	$(CC) -m32 -Wall -Werror -o $@ test-compile.c
297
298$(OUTPUT)test-compile-x32.bin:
299	$(CC) -mx32 -Wall -Werror -o $@ test-compile.c
300
301$(OUTPUT)test-zlib.bin:
302	$(BUILD) -lz
303
304$(OUTPUT)test-lzma.bin:
305	$(BUILD) -llzma
306
307$(OUTPUT)test-bpf.bin:
308	$(BUILD)
309
310$(OUTPUT)test-libbpf.bin:
311	$(BUILD) -lbpf
312
313$(OUTPUT)test-sdt.bin:
314	$(BUILD)
315
316$(OUTPUT)test-gettid.bin:
317	$(BUILD)
318
319$(OUTPUT)test-jvmti.bin:
320	$(BUILD)
321
322$(OUTPUT)test-jvmti-cmlr.bin:
323	$(BUILD)
324
325$(OUTPUT)test-llvm.bin:
326	$(BUILDXX) -std=gnu++17 				\
327		-I$(shell $(LLVM_CONFIG) --includedir) 		\
328		-L$(shell $(LLVM_CONFIG) --libdir)		\
329		$(shell $(LLVM_CONFIG) --libs Core BPF)		\
330		$(shell $(LLVM_CONFIG) --system-libs)		\
331		> $(@:.bin=.make.output) 2>&1
332
333$(OUTPUT)test-llvm-perf.bin:
334	$(BUILDXX) -std=gnu++17 				\
335		-I$(shell $(LLVM_CONFIG) --includedir) 		\
336		-L$(shell $(LLVM_CONFIG) --libdir)		\
337		$(shell $(LLVM_CONFIG) --libs Core BPF)		\
338		$(shell $(LLVM_CONFIG) --system-libs)		\
339		> $(@:.bin=.make.output) 2>&1
340
341-include $(OUTPUT)*.d
342
343$(OUTPUT)test-libaio.bin:
344	$(BUILD) -lrt
345
346$(OUTPUT)test-libzstd.bin:
347	$(BUILD) -lzstd
348
349$(OUTPUT)test-clang-bpf-co-re.bin: test-clang-bpf-co-re.c
350	{ $(CLANG) -S -g --target=bpf -o - $< |	\
351		grep BTF_KIND_VAR; } > $(@:.bin=.make.output) 2>&1 && touch $@
352
353$(OUTPUT)test-file-handle.bin:
354	$(BUILD)
355
356$(OUTPUT)test-libpfm4.bin:
357	$(BUILD) -lpfm
358
359$(OUTPUT)test-libopenssl.bin:
360	$(BUILD) $(shell $(PKG_CONFIG) --libs --cflags openssl 2>/dev/null)
361
362$(OUTPUT)test-bpftool-skeletons.bin:
363	{ $(SYSTEM_BPFTOOL) version | grep '^features:.*skeletons'; } \
364		> $(@:.bin=.make.output) 2>&1 && touch $@
365
366# Testing Rust is special: we don't compile anything, it's enough to check the
367# compiler presence. Compiling a test code for this purposes is problematic,
368# because Rust will emit a dependency file without any external references,
369# meaning that if rustc will be removed the build process will still think it's
370# there.
371$(OUTPUT)test-rust.bin:
372	$(RUSTC) --version > /dev/null 2>&1
373
374###############################
375
376clean:
377	rm -f $(FILES) $(OUTPUT)*.d $(FILES:.bin=.make.output)
378