[RFC,v2,6/9] support/scripts: add create-sdk-relocation-script for SDK relocation

Submitted by Wolfgang Grandegger on March 16, 2017, 2:23 p.m.

Details

Message ID 1489674207-6876-7-git-send-email-wg@grandegger.com
State Superseded
Headers show

Commit Message

Wolfgang Grandegger March 16, 2017, 2:23 p.m.
It will create the script "relocate-sdk.sh" in HOST_DIR allowing
to adjust the path to the SDK directory in all text files after
it has been moved to a new location.

Signed-off-by: Wolfgang Grandegger <wg@grandegger.com>
---
 support/scripts/create-sdk-relocation-script | 72 ++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
 create mode 100755 support/scripts/create-sdk-relocation-script

Patch hide | download patch | download mbox

diff --git a/support/scripts/create-sdk-relocation-script b/support/scripts/create-sdk-relocation-script
new file mode 100755
index 0000000..7a3d5a3
--- /dev/null
+++ b/support/scripts/create-sdk-relocation-script
@@ -0,0 +1,72 @@ 
+#!/bin/sh
+
+# Creates the script "relocate-sdk.sh" and the "location" file in the
+# root directroy of the buildroot host tree. After copying that host
+# tree to a new location, the script in the top directory should be
+# executed to relocate the SDK. It actually will replace the string
+# of the old location with the new one in *all* text files.
+
+if [ "$#" -ne 1 -o ! -d "$1" ]; then
+    echo "Usage: $0 <buildroot-host-directory-path>"
+    exit 1
+fi
+
+# The SDK is under buildroot's "host" direrctory
+SDKDIR="$1"
+# We create the script in the top directory of the SDK
+SCRIPTFILE="${SDKDIR}/relocate-sdk.sh"
+# File holding the location of the buildroot SDK
+LOCATIONFILE="usr/share/buildroot/sdk-location"
+
+echo "Creating ${SCRIPTFILE} for SDK relocation ..."
+
+cat << EOF > "${SCRIPTFILE}"
+#!/bin/sh
+#
+# Automatically generated by $0: don't edit
+#
+if [ "\$#" -ne 0 ]; then
+    echo "Run this script to relocate the buildroot SDK at that location"
+    exit 1
+fi
+
+FILEPATH="\$(readlink -f \$0)"
+NEWPATH="\$(dirname \${FILEPATH})"
+
+cd \${NEWPATH}
+if [ ! -r ./${LOCATIONFILE} ]; then
+    echo "Previous location of the buildroot SDK not found!"
+    exit 1
+fi
+OLDPATH="\$(cat ./${LOCATIONFILE})"
+
+if [ \${NEWPATH} = \${OLDPATH} ]; then
+    echo "This buildroot SDK has already been relocated!"
+    exit 0
+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
+for FILE in \$(grep -r  "\${OLDPATH}"  . -l ) ; do
+    if [ ! -h \${FILE} ] && [ -n  "\$(file -b --mime-type \${FILE} | grep '^text/' )" ] && [ "\${FILE}" != "./${LOCATIONFILE}" ]
+    then
+        sed -i s,"\${OLDPATH}",\${NEWPATH},g \${FILE}
+    fi
+done
+
+# Finally, we update the location file itself
+sed -i s,"\${OLDPATH}",\${NEWPATH},g ./${LOCATIONFILE}
+
+# Check if the path substitution did work properly
+if [ "\${NEWPATH}" != "\$(cat ./${LOCATIONFILE})" ]; then
+    echo "Something went wrong with substituting the path!"
+    echo "Please choose another location for your SDK!"
+fi
+EOF
+chmod +x "${SCRIPTFILE}"
+mkdir -p $(dirname "$1/${LOCATIONFILE}")
+# Finally write the SDK location
+echo "$SDKDIR" > "${SDKDIR}/${LOCATIONFILE}"