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