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
@@ -86,4 +86,54 @@ export async function run(command, args = [], options = {}) {
8686 await once ( proc , 'exit' ) ;
8787
8888 if ( proc . exitCode != 0 ) throw new Error ( `CRASH(${ proc . exitCode } ): ${ commandDetails } ` ) ;
89- }
89+ }
90+
91+ /**
92+ * @returns the libc (`musl` or `glibc`), if the platform is linux, otherwise null.
93+ */
94+ export function getLibc ( ) {
95+ if ( process . platform !== 'linux' ) return null ;
96+
97+ /**
98+ * executes `ldd --version`. on Alpine linux, `ldd` and `ldd --version` return exit code 1 and print the version
99+ * info to stderr, but on other platforms, `ldd --version` prints to stdout and returns exit code 0.
100+ *
101+ * So, this script works on both by return stderr if the command returns a non-zero exit code, otherwise stdout.
102+ */
103+ function lddVersion ( ) {
104+ try {
105+ return execSync ( 'ldd --version' , { encoding : 'utf-8' } ) ;
106+ } catch ( error ) {
107+ return error . stderr ;
108+ }
109+ }
110+
111+ console . error ( { ldd : lddVersion ( ) } ) ;
112+ return lddVersion ( ) . includes ( 'musl' ) ? 'musl' : 'glibc' ;
113+ }
114+
115+ /**
116+ * @returns the name of the package inside the libmognocrypt prebuild tarball for the current platform, arch and libc (if linux).
117+ */
118+ export function getLibmongocryptPackageName ( ) {
119+ const platformFactory = {
120+ 'darwin' : ( ) => 'macos' ,
121+ 'win32' : ( ) => 'windows-test' ,
122+ 'linux' : ( ) => {
123+ const key = `${ getLibc ( ) } -${ process . arch } ` ;
124+ return {
125+ [ 'musl-x64' ] : 'alpine-amd64-earthly' ,
126+ [ 'musl-arm64' ] : 'alpine-arm64-earthly' ,
127+ [ 'glibc-ppc64' ] : 'rhel-71-ppc64el' ,
128+ [ 'glibc-s390x' ] : 'rhel72-zseries-test' ,
129+ [ 'glibc-arm64' ] : 'ubuntu1804-arm64' ,
130+ [ 'glibc-x64' ] : 'rhel-70-64-bit' ,
131+ } [ key ]
132+ }
133+ } [ process . platform ] ?? ( ( ) => {
134+ throw new Error ( 'unexpected platform.' )
135+ } ) ;
136+
137+ return platformFactory ( ) ;
138+ }
139+
0 commit comments