11// @ts -check
22
3- import { execSync } from "child_process" ;
43import path from "path" ;
54import url from 'node:url' ;
65import { spawn } from "node:child_process" ;
76import { once } from "node:events" ;
7+ import { execSync } from "child_process" ;
88
99const __dirname = path . dirname ( url . fileURLToPath ( import . meta. url ) ) ;
1010
@@ -55,22 +55,25 @@ export function buildLibmongocryptDownloadUrl(ref, platform) {
5555}
5656
5757export function getLibmongocryptPrebuildName ( ) {
58- const platformMatrix = {
59- [ 'darwin-arm64' ] : 'macos' ,
60- [ 'darwin-x64' ] : 'macos' ,
61- [ 'linux-ppc64' ] : 'rhel-71-ppc64el' ,
62- [ 'linux-s390x' ] : 'rhel72-zseries-test' ,
63- [ 'linux-arm64' ] : 'ubuntu1804-arm64' ,
64- [ 'linux-x64' ] : 'rhel-70-64-bit' ,
65- [ 'win32-x64' ] : 'windows-test'
66- } ;
67-
68- const detectedPlatform = `${ process . platform } -${ process . arch } ` ;
69- const prebuild = platformMatrix [ detectedPlatform ] ;
70-
71- if ( prebuild == null ) throw new Error ( `Unsupported: ${ detectedPlatform } ` ) ;
72-
73- return prebuild ;
58+ const prebuildIdentifierFactory = {
59+ 'darwin' : ( ) => 'macos' ,
60+ 'win32' : ( ) => 'windows-test' ,
61+ 'linux' : ( ) => {
62+ const key = `${ getLibc ( ) } -${ process . arch } ` ;
63+ return {
64+ [ 'musl-x64' ] : 'alpine-amd64-earthly' ,
65+ [ 'musl-arm64' ] : 'alpine-arm64-earthly' ,
66+ [ 'glibc-ppc64' ] : 'rhel-71-ppc64el' ,
67+ [ 'glibc-s390x' ] : 'rhel72-zseries-test' ,
68+ [ 'glibc-arm64' ] : 'ubuntu1804-arm64' ,
69+ [ 'glibc-x64' ] : 'rhel-70-64-bit' ,
70+ } [ key ]
71+ }
72+ } [ process . platform ] ?? ( ( ) => {
73+ throw new Error ( `Unsupported platform` ) ;
74+ } ) ;
75+
76+ return prebuildIdentifierFactory ( ) ;
7477}
7578
7679/** `xtrace` style command runner, uses spawn so that stdio is inherited */
@@ -86,4 +89,28 @@ export async function run(command, args = [], options = {}) {
8689 await once ( proc , 'exit' ) ;
8790
8891 if ( proc . exitCode != 0 ) throw new Error ( `CRASH(${ proc . exitCode } ): ${ commandDetails } ` ) ;
89- }
92+ }
93+
94+ /**
95+ * @returns the libc (`musl` or `glibc`), if the platform is linux, otherwise null.
96+ */
97+ function getLibc ( ) {
98+ if ( process . platform !== 'linux' ) return null ;
99+
100+ /**
101+ * executes `ldd --version`. on Alpine linux, `ldd` and `ldd --version` return exit code 1 and print the version
102+ * info to stderr, but on other platforms, `ldd --version` prints to stdout and returns exit code 0.
103+ *
104+ * So, this script works on both by return stderr if the command returns a non-zero exit code, otherwise stdout.
105+ */
106+ function lddVersion ( ) {
107+ try {
108+ return execSync ( 'ldd --version' , { encoding : 'utf-8' } ) ;
109+ } catch ( error ) {
110+ return error . stderr ;
111+ }
112+ }
113+
114+ console . error ( { ldd : lddVersion ( ) } ) ;
115+ return lddVersion ( ) . includes ( 'musl' ) ? 'musl' : 'glibc' ;
116+ }
0 commit comments