Commit cdf0e80e authored by Alexei Starovoitov's avatar Alexei Starovoitov
Browse files

Merge branch 'bpf: tools: support build selftests/bpf with clang'

Yonghong Song says:

====================

To build kernel with clang, people typically use
  make -j60 LLVM=1 LLVM_IAS=1
LLVM_IAS=1 is not required for non-LTO build but
is required for LTO build. In my environment,
I am always having LLVM_IAS=1 regardless of
whether LTO is enabled or not.

After kernel is build with clang, the following command
can be used to build selftests with clang:
  make -j60 -C tools/testing/selftests/bpf LLVM=1 LLVM_IAS=1

I am using latest bpf-next kernel code base and
latest clang built from source from
  https://github.com/llvm/llvm-project.git


Using earlier version of llvm may have compilation errors, see
  tools/testing/selftests/bpf
due to continuous development in llvm bpf features and selftests
to use these features.

To run bpf selftest properly, you need have certain necessary
kernel configs like at:
  bpf-next:tools/testing/selftests/bpf/config
(not that this is not a complete .config file and some other configs
 might still be needed.)

Currently, using the above command, some compilations
still use gcc and there are also compilation errors and warnings.
This patch set intends to fix these issues.
Patch #1 and #2 fixed the issue so clang/clang++ is
used instead of gcc/g++. Patch #3 fixed a compilation
failure. Patch #4 and #5 fixed various compiler warnings.

Changelog:
  v2 -> v3:
    . more test environment description in cover letter. (Sedat)
    . use a different fix, but similar to other use in selftests/bpf
      Makefile, to exclude header files from CXX compilation command
      line. (Andrii)
    . fix codes instead of adding -Wno-format-security. (Andrii)
  v1 -> v2:
    . add -Wno-unused-command-line-argument and -Wno-format-security
      for clang only as (1). gcc does not exhibit those
      warnings, and (2). -Wno-unused-command-line-argument is
      only supported by clang. (Sedat)
====================

Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents d3d93e34 8af50142
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -157,7 +157,7 @@ static int netlink_recv(int sock, __u32 nl_pid, __u32 seq,
		if (len == 0)
			break;

		for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
		for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, (unsigned int)len);
		     nh = NLMSG_NEXT(nh, len)) {
			if (nh->nlmsg_pid != nl_pid) {
				ret = -LIBBPF_ERRNO__WRNGPID;
+10 −2
Original line number Diff line number Diff line
@@ -39,8 +39,6 @@ EXTRA_WARNINGS += -Wundef
EXTRA_WARNINGS += -Wwrite-strings
EXTRA_WARNINGS += -Wformat

CC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?)

# Makefiles suck: This macro sets a default value of $(2) for the
# variable named by $(1), unless the variable has been set by
# environment or command line. This is necessary for CC and AR
@@ -52,12 +50,22 @@ define allow-override
    $(eval $(1) = $(2)))
endef

ifneq ($(LLVM),)
$(call allow-override,CC,clang)
$(call allow-override,AR,llvm-ar)
$(call allow-override,LD,ld.lld)
$(call allow-override,CXX,clang++)
$(call allow-override,STRIP,llvm-strip)
else
# Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix.
$(call allow-override,CC,$(CROSS_COMPILE)gcc)
$(call allow-override,AR,$(CROSS_COMPILE)ar)
$(call allow-override,LD,$(CROSS_COMPILE)ld)
$(call allow-override,CXX,$(CROSS_COMPILE)g++)
$(call allow-override,STRIP,$(CROSS_COMPILE)strip)
endif

CC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?)

ifneq ($(LLVM),)
HOSTAR  ?= llvm-ar
+6 −1
Original line number Diff line number Diff line
@@ -28,6 +28,11 @@ CFLAGS += -g -Og -rdynamic -Wall $(GENFLAGS) $(SAN_CFLAGS) \
	  -Dbpf_load_program=bpf_test_load_program
LDLIBS += -lcap -lelf -lz -lrt -lpthread

# Silence some warnings when compiled with clang
ifneq ($(LLVM),)
CFLAGS += -Wno-unused-command-line-argument
endif

# Order correspond to 'make run_tests' order
TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
	test_verifier_log test_dev_cgroup \
@@ -481,7 +486,7 @@ $(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
# Make sure we are able to include and link libbpf against c++.
$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
	$(call msg,CXX,,$@)
	$(Q)$(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@
	$(Q)$(CXX) $(CFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@

# Benchmark runner
$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h
+2 −2
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ void test_fexit_sleep(void)
		goto cleanup;

	cpid = clone(do_sleep, child_stack + STACK_SIZE, CLONE_FILES | SIGCHLD, fexit_skel);
	if (CHECK(cpid == -1, "clone", strerror(errno)))
	if (CHECK(cpid == -1, "clone", "%s\n", strerror(errno)))
		goto cleanup;

	/* wait until first sys_nanosleep ends and second sys_nanosleep starts */
@@ -65,7 +65,7 @@ void test_fexit_sleep(void)
	/* kill the thread to unwind sys_nanosleep stack through the trampoline */
	kill(cpid, 9);

	if (CHECK(waitpid(cpid, &wstatus, 0) == -1, "waitpid", strerror(errno)))
	if (CHECK(waitpid(cpid, &wstatus, 0) == -1, "waitpid", "%s\n", strerror(errno)))
		goto cleanup;
	if (CHECK(WEXITSTATUS(wstatus) != 0, "exitstatus", "failed"))
		goto cleanup;
+2 −2
Original line number Diff line number Diff line
@@ -68,10 +68,10 @@ static void test_ns_current_pid_tgid_new_ns(void)
	cpid = clone(test_current_pid_tgid, child_stack + STACK_SIZE,
		     CLONE_NEWPID | SIGCHLD, NULL);

	if (CHECK(cpid == -1, "clone", strerror(errno)))
	if (CHECK(cpid == -1, "clone", "%s\n", strerror(errno)))
		return;

	if (CHECK(waitpid(cpid, &wstatus, 0) == -1, "waitpid", strerror(errno)))
	if (CHECK(waitpid(cpid, &wstatus, 0) == -1, "waitpid", "%s\n", strerror(errno)))
		return;

	if (CHECK(WEXITSTATUS(wstatus) != 0, "newns_pidtgid", "failed"))
Loading