summaryrefslogtreecommitdiffhomepage
path: root/build.sh
blob: 8716cde922251b0256c3ba5d24d8514bd7310a45 (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
#!/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

################################################################################
# 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:-"$SCRIPT_DIR/target"}

source env.sh ""

if [[ "${1:-""}" != "--dev-build" ]]; then
    if [[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]]; then
        echo "Dirty working directory!"
        echo "You should only build releases in clean working directories in order to make it"
        echo "easier to reproduce the same build."
        exit 1
    fi

    if [[ ("$(uname -s)" == "Darwin") ]]; then
        echo "Configuring environment for signing of binaries"
        if [[ -z ${CSC_LINK-} ]]; then
            echo "The variable CSC_LINK is not set. It needs to point to a file containing the"
            echo "private key used for signing of binaries."
            exit 1
        fi
        if [[ -z ${CSC_KEY_PASSWORD-} ]]; then
            read -sp "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
    else
        unset CSC_LINK CSC_KEY_PASSWORD
        export CSC_IDENTITY_AUTO_DISCOVERY=false
    fi
else
    echo "!! Development build. Not for general distribution !!"
    unset CSC_LINK CSC_KEY_PASSWORD
    export CSC_IDENTITY_AUTO_DISCOVERY=false
fi

if [[ "${1:-""}" == "--dev-build" || $(git describe) != "$PRODUCT_VERSION" ]]; then
    GIT_COMMIT=$(git rev-parse --short HEAD)
    PRODUCT_VERSION="$PRODUCT_VERSION-dev-$GIT_COMMIT"
    echo "Modifying product version to $PRODUCT_VERSION"
else
    echo "Removing old Rust build artifacts"
    cargo +stable clean
fi

echo "Building Mullvad VPN $PRODUCT_VERSION"
SEMVER_VERSION=$(echo $PRODUCT_VERSION | sed -Ee 's/($|-.*)/.0\1/g')

function restore_metadata_backups() {
    pushd "$SCRIPT_DIR"
    mv gui/package.json.bak gui/package.json || true
    mv gui/package-lock.json.bak gui/package-lock.json || true
    mv Cargo.lock.bak Cargo.lock || true
    mv mullvad-daemon/Cargo.toml.bak mullvad-daemon/Cargo.toml || true
    mv mullvad-cli/Cargo.toml.bak mullvad-cli/Cargo.toml || true
    mv mullvad-problem-report/Cargo.toml.bak mullvad-problem-report/Cargo.toml || true
    mv talpid-openvpn-plugin/Cargo.toml.bak talpid-openvpn-plugin/Cargo.toml || true
    mv dist-assets/windows/version.h.bak dist-assets/windows/version.h || true
    popd
}
trap 'restore_metadata_backups' EXIT

sed -i.bak \
    -Ee "s/\"version\": \"[^\"]+\",/\"version\": \"$SEMVER_VERSION\",/g" \
    gui/package.json
cp gui/package-lock.json gui/package-lock.json.bak

cp Cargo.lock Cargo.lock.bak
sed -i.bak \
    -Ee "s/^version = \"[^\"]+\"\$/version = \"$SEMVER_VERSION\"/g" \
    mullvad-daemon/Cargo.toml \
    mullvad-cli/Cargo.toml \
    mullvad-problem-report/Cargo.toml \
    talpid-openvpn-plugin/Cargo.toml

SEMVER_ARRAY=($(echo $SEMVER_VERSION | sed -Ee 's/[.-]+/ /g'))
SEMVER_MAJOR=${SEMVER_ARRAY[0]}
SEMVER_MINOR=${SEMVER_ARRAY[1]}
SEMVER_PATCH=${SEMVER_ARRAY[2]}

cp dist-assets/windows/version.h dist-assets/windows/version.h.bak

cat <<EOF > dist-assets/windows/version.h
#define MAJOR_VERSION $SEMVER_MAJOR
#define MINOR_VERSION $SEMVER_MINOR
#define PATCH_VERSION $SEMVER_PATCH
#define PRODUCT_VERSION "$PRODUCT_VERSION"
EOF

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

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

echo "Building Rust code in release mode using $RUSTC_VERSION..."
MULLVAD_ADD_MANIFEST="1" cargo +stable build --release

################################################################################
# Other work to prepare the release.
################################################################################

if [[ ("$(uname -s)" == "Darwin") ]]; then
    binaries=(
        mullvad-daemon
        mullvad
        problem-report
        libtalpid_openvpn_plugin.dylib
    )
elif [[ ("$(uname -s)" == "Linux") ]]; then
    binaries=(
        mullvad-daemon
        mullvad
        problem-report
        libtalpid_openvpn_plugin.so
    )
elif [[ ("$(uname -s)" == "MINGW"*) ]]; then
    binaries=(
        mullvad-daemon.exe
        mullvad.exe
        problem-report.exe
        talpid_openvpn_plugin.dll
    )
fi
for binary in ${binaries[*]}; do
    SRC="$CARGO_TARGET_DIR/release/$binary"
    DST="$SCRIPT_DIR/dist-assets/$binary"
    if [[ "$(uname -s)" == "MINGW"* || "$binary" == *.dylib ]]; then
        echo "Copying $SRC => $DST"
        cp "$SRC" "$DST"
    else
        echo "Stripping $SRC => $DST"
        strip "$SRC" -o "$DST"
    fi
done


echo "Updating relay list..."
set +e
read -d '' JSONRPC_CODE <<-JSONRPC_CODE
var buff = "";
process.stdin.on('data', function (chunk) {
    buff += chunk;
})
process.stdin.on('end', function () {
    var obj = JSON.parse(buff);
    var output = JSON.stringify(obj.result, null, '    ');
    process.stdout.write(output);
})
JSONRPC_CODE
set -e

JSONRPC_RESPONSE="$(curl -X POST \
    --fail \
     -H "Content-Type: application/json" \
     -d '{"jsonrpc": "2.0", "id": "0", "method": "relay_list_v2"}' \
     https://api.mullvad.net/rpc/)"
echo $JSONRPC_RESPONSE | node -e "$JSONRPC_CODE" >  dist-assets/relays.json


pushd "$SCRIPT_DIR/gui"

echo "Installing JavaScript dependencies..."
npm install

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

echo "Packing final release artifact..."
case "$(uname -s)" in
    Linux*)     npm run pack:linux;;
    Darwin*)    npm run pack:mac;;
    MINGW*)     npm run pack:win;;
esac

popd

for semver_path in dist/*$SEMVER_VERSION*; do
    product_path=$(echo $semver_path | sed -Ee "s/$SEMVER_VERSION/$PRODUCT_VERSION/g")
    echo "Moving $semver_path -> $product_path"
    mv $semver_path $product_path
done

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