From b11ac73cec57d7729034aecba9b737b9197576cc Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 13 Apr 2023 16:01:26 +0100 Subject: [PATCH] Linux aarch64 ci build --- .github/workflows/build_and_deploy.yaml | 4 +- .github/workflows/build_and_test.yaml | 12 ++-- .github/workflows/build_linux.yaml | 93 +++++++++++++++++++++---- dockerbuild/.cargo/config.toml | 3 + hak/keytar/build.ts | 2 +- hak/matrix-seshat/build.ts | 10 +-- hak/matrix-seshat/check.ts | 4 +- 7 files changed, 102 insertions(+), 26 deletions(-) create mode 100644 dockerbuild/.cargo/config.toml diff --git a/.github/workflows/build_and_deploy.yaml b/.github/workflows/build_and_deploy.yaml index 49e4ee02..6d1e6942 100644 --- a/.github/workflows/build_and_deploy.yaml +++ b/.github/workflows/build_and_deploy.yaml @@ -98,6 +98,7 @@ jobs: name: Linux (sqlcipher system) uses: ./.github/workflows/build_linux.yaml with: + arch: amd64 config: element.io/${{ inputs.mode || 'nightly' }} sqlcipher: system version: ${{ needs.prepare.outputs.linux-version }} @@ -109,6 +110,7 @@ jobs: name: Linux (sqlcipher static) uses: ./.github/workflows/build_linux.yaml with: + arch: amd64 deploy-mode: true config: element.io/${{ inputs.mode || 'nightly' }} sqlcipher: static @@ -153,4 +155,4 @@ jobs: uses: ./.github/workflows/reprepro.yaml secrets: inherit with: - artifact-name: linux-sqlcipher-system + artifact-name: linux-amd64-sqlcipher-system diff --git a/.github/workflows/build_and_test.yaml b/.github/workflows/build_and_test.yaml index fc09e039..26066dd0 100644 --- a/.github/workflows/build_and_test.yaml +++ b/.github/workflows/build_and_test.yaml @@ -25,14 +25,16 @@ jobs: linux: needs: fetch - name: Linux + name: "Linux (${{ matrix.arch }}) (sqlcipher: ${{ matrix.sqlcipher }})" uses: ./.github/workflows/build_linux.yaml strategy: matrix: sqlcipher: [system, static] + arch: [amd64, arm64] with: config: ${{ github.event.pull_request.base.ref == 'develop' && 'element.io/nightly' || 'element.io/release' }} sqlcipher: ${{ matrix.sqlcipher }} + arch: ${{ matrix.arch }} macos: needs: fetch @@ -52,14 +54,14 @@ jobs: artifact: macos executable: "./dist/mac-universal/Element.app/Contents/MacOS/Element" prepare_cmd: "find ./dist/mac-universal/Element.app -type f | perl -lne 'print if -B' | tr '\\n' '\\0' | xargs -0 -n1 chmod 755" - - name: "Linux (sqlcipher: system)" + - name: "Linux [amd64] (sqlcipher: system)" os: ubuntu - artifact: linux-sqlcipher-system + artifact: linux-amd64-sqlcipher-system executable: "element-desktop" prepare_cmd: "sudo apt install ./dist/*.deb" - - name: "Linux (sqlcipher: static)" + - name: "Linux [amd64] (sqlcipher: static)" os: ubuntu - artifact: linux-sqlcipher-static + artifact: linux-amd64-sqlcipher-static executable: "element-desktop" prepare_cmd: "sudo apt install ./dist/*.deb" - name: Windows (x86) diff --git a/.github/workflows/build_linux.yaml b/.github/workflows/build_linux.yaml index f9285d35..06de4453 100644 --- a/.github/workflows/build_linux.yaml +++ b/.github/workflows/build_linux.yaml @@ -4,6 +4,10 @@ on: workflow_call: inputs: + arch: + type: string + required: true + description: "The architecture to build for, one of 'amd64' | 'arm64'" config: type: string required: true @@ -28,7 +32,27 @@ jobs: defaults: run: shell: bash + env: + SQLCIPHER_STATIC: ${{ inputs.sqlcipher == 'static' && '1' || '' }} steps: + - uses: kanga333/variable-mapper@master + id: config + with: + key: "${{ inputs.arch }}" + export_to: output + map: | + { + "amd64": { + "target": "x86_64-unknown-linux-gnu", + "arch": "x86-64" + }, + "arm64": { + "target": "aarch64-unknown-linux-gnu", + "arch": "aarch64", + "build-args": "--arm64" + } + } + - uses: actions/checkout@v3 - uses: actions/download-artifact@v3 @@ -39,7 +63,7 @@ jobs: id: cache uses: actions/cache@v3 with: - key: ${{ runner.os }}-${{ inputs.sqlcipher }}-${{ hashFiles('hakDependencies.json', 'electronVersion') }} + key: ${{ runner.os }}-${{ inputs.sqlcipher }}-${{ inputs.arch }}-${{ hashFiles('hakDependencies.json', 'electronVersion') }} path: | ./.hak @@ -54,11 +78,35 @@ jobs: - name: Install Deps run: "yarn install --frozen-lockfile" + # Ideally the docker image would be ready for cross-compilation but libsqlcipher-dev is not Multi-Arch compatible + # https://unix.stackexchange.com/a/349359 + - name: Prepare for cross compilation + if: steps.cache.outputs.cache-hit != 'true' && inputs.arch == 'arm64' + run: | + sed -i 's/deb http/deb [arch=amd64] http/g' /etc/apt/sources.list + echo "deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports/ bionic main multiverse restricted universe" | tee -a /etc/apt/sources.list + echo "deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports/ bionic-updates main multiverse restricted universe" | tee -a /etc/apt/sources.list + dpkg --add-architecture arm64 + apt-get -qq update + apt-get -qq install --no-install-recommends crossbuild-essential-arm64 libsqlcipher-dev:arm64 libssl-dev:arm64 libsecret-1-dev:arm64 libgnome-keyring-dev:arm64 + rustup target add aarch64-unknown-linux-gnu + mv dockerbuild/.cargo . + + echo "AS=/usr/bin/aarch64-linux-gnu-as" >> $GITHUB_ENV + echo "STRIP=/usr/bin/aarch64-linux-gnu-strip" >> $GITHUB_ENV + echo "AR=/usr/bin/aarch64-linux-gnu-ar" >> $GITHUB_ENV + echo "CC=/usr/bin/aarch64-linux-gnu-gcc" >> $GITHUB_ENV + echo "CPP=/usr/bin/aarch64-linux-gnu-cpp" >> $GITHUB_ENV + echo "CXX=/usr/bin/aarch64-linux-gnu-g++" >> $GITHUB_ENV + echo "LD=/usr/bin/aarch64-linux-gnu-ld" >> $GITHUB_ENV + echo "FC=/usr/bin/aarch64-linux-gnu-gfortran" >> $GITHUB_ENV + echo "PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig" >> $GITHUB_ENV + echo "CFLAGS=-L/usr/lib/aarch64-linux-gnu" >> $GITHUB_ENV + echo "RUSTFLAGS=-L/usr/lib/aarch64-linux-gnu" >> $GITHUB_ENV + - name: Build Natives if: steps.cache.outputs.cache-hit != 'true' - run: "yarn build:native" - env: - SQLCIPHER_STATIC: ${{ inputs.sqlcipher == 'static' && '1' || '' }} + run: "yarn build:native --target ${{ steps.config.outputs.target }}" - name: "[Nightly] Resolve version" id: nightly @@ -73,12 +121,14 @@ jobs: echo "config-args=--deb-changelog changelog.Debian" >> $GITHUB_OUTPUT fi - cp "$DIR/control.template" debcontrol + cat "$DIR/control.template" | grep -v "Architecture: " > debcontrol + echo "Architecture: $ARCHITECTURE" >> debcontrol VERSION=${INPUT_VERSION:-$(cat package.json | jq -r .version)} echo "Version: $VERSION" >> debcontrol env: DIR: ${{ inputs.config }} INPUT_VERSION: ${{ inputs.version }} + ARCHITECTURE: ${{ inputs.arch }} - name: Build App run: | @@ -86,20 +136,35 @@ jobs: ${{ steps.nightly.outputs.config-args }} \ ${{ steps.debian.outputs.config-args }} \ --deb-custom-control=debcontrol - yarn build --publish never -l --config electron-builder.json + yarn build --publish never -l --config electron-builder.json ${{ steps.config.outputs.build-args }} - - name: Check ldd + - name: Check native libraries + continue-on-error: ${{ inputs.arch == 'arm64' }} run: | - ldd dist/linux-unpacked/resources/app.asar.unpacked/node_modules/matrix-seshat/native/index.node + shopt -s globstar + WRONG_ARCHES=$(file dist/**/*.node | grep -v "$ARCH") + if [ "$WRONG_ARCHES" != "" ]; then + echo "$WRONG_ARCHES" + exit 1 + fi + + LIBS=$(readelf -d dist/**/*.node | grep NEEDED) + echo "$LIBS" + if [ "$SQLCIPHER_STATIC" == "1" ]; then - ldd dist/linux-unpacked/resources/app.asar.unpacked/node_modules/matrix-seshat/native/index.node | grep -v libsqlcipher.so.0 - ldd dist/linux-unpacked/resources/app.asar.unpacked/node_modules/matrix-seshat/native/index.node | grep libcrypto.so.1.1 + if grep -q "libsqlcipher.so.0" <<< "$LIBS" ; then + exit 2 + fi else - ldd dist/linux-unpacked/resources/app.asar.unpacked/node_modules/matrix-seshat/native/index.node | grep libsqlcipher.so.0 - ldd dist/linux-unpacked/resources/app.asar.unpacked/node_modules/matrix-seshat/native/index.node | grep -v libcrypto.so.1.1 + if grep -q "libcrypto.so.1.1" <<< "$LIBS" ; then + exit 3 + fi + if ! grep -q "libsqlcipher.so.0" <<< "$LIBS" ; then + exit 4 + fi fi env: - SQLCIPHER_STATIC: ${{ inputs.sqlcipher == 'static' && '1' || '' }} + ARCH: ${{ steps.config.outputs.arch }} - name: Stash deb package if: inputs.deploy-mode @@ -132,6 +197,6 @@ jobs: - name: Upload Artifacts uses: actions/upload-artifact@v3 with: - name: ${{ inputs.deploy-mode && 'packages.element.io' || format('linux-sqlcipher-{0}', inputs.sqlcipher) }} + name: ${{ inputs.deploy-mode && 'packages.element.io' || format('linux-{0}-sqlcipher-{1}', inputs.arch, inputs.sqlcipher) }} path: dist retention-days: 1 diff --git a/dockerbuild/.cargo/config.toml b/dockerbuild/.cargo/config.toml new file mode 100644 index 00000000..3d050422 --- /dev/null +++ b/dockerbuild/.cargo/config.toml @@ -0,0 +1,3 @@ +[target.aarch64-unknown-linux-gnu] +linker = "aarch64-linux-gnu-gcc" +rustflags = ["-L/usr/lib/aarch64-linux-gnu"] diff --git a/hak/keytar/build.ts b/hak/keytar/build.ts index c0433c34..ec7c8b5a 100644 --- a/hak/keytar/build.ts +++ b/hak/keytar/build.ts @@ -27,7 +27,7 @@ export default async function buildKeytar(hakEnv: HakEnv, moduleInfo: Dependency await new Promise((resolve, reject) => { const proc = childProcess.spawn( path.join(moduleInfo.nodeModuleBinDir, "node-gyp" + (hakEnv.isWin() ? ".cmd" : "")), - ["rebuild"], + ["rebuild", "--arch", hakEnv.getTargetArch()], { cwd: moduleInfo.moduleBuildDir, env, diff --git a/hak/matrix-seshat/build.ts b/hak/matrix-seshat/build.ts index 5a41d89f..2753110b 100644 --- a/hak/matrix-seshat/build.ts +++ b/hak/matrix-seshat/build.ts @@ -205,14 +205,15 @@ async function buildSqlCipherUnix(hakEnv: HakEnv, moduleInfo: DependencyInfo): P const cflags = ["-DSQLITE_HAS_CODEC"]; - if (!hakEnv.isHost()) { + // If the caller has specified CFLAGS then we shouldn't specify target + // as their compiler may be incompatible (gcc) + if (!hakEnv.isHost() && !process.env.CFLAGS) { // `clang` uses more logical option naming. cflags.push(`--target=${hakEnv.getTargetId()}`); } - if (cflags.length) { - args.push(`CFLAGS=${cflags.join(" ")}`); - } + if (process.env.CFLAGS) cflags.unshift(process.env.CFLAGS); + args.push(`CFLAGS=${cflags.join(" ")}`); const ldflags: string[] = []; @@ -222,6 +223,7 @@ async function buildSqlCipherUnix(hakEnv: HakEnv, moduleInfo: DependencyInfo): P } if (ldflags.length) { + if (process.env.LDFLAGS) ldflags.unshift(process.env.LDFLAGS); args.push(`LDFLAGS=${ldflags.join(" ")}`); } diff --git a/hak/matrix-seshat/check.ts b/hak/matrix-seshat/check.ts index 4725246a..a4405e6f 100644 --- a/hak/matrix-seshat/check.ts +++ b/hak/matrix-seshat/check.ts @@ -70,7 +70,7 @@ export default async function (hakEnv: HakEnv, moduleInfo: DependencyInfo): Prom await new Promise((resolve, reject) => { const rustc = childProcess.execFile( "rustc", - ["--target", hakEnv.getTargetId(), "-o", "tmp", "-"], + ["--target", hakEnv.getTargetId(), "--emit=obj", "-o", "tmp", "-"], (err, out) => { if (err) { reject( @@ -86,6 +86,8 @@ export default async function (hakEnv: HakEnv, moduleInfo: DependencyInfo): Prom }, ); rustc.stdin!.write("fn main() {}"); + rustc.stdout!.pipe(process.stdout); + rustc.stderr!.pipe(process.stderr); rustc.stdin!.end(); }); }