summaryrefslogtreecommitdiffhomepage
path: root/build.sh
blob: df399f93d6cd411cb28b826a23a9c5e7c3a87daa (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env bash

# This script is used to build, and sign a release artifact. See `README.md` for further
# instructions.
#
# Invoke the script with --dev-build in order to skip checks, cleaning and signing.

set -eu

function log {
    local NO_COLOR="0m"
    local msg=$1
    local color=${2:-"$NO_COLOR"}
    echo -e "\033[$color$msg\033[$NO_COLOR"
}

function log_header {
    local YELLOW="33m"
    echo ""
    log "### $1 ###" $YELLOW
    echo ""
}

function log_success {
    local GREEN="32m"
    log "$1" $GREEN
}

function log_error {
    local RED="31m"
    log "!! $1" $RED
}

function log_info {
    local BOLD="1m"
    log "$1" $BOLD
}

################################################################################
# Verify and configure environment.
################################################################################

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"
RUSTC_VERSION=$(rustc +stable --version)
PRODUCT_VERSION=$(node -p "require('./gui/package.json').version" | sed -Ee 's/\.0//g')
CARGO_TARGET_DIR=${CARGO_TARGET_DIR:-"target"}

CARGO_ARGS=()
NPM_PACK_ARGS=()

BUILD_MODE="release"
while [[ "$#" -gt 0 ]]; do
    case $1 in
        --dev-build)
            BUILD_MODE="dev"
            ;;
        --universal)
            if [[ "$(uname -s)" == "Darwin" ]]; then
                TARGETS=(x86_64-apple-darwin aarch64-apple-darwin)
                NPM_PACK_ARGS+=(--universal)
            else
                log_error "--universal only works on macOS"
                exit 1
            fi
            ;;
        *)
            log_error "Unknown parameter: $1"
            exit 1
            ;;
    esac
    shift
done

if [[ "$BUILD_MODE" == "release" ]]; then
    if [[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]]; then
        log_error "Dirty working directory!"
        log_error "Will only build a signed app in a clean working directory"
        exit 1
    fi

    if [[ "$(uname -s)" == "Darwin" || "$(uname -s)" == "MINGW"* ]]; then
        log_info "Configuring environment for signing of binaries"
        if [[ -z ${CSC_LINK-} ]]; then
            log_error "The variable CSC_LINK is not set. It needs to point to a file containing the"
            log_error "private key used for signing of binaries."
            exit 1
        fi
        if [[ -z ${CSC_KEY_PASSWORD-} ]]; then
            read -spr "CSC_KEY_PASSWORD = " CSC_KEY_PASSWORD
            echo ""
            export CSC_KEY_PASSWORD
        fi
        # macOS: This needs to be set to 'true' to activate signing, even when CSC_LINK is set.
        export CSC_IDENTITY_AUTO_DISCOVERY=true

        if [[ "$(uname -s)" == "MINGW"* ]]; then
            CERT_FILE=$CSC_LINK
            CERT_PASSPHRASE=$CSC_KEY_PASSWORD
            unset CSC_LINK CSC_KEY_PASSWORD
            export CSC_IDENTITY_AUTO_DISCOVERY=false
        fi
    else
        unset CSC_LINK CSC_KEY_PASSWORD
        export CSC_IDENTITY_AUTO_DISCOVERY=false
    fi
else
    NPM_PACK_ARGS+=(--no-compression)
    log_info "!! Development build. Not for general distribution !!"
    unset CSC_LINK CSC_KEY_PASSWORD
    export CSC_IDENTITY_AUTO_DISCOVERY=false
fi

product_version_commit_hash=$(git rev-parse "$PRODUCT_VERSION^{commit}" || echo "")
current_head_commit_hash=$(git rev-parse "HEAD^{commit}")
if [[ "$BUILD_MODE" == "dev" || $product_version_commit_hash != "$current_head_commit_hash" ]]; then
    PRODUCT_VERSION="$PRODUCT_VERSION-dev-${current_head_commit_hash:0:6}"

    log_info "Disabling Apple notarization (macOS only) of installer in this dev build"
    NPM_PACK_ARGS+=(--no-apple-notarization)
    CARGO_ARGS+=(--features api-override)
else
    log_info "Removing old Rust build artifacts..."
    cargo +stable clean
    CARGO_ARGS+=(--locked)
fi

log_header "Building Mullvad VPN $PRODUCT_VERSION"

if [[ ("$(uname -s)" == "Darwin") ]]; then
    BINARIES=(
        mullvad-daemon
        mullvad
        mullvad-problem-report
        libtalpid_openvpn_plugin.dylib
        mullvad-setup
    )
elif [[ ("$(uname -s)" == "Linux") ]]; then
    BINARIES=(
        mullvad-daemon
        mullvad
        mullvad-problem-report
        libtalpid_openvpn_plugin.so
        mullvad-setup
        mullvad-exclude
    )
elif [[ ("$(uname -s)" == "MINGW"*) ]]; then
    BINARIES=(
        mullvad-daemon.exe
        mullvad.exe
        mullvad-problem-report.exe
        talpid_openvpn_plugin.dll
        mullvad-setup.exe
    )
fi

function restore_metadata_backups {
    pushd "$SCRIPT_DIR"
    log_info "Restoring version metadata files..."
    ./version-metadata.sh restore-backup --desktop
    mv Cargo.lock.bak Cargo.lock || true
    popd
}
trap 'restore_metadata_backups' EXIT

log_info "Updating version in metadata files..."
cp Cargo.lock Cargo.lock.bak
./version-metadata.sh inject "$PRODUCT_VERSION" --desktop

function sign_win {
    local NUM_RETRIES=3

    for binary in "$@"; do
        # Try multiple times in case the timestamp server cannot
        # be contacted.
        for i in $(seq 0 ${NUM_RETRIES}); do
            if signtool sign \
                -tr http://timestamp.digicert.com -td sha256 \
                -fd sha256 -d "Mullvad VPN" \
                -du "https://github.com/mullvad/mullvadvpn-app#readme" \
                -f "$CERT_FILE" \
                -p "$CERT_PASSPHRASE" "$binary"
            then
                break
            fi

            if [ "$i" -eq "${NUM_RETRIES}" ]; then
                return 1
            fi

            sleep 1
        done
    done
    return 0
}

# Build the daemon and other Rust/C++ binaries, optionally
# sign them, strip them of debug symbols and copy to `dist-assets/`.
function build {
    local current_target=${1:-""}
    local for_target_string=""
    if [[ -n $current_target ]]; then
        for_target_string=" for $current_target"
    fi

    ################################################################################
    # Compile and link all binaries.
    ################################################################################

    if [[ "$(uname -s)" == "MINGW"* ]]; then
        CPP_BUILD_MODES="Release" ./build-windows-modules.sh "$@"
    fi

    ################################################################################
    # Compile wireguard-go
    ################################################################################

    ./wireguard/build-wireguard-go.sh "$current_target"

    export MULLVAD_ADD_MANIFEST="1"

    log_header "Building Rust code in release mode using $RUSTC_VERSION$for_target_string"

    CARGO_TARGET_ARG=()
    if [[ -n $current_target ]]; then
        CARGO_TARGET_ARG+=(--target="$current_target")
    fi

    cargo +stable build "${CARGO_TARGET_ARG[@]}" "${CARGO_ARGS[@]}" --release

    ################################################################################
    # Move binaries to correct locations in dist-assets
    ################################################################################

    for binary in ${BINARIES[*]}; do
        if [[ -n $current_target ]]; then
            SRC="$CARGO_TARGET_DIR/$current_target/release/$binary"
        else
            SRC="$CARGO_TARGET_DIR/release/$binary"
        fi
        if [[ "$(uname -s)" == "Darwin" ]]; then
            # To make it easier to package universal builds on macOS the binaries are located in a
            # directory with the name of the target triple.
            DST_DIR="dist-assets/$current_target"
            DST="$DST_DIR/$binary"
            mkdir -p "$DST_DIR"
        else
            DST="dist-assets/$binary"
        fi

        if [[ "$BUILD_MODE" == "release" && "$(uname -s)" == "MINGW"* ]]; then
            sign_win "$SRC"
        fi

        if [[ "$(uname -s)" == "MINGW"* || "$binary" == *.dylib ]]; then
            log_info "Copying $SRC => $DST"
            cp "$SRC" "$DST"
        else
            log_info "Stripping $SRC => $DST"
            strip "$SRC" -o "$DST"
        fi
    done
}

if [[ "$(uname -s)" == "Darwin" || "$(uname -s)" == "Linux" ]]; then
    mkdir -p "dist-assets/shell-completions"
    for sh in bash zsh fish; do
        log_info "Generating shell completion script for $sh..."
        cargo +stable run --bin mullvad "${CARGO_ARGS[@]}" --release -- shell-completions "$sh" \
            "dist-assets/shell-completions/"
    done
fi

./update-relays.sh
./update-api-address.sh

# Compile for all defined targets, or the current architecture if unspecified.
if [[ -n ${TARGETS:-""} ]]; then
    for t in ${TARGETS[*]}; do
        source env.sh "$t"
        build "$t"
    done
else
    source env.sh ""
    build
fi

if [[ "$BUILD_MODE" == "release" && "$(uname -s)" == "MINGW"* ]]; then
    signdep=(
        windows/winfw/bin/x64-Release/winfw.dll
        windows/windns/bin/x64-Release/windns.dll
        windows/winnet/bin/x64-Release/winnet.dll
        windows/driverlogic/bin/x64-Release/driverlogic.exe
        windows/nsis-plugins/bin/Win32-Release/*.dll
        build/lib/x86_64-pc-windows-msvc/libwg.dll
    )
    sign_win "${signdep[@]}"
fi


log_header "Installing JavaScript dependencies"

pushd gui
npm ci

################################################################################
# Package release.
################################################################################

log_header "Packing final release artifact(s)"

case "$(uname -s)" in
    Linux*)     npm run pack:linux -- "${NPM_PACK_ARGS[@]}";;
    Darwin*)    npm run pack:mac -- "${NPM_PACK_ARGS[@]}";;
    MINGW*)     npm run pack:win -- "${NPM_PACK_ARGS[@]}";;
esac

popd

SEMVER_VERSION=$(echo "$PRODUCT_VERSION" | sed -Ee 's/($|-.*)/.0\1/g')
for semver_path in dist/*"$SEMVER_VERSION"*; do
    product_path=$(echo "$semver_path" | sed -Ee "s/$SEMVER_VERSION/$PRODUCT_VERSION/g")
    log_info "Moving $semver_path -> $product_path"
    mv "$semver_path" "$product_path"

    if [[ "$BUILD_MODE" == "release" && "$(uname -s)" == "MINGW"* && "$product_path" == *.exe ]]
    then
        # sign installer
        sign_win "$product_path"
    fi
done


log_success "**********************************"
log_success ""
log_success " The build finished successfully! "
log_success " You have built:"
log_success ""
log_success " $PRODUCT_VERSION"
log_success ""
log_success "**********************************"