@@ -49,6 +49,44 @@ function getBuildPath(baseBuildPath: string, triplet: Triplet) {
4949 return path . join ( baseBuildPath , triplet ) ;
5050}
5151
52+ function getNdkPath ( ndkVersion : string ) {
53+ const { ANDROID_HOME } = process . env ;
54+ assert ( typeof ANDROID_HOME === "string" , "Missing env variable ANDROID_HOME" ) ;
55+ assert (
56+ fs . existsSync ( ANDROID_HOME ) ,
57+ `Expected the Android SDK at ${ ANDROID_HOME } ` ,
58+ ) ;
59+ const installNdkCommand = `sdkmanager --install "ndk;${ ndkVersion } "` ;
60+ const ndkPath = path . resolve ( ANDROID_HOME , "ndk" , ndkVersion ) ;
61+ assert (
62+ fs . existsSync ( ndkPath ) ,
63+ `Missing Android NDK v${ ndkVersion } (at ${ ndkPath } ) - run: ${ installNdkCommand } ` ,
64+ ) ;
65+ return ndkPath ;
66+ }
67+
68+ function getNdkToolchainPath ( ndkPath : string ) {
69+ const toolchainPath = path . join (
70+ ndkPath ,
71+ "build/cmake/android.toolchain.cmake" ,
72+ ) ;
73+ assert (
74+ fs . existsSync ( toolchainPath ) ,
75+ `No CMake toolchain found in ${ toolchainPath } ` ,
76+ ) ;
77+ return toolchainPath ;
78+ }
79+
80+ function getNdkLlvmBinPath ( ndkPath : string ) {
81+ const prebuiltPath = path . join ( ndkPath , "toolchains/llvm/prebuilt" ) ;
82+ const platforms = fs . readdirSync ( prebuiltPath ) ;
83+ assert (
84+ platforms . length === 1 ,
85+ `Expected a single llvm prebuilt toolchain in ${ prebuiltPath } ` ,
86+ ) ;
87+ return path . join ( prebuiltPath , platforms [ 0 ] , "bin" ) ;
88+ }
89+
5290export const platform : Platform < Triplet [ ] , AndroidOpts > = {
5391 id : "android" ,
5492 name : "Android" ,
@@ -85,26 +123,8 @@ export const platform: Platform<Triplet[], AndroidOpts> = {
85123 cmakeJs,
86124 } ,
87125 ) {
88- const { ANDROID_HOME } = process . env ;
89- assert (
90- typeof ANDROID_HOME === "string" ,
91- "Missing env variable ANDROID_HOME" ,
92- ) ;
93- assert (
94- fs . existsSync ( ANDROID_HOME ) ,
95- `Expected the Android SDK at ${ ANDROID_HOME } ` ,
96- ) ;
97- const installNdkCommand = `sdkmanager --install "ndk;${ ndkVersion } "` ;
98- const ndkPath = path . resolve ( ANDROID_HOME , "ndk" , ndkVersion ) ;
99- assert (
100- fs . existsSync ( ndkPath ) ,
101- `Missing Android NDK v${ ndkVersion } (at ${ ndkPath } ) - run: ${ installNdkCommand } ` ,
102- ) ;
103-
104- const toolchainPath = path . join (
105- ndkPath ,
106- "build/cmake/android.toolchain.cmake" ,
107- ) ;
126+ const ndkPath = getNdkPath ( ndkVersion ) ;
127+ const toolchainPath = getNdkToolchainPath ( ndkPath ) ;
108128
109129 const commonDefinitions = [
110130 ...define ,
@@ -174,14 +194,14 @@ export const platform: Platform<Triplet[], AndroidOpts> = {
174194 async postBuild (
175195 outputPath ,
176196 triplets ,
177- { autoLink, configuration, target, build } ,
197+ { autoLink, configuration, target, build, strip , ndkVersion } ,
178198 ) {
179199 const prebuilds : Record <
180200 string ,
181201 { triplet : Triplet ; libraryPath : string } [ ]
182202 > = { } ;
183203
184- for ( const { triplet } of triplets ) {
204+ for ( const { spawn , triplet } of triplets ) {
185205 const buildPath = getBuildPath ( build , triplet ) ;
186206 assert ( fs . existsSync ( buildPath ) , `Expected a directory at ${ buildPath } ` ) ;
187207 const targets = await cmakeFileApi . readCurrentTargetsDeep (
@@ -210,9 +230,24 @@ export const platform: Platform<Triplet[], AndroidOpts> = {
210230 if ( ! ( sharedLibrary . name in prebuilds ) ) {
211231 prebuilds [ sharedLibrary . name ] = [ ] ;
212232 }
233+ const libraryPath = path . join ( buildPath , artifact . path ) ;
234+ assert (
235+ fs . existsSync ( libraryPath ) ,
236+ `Expected built library at ${ libraryPath } ` ,
237+ ) ;
238+
239+ if ( strip ) {
240+ const llvmBinPath = getNdkLlvmBinPath ( getNdkPath ( ndkVersion ) ) ;
241+ const stripToolPath = path . join ( llvmBinPath , `llvm-strip` ) ;
242+ assert (
243+ fs . existsSync ( stripToolPath ) ,
244+ `Expected llvm-strip to exist at ${ stripToolPath } ` ,
245+ ) ;
246+ await spawn ( stripToolPath , [ libraryPath ] ) ;
247+ }
213248 prebuilds [ sharedLibrary . name ] . push ( {
214249 triplet,
215- libraryPath : path . join ( buildPath , artifact . path ) ,
250+ libraryPath,
216251 } ) ;
217252 }
218253
0 commit comments