summaryrefslogtreecommitdiff
path: root/static/build.sh
blob: 37cfabf742adedaebdb779e20e759289e4d70917 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/sh

# This script is based on static/build.sh from the ncdc git repo.
# Only i486 and arm arches are supported. i486 should perform well enough, so
# x86_64 isn't really necessary. I can't test any other arches.
#
# This script assumes that you have the musl-cross cross compilers installed in
# $MUSL_CROSS_PATH.
#
# Usage:
#   ./build.sh $arch
#   where $arch = 'arm', 'i486' or 'x86_64'

MUSL_CROSS_PATH=/opt/cross
NCURSES_VERSION=6.0

export CFLAGS="-O3 -g -static"

# (The variables below are automatically set by the functions, they're defined
# here to make sure they have global scope and for documentation purposes.)

# This is the arch we're compiling for, e.g. arm/mipsel.
TARGET=
# This is the name of the toolchain we're using, and thus the value we should
# pass to autoconf's --host argument.
HOST=
# Installation prefix.
PREFIX=
# Path of the extracted source code of the package we're currently building.
srcdir=

mkdir -p tarballs


# "Fetch, Extract, Move"
fem() { # base-url name targerdir extractdir
  echo "====== Fetching and extracting $1 $2"
  cd tarballs
  if [ -n "$4" ]; then
    EDIR="$4"
  else
    EDIR=$(basename $(basename $(basename $2 .tar.bz2) .tar.gz) .tar.xz)
  fi
  if [ ! -e "$2" ]; then
    wget "$1$2" || exit
  fi
  if [ ! -d "$3" ]; then
    tar -xvf "$2" || exit
    mv "$EDIR" "$3"
  fi
  cd ..
}


prebuild() { # dirname
  if [ -e "$TARGET/$1/_built" ]; then
    echo "====== Skipping build for $TARGET/$1 (assumed to be done)"
    return 1
  fi
  echo "====== Starting build for $TARGET/$1"
  rm -rf "$TARGET/$1"
  mkdir -p "$TARGET/$1"
  cd "$TARGET/$1"
  srcdir="../../tarballs/$1"
  return 0
}


postbuild() {
  touch _built
  cd ../..
}


getncurses() {
  fem http://ftp.gnu.org/pub/gnu/ncurses/ ncurses-$NCURSES_VERSION.tar.gz ncurses
  prebuild ncurses || return
  $srcdir/configure --prefix=$PREFIX\
    --without-cxx --without-cxx-binding --without-ada --without-manpages --without-progs\
    --without-tests --without-curses-h --without-pkg-config --without-shared --without-debug\
    --without-gpm --without-sysmouse --enable-widec --with-default-terminfo-dir=/usr/share/terminfo\
    --with-terminfo-dirs=/usr/share/terminfo:/lib/terminfo:/usr/local/share/terminfo\
    --with-fallbacks="screen linux vt100 xterm xterm-256color" --host=$HOST\
    CPPFLAGS=-D_GNU_SOURCE || exit
  make || exit
  make install.libs || exit
  postbuild
}


getncdu() {
  prebuild ncdu || return
  srcdir=../../..
  $srcdir/configure --host=$HOST --with-ncursesw PKG_CONFIG=false\
    CPPFLAGS="-I$PREFIX/include -I$PREFIX/include/ncursesw"\
    LDFLAGS="-static -L$PREFIX/lib -lncursesw" CFLAGS="$CFLAGS -Wall -Wextra" || exit
  make || exit

  VER=`cd '../../..' && git describe --abbrev=5 --dirty= | sed s/^v//`
  tar -czf ../../ncdu-linux-$TARGET-$VER-unstripped.tar.gz ncdu
  $HOST-strip ncdu
  tar -czf ../../ncdu-linux-$TARGET-$VER.tar.gz ncdu
  echo "====== ncdu-linux-$TARGET-$VER.tar.gz and -unstripped created."

  postbuild
}


buildarch() {
  TARGET=$1
  case $TARGET in
    arm)    HOST=arm-linux-musleabi  DIR=arm-linux-musleabi ;;
    aarch64)HOST=aarch64-linux-musl  DIR=aarch64-linux-musl ;;
    i486)   HOST=i486-linux-musl     DIR=i486-linux-musl    ;;
    x86_64) HOST=x86_64-linux-musl   DIR=x86_64-linux-musl  ;;
    *)      echo "Unknown target: $TARGET" ;;
  esac
  PREFIX="`pwd`/$TARGET/inst"
  mkdir -p $TARGET $PREFIX
  ln -s lib $PREFIX/lib64

  OLDPATH="$PATH"
  export PATH="$PATH:$MUSL_CROSS_PATH/$DIR/bin"
  getncurses
  getncdu
  PATH="$OLDPATH"
}


buildarch $1