|
| 1 | +import { execSync } from "child_process"; |
| 2 | + |
| 3 | + |
| 4 | +/** |
| 5 | + * @returns the libc (`musl` or `glibc`), if the platform is linux, otherwise null. |
| 6 | + */ |
| 7 | +export function getLibc() { |
| 8 | + if (process.platform !== 'linux') return null; |
| 9 | + |
| 10 | + /** |
| 11 | + * executes `ldd --version`. on Alpine linux, `ldd` and `ldd --version` return exit code 1 and print the version |
| 12 | + * info to stderr, but on other platforms, `ldd --version` prints to stdout and returns exit code 0. |
| 13 | + * |
| 14 | + * So, this script works on both by return stderr if the command returns a non-zero exit code, otherwise stdout. |
| 15 | + */ |
| 16 | + function lddVersion() { |
| 17 | + try { |
| 18 | + return execSync('ldd --version', { encoding: 'utf-8' }); |
| 19 | + } catch (error) { |
| 20 | + return error.stderr; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + console.error({ ldd: lddVersion() }); |
| 25 | + return lddVersion().includes('musl') ? 'musl' : 'glibc'; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * @returns the name of the package inside the libmognocrypt prebuild tarball for the current platform, arch and libc (if linux). |
| 30 | + */ |
| 31 | +export function getLibmongocryptPackageName() { |
| 32 | + const platformFactory = { |
| 33 | + 'darwin': () => 'macos', |
| 34 | + 'win32': () => 'windows-test', |
| 35 | + 'linux': () => { |
| 36 | + const key = `${getLibc()}-${process.arch}`; |
| 37 | + return { |
| 38 | + ['musl-x64']: 'alpine-amd64-earthly', |
| 39 | + ['musl-arm64']: 'alpine-arm64-earthly', |
| 40 | + ['glibc-ppc64']: 'rhel-71-ppc64el', |
| 41 | + ['glibc-s390x']: 'rhel72-zseries-test', |
| 42 | + ['glibc-arm64']: 'ubuntu1804-arm64', |
| 43 | + ['glibc-x64']: 'rhel-70-64-bit', |
| 44 | + }[key] |
| 45 | + } |
| 46 | + }[process.platform] ?? (() => { |
| 47 | + throw new Error('unexpected platform.') |
| 48 | + }); |
| 49 | + |
| 50 | + return platformFactory(); |
| 51 | +} |
| 52 | + |
0 commit comments