summaryrefslogtreecommitdiff
path: root/build.sh
blob: dc0e26b64c686afe0fe0dfc9d0a562fb4f616af1 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env bash
# This script builds NSS with gyp and ninja.
#
# This build system is still under development.  It does not yet support all
# the features or platforms that NSS supports.

set -e

source $(dirname $0)/coreconf/nspr.sh

# Usage info
show_help() {
cat << EOF

Usage: ${0##*/} [-hcv] [-j <n>] [--nspr] [--gyp|-g] [--opt|-o] [-m32]
                [--test] [--fuzz] [--pprof] [--scan-build[=output]]
                [--asan] [--ubsan] [--msan] [--sancov[=edge|bb|func|...]]

This script builds NSS with gyp and ninja.

This build system is still under development.  It does not yet support all
the features or platforms that NSS supports.

NSS build tool options:

    -h            display this help and exit
    -c            clean before build
    -v            verbose build
    -j <n>        run at most <n> concurrent jobs
    --nspr        force a rebuild of NSPR
    --gyp|-g      force a rerun of gyp
    --opt|-o      do an opt build
    -m32          do a 32-bit build on a 64-bit system
    --test        ignore map files and export everything we have
    --fuzz        enable fuzzing mode. this always enables test builds
    --pprof       build with gperftool support
    --scan-build  run the build with scan-build (scan-build has to be in the path)
                  --scan-build=/out/path sets the output path for scan-build
    --asan        do an asan build
    --ubsan       do an ubsan build
                  --ubsan=bool,shift,... sets specific UB sanitizers
    --msan        do an msan build
    --sancov      do sanitize coverage builds
                  --sancov=func sets coverage to function level for example
EOF
}

if [ -n "$CCC" ] && [ -z "$CXX" ]; then
    export CXX="$CCC"
fi

opt_build=0
build_64=0
clean=0
rebuild_gyp=0
rebuild_nspr=0
target=Debug
verbose=0
fuzz=0
ubsan_default=bool,signed-integer-overflow,shift,vptr
sancov_default=edge,indirect-calls,8bit-counters
cwd=$(cd $(dirname $0); pwd -P)

gyp_params=(--depth="$cwd" --generator-output=".")
nspr_params=()
ninja_params=()
scanbuild=()

# try to guess sensible defaults
arch=$(python "$cwd"/coreconf/detect_host_arch.py)
if [ "$arch" = "x64" -o "$arch" = "aarch64" ]; then
    build_64=1
fi

sancov_default()
{
    clang_version=$($CC --version | grep -oE 'clang version (3\.9\.|4\.)')
    if [ -z "$clang_version" ]; then
        echo "Need at least clang-3.9 (better 4.0) for sancov." 1>&2
        exit 1
    fi

    if [ "$clang_version" = "clang version 3.9." ]; then
        echo edge,indirect-calls,8bit-counters
    else
        echo trace-pc-guard
    fi
}

enable_fuzz()
{
    fuzz=1
    nspr_sanitizer asan
    nspr_sanitizer ubsan $ubsan_default
    nspr_sanitizer sancov $(sancov_default)
    gyp_params+=(-Duse_asan=1)
    gyp_params+=(-Duse_ubsan=$ubsan_default)
    gyp_params+=(-Duse_sancov=$(sancov_default))

    # Adding debug symbols even for opt builds.
    nspr_params+=(--enable-debug-symbols)
}

# parse command line arguments
while [ $# -gt 0 ]; do
    case $1 in
        -c) clean=1 ;;
        --gyp|-g) rebuild_gyp=1 ;;
        --nspr) nspr_clean; rebuild_nspr=1 ;;
        -j) ninja_params+=(-j "$2"); shift ;;
        -v) ninja_params+=(-v); verbose=1 ;;
        --test) gyp_params+=(-Dtest_build=1) ;;
        --fuzz) gyp_params+=(-Dtest_build=1 -Dfuzz=1); enable_fuzz ;;
        --scan-build) scanbuild=(scan-build) ;;
        --scan-build=?*) scanbuild=(scan-build -o "${1#*=}") ;;
        --opt|-o) opt_build=1 ;;
        -m32|--m32) build_64=0 ;;
        --asan) gyp_params+=(-Duse_asan=1); nspr_sanitizer asan ;;
        --ubsan) gyp_params+=(-Duse_ubsan=$ubsan_default); nspr_sanitizer ubsan $ubsan_default ;;
        --ubsan=?*) gyp_params+=(-Duse_ubsan="${1#*=}"); nspr_sanitizer ubsan "${1#*=}" ;;
        --sancov) gyp_params+=(-Duse_sancov=$(sancov_default)); nspr_sanitizer sancov $(sancov_default) ;;
        --sancov=?*) gyp_params+=(-Duse_sancov="${1#*=}"); nspr_sanitizer sancov "${1#*=}" ;;
        --pprof) gyp_params+=(-Duse_pprof=1) ;;
        --msan) gyp_params+=(-Duse_msan=1); nspr_sanitizer msan ;;
        *) show_help; exit ;;
    esac
    shift
done

if [ "$opt_build" = 1 ]; then
    target=Release
else
    target=Debug
fi
if [ "$build_64" = 1 ]; then
    nspr_params+=(--enable-64bit)
else
    gyp_params+=(-Dtarget_arch=ia32)
fi

# clone fuzzing stuff
if [ "$fuzz" = 1 ]; then
    [ $verbose = 0 ] && exec 3>/dev/null || exec 3>&1

    echo "fuzz [1/2] Cloning libFuzzer files ..."
    "$cwd"/fuzz/clone_libfuzzer.sh 1>&3 2>&3

    echo "fuzz [2/2] Cloning fuzzing corpus ..."
    "$cwd"/fuzz/clone_corpus.sh 1>&3 2>&3

    exec 3>&-
fi

# set paths
target_dir="$cwd"/out/$target
mkdir -p "$target_dir"
dist_dir="$cwd"/../dist
dist_dir=$(mkdir -p "$dist_dir"; cd "$dist_dir"; pwd -P)
gyp_params+=(-Dnss_dist_dir="$dist_dir")

# save the chosen target
echo $target > "$dist_dir"/latest

# pass on CC and CCC to scanbuild
if [ "${#scanbuild[@]}" -gt 0 ]; then
    if [ -n "$CC" ]; then
       scanbuild+=(--use-cc="$CC")
    fi
    if [ -n "$CCC" ]; then
       scanbuild+=(--use-c++="$CCC")
    fi
fi

# This saves a canonical representation of arguments that we are passing to gyp
# or the NSPR build so that we can work out if a rebuild is needed.
normalize_config()
{
    conf="$1"
    mkdir -p $(dirname "$conf")
    shift
    echo CC="$CC" >"$conf"
    echo CCC="$CCC" >>"$conf"
    for i in "$@"; do echo $i; done | sort >>"$conf"
}

gyp_config="$cwd"/out/gyp_config
nspr_config="$cwd"/out/$target/nspr_config
# If we don't have a build directory make sure that we rebuild.
if [ ! -d "$target_dir" ]; then
    rebuild_nspr=1
    rebuild_gyp=1
fi

normalize_config "$gyp_config".new "${gyp_params[@]}"
if ! diff -q "$gyp_config".new "$gyp_config" >/dev/null 2>&1; then
    rebuild_gyp=1
fi

normalize_config "$nspr_config".new "${nspr_params[@]}" \
                 nspr_cflags="$nspr_cflags" nspr_cxxflags="$nspr_cxxflags" \
                 nspr_ldflags="$nspr_ldflags"
if [ ! -d "$dist_dir"/$target ] || \
   ! diff -q "$nspr_config".new "$nspr_config" >/dev/null 2>&1; then
    rebuild_nspr=1
fi

# -c = clean first
if [ "$clean" = 1 ]; then
    rebuild_gyp=1
    rebuild_nspr=1
    nspr_clean
    rm -rf "$cwd"/out
    rm -rf "$dist_dir"
fi

if [ "$rebuild_nspr" = 1 ]; then
    nspr_build "${nspr_params[@]}"
    mv -f "$nspr_config".new "$nspr_config"
fi
if [ "$rebuild_gyp" = 1 ]; then
    if [ $verbose = 1 ]; then set -v -x; else echo gyp ...; fi

    # These extra arguments aren't used in determining whether to rebuild.
    obj_dir="$dist_dir"/$target
    gyp_params+=(-Dnss_dist_obj_dir=$obj_dir)
    gyp_params+=(-Dnspr_lib_dir=$obj_dir/lib)
    gyp_params+=(-Dnspr_include_dir=$obj_dir/include/nspr)

    "${scanbuild[@]}" gyp -f ninja "${gyp_params[@]}" "$cwd"/nss.gyp
    [ $verbose = 1 ] && set +v +x

    mv -f "$gyp_config".new "$gyp_config"
fi

# Run ninja.
if hash ninja 2>/dev/null; then
    ninja=ninja
elif which ninja-build 2>/dev/null; then
    ninja=ninja-build
else
    echo "Please install ninja" 1>&2
    exit 1
fi
"${scanbuild[@]}" $ninja -C "$target_dir" "${ninja_params[@]}"