Skip to content

Commit

Permalink
prebuilt-tdlib: crosscompile to Linux arm64
Browse files Browse the repository at this point in the history
  • Loading branch information
eilvelia committed Jul 19, 2024
1 parent d3580a4 commit 0f98cd8
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 31 deletions.
52 changes: 50 additions & 2 deletions .github/workflows/prebuilt-tdlib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ jobs:
with:
name: tdlib-linux-x86_64-glibc
path: ${{ env.TO_UPLOAD }}
build-linux-arm64:
name: 'Build TDLib / Linux arm64 glibc (cross)'
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v27
with:
nix_path: nixpkgs=channel:nixos-unstable
- name: Build TDLib
run: |
cd packages/prebuilt-tdlib/ci
./build-linux.sh ${{ inputs.tdlib }} aarch64-linux-gnu.2.22
- uses: actions/upload-artifact@v4
with:
name: tdlib-linux-arm64-glibc
path: ${{ env.TO_UPLOAD }}
build-macos-x86_64:
name: 'Build TDLib / macOS x86_64'
runs-on: macos-13
Expand Down Expand Up @@ -126,7 +142,10 @@ jobs:

test:
name: 'Test / ${{ matrix.v.bin }} / ${{ matrix.v.os }}'
needs: [build-linux-x86_64, build-macos, build-windows-x86_64]
needs:
- build-linux-x86_64
- build-macos
- build-windows-x86_64
runs-on: ${{ matrix.v.os }}
strategy:
fail-fast: false
Expand Down Expand Up @@ -157,9 +176,38 @@ jobs:
env:
LIBDIR_PATH: '.'

test-linux-arm64:
name: 'Test / ${{ matrix.v.bin }} / ${{ matrix.v.os }} (docker arm64)'
needs: [build-linux-arm64]
runs-on: ${{ matrix.v.os }}
strategy:
matrix:
v:
- os: ubuntu-latest
bin: tdlib-linux-arm64-glibc
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: ${{ matrix.v.bin }}
merge-multiple: true
- run: docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
- name: Run in docker
run: |
cat > test-linux-aarch64.sh <<EOF
set -ex
cd /usr/src/app
ldd libtdjson.so # Info
npm install
npm uninstall -D prebuilt-tdlib
LIBDIR_PATH=. npm run test:integration
EOF
docker run -v $(pwd):/usr/src/app --platform linux/arm64 node:20 \
bash /usr/src/app/test-linux-aarch64.sh
publish:
name: 'Publish to npm'
needs: [test]
needs: [test, test-linux-arm64]
runs-on: ubuntu-latest
permissions:
contents: read
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ a library to create [Telegram][] clients or bots.
- The tdjson shared library (`libtdjson.so` on Linux, `libtdjson.dylib` on macOS, `tdjson.dll` on Windows), of TDLib version 1.8.0 or newer
- In rare cases, a C++ compiler and Python installed to build the node addon[^1]

[^1]: `tdl` is packaged with pre-built addons for Windows (x86_64), GNU/Linux (x86_64, arm64; glibc >= 2.22), and macOS (x86_64, arm64; v10.14+). If a pre-built binary is not available for your system, then the node addon will be built using node-gyp, requiring Python and a C++ toolchain (C++14 is required) to be installed (on Windows, MSVS or Build Tools). Pass `--build-from-source` to never use the pre-built binaries. arm64 binaries are not tested in the CI. Only Linux binaries are statically linked against libstdc++.
[^1]: `tdl` is packaged with pre-built addons for Windows (x86_64), GNU/Linux (x86_64, arm64; glibc >= 2.22), and macOS (x86_64, arm64; v10.14+). If a pre-built binary is not available for your system, then the node addon will be built using node-gyp, requiring Python and a C++ toolchain (C++14 is required) to be installed (on Windows, MSVS or Build Tools). Pass `--build-from-source` to never use the pre-built binaries. arm64 binaries are not tested in the CI. Linux binaries are statically linked against libstdc++.

<a name="installation"></a>
## Installation
Expand All @@ -49,11 +49,12 @@ The tdjson shared library should be present in the system search paths
Instead of building TDLib from source, you can possibly install pre-built TDLib
libraries distributed through the `prebuilt-tdlib` npm package. An example of
using libraries from `prebuilt-tdlib` is present in the section below. The
supported systems are x86_64 GNU/Linux, x86_64 & arm64 macOS, and x86_64
Windows. To install `prebuilt-tdlib` for a specific TDLib version, for example
v1.8.30, run `npm install [email protected]`. The available versions of
`prebuilt-tdlib` can be found by running `npm info prebuilt-tdlib dist-tags`.
See the README of [prebuilt-tdlib][] for additional information.
supported systems are x86_64 & arm64 GNU/Linux, x86_64 & arm64 macOS, and
x86_64s Windows. To install `prebuilt-tdlib` for a specific TDLib version
instead of latest, for example v1.8.30, run `npm i [email protected]`.
The available versions of `prebuilt-tdlib` can be found by running
`npm info prebuilt-tdlib dist-tags`. See the README of [prebuilt-tdlib][] for
additional information.

[prebuilt-tdlib]: packages/prebuilt-tdlib/README.md

Expand Down
18 changes: 10 additions & 8 deletions packages/prebuilt-tdlib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

This package distributes pre-built [TDLib][] shared libraries through npm.
The libraries are built on GitHub Actions ([prebuilt-tdlib.yml][]) and published
using [npm publish --provenance][provenance].
using [npm publish --provenance][npm-provenance].

[TDLib]: https://github.com/tdlib/td
[prebuilt-tdlib.yml]: ../../.github/workflows/prebuilt-tdlib.yml
[provenance]: https://docs.npmjs.com/generating-provenance-statements

The shared libraries are statically linked against OpenSSL and zlib, for one, to
prevent compatibility issues in Node.js.
[npm-provenance]: https://docs.npmjs.com/generating-provenance-statements

Supported systems:
- Linux x86_64 (requires glibc >= 2.22)
- Linux x86_64, arm64 (requires glibc >= 2.22)
- macOS x86_64, arm64 (universal, requires macOS >= 10.12)
- Windows x86_64

Expand All @@ -37,6 +34,10 @@ The TDLib version is important: there is no backward compatibility and the
interface you use may significantly change after an update. It is, though,
recommended to use the latest TDLib version.

The shared libraries are statically linked against OpenSSL and zlib, for one, to
prevent compatibility issues in Node.js. libstdc++ is also linked statically
(on Linux).

## Usage

The `prebuilt-tdlib` package exports a single function `getTdjson`, which
Expand Down Expand Up @@ -123,9 +124,9 @@ An incomplete list is available below (mostly exceptions or "notable" versions):

Changes to the building process of `prebuilt-tdlib` are noted below.

### (unreleased)
### 2024-07-19

First published as `<unpublished>`.
First published as `[email protected]`.

The building process is significantly changed in this update.

Expand All @@ -142,6 +143,7 @@ The building process is significantly changed in this update.
crosscompiled anymore).
- On Linux, TDLib is now built using zig. The minimal glibc version is 2.22
instead of 2.17.
- Added a crosscompiled prebuild for Linux arm64.

### 2024-05-08

Expand Down
2 changes: 1 addition & 1 deletion packages/prebuilt-tdlib/ci/build-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cp -L ./result/lib/libtdjson.so to-upload/libtdjson.so
cd to-upload

# Info
ldd libtdjson.so
ldd libtdjson.so || true

if [ -z "$GITHUB_ENV" ]; then
echo "Note: GITHUB_ENV not found"
Expand Down
17 changes: 12 additions & 5 deletions packages/prebuilt-tdlib/ci/tdlib-linux.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{ pkgs ? import <nixpkgs> {}, target, rev }:
{ target, rev }:
let
pkgs = if builtins.substring 0 7 target == "aarch64"
then import <nixpkgs> { crossSystem.config = "aarch64-unknown-linux-gnu"; }
else import <nixpkgs> {};
inherit (pkgs) lib stdenv;
zig-toolchain = import ./zig-toolchain.nix { inherit pkgs target; };
zig-toolchain = import ./zig-toolchain.nix { inherit target; pkgs = pkgs.buildPackages.buildPackages; };
zlib = pkgs.callPackage ./zlib-zig.nix { inherit zig-toolchain; };
openssl = pkgs.callPackage ./openssl-zig.nix { inherit zig-toolchain; };
in
Expand All @@ -13,12 +16,16 @@ stdenv.mkDerivation {
src = builtins.fetchTarball "https://github.com/tdlib/td/archive/${rev}.tar.gz";

buildInputs = [ openssl zlib ];
nativeBuildInputs = with pkgs; [ cmake gperf ];
nativeBuildInputs = with pkgs.buildPackages; [ cmake gperf ];

preConfigure = ''
export CC=${pkgs.buildPackages.stdenv.cc}/bin/cc
export CXX=${pkgs.buildPackages.stdenv.cc}/bin/c++
mkdir native-build && cd native-build
cmake ..
cmake --build . --target prepare_cross_compiling
cmake -DOPENSSL_ROOT_DIR=${pkgs.buildPackages.openssl.dev} \
-DZLIB_ROOT=${pkgs.buildPackages.zlib.dev} \
..
cmake --build . --target prepare_cross_compiling -j $NIX_BUILD_CORES
cd ..
'' + zig-toolchain.env;

Expand Down
9 changes: 0 additions & 9 deletions packages/prebuilt-tdlib/ci/zlib-zig.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,4 @@
(zlib.override { static = true; shared = false; }).overrideAttrs (final: prev: {
preConfigure = prev.preConfigure + zig-toolchain.env;
doCheck = false;
# preCheck = ''
# ls
# echo ---
# ldd ./minigzip
# ./minigzip
# echo START
# make check
# echo FINISH
# '';
})
11 changes: 11 additions & 0 deletions packages/prebuilt-tdlib/prebuild-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ const prebuilds/*: PrebuildInfo[] */ = [
cpu: ['x64']
},
libc: 'glibc'
},
{
packageName: 'linux-arm64-glibc',
prebuildDir: 'tdlib-linux-arm64-glibc',
libfile: SHARED_LINUX,
descr: 'Linux arm64 (glibc)',
requirements: {
os: ['linux'],
cpu: ['arm64']
},
libc: 'glibc'
}
]

Expand Down

0 comments on commit 0f98cd8

Please sign in to comment.