Skip to content

Commit

Permalink
prebuilt-tdlib: change the package structure to use optionalDependencies
Browse files Browse the repository at this point in the history
(this is untested for now)
  • Loading branch information
eilvelia committed Jul 17, 2024
1 parent 48862f6 commit d3580a4
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 105 deletions.
37 changes: 16 additions & 21 deletions .github/workflows/prebuilt-tdlib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ on:
default: 'latest'
# NOTE: The ZLIB_USE_STATIC_LIBS option requires CMake >= 3.24
jobs:
build-linux-x64:
build-linux-x86_64:
name: 'Build TDLib / Linux x86_64 glibc'
runs-on: ubuntu-22.04
steps:
Expand All @@ -33,9 +33,9 @@ jobs:
./build-linux.sh ${{ inputs.tdlib }} x86_64-linux-gnu.2.22
- uses: actions/upload-artifact@v4
with:
name: tdlib-linux-x64
name: tdlib-linux-x86_64-glibc
path: ${{ env.TO_UPLOAD }}
build-macos-x64:
build-macos-x86_64:
name: 'Build TDLib / macOS x86_64'
runs-on: macos-13
steps:
Expand All @@ -45,10 +45,11 @@ jobs:
nix_path: nixpkgs=channel:nixpkgs-unstable
- name: Build TDLib
run: |
cd packages/prebuilt-tdlib/ci && ./build-macos.sh ${{ inputs.tdlib }}
cd packages/prebuilt-tdlib/ci
./build-macos.sh ${{ inputs.tdlib }}
- uses: actions/upload-artifact@v4
with:
name: temp-macos-x64
name: temp-macos-x86_64
path: ${{ env.TO_UPLOAD }}
build-macos-arm64:
name: 'Build TDLib / macOS arm64'
Expand All @@ -60,14 +61,15 @@ jobs:
nix_path: nixpkgs=channel:nixpkgs-unstable
- name: Build TDLib
run: |
cd packages/prebuilt-tdlib/ci && ./build-macos.sh ${{ inputs.tdlib }}
cd packages/prebuilt-tdlib/ci
./build-macos.sh ${{ inputs.tdlib }}
- uses: actions/upload-artifact@v4
with:
name: temp-macos-arm64
path: ${{ env.TO_UPLOAD }}
build-macos:
name: Create universal macOS shared library
needs: [build-macos-x64, build-macos-arm64]
needs: [build-macos-x86_64, build-macos-arm64]
runs-on: macos-13
steps:
- uses: actions/download-artifact@v4
Expand All @@ -76,20 +78,16 @@ jobs:
- name: Combine shared libraries
run: |
mkdir -p to-upload
lipo temp-macos-x64/libtdjson.dylib temp-macos-arm64/libtdjson.dylib \
lipo temp-macos-x86_64/libtdjson.dylib temp-macos-arm64/libtdjson.dylib \
-output to-upload/libtdjson.dylib -create
cd to-upload
file libtdjson.dylib
otool -L libtdjson.dylib
# echo "--x86_64--" >> info.txt
# cat ../temp-macos-x64/libtdjson.dylib >> info.txt
# echo "--arm64--" >> info.txt
# cat ../temp-macos-arm64/libtdjson.dylib >> info.txt
- uses: actions/upload-artifact@v4
with:
name: tdlib-macos
path: to-upload
build-windows-x64:
build-windows-x86_64:
name: 'Build TDLib / Windows x86_64'
runs-on: windows-2019
steps:
Expand Down Expand Up @@ -120,30 +118,28 @@ jobs:
cmake --build . --target tdjson --config Release --parallel 4
cd ..
cp build\Release\tdjson.dll to-upload\tdjson.dll
# It should be possible to print the dependencies using dumpbin.exe
vcpkg list | Select-String openssl,zlib > to-upload\info.txt
cat to-upload\info.txt
vcpkg list | Select-String openssl,zlib
- uses: actions/upload-artifact@v4
with:
name: tdlib-windows-x64
name: tdlib-windows-x86_64
path: to-upload

test:
name: 'Test / ${{ matrix.v.bin }} / ${{ matrix.v.os }}'
needs: [build-linux-x64, build-macos, build-windows-x64]
needs: [build-linux-x86_64, build-macos, build-windows-x86_64]
runs-on: ${{ matrix.v.os }}
strategy:
fail-fast: false
matrix:
v:
- os: ubuntu-latest
bin: tdlib-linux-x64
bin: tdlib-linux-x86_64-glibc
- os: macos-13
bin: tdlib-macos
- os: macos-14
bin: tdlib-macos
- os: windows-latest
bin: tdlib-windows-x64
bin: tdlib-windows-x86_64
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand Down Expand Up @@ -184,7 +180,6 @@ jobs:
path: packages/prebuilt-tdlib/prebuilds
- run: tree packages/prebuilt-tdlib
- run: du -hsc packages/prebuilt-tdlib/prebuilds/*
- run: npm run test:integration
env:
PREBUILT_PATH: packages/prebuilt-tdlib
- run: |
Expand Down
1 change: 0 additions & 1 deletion packages/prebuilt-tdlib/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
prebuilds/tdlib-*
prebuilds/*.json
to-upload/
21 changes: 0 additions & 21 deletions packages/prebuilt-tdlib/LICENSE

This file was deleted.

55 changes: 55 additions & 0 deletions packages/prebuilt-tdlib/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Blue Oak Model License

Version 1.0.0

## Purpose

This license gives everyone as much permission to work with
this software as possible, while protecting contributors
from liability.

## Acceptance

In order to receive this license, you must agree to its
rules. The rules of this license are both obligations
under that agreement and conditions to your license.
You must not do anything with this software that triggers
a rule that you cannot or will not follow.

## Copyright

Each contributor licenses you to do everything with this
software that would otherwise infringe that contributor's
copyright in it.

## Notices

You must ensure that everyone who gets a copy of
any part of this software from you, with or without
changes, also gets the text of this license or a link to
<https://blueoakcouncil.org/license/1.0.0>.

## Excuse

If anyone notifies you in writing that you have not
complied with [Notices](#notices), you can keep your
license by taking all practical steps to comply within 30
days after the notice. If you do not do so, your license
ends immediately.

## Patent

Each contributor licenses you to do everything with this
software that would otherwise infringe any patent claims
they can license or become able to license.

## Reliability

No contributor can revoke this license.

## No Liability

***As far as the law allows, this software comes as is,
without any warranty or condition, and no contributor
will be liable to anyone for any damages related to this
software or this license, under any kind of legal claim.***
31 changes: 22 additions & 9 deletions packages/prebuilt-tdlib/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Prebuilt TDLib

This package distributes pre-built [TDLib][] shared libraries through npm.
The libraries are built on GitHub Actions: [prebuilt-tdlib.yml][].
The libraries are built on GitHub Actions ([prebuilt-tdlib.yml][]) and published
using [npm publish --provenance][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 to prevent
compatibility issues in Node.js.
The shared libraries are statically linked against OpenSSL and zlib, for one, to
prevent compatibility issues in Node.js.

Supported systems:
- Linux x86_64 (requires glibc >= 2.22)
Expand All @@ -32,7 +34,8 @@ $ npm install [email protected]
`$ npm info prebuilt-tdlib dist-tags` to get the list of available versions.

The TDLib version is important: there is no backward compatibility and the
interface you use may significantly change after an update.
interface you use may significantly change after an update. It is, though,
recommended to use the latest TDLib version.

## Usage

Expand Down Expand Up @@ -111,7 +114,6 @@ An incomplete list is available below (mostly exceptions or "notable" versions):
| npm tag | notes |
| ------- | ----- |
| [![npm](https://img.shields.io/npm/v/prebuilt-tdlib/td-1.8.26.svg)](https://www.npmjs.com/package/prebuilt-tdlib/v/td-1.8.26) | [tdlib [b1b33cf42790ca10ef34abc2ac8828ae704f1f56](https://github.com/tdlib/td/commit/b1b33cf42790ca10ef34abc2ac8828ae704f1f56)] |
| [![npm](https://img.shields.io/npm/v/prebuilt-tdlib/td-1.8.19.svg)](https://www.npmjs.com/package/prebuilt-tdlib/v/td-1.8.19) | |
| [![npm](https://img.shields.io/npm/v/prebuilt-tdlib/td-1.8.14.svg)](https://www.npmjs.com/package/prebuilt-tdlib/v/td-1.8.14) | [tdlib [66234ae2537a99ec0eaf7b0857245a6e5c2d2bc9](https://github.com/tdlib/td/commit/66234ae2537a99ec0eaf7b0857245a6e5c2d2bc9)] |
| [![npm](https://img.shields.io/npm/v/prebuilt-tdlib/td-1.8.12.svg)](https://www.npmjs.com/package/prebuilt-tdlib/v/td-1.8.12) | [tdlib [70bee089d492437ce931aa78446d89af3da182fc](https://github.com/tdlib/td/commit/70bee089d492437ce931aa78446d89af3da182fc)] |
| [![npm](https://img.shields.io/npm/v/prebuilt-tdlib/td-1.8.7.svg)](https://www.npmjs.com/package/prebuilt-tdlib/v/td-1.8.7) | [tdlib [de5379f00b6af7686f197037ca3b494e6277e523](https://github.com/tdlib/td/commit/de5379f00b6af7686f197037ca3b494e6277e523)] |
Expand All @@ -123,10 +125,21 @@ Changes to the building process of `prebuilt-tdlib` are noted below.

### (unreleased)

The building process of Linux and macOS builds is significantly changed.

- on macOS, TDLib is built using macOS SDK from nixpkgs, and the minimal
supported macOS version is now 10.12 instead of 10.14.
First published as `<unpublished>`.

The building process is significantly changed in this update.

- Changed the structure of the package: instead of packing all binaries into the
prebuilt-tdlib package, every binary is split into a separate package, and all
the packages are specified in `optionalDependencies` of `prebuilt-tdlib`. The
same approach is used by, e.g., esbuild and swc. This installs a binary for
the user's system only, allowing `prebuilt-tdlib` to potentially scale for
more architectures and libc variants. One downside is that `node_modules`
can't simply be copied to a different platform anymore.
- On macOS, TDLib is built using macOS SDK from nixpkgs, and the minimal
supported macOS version is now 10.12 instead of 10.14. The arm64 macOS
library is now tested in the CI using the macos-14 GitHub runner (and not
crosscompiled anymore).
- On Linux, TDLib is now built using zig. The minimal glibc version is 2.22
instead of 2.17.

Expand Down
3 changes: 2 additions & 1 deletion packages/prebuilt-tdlib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** Currently present for forward compatibility only. */
export type Options = {
libc: 'glibc'
readonly forceLibc?: 'glibc' | 'musl'
}

export declare function getTdjson(options?: Options): string
58 changes: 40 additions & 18 deletions packages/prebuilt-tdlib/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
// @flow

const path = require('path')
const fs = require('fs')
const { prebuilds } = require('./prebuild-list')

const SCOPE = '@prebuilt-tdlib'

// For now, the `Options` object is present for forward compatibility.
// TODO: Add an option like "fallback"?
/*::
export type Options = {
// Can be 'glibc' | 'musl' in the future
libc: 'glibc'
+forceLibc?: 'glibc' | 'musl'
}
*/

function prebuild (pathcomps/*: string[] */) {
return path.resolve(__dirname, 'prebuilds', ...pathcomps)
}
let libcNameCache = null

// eslint-disable-next-line no-unused-vars
function getTdjson (options/*:: ?: Options */)/*: string */ {
const platform = process.platform + '-' + process.arch
switch (platform) {
case 'win32-x64': return prebuild(['tdlib-windows-x64', 'tdjson.dll'])
case 'darwin-x64': return prebuild(['tdlib-macos', 'libtdjson.dylib'])
case 'darwin-arm64': return prebuild(['tdlib-macos', 'libtdjson.dylib'])
case 'linux-x64': return prebuild(['tdlib-linux-x64', 'libtdjson.so'])
case 'android-x64': return prebuild(['tdlib-linux-x64', 'libtdjson.so'])
default: throw new Error(`The ${platform} platform is not supported`)
function detectLibc ()/*: 'glibc' | 'musl' */ {
// This function can return false results: it currently only checks for
// Alpine Linux as a heuristic (the same approach is used by node-gyp-build).
if (libcNameCache != null) return libcNameCache
let result = 'glibc'
try {
if (fs.existsSync('/etc/alpine-release'))
result = 'musl'
} catch (e) {
// Intentionally empty (defaults to 'glibc')
}
libcNameCache = result
return result
}

module.exports = { getTdjson }
exports.getTdjson = function getTdjson (options/*:: ?: Options */)/*: string */ {
let { platform, arch } = process
if (platform === 'android') platform = 'linux'
const libc = options?.forceLibc || (platform === 'linux' ? detectLibc() : null)
for (const prebuild of prebuilds) {
if (prebuild.requirements.os != null)
if (!prebuild.requirements.os.includes(platform)) continue
if (prebuild.requirements.cpu != null)
if (!prebuild.requirements.cpu.includes(arch)) continue
if (prebuild.libc != null && prebuild.libc !== libc) continue
// Found a prebuild for the current platform
const pkg = `${SCOPE}/${prebuild.packageName}/${prebuild.libfile}`
try {
return require.resolve(pkg)
} catch (e) {
throw new Error(`Could not load ${pkg} (are optionalDependencies installed?): ${e?.message}`)
}
}
let entirePlatform = `${platform}-${arch}`
if (libc != null) entirePlatform += '-' + libc
throw new Error(`The ${entirePlatform} platform is not supported`)
}
10 changes: 8 additions & 2 deletions packages/prebuilt-tdlib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
},
"files": [
"index.js",
"prebuild-list.js",
"index.d.ts",
"prebuilds"
"LICENSE.md"
],
"optionalDependencies": {},
"author": "Bannerets <[email protected]>",
"license": "MIT",
"license": "BlueOak-1.0.0",
"keywords": [
"telegram",
"telegram-api",
Expand All @@ -28,5 +30,9 @@
"url": "https://github.com/Bannerets/tdl/issues"
},
"homepage": "https://github.com/Bannerets/tdl/tree/main/packages/prebuilt-tdlib#readme",
"tdlib": {
"commit": "unknown",
"version": "unknown"
},
"private": true
}
Loading

0 comments on commit d3580a4

Please sign in to comment.