Subject: Re: [PATCH v2 2/4] selftests: Fix seccomp to support relocatable build (O=objdir)

From: Kees Cook <[email-masked]>
Date: Wed, 4 Mar 2020 18:20:45 -0800

On Wed, Mar 04, 2020 at 05:36:27PM -0700, Shuah Khan wrote:
> Fix seccomp relocatable builds. This is a simple fix to use the
> right lib.mk variable TEST_CUSTOM_PROGS to continue to do custom
> build to preserve dependency on kselftest_harness.h local header.
> This change applies cutom rule to seccomp_bpf seccomp_benchmark
> for a simpler logic. 
> 
> Uses $(OUTPUT) defined in lib.mk to handle build relocation.
> 
> The following use-cases work with this change:
> 
> In seccomp directory:
> make all and make clean

This works.

> 
> From top level from main Makefile:
> make kselftest-install O=objdir ARCH=arm64 HOSTCC=gcc \
>  CROSS_COMPILE=aarch64-linux-gnu- TARGETS=seccomp

This fails for me:

$ make kselftest-install O=objdir ARCH=arm64 HOSTCC=gcc CROSS_COMPILE=aarch64-linux-gnu- TARGETS=seccomp
make[1]: Entering directory '/home/kees/src/linux/objdir'
make --no-builtin-rules INSTALL_HDR_PATH=$BUILD/usr \
        ARCH=arm64 -C ../../.. headers_install
make[4]: ../scripts/Makefile.build: No such file or directory
make[4]: *** No rule to make target '../scripts/Makefile.build'.  Stop.
make[3]: *** [Makefile:501: scripts_basic] Error 2
make[2]: *** [Makefile:151: khdr] Error 2
make[1]: *** [/home/kees/src/linux/Makefile:1221: kselftest-install] Error 2
make[1]: Leaving directory '/home/kees/src/linux/objdir'
make: *** [Makefile:180: sub-make] Error 2

(My "objdir" is empty)

If I remove O=objdir everything is fine. And see below...

> Signed-off-by: Shuah Khan <[email-masked]>
> ---
>  tools/testing/selftests/seccomp/Makefile | 19 +++++++++----------
>  1 file changed, 9 insertions(+), 10 deletions(-)
> 
> diff --git a/tools/testing/selftests/seccomp/Makefile b/tools/testing/selftests/seccomp/Makefile
> index 1760b3e39730..355bcbc0394a 100644
> --- a/tools/testing/selftests/seccomp/Makefile
> +++ b/tools/testing/selftests/seccomp/Makefile
> @@ -1,17 +1,16 @@
>  # SPDX-License-Identifier: GPL-2.0
> -all:
> -
> -include ../lib.mk
> +CFLAGS += -Wl,-no-as-needed -Wall
> +LDFLAGS += -lpthread
>  
>  .PHONY: all clean
>  
> -BINARIES := seccomp_bpf seccomp_benchmark
> -CFLAGS += -Wl,-no-as-needed -Wall
> +include ../lib.mk
> +
> +# OUTPUT set by lib.mk
> +TEST_CUSTOM_PROGS := $(OUTPUT)/seccomp_bpf $(OUTPUT)/seccomp_benchmark
>  
> -seccomp_bpf: seccomp_bpf.c ../kselftest_harness.h
> -	$(CC) $(CFLAGS) $(LDFLAGS) $< -lpthread -o $@
> +$(TEST_CUSTOM_PROGS): ../kselftest_harness.h
>  
> -TEST_PROGS += $(BINARIES)
> -EXTRA_CLEAN := $(BINARIES)
> +all: $(TEST_CUSTOM_PROGS)
>  
> -all: $(BINARIES)
> +EXTRA_CLEAN := $(TEST_CUSTOM_PROGS)
> -- 
> 2.20.1
> 

Instead of the TEST_CUSTOM_PROGS+all dance, you can just add an explicit
dependency, with the final seccomp/Makefile looking like this:

# SPDX-License-Identifier: GPL-2.0
CFLAGS += -Wl,-no-as-needed -Wall
LDFLAGS += -lpthread

TEST_GEN_PROGS := seccomp_bpf seccomp_benchmark

include ../lib.mk

# Additional dependencies
$(OUTPUT)/seccomp_bpf: ../kselftest_harness.h

(Though this fails in the same way as above when run from the top-level
directory.)

-Kees

-- 
Kees Cook