[PATCH] scripts/ext-toolchain.sh: modify ar/ranlib scripts

Ye Holmes yeholmes at outlook.com
Mon Jan 4 07:26:44 EST 2021


Modify external toolchain scripts `PREFIX-ar and `PREFIX-ranlib
to handle object files compiled with LTO enabled; and simplify
external toolchain scripts `PREFIX-gcc-ar and `PREFIX-gcc-ranlib
to avoid failing to create static library archives.

Signed-off-by: Ye Holmes <yeholmes at outlook.com>
---
Using external prebuilt gcc toolchain, fails to create valid
static libraries via `PREFIX-ar tool due to LTO compilation
flag, which a number of packages has enabled, for example:

aarch64-none-linux-gnu-ar: CMakeFiles/ubusd_library.dir/ubusd.c.o:
  plugin needed to handle lto object
aarch64-none-linux-gnu-ranlib: libubusd_library.a(ubusd.c.o):
  plugin needed to handle lto object
[ 70%] Linking C executable ubusd
  openwrt/tmp/ubusd.lX2qbV.ltrans0.ltrans.o: in function `ubus_msg_dequeue':
  ubus-2020-12-04-d1d9ddf9/ubusd_main.c:32:
  undefined reference to `ubus_msg_free'

One possible solution is to force the `PREFIX-ar script to invoke
`PREFIX-gcc-ar (and `PREFIX-gcc-ranlib) unconditionally:
$ cat ./staging_dir/PREFIX/bin/aarch64-none-linux-gnu-ar
exec "/path/to/external/toolchain/aarch64-none-linux-gnu-gcc-ar" "$@"

Another problem with `PREFIX-gcc-ar and `PREFIX-gcc-ranlib is that they
have too many unnecessary options in the generated scripts, so that
they essentially does not work properly:

aarch64-none-linux-gnu-gcc-ar cr hostapd_multi.a LIST-OBJECTS.o:
aarch64-none-linux-gnu/bin/ar: two different operation options specified

A tested solution is to simplify `PREFIX-gcc-ar and `PREFIX-gcc-ranlib
scripts, the result is that `PREFIX-ar and `PREFIX-gcc-ar are identical,
while `PREFIX-ranlib and `PREFIX-gcc-ranlib scripts are identical, too.
---
 scripts/ext-toolchain.sh | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/scripts/ext-toolchain.sh b/scripts/ext-toolchain.sh
index ee7d9532f5..27c168789a 100755
--- a/scripts/ext-toolchain.sh
+++ b/scripts/ext-toolchain.sh
@@ -198,6 +198,33 @@ find_bins() {
 	return 1
 }
 
+wrap_bin_ar() {
+	local out="$1"
+	local bin="$2"
+
+	echo '#!/bin/sh'                                                   > "$out"
+	if [[ "$bin" =~ .+gcc-ar$ ]] ; then
+		echo 'exec "'"$bin"'" "$@"'                                   >> "$out"
+	else
+		echo 'exec "'"${bin%-ar}-gcc-ar"'" "$@"'                      >> "$out"
+	fi
+
+	chmod +x "$out"
+}
+
+wrap_bin_ranlib() {
+	local out="$1"
+	local bin="$2"
+
+	echo '#!/bin/sh'                                                   > "$out"
+	if [[ "$bin" =~ .+gcc-ranlib$ ]] ; then
+		echo 'exec "'"$bin"'" "$@"'                                   >> "$out"
+	else
+		echo 'exec "'"${bin%-ranlib}-gcc-ranlib"'" "$@"'              >> "$out"
+	fi
+
+	chmod +x "$out"
+}
 
 wrap_bin_cc() {
 	local out="$1"
@@ -256,6 +283,12 @@ wrap_bins() {
 				fi
 
 				case "${cmd##*/}" in
+					*-ar)
+						wrap_bin_ar "$out" "$bin"
+					;;
+					*-ranlib)
+						wrap_bin_ranlib "$out" "$bin"
+					;;
 					*-*cc|*-*cc-*|*-*++|*-*++-*|*-cpp)
 						wrap_bin_cc "$out" "$bin"
 					;;
-- 
2.25.1




More information about the openwrt-devel mailing list