|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright (c) 2025-2026 Element Creations Ltd. |
| 4 | +# Copyright 2024 New Vector Ltd. |
| 5 | +# |
| 6 | +# SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial. |
| 7 | +# Please see LICENSE files in the repository root for full details. |
| 8 | + |
| 9 | +set -e |
| 10 | +set -u |
| 11 | + |
| 12 | +# Usage: |
| 13 | +# |
| 14 | +# ./tools/sdk/build-rust-sdk --help |
| 15 | +# |
| 16 | + |
| 17 | +# Notes: |
| 18 | +# |
| 19 | +# * matrix-rust-components-kotlin will be cloned into |
| 20 | +# ../matrix-rust-components-kotlin and any changes in there will be |
| 21 | +# overwritten (without prompting!) |
| 22 | +# |
| 23 | +# * If you opt to build a remote repo, it will be cloned into |
| 24 | +# ../matrix-rust-sdk-<<DATE>> |
| 25 | + |
| 26 | +## Defaults |
| 27 | + |
| 28 | +buildLocal=0 |
| 29 | +rustSdkPath="../matrix-rust-sdk/" |
| 30 | +rustSdkUrl="https://github.com/matrix-org/matrix-rust-sdk.git" |
| 31 | +rustSdkBranch="main" |
| 32 | +buildApp=1 |
| 33 | + |
| 34 | +default_arch="$(uname -m)-linux-android" |
| 35 | +# On ARM MacOS, `uname -m` returns arm64, but the toolchain is called aarch64 |
| 36 | +default_arch="${default_arch/arm64/aarch64}" |
| 37 | + |
| 38 | +target_arch="${default_arch}" |
| 39 | + |
| 40 | +sdkArg="" |
| 41 | + |
| 42 | +## Argument parsing |
| 43 | + |
| 44 | +TEMP=$(getopt -o 'rs:b:at:h' --long 'remote,sdk:,branch:,build-app,target-arch,help' -- "$@") |
| 45 | + |
| 46 | +if [ $? -ne 0 ]; then |
| 47 | + echo 'Terminating...' >&2 |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +eval set -- "$TEMP" |
| 52 | +unset TEMP |
| 53 | + |
| 54 | +while true; do |
| 55 | + case "$1" in |
| 56 | + 'r'|'--remote') |
| 57 | + buildLocal=1 |
| 58 | + shift |
| 59 | + continue |
| 60 | + ;; |
| 61 | + 's'|'--sdk') |
| 62 | + sdkArg="$2" |
| 63 | + shift 2 |
| 64 | + continue |
| 65 | + ;; |
| 66 | + 'b'|'--branch') |
| 67 | + rustSdkBranch="$2" |
| 68 | + shift 2 |
| 69 | + continue |
| 70 | + ;; |
| 71 | + 'a'|'--build-app') |
| 72 | + buildApp=0 |
| 73 | + shift |
| 74 | + continue |
| 75 | + ;; |
| 76 | + 't'|'--target-arch') |
| 77 | + target_arch="$2" |
| 78 | + shift 2 |
| 79 | + continue |
| 80 | + ;; |
| 81 | + 'h'|'--help') |
| 82 | + cat << END |
| 83 | +SYNOPSIS |
| 84 | +
|
| 85 | + $0 [-s|--sdk=PATH] [-a|--build-app] [-t|--target-arch=TARGET]" |
| 86 | +
|
| 87 | + $0 --remote [-s|--sdk=URL] [-b|--branch=BRANCH] [-a|--build-app] [-t|--target-arch=TARGET]" |
| 88 | +
|
| 89 | +ARGUMENTS |
| 90 | +
|
| 91 | + -a --build-app |
| 92 | + Build the Android app after the SDK is built. |
| 93 | +
|
| 94 | + -b --branch |
| 95 | + If --remote is supplied, the branch of the remote repo to build. |
| 96 | +
|
| 97 | + -r --remote |
| 98 | + Fetch the SDK code from a remote repo instead of building a local version. |
| 99 | +
|
| 100 | + -s --sdk |
| 101 | + The local path of the SDK to build, or the URL of the remote repo if --remote is provided. |
| 102 | +
|
| 103 | + -t --target-arch |
| 104 | + The architecture for which to build the app. Defaults to the architecture of this machine (${default_arch}). |
| 105 | +
|
| 106 | +EXAMPLES |
| 107 | +
|
| 108 | + $0 |
| 109 | + Build the default local rust SDK (../matrix-rust-sdk) |
| 110 | +
|
| 111 | + $0 --sdk=/home/andy/code/matrix-rust-sdk |
| 112 | + Build the supplied local rust SDK |
| 113 | +
|
| 114 | + $0 --remote |
| 115 | + Build the default remote SDK |
| 116 | +
|
| 117 | + $0 --remote --branch=featureA |
| 118 | + Build the "featureA" branch of the remote SDK |
| 119 | +
|
| 120 | + $0 --remote --sdk=https://github.com/andybalaam/matrix-rust-sdk.git |
| 121 | + Build an alternative remote SDK |
| 122 | +
|
| 123 | + $0 --build-app |
| 124 | + Build the app after building the SDK |
| 125 | +
|
| 126 | + $0 --build-app --target-arch=x86_64-linux-android |
| 127 | + Build the app after building the SDK, for the x86_64 target architecture |
| 128 | +
|
| 129 | +END |
| 130 | + exit 0 |
| 131 | + ;; |
| 132 | + '--') |
| 133 | + shift |
| 134 | + break |
| 135 | + ;; |
| 136 | + *) |
| 137 | + echo 'Unrecognised argument!' >&2 |
| 138 | + exit 2 |
| 139 | + ;; |
| 140 | + esac |
| 141 | +done |
| 142 | + |
| 143 | +if [ -n "${sdkArg}" ]; then |
| 144 | + if [ "${buildLocal}" == "0" ]; then |
| 145 | + rustSdkPath="${sdkArg}" |
| 146 | + else |
| 147 | + rustSdkUrl="${sdkArg}" |
| 148 | + fi |
| 149 | +fi |
| 150 | + |
| 151 | +#echo "buildLocal=${buildLocal}" |
| 152 | +#echo "rustSdkPath=${rustSdkPath}" |
| 153 | +#echo "rustSdkUrl=${rustSdkUrl}" |
| 154 | +#echo "rustSdkBranch=${rustSdkBranch}" |
| 155 | +#echo "buildApp=${buildApp}" |
| 156 | + |
| 157 | +## Find the date |
| 158 | + |
| 159 | +if [[ "$OSTYPE" == "linux-gnu"* ]]; then |
| 160 | + date=$(date +%Y%m%d%H%M%S) |
| 161 | +else |
| 162 | + date=$(gdate +%Y%m%d%H%M%S) |
| 163 | +fi |
| 164 | + |
| 165 | +elementPwd=$(pwd) |
| 166 | + |
| 167 | +## Check the local build dir is valid, or clone the remote repo |
| 168 | + |
| 169 | +if [ "${buildLocal}" == "0" ]; then |
| 170 | + if [ ! -d "${rustSdkPath}" ]; then |
| 171 | + printf "\nFolder ${rustSdkPath} does not exist. Please clone the\n" >&2 |
| 172 | + printf "matrix-rust-sdk repository into ../matrix-rust-sdk\n" >&2 |
| 173 | + printf "or supply the --sdk argument.\n\n" >&2 |
| 174 | + exit 3 |
| 175 | + fi |
| 176 | +else |
| 177 | + printf "\n## Cloning the SDK repo...\n\n" |
| 178 | + |
| 179 | + cd .. |
| 180 | + git clone "${rustSdkUrl}" matrix-rust-sdk-"$date" |
| 181 | + cd matrix-rust-sdk-"$date" |
| 182 | + git checkout "${rustSdkBranch}" |
| 183 | + rustSdkPath=$(pwd) |
| 184 | +fi |
| 185 | + |
| 186 | +cd "${elementPwd}" |
| 187 | + |
| 188 | +## Clone matrix-rust-components-kotlin if needed |
| 189 | + |
| 190 | +if [ ! -d "../matrix-rust-components-kotlin" ]; then |
| 191 | + printf "\nFolder ../matrix-rust-components-kotlin does not exist." |
| 192 | + printf "Cloning the repository into ../matrix-rust-components-kotlin.\n\n" |
| 193 | + git clone \ |
| 194 | + https://github.com/matrix-org/matrix-rust-components-kotlin.git \ |
| 195 | + ../matrix-rust-components-kotlin |
| 196 | +fi |
| 197 | + |
| 198 | +printf "\n## Resetting matrix-rust-components-kotlin to the latest main...\n\n" |
| 199 | + |
| 200 | +cd ../matrix-rust-components-kotlin |
| 201 | +git reset --hard |
| 202 | +git checkout main |
| 203 | +git pull |
| 204 | + |
| 205 | +## Build the SDK |
| 206 | + |
| 207 | +printf "\n## Building the SDK for ${target_arch}...\n\n" |
| 208 | + |
| 209 | +./scripts/build.sh \ |
| 210 | + -p "${rustSdkPath}" \ |
| 211 | + -m sdk \ |
| 212 | + -t "${target_arch}" \ |
| 213 | + -o "${elementPwd}/libraries/rustsdk" |
| 214 | + |
| 215 | +cd "${elementPwd}" |
| 216 | + |
| 217 | +mv \ |
| 218 | + ./libraries/rustsdk/sdk-android-debug.aar \ |
| 219 | + ./libraries/rustsdk/matrix-rust-sdk.aar |
| 220 | + |
| 221 | +mkdir -p ./libraries/rustsdk/sdks |
| 222 | + |
| 223 | +cp \ |
| 224 | + "./libraries/rustsdk/matrix-rust-sdk.aar" \ |
| 225 | + "./libraries/rustsdk/sdks/matrix-rust-sdk-${date}.aar" |
| 226 | + |
| 227 | +## Build the app |
| 228 | + |
| 229 | +if [ "${buildApp}" == "0" ]; then |
| 230 | + printf "\n## Building the application...\n\n" |
| 231 | + ./gradlew assembleDebug |
| 232 | +fi |
| 233 | + |
| 234 | +## Clean remote checkout of SDK repo |
| 235 | + |
| 236 | +if [ "${buildLocal}" != "0" ]; then |
| 237 | + printf "\n## Cleaning up...\n\n" |
| 238 | + rm -rf "../matrix-rust-sdk-$date" |
| 239 | +fi |
| 240 | + |
| 241 | +printf "\n## Done!\n" |
0 commit comments