Skip to contentSkip to conversation

Makefile: fix SDK relocation for per-package-dirs

PATCH In review Brandon Maier via buildroot 2 participants 3 messages Nov 10, 2022 – Nov 28, 2022 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Makefile: fix SDK relocation for per-package-dirs

The relocate-sdk.sh script does not work correctly when BR2_PER_PACKAGE_DIRECTORIES is enabled. relocate-sdk.sh expects everything to point at $HOST_DIR, but each package will be pointing at its $(O)/per-package/*/host.

Before packing up the SDK, scrub the HOST_DIR to replace all matches of the per-package directory to point at the final HOST_DIR.

---
 DEVELOPERS                 |  1 +
 Makefile                   |  1 +
 support/scripts/fix-perpkg | 31 +++++++++++++++++++++++++++++++
 3 files changed, 33 insertions(+)
 create mode 100755 support/scripts/fix-perpkg
DEVELOPERS +1 -0
index 81e6cd54ab..238f8754e3 100644
@@ -488,6 +488,7 @@ F:	package/ncdu/
 
 N:	Brandon Maier <[email protected]>
 F:	package/vmtouch/
+F:	support/scripts/fix-perpkg
 
 N:	Brock Williams <[email protected]>
 F:	package/pdmenu/
Makefile +1 -0
index 7c1c07a2e4..929042396a 100644
@@ -595,6 +595,7 @@ prepare-sdk: world
 	@$(call MESSAGE,"Rendering the SDK relocatable")
 	PER_PACKAGE_DIR=$(PER_PACKAGE_DIR) $(TOPDIR)/support/scripts/fix-rpath host
 	PER_PACKAGE_DIR=$(PER_PACKAGE_DIR) $(TOPDIR)/support/scripts/fix-rpath staging
+	PER_PACKAGE_DIR=$(PER_PACKAGE_DIR) $(TOPDIR)/support/scripts/fix-perpkg
 	$(INSTALL) -m 755 $(TOPDIR)/support/misc/relocate-sdk.sh $(HOST_DIR)/relocate-sdk.sh
 	mkdir -p $(HOST_DIR)/share/buildroot
 	echo $(HOST_DIR) > $(HOST_DIR)/share/buildroot/sdk-location
support/scripts/fix-perpkg new file +31 -0
new file mode 100755
index 0000000000..56d7d7a45d
@@ -0,0 +1,31 @@
+#!/usr/bin/env bash
+
+usage() {
+	cat <<-EOF >&2
+	Usage: fix-perpkg
+
+	This script rewrites file's PER_PACKAGE_DIR to point at the HOST_DIR
+	before they are bundled into an SDK. So that relocate-sdk.sh can find
+	and relocate them.
+	EOF
+	exit 1
+}
+
+if [ "$#" -gt 1 ]; then
+	usage
+fi
+
+# Check if per-package-directories is disabled
+if ! [ -e "$PER_PACKAGE_DIR" ]; then
+	exit
+fi
+
+# Make sure `file` uses the right language
+export LC_ALL=C
+
+# Replace the old path with the new one in all text files
+grep -lr "${PER_PACKAGE_DIR}/[-_a-zA-Z0-9]\+/host" "${HOST_DIR}" \
+	| file --mime-type -f - \
+	| grep ': \+text/[-_a-zA-Z0-9]*$' \
+	| sed 's|: \+text/[-_a-zA-Z0-9]*$||' \
+	| xargs -r sed -i "s|${PER_PACKAGE_DIR}/[-_a-zA-Z0-9]\+/host|${HOST_DIR}|g"
Signature · 6 lines
2.38.1

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#1 lore raw

Hi Brandon,

I was going to apply, but I have a few comments/improvements that I prefer if
you bring them to completion.

Quoted 33 lines · Brandon Maier via buildroot

The relocate-sdk.sh script does not work correctly when BR2_PER_PACKAGE_DIRECTORIES is enabled. relocate-sdk.sh expects everything to point at $HOST_DIR, but each package will be pointing at its $(O)/per-package/*/host.

Before packing up the SDK, scrub the HOST_DIR to replace all matches of the per-package directory to point at the final HOST_DIR.

---
  DEVELOPERS                 |  1 +
  Makefile                   |  1 +
  support/scripts/fix-perpkg | 31 +++++++++++++++++++++++++++++++
  3 files changed, 33 insertions(+)

create mode 100755 support/scripts/fix-perpkg

DEVELOPERS +1 -0
index 81e6cd54ab..238f8754e3 100644
@@ -488,6 +488,7 @@ F:	package/ncdu/
 
  N:	Brandon Maier <[email protected]>
  F:	package/vmtouch/
+F:	support/scripts/fix-perpkg
 
  N:	Brock Williams <[email protected]>
  F:	package/pdmenu/
Makefile +0 -0
index 7c1c07a2e4..929042396a 100644

Makefile prepare-sdk: world · line 598

Quoted 4 lines
Makefile +1 -0
@@ -595,6 +595,7 @@ prepare-sdk: world
  	@$(call MESSAGE,"Rendering the SDK relocatable")
  	PER_PACKAGE_DIR=$(PER_PACKAGE_DIR) $(TOPDIR)/support/scripts/fix-rpath host
  	PER_PACKAGE_DIR=$(PER_PACKAGE_DIR) $(TOPDIR)/support/scripts/fix-rpath staging
+	PER_PACKAGE_DIR=$(PER_PACKAGE_DIR) $(TOPDIR)/support/scripts/fix-perpkg

I'm not sure if it's really useful to make a separate script out of this -
it's just a simple pipeline after all.

Quoted 18 lines
Makefile +0 -0
  	$(INSTALL) -m 755 $(TOPDIR)/support/misc/relocate-sdk.sh $(HOST_DIR)/relocate-sdk.sh
  	mkdir -p $(HOST_DIR)/share/buildroot
  	echo $(HOST_DIR) > $(HOST_DIR)/share/buildroot/sdk-location
support/scripts/fix-perpkg new file +9 -0
new file mode 100755
index 0000000000..56d7d7a45d
@@ -0,0 +1,31 @@
+#!/usr/bin/env bash
+
+usage() {
+	cat <<-EOF >&2
+	Usage: fix-perpkg
+
+	This script rewrites file's PER_PACKAGE_DIR to point at the HOST_DIR
+	before they are bundled into an SDK. So that relocate-sdk.sh can find
+	and relocate them.

support/scripts/fix-perpkg line 19

Quoted 10 lines
support/scripts/fix-perpkg new file +10 -0
+	EOF
+	exit 1
+}
+
+if [ "$#" -gt 1 ]; then
+	usage
+fi
+
+# Check if per-package-directories is disabled
+if ! [ -e "$PER_PACKAGE_DIR" ]; then

PER_PACKAGE_DIR is always set, even if per-package-directories is disabled, so
the comment is wrong. I anyway don't think it's necessary to check for this - if PPD is disabled, then there will never be a match.

support/scripts/fix-perpkg line 27

Quoted 8 lines
support/scripts/fix-perpkg new file +8 -0
+	exit
+fi
+
+# Make sure `file` uses the right language
+export LC_ALL=C
+
+# Replace the old path with the new one in all text files
+grep -lr "${PER_PACKAGE_DIR}/[-_a-zA-Z0-9]\+/host" "${HOST_DIR}" \

Check what is used in PPD_FIXUP_PATHS:

grep --binary-files=without-match -lr '$(PER_PACKAGE_DIR)/[^/]\+/' $(HOST_DIR)

The --binary-files=without-match speeds things up a lot because it will
immediately skip large binary files.

PPD_FIXUP_PATHS also uses a shell loop instead of xargs; not sure which one is
better.

support/scripts/fix-perpkg line 30

Quoted 3 lines
support/scripts/fix-perpkg new file +3 -0
+	| file --mime-type -f - \
+	| grep ': \+text/[-_a-zA-Z0-9]*$' \
+	| sed 's|: \+text/[-_a-zA-Z0-9]*$||' \

You can merge those two in a single

	| sed -n '/\(.*\): \+text\/[-_a-zA-Z0-9]*$/s//\1/p' \

support/scripts/fix-perpkg line 31

Quoted 1 line
support/scripts/fix-perpkg new file +1 -0
+	| xargs -r sed -i "s|${PER_PACKAGE_DIR}/[-_a-zA-Z0-9]\+/host|${HOST_DIR}|g"

There is no constraint on package names containing only those symbols, so
better use [^/]\+ instead.

However, I think it would be better to refactor fix-perpkg against
PPD_FIXUP_PATHS. It only differens in the replacement part of the final sed, so that can easily be done with a make function.

Regards,
Arnout
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#2 lore raw
Quoted 10 lines

From: Arnout Vandecappelle <[email protected]>

support/scripts/fix-perpkg line 27

Quoted 2 lines · Brandon Maier via buildroot
support/scripts/fix-perpkg new file +2 -0
+# Replace the old path with the new one in all text files
+grep -lr "${PER_PACKAGE_DIR}/[-_a-zA-Z0-9]\+/host" "${HOST_DIR}" \

Check what is used in PPD_FIXUP_PATHS:

grep --binary-files=without-match -lr '$(PER_PACKAGE_DIR)/[^/]\+/' $(HOST_DIR)

Quoted 6 lines

The --binary-files=without-match speeds things up a lot because it will
immediately skip large binary files.

PPD_FIXUP_PATHS also uses a shell loop instead of xargs; not sure which
one is
better.

Before switching to --binary-files, the shell for-loop was much slower closer to a minute for me. With --binary-files it's about 5 seconds, which is closer to the xargs which is about 1 second. So I don't mind just using the PPD_FIXUP_PATHS code.

Quoted 5 lines

However, I think it would be better to refactor fix-perpkg against
PPD_FIXUP_PATHS. It only differens in the replacement part of the final sed, so
that can easily be done with a make function.

Agreed with all comments. And reusing PPD_FIXUP_PATHS will address them. Will send updated patch shortly.

Thanks,
Brandon

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot