Skip to contentSkip to conversation

core/sdk: pre-calculate files in need of relocation

PATCH Applied Florian Larysch 2 participants 3 messages May 31, 2025 – Feb 13 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 core/sdk: pre-calculate files in need of relocation

Currently, the relocate-sdk.sh script scans the whole extracted SDK tree to find instances of paths it needs to replace, which can take a significant amount of time when the SDK is large, particularly relative to the number of files that actually need to change.

However, the resulting list only depends on the SDK tarball itself, so we can calculate it at build time and ship it with the tarball so relocate-sdk.sh can use it directly.

Testing this on my machine with somewhat IOPS-limited rotating media, the time goes down from:

$ time ./relocate-sdk.sh
Relocating the buildroot SDK from [...] to [...] ...
./relocate-sdk.sh  5.19s user 26.21s system 9% cpu 5:34.40 total

To:

$ time ./relocate-sdk.sh
Relocating the buildroot SDK from [...] to [...] ...
./relocate-sdk.sh  0.49s user 0.29s system 103% cpu 0.749 total
---
 Makefile                     | 10 ++++++++++
 support/misc/relocate-sdk.sh | 11 +++--------
 2 files changed, 13 insertions(+), 8 deletions(-)
Makefile +10 -0
index c14023fd98..95f207150b 100644
@@ -600,6 +600,16 @@ prepare-sdk: world
 	@$(call MESSAGE,"Preparing the SDK")
 	$(INSTALL) -m 755 $(TOPDIR)/support/misc/relocate-sdk.sh $(HOST_DIR)/relocate-sdk.sh
 	mkdir -p $(HOST_DIR)/share/buildroot
+	(\
+		export LC_ALL=C; \
+		grep -lr '$(HOST_DIR)' '$(HOST_DIR)' | while read -r FILE; do \
+			if file -b --mime-type "$$FILE" | grep -q '^text/' && \
+			   [ "$$FILE" != '$(HOST_DIR)/share/buildroot/sdk-location' ] && \
+			   [ "$$FILE" != '$(HOST_DIR)/share/buildroot/sdk-relocs' ]; then \
+				echo "$$FILE"; \
+			fi; \
+		done \
+	) | sed -e 's|^$(HOST_DIR)|.|g' > $(HOST_DIR)/share/buildroot/sdk-relocs
 	echo $(HOST_DIR) > $(HOST_DIR)/share/buildroot/sdk-location
 
 BR2_SDK_PREFIX ?= $(GNU_TARGET_NAME)_sdk-buildroot
support/misc/relocate-sdk.sh +3 -8
index 981d272425..d25bc55279 100755
@@ -37,15 +37,10 @@ fi
 
 echo "Relocating the buildroot SDK from ${OLDPATH} to ${NEWPATH} ..."
 
-# 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 "${OLDPATH}" . | while read -r FILE ; do
-    if file -b --mime-type "${FILE}" | grep -q '^text/' && [ "${FILE}" != "${LOCFILE}" ]
-    then
-        sed -i "s|${OLDPATH}|${NEWPATH}|g" "${FILE}"
-    fi
-done
+while read -r FILE ; do
+    sed -i "s|${OLDPATH}|${NEWPATH}|g" "${FILE}"
+done < share/buildroot/sdk-relocs
 
 # At the very end, we update the location file to not break the
 # SDK if this script gets interruted.
Signature · 6 lines
2.49.0

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#1 lore raw
Quoted 16 lines · Florian Larysch

Currently, the relocate-sdk.sh script scans the whole extracted SDK tree to find instances of paths it needs to replace, which can take a significant amount of time when the SDK is large, particularly relative to the number of files that actually need to change.

However, the resulting list only depends on the SDK tarball itself, so we can calculate it at build time and ship it with the tarball so relocate-sdk.sh can use it directly.

Testing this on my machine with somewhat IOPS-limited rotating media, the time goes down from:

$ time ./relocate-sdk.sh
Relocating the buildroot SDK from [...] to [...] ...
./relocate-sdk.sh  5.19s user 26.21s system 9% cpu 5:34.40 total
Quoted 7 lines

To:

$ time ./relocate-sdk.sh
Relocating the buildroot SDK from [...] to [...] ...
./relocate-sdk.sh  0.49s user 0.29s system 103% cpu 0.749 total

Applied to master, thanks!

Regards,
Arnout

Quoted 39 lines
---
  Makefile                     | 10 ++++++++++
  support/misc/relocate-sdk.sh | 11 +++--------
  2 files changed, 13 insertions(+), 8 deletions(-)
Makefile +10 -0
index c14023fd98..95f207150b 100644
@@ -600,6 +600,16 @@ prepare-sdk: world
  	@$(call MESSAGE,"Preparing the SDK")
  	$(INSTALL) -m 755 $(TOPDIR)/support/misc/relocate-sdk.sh $(HOST_DIR)/relocate-sdk.sh
  	mkdir -p $(HOST_DIR)/share/buildroot
+	(\
+		export LC_ALL=C; \
+		grep -lr '$(HOST_DIR)' '$(HOST_DIR)' | while read -r FILE; do \
+			if file -b --mime-type "$$FILE" | grep -q '^text/' && \
+			   [ "$$FILE" != '$(HOST_DIR)/share/buildroot/sdk-location' ] && \
+			   [ "$$FILE" != '$(HOST_DIR)/share/buildroot/sdk-relocs' ]; then \
+				echo "$$FILE"; \
+			fi; \
+		done \
+	) | sed -e 's|^$(HOST_DIR)|.|g' > $(HOST_DIR)/share/buildroot/sdk-relocs
  	echo $(HOST_DIR) > $(HOST_DIR)/share/buildroot/sdk-location
 
  BR2_SDK_PREFIX ?= $(GNU_TARGET_NAME)_sdk-buildroot
support/misc/relocate-sdk.sh +0 -4
index 981d272425..d25bc55279 100755
@@ -37,15 +37,10 @@ fi
 
  echo "Relocating the buildroot SDK from ${OLDPATH} to ${NEWPATH} ..."
 
-# 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 "${OLDPATH}" . | while read -r FILE ; do
-    if file -b --mime-type "${FILE}" | grep -q '^text/' && [ "${FILE}" != "${LOCFILE}" ]

support/misc/relocate-sdk.sh fi · line 46

Quoted 10 lines
support/misc/relocate-sdk.sh +3 -4
-    then
-        sed -i "s|${OLDPATH}|${NEWPATH}|g" "${FILE}"
-    fi
-done
+while read -r FILE ; do
+    sed -i "s|${OLDPATH}|${NEWPATH}|g" "${FILE}"
+done < share/buildroot/sdk-relocs
 
  # At the very end, we update the location file to not break the
  # SDK if this script gets interruted.

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

#2 lore raw
In reply to Florian Larysch

In reply of:

Quoted 16 lines

Currently, the relocate-sdk.sh script scans the whole extracted SDK tree to find instances of paths it needs to replace, which can take a significant amount of time when the SDK is large, particularly relative to the number of files that actually need to change.

However, the resulting list only depends on the SDK tarball itself, so we can calculate it at build time and ship it with the tarball so relocate-sdk.sh can use it directly.

Testing this on my machine with somewhat IOPS-limited rotating media, the time goes down from:

$ time ./relocate-sdk.sh
Relocating the buildroot SDK from [...] to [...] ...
./relocate-sdk.sh  5.19s user 26.21s system 9% cpu 5:34.40 total
Quoted 7 lines

To:

$ time ./relocate-sdk.sh
Relocating the buildroot SDK from [...] to [...] ...
./relocate-sdk.sh  0.49s user 0.29s system 103% cpu 0.749 total

Applied to 2025.02.x & 2025.11.x. Thanks

Quoted 56 lines
---
 Makefile                     | 10 ++++++++++
 support/misc/relocate-sdk.sh | 11 +++--------
 2 files changed, 13 insertions(+), 8 deletions(-)
Makefile +10 -0
index c14023fd98..95f207150b 100644
@@ -600,6 +600,16 @@ prepare-sdk: world
 	@$(call MESSAGE,"Preparing the SDK")
 	$(INSTALL) -m 755 $(TOPDIR)/support/misc/relocate-sdk.sh $(HOST_DIR)/relocate-sdk.sh
 	mkdir -p $(HOST_DIR)/share/buildroot
+	(\
+		export LC_ALL=C; \
+		grep -lr '$(HOST_DIR)' '$(HOST_DIR)' | while read -r FILE; do \
+			if file -b --mime-type "$$FILE" | grep -q '^text/' && \
+			   [ "$$FILE" != '$(HOST_DIR)/share/buildroot/sdk-location' ] && \
+			   [ "$$FILE" != '$(HOST_DIR)/share/buildroot/sdk-relocs' ]; then \
+				echo "$$FILE"; \
+			fi; \
+		done \
+	) | sed -e 's|^$(HOST_DIR)|.|g' > $(HOST_DIR)/share/buildroot/sdk-relocs
 	echo $(HOST_DIR) > $(HOST_DIR)/share/buildroot/sdk-location
 
 BR2_SDK_PREFIX ?= $(GNU_TARGET_NAME)_sdk-buildroot
support/misc/relocate-sdk.sh +3 -8
index 981d272425..d25bc55279 100755
@@ -37,15 +37,10 @@ fi
 
 echo "Relocating the buildroot SDK from ${OLDPATH} to ${NEWPATH} ..."
 
-# 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 "${OLDPATH}" . | while read -r FILE ; do
-    if file -b --mime-type "${FILE}" | grep -q '^text/' && [ "${FILE}" != "${LOCFILE}" ]
-    then
-        sed -i "s|${OLDPATH}|${NEWPATH}|g" "${FILE}"
-    fi
-done
+while read -r FILE ; do
+    sed -i "s|${OLDPATH}|${NEWPATH}|g" "${FILE}"
+done < share/buildroot/sdk-relocs
 
 # At the very end, we update the location file to not break the
 # SDK if this script gets interruted.
Signature · 6 lines
2.49.0

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

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