xref: /linux/tools/build/feature/Makefile (revision 89d001d48ec792b972ae2d09e59d8b8a5f2124db)
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-libperl.bin                       \
35         test-libpython.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-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-stackprotector-all.bin:
133	$(BUILD) -fstack-protector-all
134
135$(OUTPUT)test-fortify-source.bin:
136	$(BUILD) -O2 -D_FORTIFY_SOURCE=2
137
138$(OUTPUT)test-bionic.bin:
139	$(BUILD)
140
141$(OUTPUT)test-libcap.bin:
142	$(BUILD) -lcap
143
144$(OUTPUT)test-libelf.bin:
145	$(BUILD) -lelf
146
147$(OUTPUT)test-eventfd.bin:
148	$(BUILD)
149
150$(OUTPUT)test-glibc.bin:
151	$(BUILD)
152
153$(OUTPUT)test-scandirat.bin:
154	$(BUILD)
155
156$(OUTPUT)test-sched_getcpu.bin:
157	$(BUILD)
158
159$(OUTPUT)test-setns.bin:
160	$(BUILD)
161
162$(OUTPUT)test-libopencsd.bin:
163	$(BUILD) # -lopencsd_c_api -lopencsd provided by
164		 # $(FEATURE_CHECK_LDFLAGS-libopencsd)
165
166DWLIBS := -ldw
167ifeq ($(findstring -static,${LDFLAGS}),-static)
168  DWLIBS += -lelf -lz -llzma -lbz2 -lzstd
169
170  LIBDW_VERSION := $(shell $(PKG_CONFIG) --modversion libdw).0.0
171  LIBDW_VERSION_1 := $(word 1, $(subst ., ,$(LIBDW_VERSION)))
172  LIBDW_VERSION_2 := $(word 2, $(subst ., ,$(LIBDW_VERSION)))
173
174  # Elfutils merged libebl.a into libdw.a starting from version 0.177,
175  # Link libebl.a only if libdw is older than this version.
176  ifeq ($(shell test $(LIBDW_VERSION_2) -lt 177; echo $$?),0)
177    DWLIBS += -lebl
178  endif
179
180  # Must put -ldl after -lebl for dependency
181  DWARFLIBS += -ldl
182endif
183
184$(OUTPUT)test-libdw.bin:
185	$(BUILD) $(DWLIBS)
186
187$(OUTPUT)test-libelf-getphdrnum.bin:
188	$(BUILD) -lelf
189
190$(OUTPUT)test-libelf-gelf_getnote.bin:
191	$(BUILD) -lelf
192
193$(OUTPUT)test-libelf-getshdrstrndx.bin:
194	$(BUILD) -lelf
195
196$(OUTPUT)test-libelf-zstd.bin:
197	$(BUILD) -lelf -lz -lzstd
198
199$(OUTPUT)test-libdebuginfod.bin:
200	$(BUILD) -ldebuginfod
201
202$(OUTPUT)test-libnuma.bin:
203	$(BUILD) -lnuma
204
205$(OUTPUT)test-numa_num_possible_cpus.bin:
206	$(BUILD) -lnuma
207
208$(OUTPUT)test-libunwind.bin:
209	$(BUILD) -include libunwind.h -lelf -llzma -lunwind
210
211$(OUTPUT)test-libunwind-debug-frame.bin:
212	$(BUILD) -include libunwind.h -lelf -llzma -lunwind
213
214define LIBUNWIND_RULE
215$$(OUTPUT)test-libunwind-$(1).bin:
216	$$(CC) $$(CFLAGS) -MD -Wall -Werror -include libunwind-$(1).h -o $$@ \
217		test-libunwind.c $$(LDFLAGS) -lelf -llzma -lunwind-$(1) \
218		> $$(@:.bin=.make.output) 2>&1
219
220$$(OUTPUT)test-libunwind-debug-frame-$(1).bin:
221	$$(CC) $$(CFLAGS) -MD -Wall -Werror -include libunwind-$(1).h -o $$@ \
222		test-libunwind-debug-frame.c $$(LDFLAGS) -lelf -llzma -lunwind-$(1) \
223		> $$(@:.bin=.make.output) 2>&1
224
225endef
226$(foreach arch,$(LIBUNWIND_ARCHS), \
227    $(eval $(call LIBUNWIND_RULE,$(arch))) \
228)
229
230$(OUTPUT)test-libslang.bin:
231	$(BUILD) -lslang
232
233$(OUTPUT)test-libtraceevent.bin:
234	$(BUILD) -ltraceevent
235
236$(OUTPUT)test-libcpupower.bin:
237	$(BUILD) -lcpupower
238
239$(OUTPUT)test-libtracefs.bin:
240	 $(BUILD) $(shell $(PKG_CONFIG) --cflags libtracefs 2>/dev/null) -ltracefs
241
242$(OUTPUT)test-gtk4.bin:
243	$(BUILD) $(shell $(PKG_CONFIG) --libs --cflags gtk4 2>/dev/null)
244
245grep-libs  = $(filter -l%,$(1))
246strip-libs = $(filter-out -l%,$(1))
247
248PERL_EMBED_LDOPTS = $(shell perl -MExtUtils::Embed -e ldopts 2>/dev/null)
249PERL_EMBED_LDFLAGS = $(call strip-libs,$(PERL_EMBED_LDOPTS))
250PERL_EMBED_LIBADD = $(call grep-libs,$(PERL_EMBED_LDOPTS))
251PERL_EMBED_CCOPTS = $(shell perl -MExtUtils::Embed -e ccopts 2>/dev/null)
252FLAGS_PERL_EMBED=$(PERL_EMBED_CCOPTS) $(PERL_EMBED_LDOPTS)
253
254ifeq ($(CC_NO_CLANG), 0)
255  PERL_EMBED_LDOPTS := $(filter-out -specs=%,$(PERL_EMBED_LDOPTS))
256  PERL_EMBED_CCOPTS := $(filter-out -flto=auto -ffat-lto-objects, $(PERL_EMBED_CCOPTS))
257  PERL_EMBED_CCOPTS := $(filter-out -specs=%,$(PERL_EMBED_CCOPTS))
258  FLAGS_PERL_EMBED += -Wno-compound-token-split-by-macro
259endif
260
261$(OUTPUT)test-libperl.bin:
262	$(BUILD) $(FLAGS_PERL_EMBED)
263
264$(OUTPUT)test-libpython.bin:
265	$(BUILD) $(FLAGS_PYTHON_EMBED)
266
267$(OUTPUT)test-libbfd.bin:
268	$(BUILD_BFD) || $(BUILD_BFD) -lzstd
269
270$(OUTPUT)test-libbfd-threadsafe.bin:
271	$(BUILD_BFD) || $(BUILD_BFD) -lzstd || $(BUILD_BFD) -liberty || $(BUILD_BFD) -liberty -lz
272
273$(OUTPUT)test-disassembler-four-args.bin:
274	$(BUILD_BFD) -lopcodes || $(BUILD_BFD) -lopcodes -lzstd || $(BUILD_BFD) -lopcodes -liberty || \
275	$(BUILD_BFD) -lopcodes -liberty -lz
276
277$(OUTPUT)test-disassembler-init-styled.bin:
278	$(BUILD_BFD) -lopcodes ||  $(BUILD_BFD) -lopcodes -liberty || \
279	$(BUILD_BFD) -lopcodes -liberty -lz
280
281$(OUTPUT)test-reallocarray.bin:
282	$(BUILD)
283
284$(OUTPUT)test-libbfd-liberty.bin:
285	$(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty
286
287$(OUTPUT)test-libbfd-liberty-z.bin:
288	$(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty -lz
289
290$(OUTPUT)test-cplus-demangle.bin:
291	$(BUILD) -liberty
292
293$(OUTPUT)test-cxa-demangle.bin:
294	$(BUILDXX)
295
296$(OUTPUT)test-backtrace.bin:
297	$(BUILD)
298
299$(OUTPUT)test-timerfd.bin:
300	$(BUILD)
301
302$(OUTPUT)test-babeltrace2-ctf-writer.bin:
303	$(BUILD) # -lbabeltrace2-ctf-writer provided by $(FEATURE_CHECK_LDFLAGS-babeltrace2-ctf-writer)
304
305$(OUTPUT)test-libcapstone.bin:
306	$(BUILD) # -lcapstone provided by $(FEATURE_CHECK_LDFLAGS-libcapstone)
307
308$(OUTPUT)test-libcheck.bin:
309	$(BUILD) # -lcheck is provided by $(FEATURE_CHECK_LDFLAGS-libcheck)
310
311$(OUTPUT)test-compile-32.bin:
312	$(CC) -m32 -Wall -Werror -o $@ test-compile.c
313
314$(OUTPUT)test-compile-x32.bin:
315	$(CC) -mx32 -Wall -Werror -o $@ test-compile.c
316
317$(OUTPUT)test-zlib.bin:
318	$(BUILD) -lz
319
320$(OUTPUT)test-lzma.bin:
321	$(BUILD) -llzma
322
323$(OUTPUT)test-bpf.bin:
324	$(BUILD)
325
326$(OUTPUT)test-libbpf.bin:
327	$(BUILD) -lbpf
328
329$(OUTPUT)test-sdt.bin:
330	$(BUILD)
331
332$(OUTPUT)test-gettid.bin:
333	$(BUILD)
334
335$(OUTPUT)test-jvmti.bin:
336	$(BUILD)
337
338$(OUTPUT)test-jvmti-cmlr.bin:
339	$(BUILD)
340
341$(OUTPUT)test-llvm.bin:
342	$(BUILDXX) -std=gnu++17 				\
343		-I$(shell $(LLVM_CONFIG) --includedir) 		\
344		-L$(shell $(LLVM_CONFIG) --libdir)		\
345		$(shell $(LLVM_CONFIG) --libs Core BPF)		\
346		$(shell $(LLVM_CONFIG) --system-libs)		\
347		> $(@:.bin=.make.output) 2>&1
348
349$(OUTPUT)test-llvm-perf.bin:
350	$(BUILDXX) -std=gnu++17 				\
351		-I$(shell $(LLVM_CONFIG) --includedir) 		\
352		-L$(shell $(LLVM_CONFIG) --libdir)		\
353		$(shell $(LLVM_CONFIG) --libs Core BPF)		\
354		$(shell $(LLVM_CONFIG) --system-libs)		\
355		> $(@:.bin=.make.output) 2>&1
356
357-include $(OUTPUT)*.d
358
359$(OUTPUT)test-libaio.bin:
360	$(BUILD) -lrt
361
362$(OUTPUT)test-libzstd.bin:
363	$(BUILD) -lzstd
364
365$(OUTPUT)test-clang-bpf-co-re.bin: test-clang-bpf-co-re.c
366	{ $(CLANG) -S -g --target=bpf -o - $< |	\
367		grep BTF_KIND_VAR; } > $(@:.bin=.make.output) 2>&1 && touch $@
368
369$(OUTPUT)test-file-handle.bin:
370	$(BUILD)
371
372$(OUTPUT)test-libpfm4.bin:
373	$(BUILD) -lpfm
374
375$(OUTPUT)test-libopenssl.bin:
376	$(BUILD) $(shell $(PKG_CONFIG) --libs --cflags openssl 2>/dev/null)
377
378$(OUTPUT)test-bpftool-skeletons.bin:
379	{ $(SYSTEM_BPFTOOL) version | grep '^features:.*skeletons'; } \
380		> $(@:.bin=.make.output) 2>&1 && touch $@
381
382# Testing Rust is special: we don't compile anything, it's enough to check the
383# compiler presence. Compiling a test code for this purposes is problematic,
384# because Rust will emit a dependency file without any external references,
385# meaning that if rustc will be removed the build process will still think it's
386# there.
387$(OUTPUT)test-rust.bin:
388	$(RUSTC) --version > /dev/null 2>&1
389
390###############################
391
392clean:
393	rm -f $(FILES) $(OUTPUT)*.d $(FILES:.bin=.make.output)
394