All of lore.kernel.org
 help / color / mirror / Atom feed
From: Florian Fainelli via buildroot <buildroot@buildroot.org>
To: buildroot@buildroot.org
Cc: "Eric Le Bihan" <eric.le.bihan.dev@free.fr>,
	"Gary Bisson" <bisson.gary@gmail.com>,
	"Jérôme Pouiller" <jezz@sysmic.org>,
	"Florian Fainelli" <florian.fainelli@broadcom.com>,
	"Bernd Kuhls" <bernd@kuhls.net>,
	"Thomas Perale" <thomas.perale@mind.be>,
	"Maxime Chevallier" <maxime.chevallier@bootlin.com>,
	"Refik Tuzakli" <tuzakli.refik@gmail.com>,
	"Francois Perrad" <francois.perrad.86@gmail.com>,
	"Thomas Petazzoni" <thomas.petazzoni@bootlin.com>,
	"Arnout Vandecappelle" <arnout@rnout.be>,
	"Petr Vorel" <petr.vorel@gmail.com>,
	"Spenser Gilliland" <spenser@gillilanding.com>,
	"Giulio Benetti" <giulio.benetti@benettiengineering.com>,
	"Pascal de Bruijn" <p.debruijn@unilogic.nl>,
	"Luca Ceresoli" <luca.ceresoli@bootlin.com>,
	"Romain Naour" <romain.naour@gmail.com>,
	"Mahyar Koshkouei" <mahyar.koshkouei@gmail.com>
Subject: [Buildroot] [PATCH v2 09/23] linux: build out of tree
Date: Wed, 28 Oct 2026 14:37:30 -0700	[thread overview]
Message-ID: <20261028213744.511429-10-florian.fainelli@broadcom.com> (raw)
In-Reply-To: <20261028213744.511429-1-florian.fainelli@broadcom.com>

Introduce <pkg>_SUPPORTS_OUT_OF_TREE. A package that sets it is always
built out of tree:

 - <pkg>_SRCDIR is $(SRC_DIR)/<basename>, i.e. output/source/..., where
   the sources are extracted and patched, while <pkg>_BUILDDIR remains
   in $(BUILD_DIR) and only contains generated files. Other packages
   keep extracting in $(BUILD_DIR), so they are not affected;
 - when <pkg>_OVERRIDE_SRCDIR is set, <pkg>_SRCDIR is the overridden
   directory, used in place: nothing is rsynced into $(BUILD_DIR), only
   the pre/post rsync hooks still run, and the source-related stamp
   files (rsynced, patched, ...) live in the build directory so that
   nothing is created in the user's tree;
 - kconfig packages start from the source directory with O=<builddir>,
   which makes kbuild generate its wrapper Makefile in the build
   directory, so that all the existing -C $(LINUX_DIR) invocations keep
   working unmodified;
 - <pkg>-dirclean and 'make clean' also remove the extracted sources.

Enable it for the Linux kernel. When the sources are overridden, the
source tree must be clean (no in-tree .config, include/config,
arch/*/include/generated).

Signed-off-by: Florian Fainelli <florian.fainelli@broadcom.com>
---
 Makefile               |  4 ++--
 linux/linux.mk         |  5 +++++
 package/pkg-generic.mk | 46 ++++++++++++++++++++++++++++++------------
 package/pkg-kconfig.mk |  6 +++++-
 4 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/Makefile b/Makefile
index c1f12a4bfc38..7aa43126540c 100644
--- a/Makefile
+++ b/Makefile
@@ -211,7 +211,7 @@ endif
 # Avoids doing the $(or...) everytime
 BR_GRAPH_OUT := $(or $(BR2_GRAPH_OUT),pdf)
 
-SRC_DIR := $(BASE_DIR)/build
+SRC_DIR := $(BASE_DIR)/source
 BUILD_DIR := $(BASE_DIR)/build
 BINARIES_DIR := $(BASE_DIR)/images
 BASE_TARGET_DIR := $(BASE_DIR)/target
@@ -1133,7 +1133,7 @@ show-vars:
 .PHONY: clean
 clean:
 	rm -rf $(BASE_TARGET_DIR) $(BINARIES_DIR) $(HOST_DIR) $(HOST_DIR_SYMLINK) \
-		$(BUILD_DIR) $(BASE_DIR)/staging \
+		$(BUILD_DIR) $(SRC_DIR) $(BASE_DIR)/staging \
 		$(LEGAL_INFO_DIR) $(GRAPHS_DIR) $(PER_PACKAGE_DIR) $(O)/pkg-stats.*
 
 .PHONY: distclean
diff --git a/linux/linux.mk b/linux/linux.mk
index a90e09afe906..5734d78aa9a2 100644
--- a/linux/linux.mk
+++ b/linux/linux.mk
@@ -6,6 +6,11 @@
 
 LINUX_VERSION = $(call qstrip,$(BR2_LINUX_KERNEL_VERSION))
 LINUX_LICENSE = GPL-2.0
+
+# The kernel is always built out of tree: the sources are in $(LINUX_SRCDIR)
+# and the kernel is built in $(LINUX_BUILDDIR) with O=. When
+# LINUX_OVERRIDE_SRCDIR is set, the sources are not rsynced.
+LINUX_SUPPORTS_OUT_OF_TREE = YES
 LINUX_LICENSE_FILES = $(call qstrip,$(BR2_LINUX_KERNEL_LICENSE_FILES))
 
 LINUX_CPE_ID_VENDOR = linux
diff --git a/package/pkg-generic.mk b/package/pkg-generic.mk
index 138fe9ccef4f..0f4bf73fa064 100644
--- a/package/pkg-generic.mk
+++ b/package/pkg-generic.mk
@@ -171,7 +171,7 @@ endef
 ################################################################################
 
 # Retrieve the archive
-$(SRC_DIR)/%/.stamp_downloaded:
+$(BUILD_DIR)/%/.stamp_downloaded $(SRC_DIR)/%/.stamp_downloaded:
 	@$(call step_start,download)
 	$(call prepare-per-package-directory,$($(PKG)_FINAL_DOWNLOAD_DEPENDENCIES))
 	$(foreach hook,$($(PKG)_PRE_DOWNLOAD_HOOKS),$(call $(hook))$(sep))
@@ -196,7 +196,7 @@ $(SRC_DIR)/%/.stamp_downloaded:
 	$(Q)touch $@
 
 # Retrieve actual source archive, e.g. for prebuilt external toolchains
-$(SRC_DIR)/%/.stamp_actual_downloaded:
+$(BUILD_DIR)/%/.stamp_actual_downloaded $(SRC_DIR)/%/.stamp_actual_downloaded:
 	@$(call step_start,actual-download)
 	$(call DOWNLOAD,$($(PKG)_ACTUAL_SOURCE_SITE)/$($(PKG)_ACTUAL_SOURCE_TARBALL))
 	$(Q)mkdir -p $(@D)
@@ -204,7 +204,7 @@ $(SRC_DIR)/%/.stamp_actual_downloaded:
 	$(Q)touch $@
 
 # Unpack the archive
-$(SRC_DIR)/%/.stamp_extracted:
+$(BUILD_DIR)/%/.stamp_extracted $(SRC_DIR)/%/.stamp_extracted:
 	@$(call step_start,extract)
 	@$(call MESSAGE,"Extracting")
 	$(call prepare-per-package-directory,$($(PKG)_FINAL_EXTRACT_DEPENDENCIES))
@@ -219,19 +219,19 @@ $(SRC_DIR)/%/.stamp_extracted:
 
 # Rsync the source directory if the <pkg>_OVERRIDE_SRCDIR feature is
 # used.
-$(SRC_DIR)/%/.stamp_rsynced:
+$(BUILD_DIR)/%/.stamp_rsynced $(SRC_DIR)/%/.stamp_rsynced:
 	@$(call step_start,rsync)
-	@$(call MESSAGE,"Syncing from source dir $(SRCDIR)")
+	@$(call MESSAGE,"$(if $($(PKG)_OUT_OF_TREE),Using out-of-tree source dir,Syncing from source dir) $(SRCDIR)")
 	@mkdir -p $(@D)
 	$(foreach hook,$($(PKG)_PRE_RSYNC_HOOKS),$(call $(hook))$(sep))
 	@test -d $(SRCDIR) || (echo "ERROR: $(SRCDIR) does not exist" ; exit 1)
-	rsync -au --chmod=u=rwX,go=rX $($(PKG)_OVERRIDE_SRCDIR_RSYNC_EXCLUSIONS) $(RSYNC_VCS_EXCLUSIONS) $(call qstrip,$(SRCDIR))/ $(@D)
+	$(if $($(PKG)_OUT_OF_TREE),,rsync -au --chmod=u=rwX,go=rX $($(PKG)_OVERRIDE_SRCDIR_RSYNC_EXCLUSIONS) $(RSYNC_VCS_EXCLUSIONS) $(call qstrip,$(SRCDIR))/ $(@D))
 	$(foreach hook,$($(PKG)_POST_RSYNC_HOOKS),$(call $(hook))$(sep))
 	@$(call step_end,rsync)
 	$(Q)touch $@
 
 # Patch
-$(SRC_DIR)/%/.stamp_patched:
+$(BUILD_DIR)/%/.stamp_patched $(SRC_DIR)/%/.stamp_patched:
 	@$(call step_start,patch)
 	@$(call MESSAGE,"Patching")
 	$(foreach hook,$($(PKG)_PRE_PATCH_HOOKS),$(call $(hook))$(sep))
@@ -390,6 +390,7 @@ $(BUILD_DIR)/%/.stamp_installed:
 $(BUILD_DIR)/%/.stamp_dircleaned:
 	$(if $(BR2_PER_PACKAGE_DIRECTORIES),rm -Rf $(PER_PACKAGE_DIR)/$(NAME))
 	rm -Rf $(@D)
+	$(if $(filter $(SRC_DIR)/%,$($(PKG)_SRCDIR)),rm -Rf $($(PKG)_SRCDIR))
 
 ################################################################################
 # virt-provides-single -- check that provider-pkg is the declared provider for
@@ -559,13 +560,32 @@ ifndef $(2)_STRIP_COMPONENTS
 endif
 
 $(2)_SRCDIR = $$($(2)_DIR)
-$(2)_BUILDDIR = $$($(2)_DIR)
+$(2)_BUILDDIR ?= $$($(2)_DIR)
 $(2)_SRCSUBDIR = $$($(2)_SRCDIR)/$$($(2)_SUBDIR)
 
 ifneq ($$($(2)_OVERRIDE_SRCDIR),)
 $(2)_VERSION = custom
 endif
 
+# Packages that set <pkg>_SUPPORTS_OUT_OF_TREE = YES are always built out of
+# tree: the sources live in $(SRC_DIR), and only the build directory lives in
+# $(BUILD_DIR). When <pkg>_OVERRIDE_SRCDIR is set, the sources are used
+# where they are (no rsync). The source-related stamp files must then not be
+# created in the (user-owned) source directory, so they live in the build
+# directory.
+# Out-of-tree builds are disabled by default. A package infrastructure may
+# set a different default before calling this macro.
+$(3)_SUPPORTS_OUT_OF_TREE ?= NO
+$(2)_STAMP_SRCDIR = $$($(2)_SRCDIR)
+ifeq ($$($(3)_SUPPORTS_OUT_OF_TREE),YES)
+$(2)_OUT_OF_TREE = YES
+$(2)_SRCDIR = $$(SRC_DIR)/$$($(2)_BASENAME)
+ifneq ($$($(2)_OVERRIDE_SRCDIR),)
+$(2)_SRCDIR = $$(abspath $$(call qstrip,$$($(2)_OVERRIDE_SRCDIR)))
+$(2)_STAMP_SRCDIR = $$($(2)_BUILDDIR)
+endif
+endif
+
 ifndef $(2)_SOURCE
  ifdef $(3)_SOURCE
   $(2)_SOURCE = $$($(3)_SOURCE)
@@ -844,11 +864,11 @@ $(2)_TARGET_INSTALL_IMAGES =	$$($(2)_BUILDDIR)/.stamp_images_installed
 $(2)_TARGET_INSTALL_HOST =	$$($(2)_BUILDDIR)/.stamp_host_installed
 $(2)_TARGET_BUILD =		$$($(2)_BUILDDIR)/.stamp_built
 $(2)_TARGET_CONFIGURE =		$$($(2)_BUILDDIR)/.stamp_configured
-$(2)_TARGET_RSYNC =		$$($(2)_SRCDIR)/.stamp_rsynced
-$(2)_TARGET_PATCH =		$$($(2)_SRCDIR)/.stamp_patched
-$(2)_TARGET_EXTRACT =		$$($(2)_SRCDIR)/.stamp_extracted
-$(2)_TARGET_SOURCE =		$$($(2)_SRCDIR)/.stamp_downloaded
-$(2)_TARGET_ACTUAL_SOURCE =	$$($(2)_SRCDIR)/.stamp_actual_downloaded
+$(2)_TARGET_RSYNC =		$$($(2)_STAMP_SRCDIR)/.stamp_rsynced
+$(2)_TARGET_PATCH =		$$($(2)_STAMP_SRCDIR)/.stamp_patched
+$(2)_TARGET_EXTRACT =		$$($(2)_STAMP_SRCDIR)/.stamp_extracted
+$(2)_TARGET_SOURCE =		$$($(2)_STAMP_SRCDIR)/.stamp_downloaded
+$(2)_TARGET_ACTUAL_SOURCE =	$$($(2)_STAMP_SRCDIR)/.stamp_actual_downloaded
 $(2)_TARGET_DIRCLEAN =		$$($(2)_BUILDDIR)/.stamp_dircleaned
 
 # default extract command
diff --git a/package/pkg-kconfig.mk b/package/pkg-kconfig.mk
index 8a63d55be033..5e46c62c58e6 100644
--- a/package/pkg-kconfig.mk
+++ b/package/pkg-kconfig.mk
@@ -147,8 +147,12 @@ $$($(2)_KCONFIG_FILE) $$($(2)_KCONFIG_FRAGMENT_FILES): | $(1)-patch
 		fi; \
 	done
 
+# For an out-of-tree build, the first kconfig run is what creates the
+# wrapper Makefile in the build directory, so it must be started from
+# the source directory.
 $(2)_KCONFIG_MAKE = \
-	$$($(2)_MAKE_ENV) $$($(2)_MAKE) -C $$($(2)_BUILDDIR) \
+	$$($(2)_MAKE_ENV) $$($(2)_MAKE) \
+	$$(if $$($(2)_OUT_OF_TREE),-C $$($(2)_SRCDIR) O=$$($(2)_BUILDDIR),-C $$($(2)_BUILDDIR)) \
 		$$(PKG_KCONFIG_COMMON_OPTS) $$($(2)_KCONFIG_OPTS)
 
 # The specified source configuration file and any additional configuration file
-- 
2.34.1

_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

  parent reply	other threads:[~2026-10-28 21:39 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-28 21:37 [Buildroot] [PATCH v2 00/23] Support building the Linux kernel out of tree Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 01/23] package: prepare removal of <pkg>_DIR Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 02/23] infra: introduce a global SRC_DIR variable Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 03/23] package: fix <pkg>_SRCDIR and <pkg>_BUILDDIR values Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 04/23] boot/boot-wrapper-aarch64: needs LINUX_BUILDDIR Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 05/23] boot/mxs-bootlets: needs (BAREBOX, LINUX, UBOOT)_BUILDDIR Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 06/23] linux/extensions: point to proper _SRCDIR and @D Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 07/23] linux/linux: LINUX_VERSION_PROBED should run from build directory Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 08/23] package: use <pkg>_SRCSUBDIR where <pkg>_SRCDIR used to include _SUBDIR Florian Fainelli via buildroot
2026-10-28 21:37 ` Florian Fainelli via buildroot [this message]
2026-10-28 21:37 ` [Buildroot] [PATCH v2 10/23] packages: sync host and target patch hooks Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 11/23] omniorb: avoid target-only patch hook Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 12/23] lua: rework post-patch hook usage Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 13/23] infra: host packages inherit the target extra downloads Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 14/23] package/libtool: use post-patch hooks Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 15/23] package/libtool: also apply libtool-patch to host libtool Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 16/23] package/{ffmpeg, libvpx}: use $(@D) instead of $(pkg_SRCDIR) when possible Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 17/23] package/perl: apply the perl-cross even for the host variant Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 18/23] infra/autotools: warn for package that autoreconf without libtool patch Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 19/23] packages: replace $(PKG)_DIR with @D where possible Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 20/23] linux: support custom logo and devicetree files in out-of-tree builds Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 21/23] package/linux-tools: support an out-of-tree kernel build Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 22/23] package: read kernel headers from LINUX_SRCDIR Florian Fainelli via buildroot
2026-10-28 21:37 ` [Buildroot] [PATCH v2 23/23] docs/manual: document out-of-tree package builds and the new srcdir variables Florian Fainelli via buildroot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261028213744.511429-10-florian.fainelli@broadcom.com \
    --to=buildroot@buildroot.org \
    --cc=arnout@rnout.be \
    --cc=bernd@kuhls.net \
    --cc=bisson.gary@gmail.com \
    --cc=eric.le.bihan.dev@free.fr \
    --cc=florian.fainelli@broadcom.com \
    --cc=francois.perrad.86@gmail.com \
    --cc=giulio.benetti@benettiengineering.com \
    --cc=jezz@sysmic.org \
    --cc=luca.ceresoli@bootlin.com \
    --cc=mahyar.koshkouei@gmail.com \
    --cc=maxime.chevallier@bootlin.com \
    --cc=p.debruijn@unilogic.nl \
    --cc=petr.vorel@gmail.com \
    --cc=romain.naour@gmail.com \
    --cc=spenser@gillilanding.com \
    --cc=thomas.perale@mind.be \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=tuzakli.refik@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.