aboutsummaryrefslogtreecommitdiff
path: root/scripts/cross.sh
blob: bdf03ed983788189f9cae7e4f6197db63e0ef92a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash

# Convenience wrapper to set $CROSS_COMPILE from short name using "ccc"
# symlink (Cross C Compiler) to a directory of cross compilers named
# $TARGET-*-cross. Tested with scripts/mcm-buildall.sh output.

# Usage: scripts/cross.sh $TARGET make distclean defconfig toybox
# With no arguments, lists available targets. Use target "all" to iterate
# through each $TARGET from the list.

CCC="$(dirname "$(readlink -f "$0")")"/../ccc
if [ ! -d "$CCC" ]
then
  echo "Create symlink 'ccc' to cross compiler directory, ala:"
  echo "  ln -s ~/musl-cross-make/ccc ccc"
  exit 1
fi

unset X Y

# Display target list?
list()
{
  ls "$CCC" | sed 's/-.*//' | sort -u | xargs
}
[ $# -eq 0 ] && list && exit

[ -z "$TOP" ] && TOP="$PWD/root/log"
mkdir -p "$TOP" || exit 1

X="$1"
shift

# build all targets?
if [ "$X" == all ]
then
  for TARGET in $(list)
  do
    LOG="$TOP/cross-log-$TARGET"
    {
      export TARGET
      echo -en "\033]2;$TARGET $*\007"

      rm -f "$LOG".{failed,success}
      "$0" $TARGET "$@" 2>&1
      X=$?
      [ $X -eq 0 -o $X -eq 42 ] && mv "$LOG".{txt,success}
    } |& tee "$LOG".txt
    [ -z "$ALL" ] && [ ! -e "$LOG".success ] &&
      { mv "$LOG".{txt,failed} ; break;}
  done

  exit
fi

# Call command with CROSS_COMPILE= as its first argument

Y=$(echo "$CCC/$X"-*cross)
Z=$(basename "$Y")
Y=$(readlink -f "$CCC"/$X-*cross)
export TARGET="${Z/-*/}"
X="$Y/bin/${Z/-cross/-}"
[ ! -e "${X}cc" ] && echo "${X}cc not found" && exit 1

CROSS_COMPILE="$X" "$@"