summaryrefslogtreecommitdiff
path: root/build-common.sh
blob: e25bfc9a5c0e2154a2df03d2edfe3cc7a79a59e2 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# latest version of this file can be found at
# sso://googleplex-android/platform/external/lldb-utils
#
# inputs
# $PROJ - project name
# $VER - project version
# $1 - (temporary) output directory
# $2 - build directory for build artefacts
# $3 - build number
#
# this file does the following:
#
# 1) define the following env vars
#    OS - linux|darwin|windows
#    CORES - numer of cores (for parallel builds)
#    PATH (with appropriate compilers)
#    CFLAGS/CXXFLAGS/LDFLAGS
#    RD - root directory for source and object files
#    INSTALL - install directory
#    SCRIPT_FILE - absolute path to the parent build script
#    SCRIPT_DIR - absolute path to the parent build script's directory
#    COMMON_FILE - absolute path to this file
# 2) cd $RD
#
# after placing all your build products into $INSTALL you should call finalize_build to produce
# the final build artifact

# exit on error
set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[1]}")" && pwd -P)"
SCRIPT_FILE="$SCRIPT_DIR/$(basename "${BASH_SOURCE[1]}")"
COMMON_FILE="$SCRIPT_DIR/$(basename "${BASH_SOURCE[0]}")"

# calculate the root directory from the script path
# this script lives three directories down from the root
# external/lldb-utils/prebuilts/build-common.sh
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd -P)"
cd "$ROOT_DIR"

function die() {
	echo "$*" > /dev/stderr
	echo "Usage: $0 <out_dir> <dest_dir> <build_number>" > /dev/stderr
	exit 1
}

(($# > 3)) && die "[$0] Unknown parameter: $4"

OUT="$1"
DEST="$2"
BNUM="$3"

[ ! "$OUT"  ] && die "## Error: Missing out folder"
[ ! "$DEST" ] && die "## Error: Missing destination folder"
[ ! "$BNUM" ] && die "## Error: Missing build number"

mkdir -p "$OUT" "$DEST"
OUT="$(cd "$OUT" && pwd -P)"
DEST="$(cd "$DEST" && pwd -P)"

cat <<END_INFO
## Building $PROJ ##
## Out Dir  : $OUT
## Dest Dir : $DEST
## Build Num: $BNUM

END_INFO

UNAME="$(uname)"
UPSTREAM=sso://googleplex-android/platform/prebuilts
case "$UNAME" in
Linux)
    OS='linux'
    INSTALL_VER=$VER
    ;;
Darwin)
    OS='darwin'
    OSX_MIN=10.8
    export CC=clang
    export CXX=$CC++
    export CFLAGS="$CFLAGS -mmacosx-version-min=$OSX_MIN"
    export CXXFLAGS="$CXXFLAGS -mmacosx-version-min=$OSX_MIN -stdlib=libc++"
    export LDFLAGS="$LDFLAGS -mmacosx-version-min=$OSX_MIN"
    INSTALL_VER=$VER
    ;;
*_NT-*)
    OS='windows'
    CORES=$NUMBER_OF_PROCESSORS
    # VS2013 x64 Native Tools Command Prompt
    case "$MSVS" in
    2013)
        devenv() {
            cmd /c "${VS120COMNTOOLS}VsDevCmd.bat" '&' devenv.com "$@"
        }
        INSTALL_VER=${VER}_${MSVS}
        ;;
    *)
        # g++/make build
        export CC=x86_64-w64-mingw32-gcc
        export CXX=x86_64-w64-mingw32-g++
        export LD=x86_64-w64-mingw32-ld
        ;;
    esac
    ;;
*)
    exit 1
    ;;
esac

RD=$OUT/$PROJ
INSTALL="$RD/install"

cd /tmp # windows can't delete if you're in the dir
rm -rf $RD
mkdir -p $INSTALL
mkdir -p $RD
cd $RD

# clone prebuilt gcc
case "$OS" in
linux)
    # can't get prebuilt clang working so we're using host clang-3.5 https://b/22748915
    #CLANG_DIR=$RD/clang
    #git clone $UPSTREAM/clang/linux-x86/host/3.6 $CLANG_DIR
    #export CC="$CLANG_DIR/bin/clang"
    #export CXX="$CC++"
    export CC=clang-3.5
    export CXX=clang++-3.5

    GCC_DIR=$RD/gcc
    git clone $UPSTREAM/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8 $GCC_DIR

    find "$GCC_DIR" -name x86_64-linux -exec ln -fns {} {}-gnu \;

    FLAGS+=(-fuse-ld=gold)
    FLAGS+=(--gcc-toolchain="$GCC_DIR")
    FLAGS+=(--sysroot "$GCC_DIR/sysroot")
    FLAGS+=(-B"$GCC_DIR/bin/x86_64-linux-")
    export CFLAGS="$CFLAGS ${FLAGS[*]}"
    export CXXFLAGS="$CXXFLAGS ${FLAGS[*]}"
    export LDFLAGS="$LDFLAGS -m64"
    ;;
esac

function finalize_build() {
    cp "$SCRIPT_FILE" "$COMMON_FILE" "$INSTALL"
    (cd "$INSTALL" && zip --symlinks -r "$DEST/$PROJ-$BNUM.zip" .)
}