scripts: add srecimage.pl, a script to convert binaries to SREC format
[openwrt/openwrt.git] / scripts / patch-specs.sh
1 #!/usr/bin/env bash
2
3 DIR="$1"
4
5 if [ -d "$DIR" ]; then
6 DIR="$(cd "$DIR"; pwd)"
7 else
8 echo "Usage: $0 toolchain-dir"
9 exit 1
10 fi
11
12 echo -n "Locating cpp ... "
13 for bin in bin usr/bin usr/local/bin; do
14 for cmd in "$DIR/$bin/"*-cpp; do
15 if [ -x "$cmd" ]; then
16 echo "$cmd"
17 CPP="$cmd"
18 break
19 fi
20 done
21 done
22
23 if [ ! -x "$CPP" ]; then
24 echo "Can't locate a cpp executable in '$DIR' !"
25 exit 1
26 fi
27
28 patch_specs() {
29 local found=0
30
31 for lib in $(STAGING_DIR="$DIR" "$CPP" -x c -v /dev/null 2>&1 | sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'); do
32 if [ -d "$lib" ]; then
33 grep -qs "STAGING_DIR" "$lib/specs" && rm -f "$lib/specs"
34 if [ $found -lt 1 ]; then
35 echo -n "Patching specs ... "
36 STAGING_DIR="$DIR" "$CPP" -dumpspecs | awk '
37 mode ~ "link" {
38 sub("%{L.}", "%{L*} -L %:getenv(STAGING_DIR /usr/lib) -rpath-link %:getenv(STAGING_DIR /usr/lib)")
39 }
40 mode ~ "cpp" {
41 $0 = $0 " -idirafter %:getenv(STAGING_DIR /usr/include)"
42 }
43 {
44 print $0
45 mode = ""
46 }
47 /^\*cpp:/ {
48 mode = "cpp"
49 }
50 /^\*link.*:/ {
51 mode = "link"
52 }
53 ' > "$lib/specs"
54 echo "ok"
55 found=1
56 fi
57 fi
58 done
59
60 [ $found -gt 0 ]
61 return $?
62 }
63
64
65 VERSION="$(STAGING_DIR="$DIR" "$CPP" --version | sed -ne 's/^.* (.*) //; s/ .*$//; 1p')"
66 VERSION="${VERSION:-unknown}"
67
68 case "${VERSION##* }" in
69 2.*|3.*|4.0.*|4.1.*|4.2.*)
70 echo "The compiler version does not support getenv() in spec files."
71 echo -n "Wrapping binaries instead ... "
72
73 if "${0%/*}/ext-toolchain.sh" --toolchain "$DIR" --wrap "${CPP%/*}"; then
74 echo "ok"
75 exit 0
76 else
77 echo "failed"
78 exit $?
79 fi
80 ;;
81 *)
82 if patch_specs; then
83 echo "Toolchain successfully patched."
84 exit 0
85 else
86 echo "Failed to locate library directory!"
87 exit 1
88 fi
89 ;;
90 esac