From 469e1eb8a8b5251d2d4d4137cd138f8355e00d5e Mon Sep 17 00:00:00 2001 From: Nikolai Merinov Date: Tue, 11 Feb 2025 15:48:41 +0200 Subject: [PATCH] BuildSystem: Provide Overlay Feeds base infrastructure implementation Problem: OpenWRT Build system provide no way to extend package from 3rd party feed. As a result in order to manage CVE patches or small modification to a single package we should make copy of package in feeds that we own and monitor any changes in the package original feed to not miss upstream updates. Solution: Include all $(PKG_DIR_NAME)/Makefile.append files as part of first $(eval $(call BuildPackage,packagename)) call in original $(PKG_DIR_NAME)/Makefile. Add append directories to PKG_FILE_DEPENDS in order to invalidate build when append files changed. Together with this change provide several useful functions and variables: Variable $(THISDIR) -- defined in context of Makefile.append files to and points to the directory where Makefile.append recipe. Can be used to find additional patches, config fragments, etc. Variable $(OVERLAY_PATCHES) -- can be used to provide additional patch files for package sources in the following manner: OVERLAY_PATCHES := $(OVERLAY_PATCHES) $(THISDIR)/patches/filename.patch OVERLAY_PATCHES := $(OVERLAY_PATCHES) $(wildcard $(THISDIR)/patches/*.patch) Variable $(newline) -- contains single "\n" symbol. Required to append/prepend OpenWRT multiline variables with additional line. Functions (append,VARNAME,value[,override]) -- used to append "value" string to variable VARNAME. Behave like "+=" with two exceptions: 1. Append "value" without extra space before the value 2. The "value" expanded immediately before the assignment, while VARNAME is treated as recursive expand variable. Such behavior allow you to use $(THISDIR) variable together with "append" function. For example the following call will expand $(THISDIR) immediately: $(call append,Package/helloworld/config,$(newline)source "$(THISDIR)/Config.in") Function (prepend,VARNAME,value[,override]) -- used to prepend "value" string to variable VARNAME. Shares "append" function behavior regards expansion time. In order to append string with delayed expansion time you can replace $(...) with $$(...). For exaple the following call will delay $(Build/IncludeOverlay) expansion: $(call prepend,BuildPackage,$$(Build/IncludeOverlay)$(newline)) Function (remove,VARNAME,value[,override]) -- behave like "filter-out" function, but allow to control expansion time of the passed "value". Function (rewrite,VARNAME,value[,override]) -- generalized function that allow arbitrary change of VARNAME variable value controlling "value" expansion time. All this functions support optional last argument that allow to modify "override VARNAME" instead of non-override version of VARNAME. References: PCF-1615 Signed-off-by: Ivan Efimov Signed-off-by: Nicolai Scurtu Signed-off-by: Vadym Omelchuk Signed-off-by: Vladimir Shulika Signed-off-by: Nikolai Merinov --- include/overlay-feed/helpers.mk | 22 ++++++++++++++++++++++ include/overlay-feed/package.mk | 27 +++++++++++++++++++++++++++ include/package.mk | 2 ++ 3 files changed, 51 insertions(+) create mode 100644 include/overlay-feed/helpers.mk create mode 100644 include/overlay-feed/package.mk diff --git a/include/overlay-feed/helpers.mk b/include/overlay-feed/helpers.mk new file mode 100644 index 0000000000..6f894f32d2 --- /dev/null +++ b/include/overlay-feed/helpers.mk @@ -0,0 +1,22 @@ +define rewrite +$(eval $(3) define $(1) +$(2) +endef) +endef + +define prepend +$(call rewrite,$(1),$(2)$(value $(1)),$(3)) +endef + +define append +$(call rewrite,$(1),$(value $(1))$(2),$(3)) +endef + +define remove +$(call rewrite,$(1),$(filter-out $(2),$(value $(1))),$(3)) +endef + +define newline + + +endef diff --git a/include/overlay-feed/package.mk b/include/overlay-feed/package.mk new file mode 100644 index 0000000000..8b1478609c --- /dev/null +++ b/include/overlay-feed/package.mk @@ -0,0 +1,27 @@ +include $(INCLUDE_DIR)/overlay-feed/helpers.mk + +# All patches which paths added into OVERLAY_PATCHES +# will be applied on package build dir after Prepare step +# of main package +OVERLAY_PATCHES ?= + +define Overlay/Prepare/Patch + $(foreach pf,$(filter %.patch,$(OVERLAY_PATCHES)), \ + patch -d "$(PKG_BUILD_DIR)" -p1 < $(pf) $$(newline) \ + ) +endef +Hooks/Prepare/Post += Overlay/Prepare/Patch + +OVERLAY_MAKEFILE_APPENDS := $(foreach feed,$(shell $(TOPDIR)/scripts/feeds list -n 2>/dev/null),$(shell find -L $(TOPDIR)/feeds/$(feed)/ -path '*/$(PKG_DIR_NAME)/Makefile.append' | sort)) +PKG_FILE_DEPENDS += $(foreach mk,$(OVERLAY_MAKEFILE_APPENDS), $(dir $(mk))) + +define Build/IncludeOverlay + # Clean out Build/IncludeOverlay to prevent recursive expansion + $(eval Build/IncludeOverlay=) + $(foreach mk,$(OVERLAY_MAKEFILE_APPENDS), \ + $(eval THISDIR := $(dir $(mk))) \ + $(eval -include $(mk)) \ + ) +endef + +$(call prepend,BuildPackage,$$(Build/IncludeOverlay)$(newline)) diff --git a/include/package.mk b/include/package.mk index 7fbecf98dc..c906d5523c 100644 --- a/include/package.mk +++ b/include/package.mk @@ -379,3 +379,5 @@ dist: distcheck: $(Build/DistCheck) + +include $(INCLUDE_DIR)/overlay-feed/package.mk