Skip to contentSkip to conversation

iw: Makefile: support out-of-tree builds

PATCH In review Maxin John 2 participants 3 messages Apr 29 – May 6 Kbuild

lore
Share & tools
lore

Maxin John

author
#0 lore raw

PATCH v2 1/1 iw: Makefile: support out-of-tree builds

Enable out-of-tree builds for external frameworks like OpenEmbedded, so the build can run from a separate directory without changing the source tree.

Supports kernel-style out-of-tree builds using: make O=<builddir>

---
Changes in v2:
- Add support for kernel-style out-of-tree builds using:
make O=<builddir>
- Make SRCDIR computation whitespace-safe by switching to realpath
- Remove ambiguous use of "-I." from CPPFLAGS

---
 Makefile | 38 ++++++++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 6 deletions(-)
Makefile +32 -6
index 2652fac4e6ee..91961650269d 100644
@@ -1,5 +1,29 @@
 MAKEFLAGS += --no-print-directory
 
+# Support building in a separate output directory (O): 'make O=<builddir>'
+SRCDIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
+
+ifneq ($(O),)
+$(shell mkdir -p "$(O)")
+BUILDDIR := $(realpath $(O))
+$(if $(BUILDDIR),,$(error failed to create output directory "$(O)"))
+endif
+
+ifneq ($(words $(subst :, ,$(SRCDIR))),1)
+$(error source path cannot contain spaces or colons: $(SRCDIR))
+endif
+
+ifneq ($(O),)
+_goals := $(or $(MAKECMDGOALS),all)
+.PHONY: sub-make $(_goals)
+$(_goals): sub-make
+	@:
+sub-make:
+	+@$(MAKE) -C $(BUILDDIR) -f $(SRCDIR)/Makefile O= $(_goals)
+else
+VPATH := $(SRCDIR)
+.PHONY: all check clean install
+
 PREFIX ?= /usr
 SBINDIR ?= $(PREFIX)/sbin
 MANDIR ?= $(PREFIX)/share/man
@@ -20,8 +44,9 @@ CFLAGS += -Wdeclaration-after-statement
 CFLAGS += -D__SANE_USERSPACE_TYPES__
 CFLAGS += $(CFLAGS_EVAL)
 CFLAGS += $(EXTRA_CFLAGS)
+CPPFLAGS += -I$(CURDIR) -I$(SRCDIR)
 
-_OBJS := $(sort $(patsubst %.c,%.o,$(wildcard *.c)))
+_OBJS := $(sort $(patsubst $(SRCDIR)/%.c,%.o,$(wildcard $(SRCDIR)/*.c)))
 VERSION_OBJS := $(filter-out version.o, $(_OBJS))
 OBJS := $(VERSION_OBJS) version.o
 
@@ -93,13 +118,13 @@ endif
 all: $(ALL)
 
 version.c: version.sh $(patsubst %.o,%.c,$(VERSION_OBJS)) nl80211.h iw.h Makefile \
-		$(wildcard .git/index .git/refs/tags)
+		$(wildcard $(SRCDIR)/.git/index $(SRCDIR)/.git/refs/tags)
 	@$(NQ) ' GEN ' $@
-	$(Q)./version.sh $@
+	$(Q)$(SRCDIR)/version.sh $@
 
-nl80211-commands.inc: nl80211.h
+nl80211-commands.inc: $(SRCDIR)/nl80211.h
 	@$(NQ) ' GEN ' $@
-	$(Q)sed 's%^\tNL80211_CMD_%%;t n;d;:n s%^\([^=]*\),.*%\t[NL80211_CMD_\1] = \"\L\1\",%;t;d' nl80211.h | grep -v "reserved" > $@
+	$(Q)sed 's%^\tNL80211_CMD_%%;t n;d;:n s%^\([^=]*\),.*%\t[NL80211_CMD_\1] = \"\L\1\",%;t;d' $< | grep -v "reserved" > $@
 
 %.o: %.c iw.h nl80211.h nl80211-commands.inc
 	@$(NQ) ' CC  ' $@
@@ -112,7 +137,7 @@ iw:	$(OBJS)
 endif
 
 check:
-	$(Q)$(MAKE) all CC="REAL_CC=$(CC) CHECK=\"sparse -Wall\" cgcc"
+	$(Q)$(MAKE) -f $(SRCDIR)/Makefile all CC="REAL_CC=$(CC) CHECK=\"sparse -Wall\" cgcc"
 
 %.gz: %
 	@$(NQ) ' GZIP' $<
@@ -128,3 +153,4 @@ install: iw iw.8.gz
 
 clean:
 	$(Q)rm -f iw *.o *~ *.gz version.c *-stamp nl80211-commands.inc
+endif
Signature · 1 line
2.47.3
#1 lore raw
Quoted 4 lines · Maxin John

- Add support for kernel-style out-of-tree builds using:
make O=<builddir>
- Make SRCDIR computation whitespace-safe by switching to realpath
- Remove ambiguous use of "-I." from CPPFLAGS

Thanks for fixing that.

Right now I don't really see _why_ it doesn't work, but it seems confusing that if you build in-tree first and then try to build out-of- tree, it just says

make[1]: Nothing to be done for 'all'.

Maybe that should abort? Or work?

It seems that removing VPATH and changing the %.o rule

-%.o: %.c iw.h nl80211.h nl80211-commands.inc
+%.o: $(SRCDIR)/%.c $(SRCDIR)/iw.h $(SRCDIR)/nl80211.h nl80211-commands.inc

makes that work?

I'll leave it to you though.

johannes

author
#2 lore raw

Hi,

Quoted 10 lines · Johannes Berg
Quoted 4 lines · Maxin John

- Add support for kernel-style out-of-tree builds using:
make O=<builddir>
- Make SRCDIR computation whitespace-safe by switching to realpath
- Remove ambiguous use of "-I." from CPPFLAGS

Thanks for fixing that.

Quoted 7 lines

Right now I don't really see _why_ it doesn't work, but it seems confusing that if you build in-tree first and then try to build out-of- tree, it just says

make[1]: Nothing to be done for 'all'.

Maybe that should abort? Or work?

Thanks for sharing this observation. Yes, this is not ideal. I will update that part based on your suggestion.

Quoted 8 lines

It seems that removing VPATH and changing the %.o rule

-%.o: %.c iw.h nl80211.h nl80211-commands.inc
+%.o: $(SRCDIR)/%.c $(SRCDIR)/iw.h $(SRCDIR)/nl80211.h nl80211-commands.inc

makes that work?

I'll leave it to you though.

I will send a v3 to fix it.

Quoted 1 line

johannes

Best Regards,
Maxin