build: refactor dependency formatting

Refactor dependencies and extra dependencies logic into a helper define
and document it.

Signed-off-by: George Sapkin <george@sapk.in>
Link: https://github.com/openwrt/openwrt/pull/21369
(cherry picked from commit 779fa7ff6c)
Link: https://github.com/openwrt/openwrt/pull/21547
Signed-off-by: Robert Marko <robimarko@gmail.com>
This commit is contained in:
George Sapkin 2026-01-04 03:46:38 +02:00 committed by Robert Marko
parent 5976c47b9f
commit b4e537e9cd

View file

@ -78,6 +78,55 @@ define FixupDependencies
$(call AddDependency,$(1),$$(DEPS))
endef
# Format dependencies and extra dependencies
#
# ABI-version EXTRA_DEPENDS so dependencies can be correctly looked up using the
# existing semantics without the ABI specified. This is needed since ABI-
# versioned libraries don't provide `${package_name}=${package_version}`, so
# that same library but with different ABI versions can be installed side by
# side.
#
# Remove duplicate dependencies when EXTRA_DEPENDS specifies a versioned one
# that is already in DEPENDS.
#
# 1: list of dependencies
# 2: list of extra dependencies
define FormatDepends
$(strip
$(eval _COMMA_SEP := __COMMA_SEP__)
$(eval _SPACE_SEP := __SPACE_SEP__)
$(eval _DEPENDS := $(1))
$(eval _EXTRA_DEPENDS_ABI := )
$(eval _DEP_ITEMS := $(subst $(_COMMA_SEP),$(space),$(subst $(space),$(_SPACE_SEP),$(subst $(comma),$(_COMMA_SEP),$(2)))))
$(foreach dep,$(_DEP_ITEMS),
$(eval _EXTRA_DEP := )
$(eval _CUR_DEP := $(subst $(_SPACE_SEP),$(space),$(strip $(dep))))
$(eval _PKG_NAME := $(word 1,$(_CUR_DEP)))
$(if $(findstring $(paren_left), $(_PKG_NAME)),
$(error "Unsupported extra dependency format: no space before '(': $(_CUR_DEP)"))
)
$(eval _ABI_SUFFIX := $(call GetABISuffix,$(_PKG_NAME)))
$(eval _PKG_NAME_ABI := $(_PKG_NAME)$(_ABI_SUFFIX))
$(eval _VERSION_CONSTRAINT := $(word 2,$(_CUR_DEP)))
$(if $(_VERSION_CONSTRAINT),
$(eval _EXTRA_DEP := $(_PKG_NAME_ABI) $(_VERSION_CONSTRAINT)),
$(error "Extra dependencies must have version constraints. $(_PKG_NAME) seems to be unversioned.")
)
$(if $(and $(_EXTRA_DEPENDS_ABI),$(_EXTRA_DEP)),
$(eval _EXTRA_DEPENDS_ABI := $(_EXTRA_DEPENDS_ABI)$(comma)$(_EXTRA_DEP)),
$(eval _EXTRA_DEPENDS_ABI := $(_EXTRA_DEP))
)
$(if $(_DEPENDS),
$(eval _DEPENDS := $(filter-out $(_PKG_NAME_ABI),$(_DEPENDS)))
)
)
$(eval _DEPENDS := $(call mergelist,$(_DEPENDS)))
$(_EXTRA_DEPENDS_ABI)$(if $(_DEPENDS),$(comma) $(_DEPENDS))
)
endef
# Format provide and add ABI and version if it's not a virtual provide marked
# with an @.
#
@ -308,36 +357,7 @@ endif
Package/$(1)/DEPENDS := $$(call mergelist,$$(Package/$(1)/DEPENDS))
Package/$(1)/DEPENDS := $$(EXTRA_DEPENDS)$$(if $$(Package/$(1)/DEPENDS),$$(comma) $$(Package/$(1)/DEPENDS))
else
_SEP := __COMMA_SEP__
_SPACE := __SPACE_SEP__
_DEPENDS := $$(Package/$(1)/DEPENDS)
_EXTRA_DEPENDS_ABI :=
_DEP_ITEMS := $$(subst $$(_SEP),$$(space),$$(subst $$(space),$$(_SPACE),$$(subst $$(comma),$$(_SEP),$$(EXTRA_DEPENDS))))
$$(foreach dep,$$(_DEP_ITEMS), \
$$(eval _CUR_DEP := $$(subst $$(_SPACE),$$(space),$$(strip $$(dep)))) \
$$(eval _PKG_NAME := $$(word 1,$$(_CUR_DEP))) \
$$(if $$(findstring $$(paren_left), $$(_PKG_NAME)), \
$$(error "Unsupported extra dependency format: no space before '(': $$(_CUR_DEP)")) \
) \
$$(eval _ABI_SUFFIX := $$(call GetABISuffix,$$(_PKG_NAME))) \
$$(eval _PKG_NAME_ABI := $$(_PKG_NAME)$$(_ABI_SUFFIX)) \
$$(eval _VERSION_CONSTRAINT := $$(word 2,$$(_CUR_DEP))) \
$$(if $$(_VERSION_CONSTRAINT), \
$$(eval _EXTRA_DEP := $$(_PKG_NAME_ABI) $$(_VERSION_CONSTRAINT)), \
$$(error "Extra dependencies must have version constraints. $$(_PKG_NAME) seems to be unversioned.") \
) \
$$(if $$(_EXTRA_DEPENDS_ABI), \
$$(eval _EXTRA_DEPENDS_ABI := $$(_EXTRA_DEPENDS_ABI)$$(comma)$$(_EXTRA_DEP)), \
$$(eval _EXTRA_DEPENDS_ABI := $$(_EXTRA_DEP)) \
) \
$$(if $$(_DEPENDS), \
$$(eval _DEPENDS := $$(filter-out $$(_PKG_NAME_ABI),$$(_DEPENDS))) \
) \
)
_DEPENDS := $$(call mergelist,$$(_DEPENDS))
Package/$(1)/DEPENDS := $$(_EXTRA_DEPENDS_ABI)$$(if $$(_DEPENDS),$$(comma) $$(_DEPENDS))
Package/$(1)/DEPENDS := $$(call FormatDepends,$$(Package/$(1)/DEPENDS),$$(EXTRA_DEPENDS))
endif
else
Package/$(1)/DEPENDS := $$(call mergelist,$$(Package/$(1)/DEPENDS))