a61c842c6dcba6ad7be9389085d64a8c94551465
[openwrt/openwrt.git] / scripts / set-abi-version.sh
1 #!/usr/bin/env bash
2 #
3 # Copyright (C) 2020 OpenWrt.org
4 #
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
7 #
8 SELF=${0##*/}
9
10 [ -n "$PATCHELF" ] || {
11 echo "$SELF: patchelf command not defined (PATCHELF variable not set)"
12 exit 1
13 }
14
15 ABI_VER="$1"
16 PATCH_DIR="$2"
17 REF_LIST="$3"
18
19 [ -n "$ABI_VER" -a -n "$PATCH_DIR" ] || {
20 echo "$SELF: no ABI version or files/directories specified"
21 echo "usage: $SELF <VERSION> [<PATH>...]"
22 exit 1
23 }
24
25 cmd() {
26 echo "$@" >&2
27 "$@" || exit 1
28 }
29
30 gen_lib_list() {
31 while read F; do
32 F="${F##*/}"
33 case "$F" in
34 lib*.so*);;
35 *) continue;;
36 esac
37 echo -n "$F:"
38 done < "$REF_LIST"
39 }
40
41 find "$PATCH_DIR" -type f -a -name 'lib*.so*' | \
42 (
43 while read F; do
44 NEW_F="${F%%.so*}.so.$ABI_VER"
45 NEW_NAME="${NEW_F##*/}"
46 [ "$NEW_F" != "$F" ] || continue
47 cmd mv "$F" "$NEW_F"
48 [ "$REF_LIST" ] || cmd ln -s "$NEW_NAME" "$F"
49 cmd $PATCHELF --set-soname "$NEW_NAME" "$NEW_F"
50 done
51 )
52
53 [ -n "$REF_LIST" ] || exit 0
54
55 LIBS="$(gen_lib_list)"
56 LIBS="${LIBS%%:}"
57 find "$PATCH_DIR" -type f -a -exec file {} \; | \
58 sed -n -e 's/^\(.*\):.*ELF.*\(executable\|relocatable\|shared object\).*,.*/\1:\2/p' | \
59 (
60 IFS=":"
61 while read F S; do
62 $PATCHELF --print-needed "$F" | while read L; do
63 BASE_L="${L%%.so*}"
64 for lib in $LIBS; do
65 base_lib="${lib%%.so*}"
66 [ "$BASE_L" = "$base_lib" ] || continue
67 [ "$l" = "$lib" ] && continue
68 cmd $PATCHELF --replace-needed "$L" "$lib" "$F"
69 done
70 done
71 done
72 true
73 )