@@ -74,6 +74,26 @@ const outPathOption = new Option(
74
74
"Specify the output directory to store the final build artifacts" ,
75
75
) . default ( false , "./{build}/{configuration}" ) ;
76
76
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
+
77
97
const noAutoLinkOption = new Option (
78
98
"--no-auto-link" ,
79
99
"Don't mark the output as auto-linkable by react-native-node-api" ,
@@ -92,6 +112,7 @@ let program = new Command("cmake-rn")
92
112
. addOption ( buildPathOption )
93
113
. addOption ( outPathOption )
94
114
. addOption ( configurationOption )
115
+ . addOption ( defineOption )
95
116
. addOption ( cleanOption )
96
117
. addOption ( noAutoLinkOption )
97
118
. addOption ( noWeakNodeApiLinkageOption ) ;
@@ -269,14 +290,15 @@ async function configureProject<T extends string>(
269
290
const { target, buildPath, outputPath } = context ;
270
291
const { verbose, source, weakNodeApiLinkage } = options ;
271
292
272
- const nodeApiVariables =
293
+ const nodeApiDefinitions =
273
294
weakNodeApiLinkage && isSupportedTriplet ( target )
274
295
? getWeakNodeApiVariables ( target )
275
296
: // TODO: Make this a part of the platform definition
276
297
{ } ;
277
298
278
- const declarations = {
279
- ...nodeApiVariables ,
299
+ const definitions = {
300
+ ...nodeApiDefinitions ,
301
+ ...options . define ,
280
302
CMAKE_LIBRARY_OUTPUT_DIRECTORY : outputPath ,
281
303
} ;
282
304
@@ -288,7 +310,7 @@ async function configureProject<T extends string>(
288
310
"-B" ,
289
311
buildPath ,
290
312
...platform . configureArgs ( context , options ) ,
291
- ...toDeclarationArguments ( declarations ) ,
313
+ ...toDefineArguments ( definitions ) ,
292
314
] ,
293
315
{
294
316
outputMode : verbose ? "inherit" : "buffered" ,
@@ -321,11 +343,18 @@ async function buildProject<T extends string>(
321
343
) ;
322
344
}
323
345
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
+ } ) ;
329
358
}
330
359
331
360
export { program } ;
0 commit comments