xref: /linux/tools/build/feature/Makefile (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1# SPDX-License-Identifier: GPL-2.0
2include ../../scripts/Makefile.include
3
4FILES=                                          \
5         test-all.bin                           \
6         test-backtrace.bin                     \
7         test-bionic.bin                        \
8         test-libdw.bin                         \
9         test-eventfd.bin                       \
10         test-fortify-source.bin                \
11         test-get_current_dir_name.bin          \
12         test-glibc.bin                         \
13         test-gtk2.bin                          \
14         test-gtk2-infobar.bin                  \
15         test-hello.bin                         \
16         test-libaudit.bin                      \
17         test-libbfd.bin                        \
18         test-libbfd-buildid.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-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-libslang-include-subdir.bin       \
38         test-libtraceevent.bin                 \
39         test-libcpupower.bin                   \
40         test-libtracefs.bin                    \
41         test-libcrypto.bin                     \
42         test-libunwind.bin                     \
43         test-libunwind-debug-frame.bin         \
44         test-libunwind-x86.bin                 \
45         test-libunwind-x86_64.bin              \
46         test-libunwind-arm.bin                 \
47         test-libunwind-aarch64.bin             \
48         test-libunwind-debug-frame-arm.bin     \
49         test-libunwind-debug-frame-aarch64.bin \
50         test-pthread-attr-setaffinity-np.bin   \
51         test-pthread-barrier.bin		\
52         test-stackprotector-all.bin            \
53         test-timerfd.bin                       \
54         test-libbabeltrace.bin                 \
55         test-libcapstone.bin			\
56         test-compile-32.bin                    \
57         test-compile-x32.bin                   \
58         test-zlib.bin                          \
59         test-lzma.bin                          \
60         test-bpf.bin                           \
61         test-libbpf.bin                        \
62         test-get_cpuid.bin                     \
63         test-sdt.bin                           \
64         test-cxx.bin                           \
65         test-gettid.bin			\
66         test-jvmti.bin				\
67         test-jvmti-cmlr.bin			\
68         test-scandirat.bin			\
69         test-sched_getcpu.bin			\
70         test-setns.bin				\
71         test-libopencsd.bin			\
72         test-clang.bin				\
73         test-llvm.bin				\
74         test-llvm-perf.bin   \
75         test-libaio.bin			\
76         test-libzstd.bin			\
77         test-clang-bpf-co-re.bin		\
78         test-file-handle.bin			\
79         test-libpfm4.bin
80
81FILES := $(addprefix $(OUTPUT),$(FILES))
82
83# Some distros provide the command $(CROSS_COMPILE)pkg-config for
84# searching packges installed with Multiarch. Use it for cross
85# compilation if it is existed.
86ifneq (, $(shell which $(CROSS_COMPILE)pkg-config))
87  PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
88else
89  PKG_CONFIG ?= pkg-config
90
91  # PKG_CONFIG_PATH or PKG_CONFIG_LIBDIR, alongside PKG_CONFIG_SYSROOT_DIR
92  # for modified system root, are required for the cross compilation.
93  # If these PKG_CONFIG environment variables are not set, Multiarch library
94  # paths are used instead.
95  ifdef CROSS_COMPILE
96    ifeq ($(PKG_CONFIG_LIBDIR)$(PKG_CONFIG_PATH)$(PKG_CONFIG_SYSROOT_DIR),)
97      CROSS_ARCH = $(shell $(CC) -dumpmachine)
98      PKG_CONFIG_LIBDIR := /usr/local/$(CROSS_ARCH)/lib/pkgconfig/
99      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/lib/$(CROSS_ARCH)/pkgconfig/
100      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/lib/$(CROSS_ARCH)/pkgconfig/
101      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/share/pkgconfig/
102      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/share/pkgconfig/
103      export PKG_CONFIG_LIBDIR
104    endif
105  endif
106endif
107
108all: $(FILES)
109
110__BUILD = $(CC) $(CFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.c,$(@F)) $(LDFLAGS)
111  BUILD = $(__BUILD) > $(@:.bin=.make.output) 2>&1
112  BUILD_BFD = $(BUILD) -DPACKAGE='"perf"' -lbfd -ldl
113  BUILD_ALL = $(BUILD) -fstack-protector-all -O2 -D_FORTIFY_SOURCE=2 -ldw -lelf -lnuma -lelf -lslang $(FLAGS_PERL_EMBED) $(FLAGS_PYTHON_EMBED) -DPACKAGE='"perf"' -lbfd -ldl -lz -llzma -lzstd -lcap
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) || $(BUILD_ALL) -lopcodes -liberty
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-get_current_dir_name.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-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) -lelf -llzma
210
211$(OUTPUT)test-libunwind-debug-frame.bin:
212	$(BUILD) -lelf -llzma
213$(OUTPUT)test-libunwind-x86.bin:
214	$(BUILD) -lelf -llzma -lunwind-x86
215
216$(OUTPUT)test-libunwind-x86_64.bin:
217	$(BUILD) -lelf -llzma -lunwind-x86_64
218
219$(OUTPUT)test-libunwind-arm.bin:
220	$(BUILD) -lelf -llzma -lunwind-arm
221
222$(OUTPUT)test-libunwind-aarch64.bin:
223	$(BUILD) -lelf -llzma -lunwind-aarch64
224
225$(OUTPUT)test-libunwind-debug-frame-arm.bin:
226	$(BUILD) -lelf -llzma -lunwind-arm
227
228$(OUTPUT)test-libunwind-debug-frame-aarch64.bin:
229	$(BUILD) -lelf -llzma -lunwind-aarch64
230
231$(OUTPUT)test-libaudit.bin:
232	$(BUILD) -laudit
233
234$(OUTPUT)test-libslang.bin:
235	$(BUILD) -lslang
236
237$(OUTPUT)test-libslang-include-subdir.bin:
238	$(BUILD) -lslang
239
240$(OUTPUT)test-libtraceevent.bin:
241	$(BUILD) -ltraceevent
242
243$(OUTPUT)test-libcpupower.bin:
244	$(BUILD) -lcpupower
245
246$(OUTPUT)test-libtracefs.bin:
247	 $(BUILD) $(shell $(PKG_CONFIG) --cflags libtracefs 2>/dev/null) -ltracefs
248
249$(OUTPUT)test-libcrypto.bin:
250	$(BUILD) -lcrypto
251
252$(OUTPUT)test-gtk2.bin:
253	$(BUILD) $(shell $(PKG_CONFIG) --libs --cflags gtk+-2.0 2>/dev/null) -Wno-deprecated-declarations
254
255$(OUTPUT)test-gtk2-infobar.bin:
256	$(BUILD) $(shell $(PKG_CONFIG) --libs --cflags gtk+-2.0 2>/dev/null)
257
258grep-libs  = $(filter -l%,$(1))
259strip-libs = $(filter-out -l%,$(1))
260
261PERL_EMBED_LDOPTS = $(shell perl -MExtUtils::Embed -e ldopts 2>/dev/null)
262PERL_EMBED_LDFLAGS = $(call strip-libs,$(PERL_EMBED_LDOPTS))
263PERL_EMBED_LIBADD = $(call grep-libs,$(PERL_EMBED_LDOPTS))
264PERL_EMBED_CCOPTS = $(shell perl -MExtUtils::Embed -e ccopts 2>/dev/null)
265FLAGS_PERL_EMBED=$(PERL_EMBED_CCOPTS) $(PERL_EMBED_LDOPTS)
266
267ifeq ($(CC_NO_CLANG), 0)
268  PERL_EMBED_LDOPTS := $(filter-out -specs=%,$(PERL_EMBED_LDOPTS))
269  PERL_EMBED_CCOPTS := $(filter-out -flto=auto -ffat-lto-objects, $(PERL_EMBED_CCOPTS))
270  PERL_EMBED_CCOPTS := $(filter-out -specs=%,$(PERL_EMBED_CCOPTS))
271  FLAGS_PERL_EMBED += -Wno-compound-token-split-by-macro
272endif
273
274$(OUTPUT)test-libperl.bin:
275	$(BUILD) $(FLAGS_PERL_EMBED)
276
277$(OUTPUT)test-libpython.bin:
278	$(BUILD) $(FLAGS_PYTHON_EMBED)
279
280$(OUTPUT)test-libbfd.bin:
281	$(BUILD_BFD)
282
283$(OUTPUT)test-libbfd-buildid.bin:
284	$(BUILD_BFD) || $(BUILD_BFD) -liberty || $(BUILD_BFD) -liberty -lz
285
286$(OUTPUT)test-disassembler-four-args.bin:
287	$(BUILD_BFD) -lopcodes || $(BUILD_BFD) -lopcodes -liberty || \
288	$(BUILD_BFD) -lopcodes -liberty -lz
289
290$(OUTPUT)test-disassembler-init-styled.bin:
291	$(BUILD_BFD) -lopcodes || $(BUILD_BFD) -lopcodes -liberty || \
292	$(BUILD_BFD) -lopcodes -liberty -lz
293
294$(OUTPUT)test-reallocarray.bin:
295	$(BUILD)
296
297$(OUTPUT)test-libbfd-liberty.bin:
298	$(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty
299
300$(OUTPUT)test-libbfd-liberty-z.bin:
301	$(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty -lz
302
303$(OUTPUT)test-cplus-demangle.bin:
304	$(BUILD) -liberty
305
306$(OUTPUT)test-cxa-demangle.bin:
307	$(BUILDXX)
308
309$(OUTPUT)test-backtrace.bin:
310	$(BUILD)
311
312$(OUTPUT)test-timerfd.bin:
313	$(BUILD)
314
315$(OUTPUT)test-libbabeltrace.bin:
316	$(BUILD) # -lbabeltrace provided by $(FEATURE_CHECK_LDFLAGS-libbabeltrace)
317
318$(OUTPUT)test-libcapstone.bin:
319	$(BUILD) # -lcapstone provided by $(FEATURE_CHECK_LDFLAGS-libcapstone)
320
321$(OUTPUT)test-compile-32.bin:
322	$(CC) -m32 -o $@ test-compile.c
323
324$(OUTPUT)test-compile-x32.bin:
325	$(CC) -mx32 -o $@ test-compile.c
326
327$(OUTPUT)test-zlib.bin:
328	$(BUILD) -lz
329
330$(OUTPUT)test-lzma.bin:
331	$(BUILD) -llzma
332
333$(OUTPUT)test-get_cpuid.bin:
334	$(BUILD)
335
336$(OUTPUT)test-bpf.bin:
337	$(BUILD)
338
339$(OUTPUT)test-libbpf.bin:
340	$(BUILD) -lbpf
341
342$(OUTPUT)test-libbpf-btf__load_from_kernel_by_id.bin:
343	$(BUILD) -lbpf
344
345$(OUTPUT)test-libbpf-bpf_prog_load.bin:
346	$(BUILD) -lbpf
347
348$(OUTPUT)test-libbpf-bpf_map_create.bin:
349	$(BUILD) -lbpf
350
351$(OUTPUT)test-libbpf-bpf_object__next_program.bin:
352	$(BUILD) -lbpf
353
354$(OUTPUT)test-libbpf-bpf_object__next_map.bin:
355	$(BUILD) -lbpf
356
357$(OUTPUT)test-libbpf-bpf_program__set_insns.bin:
358	$(BUILD) -lbpf
359
360$(OUTPUT)test-libbpf-btf__raw_data.bin:
361	$(BUILD) -lbpf
362
363$(OUTPUT)test-sdt.bin:
364	$(BUILD)
365
366$(OUTPUT)test-cxx.bin:
367	$(BUILDXX) -std=gnu++11
368
369$(OUTPUT)test-gettid.bin:
370	$(BUILD)
371
372$(OUTPUT)test-jvmti.bin:
373	$(BUILD)
374
375$(OUTPUT)test-jvmti-cmlr.bin:
376	$(BUILD)
377
378$(OUTPUT)test-llvm.bin:
379	$(BUILDXX) -std=gnu++17 				\
380		-I$(shell $(LLVM_CONFIG) --includedir) 		\
381		-L$(shell $(LLVM_CONFIG) --libdir)		\
382		$(shell $(LLVM_CONFIG) --libs Core BPF)		\
383		$(shell $(LLVM_CONFIG) --system-libs)		\
384		> $(@:.bin=.make.output) 2>&1
385
386$(OUTPUT)test-llvm-perf.bin:
387	$(BUILDXX) -std=gnu++17 				\
388		-I$(shell $(LLVM_CONFIG) --includedir) 		\
389		-L$(shell $(LLVM_CONFIG) --libdir)		\
390		$(shell $(LLVM_CONFIG) --libs Core BPF)		\
391		$(shell $(LLVM_CONFIG) --system-libs)		\
392		> $(@:.bin=.make.output) 2>&1
393
394$(OUTPUT)test-clang.bin:
395	$(BUILDXX) -std=gnu++17					\
396		-I$(shell $(LLVM_CONFIG) --includedir) 		\
397		-L$(shell $(LLVM_CONFIG) --libdir)		\
398		-Wl,--start-group -lclang-cpp -Wl,--end-group	\
399		$(shell $(LLVM_CONFIG) --libs Core option)	\
400		$(shell $(LLVM_CONFIG) --system-libs)		\
401		> $(@:.bin=.make.output) 2>&1
402
403-include $(OUTPUT)*.d
404
405$(OUTPUT)test-libaio.bin:
406	$(BUILD) -lrt
407
408$(OUTPUT)test-libzstd.bin:
409	$(BUILD) -lzstd
410
411$(OUTPUT)test-clang-bpf-co-re.bin:
412	$(CLANG) -S -g --target=bpf -o - $(patsubst %.bin,%.c,$(@F)) |	\
413		grep BTF_KIND_VAR
414
415$(OUTPUT)test-file-handle.bin:
416	$(BUILD)
417
418$(OUTPUT)test-libpfm4.bin:
419	$(BUILD) -lpfm
420
421###############################
422
423clean:
424	rm -f $(FILES) $(OUTPUT)*.d $(FILES:.bin=.make.output)
425