@@ -5,9 +5,9 @@ import path from "node:path";
55import { Option , oraPromise , chalk } from "@react-native-node-api/cli-utils" ;
66import {
77 createAndroidLibsDirectory ,
8- determineAndroidLibsFilename ,
98 AndroidTriplet as Triplet ,
109} from "react-native-node-api" ;
10+ import * as cmakeFileApi from "cmake-file-api" ;
1111
1212import type { Platform } from "./types.js" ;
1313
@@ -121,50 +121,64 @@ export const platform: Platform<Triplet[], AndroidOpts> = {
121121 const { ANDROID_HOME } = process . env ;
122122 return typeof ANDROID_HOME === "string" && fs . existsSync ( ANDROID_HOME ) ;
123123 } ,
124- async postBuild ( { outputPath, triplets } , { autoLink } ) {
125- // TODO: Include `configuration` in the output path
126- const libraryPathByTriplet = Object . fromEntries (
127- await Promise . all (
128- triplets . map ( async ( { triplet, outputPath } ) => {
129- assert (
130- fs . existsSync ( outputPath ) ,
131- `Expected a directory at ${ outputPath } ` ,
132- ) ;
133- // Expect binary file(s), either .node or .so
134- const dirents = await fs . promises . readdir ( outputPath , {
135- withFileTypes : true ,
136- } ) ;
137- const result = dirents
138- . filter (
139- ( dirent ) =>
140- dirent . isFile ( ) &&
141- ( dirent . name . endsWith ( ".so" ) || dirent . name . endsWith ( ".node" ) ) ,
142- )
143- . map ( ( dirent ) => path . join ( dirent . parentPath , dirent . name ) ) ;
144- assert . equal ( result . length , 1 , "Expected exactly one library file" ) ;
145- return [ triplet , result [ 0 ] ] as const ;
146- } ) ,
147- ) ,
148- ) as Record < Triplet , string > ;
149- const androidLibsFilename = determineAndroidLibsFilename (
150- Object . values ( libraryPathByTriplet ) ,
151- ) ;
152- const androidLibsOutputPath = path . resolve ( outputPath , androidLibsFilename ) ;
124+ async postBuild ( { outputPath, triplets } , { autoLink, configuration } ) {
125+ const prebuilds : Record <
126+ string ,
127+ { triplet : Triplet ; libraryPath : string } [ ]
128+ > = { } ;
153129
154- await oraPromise (
155- createAndroidLibsDirectory ( {
156- outputPath : androidLibsOutputPath ,
157- libraryPathByTriplet,
158- autoLink,
159- } ) ,
160- {
161- text : "Assembling Android libs directory" ,
162- successText : `Android libs directory assembled into ${ chalk . dim (
163- path . relative ( process . cwd ( ) , androidLibsOutputPath ) ,
164- ) } `,
165- failText : ( { message } ) =>
166- `Failed to assemble Android libs directory: ${ message } ` ,
167- } ,
168- ) ;
130+ for ( const { triplet, buildPath } of triplets ) {
131+ assert ( fs . existsSync ( buildPath ) , `Expected a directory at ${ buildPath } ` ) ;
132+ const targets = await cmakeFileApi . readCurrentTargetsDeep (
133+ buildPath ,
134+ configuration ,
135+ "2.0" ,
136+ ) ;
137+ const sharedLibraries = targets . filter (
138+ ( target ) => target . type === "SHARED_LIBRARY" ,
139+ ) ;
140+ assert . equal (
141+ sharedLibraries . length ,
142+ 1 ,
143+ "Expected exactly one shared library" ,
144+ ) ;
145+ const [ sharedLibrary ] = sharedLibraries ;
146+ const { artifacts } = sharedLibrary ;
147+ assert (
148+ artifacts && artifacts . length === 1 ,
149+ "Expected exactly one artifact" ,
150+ ) ;
151+ const [ artifact ] = artifacts ;
152+ // Add prebuild entry, creating a new entry if needed
153+ if ( ! ( sharedLibrary . name in prebuilds ) ) {
154+ prebuilds [ sharedLibrary . name ] = [ ] ;
155+ }
156+ prebuilds [ sharedLibrary . name ] . push ( {
157+ triplet,
158+ libraryPath : path . join ( buildPath , artifact . path ) ,
159+ } ) ;
160+ }
161+
162+ for ( const [ libraryName , libraries ] of Object . entries ( prebuilds ) ) {
163+ const prebuildOutputPath = path . resolve (
164+ outputPath ,
165+ `${ libraryName } .android.node` ,
166+ ) ;
167+ await oraPromise (
168+ createAndroidLibsDirectory ( {
169+ outputPath : prebuildOutputPath ,
170+ libraries,
171+ autoLink,
172+ } ) ,
173+ {
174+ text : `Assembling Android libs directory (${ libraryName } )` ,
175+ successText : `Android libs directory (${ libraryName } ) assembled into ${ chalk . dim (
176+ path . relative ( process . cwd ( ) , prebuildOutputPath ) ,
177+ ) } `,
178+ failText : ( { message } ) =>
179+ `Failed to assemble Android libs directory (${ libraryName } ): ${ message } ` ,
180+ } ,
181+ ) ;
182+ }
169183 } ,
170184} ;
0 commit comments