@@ -74,6 +74,26 @@ const outPathOption = new Option(
7474 "Specify the output directory to store the final build artifacts" ,
7575) . default ( false , "./{build}/{configuration}" ) ;
7676
77+ const defineOption = new Option (
78+ "-D,--define <entry...>" ,
79+ "Define cache variables passed when configuring projects" ,
80+ ) . argParser < Record < string , string | CmakeTypedDefinition > > (
81+ ( input , previous = { } ) => {
82+ // TODO: Implement splitting of value using a regular expression (using named groups) for the format <var>[:<type>]=<value>
83+ // and return an object keyed by variable name with the string value as value or alternatively an array of [value, type]
84+ const match = input . match (
85+ / ^ (?< name > [ ^ : = ] + ) ( : (?< type > [ ^ = ] + ) ) ? = (?< value > .+ ) $ / ,
86+ ) ;
87+ if ( ! match || ! match . groups ) {
88+ throw new Error (
89+ `Invalid format for -D/--define argument: ${ input } . Expected <var>[:<type>]=<value>` ,
90+ ) ;
91+ }
92+ const { name, type, value } = match . groups ;
93+ return { ...previous , [ name ] : type ? { value, type } : value } ;
94+ } ,
95+ ) ;
96+
7797const noAutoLinkOption = new Option (
7898 "--no-auto-link" ,
7999 "Don't mark the output as auto-linkable by react-native-node-api" ,
@@ -92,6 +112,7 @@ let program = new Command("cmake-rn")
92112 . addOption ( buildPathOption )
93113 . addOption ( outPathOption )
94114 . addOption ( configurationOption )
115+ . addOption ( defineOption )
95116 . addOption ( cleanOption )
96117 . addOption ( noAutoLinkOption )
97118 . addOption ( noWeakNodeApiLinkageOption ) ;
@@ -269,14 +290,15 @@ async function configureProject<T extends string>(
269290 const { target, buildPath, outputPath } = context ;
270291 const { verbose, source, weakNodeApiLinkage } = options ;
271292
272- const nodeApiVariables =
293+ const nodeApiDefinitions =
273294 weakNodeApiLinkage && isSupportedTriplet ( target )
274295 ? getWeakNodeApiVariables ( target )
275296 : // TODO: Make this a part of the platform definition
276297 { } ;
277298
278- const declarations = {
279- ...nodeApiVariables ,
299+ const definitions = {
300+ ...nodeApiDefinitions ,
301+ ...options . define ,
280302 CMAKE_LIBRARY_OUTPUT_DIRECTORY : outputPath ,
281303 } ;
282304
@@ -288,7 +310,7 @@ async function configureProject<T extends string>(
288310 "-B" ,
289311 buildPath ,
290312 ...platform . configureArgs ( context , options ) ,
291- ...toDeclarationArguments ( declarations ) ,
313+ ...toDefineArguments ( definitions ) ,
292314 ] ,
293315 {
294316 outputMode : verbose ? "inherit" : "buffered" ,
@@ -321,11 +343,18 @@ async function buildProject<T extends string>(
321343 ) ;
322344}
323345
324- function toDeclarationArguments ( declarations : Record < string , string > ) {
325- return Object . entries ( declarations ) . flatMap ( ( [ key , value ] ) => [
326- "-D" ,
327- `${ key } =${ value } ` ,
328- ] ) ;
346+ type CmakeTypedDefinition = { value : string ; type : string } ;
347+
348+ function toDefineArguments (
349+ declarations : Record < string , string | CmakeTypedDefinition > ,
350+ ) {
351+ return Object . entries ( declarations ) . flatMap ( ( [ key , definition ] ) => {
352+ if ( typeof definition === "string" ) {
353+ return [ "-D" , `${ key } =${ definition } ` ] ;
354+ } else {
355+ return [ "-D" , `${ key } :${ definition . type } =${ definition . value } ` ] ;
356+ }
357+ } ) ;
329358}
330359
331360export { program } ;
0 commit comments