[PATCH v2] selftests: Include directories of header files

From: Andrew Delgadilo
Date: Tue Oct 05 2021 - 18:27:54 EST


From: Andrew Delgadillo <adelg@xxxxxxxxxx>

clang complains about using -o when one specifies header files on the
commandline. An example can be seen when building the futex selftests:

$ make -C tools/testing/selftests TARGETS=futex
clang -Wall -g -O2 -Wall -D_GNU_SOURCE -pthread -I../include \
-I../../ -I../../../../../usr/include/ -I/kselftest/usr/include \
futex_wait_timeout.c ../include/futextest.h ../include/atomic.h \
../include/logging.h -lpthread -lrt -o \
tools/testing/selftests/futex/functional/futex_wait_timeout
clang: error: cannot specify -o when generating multiple output files

To fix this, instead of specifying the headers explicitliy, we include
(with -I) their directories. Note that -I does not work with files, only
directories [1,2]

There are still errors when building with clang, but this patch does a
way with one class of those errors.

Tested: No new errors when building with LLVM=0/1. In fact, clang no
longer errors out with "cannot specify -o ..." anymore.

[1] https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Directory-Options.html#Directory-Options
[2] https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-i-dir

Suggested-by: gthelen@xxxxxxxxxx
Signed-off-by: Andrew Delgadillo <adelg@xxxxxxxxxx>
---
Changes since v1:
- Instead of removing the headers from the cmdline altogether, include
their directories.
- Add more detail to the tested tag.
- Add missing Suggested-By tag from original patch.

tools/testing/selftests/filesystems/binderfs/Makefile | 4 ++--
tools/testing/selftests/lib.mk | 5 ++++-
tools/testing/selftests/x86/Makefile | 4 ++--
3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/filesystems/binderfs/Makefile b/tools/testing/selftests/filesystems/binderfs/Makefile
index 8af25ae96049..e2f09acb15b4 100644
--- a/tools/testing/selftests/filesystems/binderfs/Makefile
+++ b/tools/testing/selftests/filesystems/binderfs/Makefile
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: GPL-2.0

-CFLAGS += -I../../../../../usr/include/ -pthread
+CFLAGS += -I../../../../../usr/include/ -I$(selfdir) -pthread
TEST_GEN_PROGS := binderfs_test

-binderfs_test: binderfs_test.c ../../kselftest.h ../../kselftest_harness.h
+binderfs_test: binderfs_test.c

include ../../lib.mk
diff --git a/tools/testing/selftests/lib.mk b/tools/testing/selftests/lib.mk
index fa2ac0e56b43..0edf2a421771 100644
--- a/tools/testing/selftests/lib.mk
+++ b/tools/testing/selftests/lib.mk
@@ -137,12 +137,15 @@ COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
endif

+
# Selftest makefiles can override those targets by setting
# OVERRIDE_TARGETS = 1.
ifeq ($(OVERRIDE_TARGETS),)
LOCAL_HDRS := $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
$(OUTPUT)/%:%.c $(LOCAL_HDRS)
- $(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
+ $(LINK.c) $(filter-out %.h,$^) \
+ $(addprefix -I,$(dir $(filter %.h,$^))) \
+ $(LDLIBS) -o $@

$(OUTPUT)/%.o:%.S
$(COMPILE.S) $^ -o $@
diff --git a/tools/testing/selftests/x86/Makefile b/tools/testing/selftests/x86/Makefile
index b4142cd1c5c2..f794d431f289 100644
--- a/tools/testing/selftests/x86/Makefile
+++ b/tools/testing/selftests/x86/Makefile
@@ -72,10 +72,10 @@ all_64: $(BINARIES_64)
EXTRA_CLEAN := $(BINARIES_32) $(BINARIES_64)

$(BINARIES_32): $(OUTPUT)/%_32: %.c helpers.h
- $(CC) -m32 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $^ -lrt -ldl -lm
+ $(CC) -m32 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $(filter-out %.h,$^) $(addprefix -I,$(dir $(filter %.h,$^))) -lrt -ldl -lm

$(BINARIES_64): $(OUTPUT)/%_64: %.c helpers.h
- $(CC) -m64 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $^ -lrt -ldl
+ $(CC) -m64 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $(filter-out %.h,$^) $(addprefix -I,$(dir $(filter %.h,$^))) -lrt -ldl

# x86_64 users should be encouraged to install 32-bit libraries
ifeq ($(CAN_BUILD_I386)$(CAN_BUILD_X86_64),01)
--
2.33.0.800.g4c38ced690-goog