From 134900401f0831f893ddd4f358e499f548d66433 Mon Sep 17 00:00:00 2001 From: Victor Dumas Date: Thu, 20 Oct 2022 13:55:12 +0200 Subject: [PATCH] support/scripts/fix-rpath: parallelize patching files Using "xargs" instead of "while read" loop allows for the patching of files to be parallelized. This significantly reduces the amount of time it takes to fix all the paths. On a larger RFS(~300MB) this script was taking 5 minutes, it now only takes about 30s on a 12 core machine. Signed-off-by: Victor Dumas [Thomas: take into account the suggestion of Quentin Schulz to pass PARALLEL_JOBS through the environment down to the fix-rpath script] Signed-off-by: Thomas Petazzoni --- Makefile | 12 ++++++-- support/scripts/fix-rpath | 65 +++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index 18e337006b..e5aae17602 100644 --- a/Makefile +++ b/Makefile @@ -594,8 +594,12 @@ world: target-post-image .PHONY: prepare-sdk 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 + PARALLEL_JOBS=$(PARALLEL_JOBS) \ + PER_PACKAGE_DIR=$(PER_PACKAGE_DIR) \ + $(TOPDIR)/support/scripts/fix-rpath host + PARALLEL_JOBS=$(PARALLEL_JOBS) \ + PER_PACKAGE_DIR=$(PER_PACKAGE_DIR) \ + $(TOPDIR)/support/scripts/fix-rpath staging $(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 @@ -765,7 +769,9 @@ endif ln -sf ../usr/lib/os-release $(TARGET_DIR)/etc @$(call MESSAGE,"Sanitizing RPATH in target tree") - PER_PACKAGE_DIR=$(PER_PACKAGE_DIR) $(TOPDIR)/support/scripts/fix-rpath target + PARALLEL_JOBS=$(PARALLEL_JOBS) \ + PER_PACKAGE_DIR=$(PER_PACKAGE_DIR) \ + $(TOPDIR)/support/scripts/fix-rpath target # For a merged /usr, ensure that /lib, /bin and /sbin and their /usr # counterparts are appropriately setup as symlinks ones to the others. diff --git a/support/scripts/fix-rpath b/support/scripts/fix-rpath index 3e67e770e5..a9b348e189 100755 --- a/support/scripts/fix-rpath +++ b/support/scripts/fix-rpath @@ -46,6 +46,8 @@ Environment: TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR (default HOST_DIR/opt/ext-toolchain) + PARALLEL_JOBS number of parallel jobs to run + Returns: 0 if success or 1 in case of error EOF @@ -58,6 +60,38 @@ HOST_EXCLUDEPATHS="/share/terminfo" STAGING_EXCLUDEPATHS="/usr/include /usr/share/terminfo" TARGET_EXCLUDEPATHS="/lib/firmware" +patch_file() { + PATCHELF="${1}" + rootdir="${2}" + sanitize_extra_args="${3}" + file="${4}" + + # check if it's an ELF file + rpath=$(${PATCHELF} --print-rpath "${file}" 2>&1) + if test $? -ne 0 ; then + return 0 + fi + + # make files writable if necessary + changed=$(chmod -c u+w "${file}") + + # With per-package directory support, most RPATH of host + # binaries will point to per-package directories. This won't + # work with the --make-rpath-relative ${rootdir} invocation as + # the per-package host directory is not within ${rootdir}. So, + # we rewrite all RPATHs pointing to per-package directories so + # that they point to the global host directry. + changed_rpath=$(echo ${rpath} | sed "s@${PER_PACKAGE_DIR}/[^/]\+/host@${HOST_DIR}@") + if test "${rpath}" != "${changed_rpath}" ; then + ${PATCHELF} --set-rpath ${changed_rpath} "${file}" + fi + + # call patchelf to sanitize the rpath + ${PATCHELF} --make-rpath-relative "${rootdir}" ${sanitize_extra_args[@]} "${file}" + # restore the original permission + test "${changed}" != "" && chmod u-w "${file}" +} + main() { local rootdir local tree="${1}" @@ -123,34 +157,11 @@ main() { ;; esac - find_args+=( "-type" "f" "-print" ) + find_args+=( "-type" "f" "-print0" ) - while read file ; do - # check if it's an ELF file - rpath=$(${PATCHELF} --print-rpath "${file}" 2>&1) - if test $? -ne 0 ; then - continue - fi - - # make files writable if necessary - changed=$(chmod -c u+w "${file}") - - # With per-package directory support, most RPATH of host - # binaries will point to per-package directories. This won't - # work with the --make-rpath-relative ${rootdir} invocation as - # the per-package host directory is not within ${rootdir}. So, - # we rewrite all RPATHs pointing to per-package directories so - # that they point to the global host directry. - changed_rpath=$(echo ${rpath} | sed "s@${PER_PACKAGE_DIR}/[^/]\+/host@${HOST_DIR}@") - if test "${rpath}" != "${changed_rpath}" ; then - ${PATCHELF} --set-rpath ${changed_rpath} "${file}" - fi - - # call patchelf to sanitize the rpath - ${PATCHELF} --make-rpath-relative "${rootdir}" ${sanitize_extra_args[@]} "${file}" - # restore the original permission - test "${changed}" != "" && chmod u-w "${file}" - done < <(find "${rootdir}" ${find_args[@]}) + export -f patch_file + # Limit the number of cores used + find "${rootdir}" ${find_args[@]} | xargs -0 -r -P ${PARALLEL_JOBS} -I {} bash -c "patch_file '${PATCHELF}' '${rootdir}' '${sanitize_extra_args}' $@" _ {} # Restore patched patchelf utility test "${tree}" = "host" && mv "${PATCHELF}.__to_be_patched" "${PATCHELF}"