1+ import cp from "node:child_process" ;
2+
13import type { GypBinding } from "./gyp.js" ;
24
35const DEFAULT_NAPI_VERSION = 8 ;
@@ -6,6 +8,7 @@ export type GypToCmakeListsOptions = {
68 gyp : GypBinding ;
79 projectName : string ;
810 napiVersion ?: number ;
11+ executeCmdExpansions ?: boolean ;
912 unsupportedBehaviour ?: "skip" | "warn" | "throw" ;
1013} ;
1114
@@ -26,17 +29,23 @@ export function bindingGypToCmakeLists({
2629 gyp,
2730 projectName,
2831 napiVersion = DEFAULT_NAPI_VERSION ,
32+ executeCmdExpansions = true ,
2933 unsupportedBehaviour = "skip" ,
3034} : GypToCmakeListsOptions ) : string {
31- function filterExpansion ( value : string ) {
35+ function mapExpansion ( value : string ) : string [ ] {
3236 if ( ! isCmdExpansion ( value ) ) {
33- return true ;
37+ return [ value ] ;
38+ } else if ( executeCmdExpansions ) {
39+ const cmd = value . trim ( ) . replace ( / ^ < ! @ ? / , "" ) ;
40+ const output = cp . execSync ( cmd , { encoding : "utf-8" } ) . trim ( ) ;
41+ // Split on whitespace, if the expansion starts with "<!@"
42+ return value . trim ( ) . startsWith ( "<!@" ) ? output . split ( / \s / ) : [ output ] ;
3443 } else if ( unsupportedBehaviour === "throw" ) {
3544 throw new Error ( `Unsupported command expansion: ${ value } ` ) ;
3645 } else if ( unsupportedBehaviour === "warn" ) {
3746 console . warn ( `Unsupported command expansion: ${ value } ` ) ;
3847 }
39- return false ;
48+ return [ value ] ;
4049 }
4150
4251 const lines : string [ ] = [
@@ -57,12 +66,12 @@ export function bindingGypToCmakeLists({
5766 // TODO: Handle "ldflags"
5867
5968 const escapedJoinedSources = target . sources
60- . filter ( filterExpansion )
69+ . flatMap ( mapExpansion )
6170 . map ( escapePath )
6271 . join ( " " ) ;
6372
6473 const escapedJoinedIncludes = ( target . include_dirs || [ ] )
65- . filter ( filterExpansion )
74+ . flatMap ( mapExpansion )
6675 . map ( escapePath )
6776 . join ( " " ) ;
6877
@@ -71,9 +80,8 @@ export function bindingGypToCmakeLists({
7180 `add_library(${ targetName } SHARED ${ escapedJoinedSources } \${CMAKE_JS_SRC})` ,
7281 `set_target_properties(${ targetName } PROPERTIES PREFIX "" SUFFIX ".node")` ,
7382 `target_include_directories(${ targetName } PRIVATE ${ escapedJoinedIncludes } \${CMAKE_JS_INC})` ,
74- `target_link_libraries(${ targetName } PRIVATE \${CMAKE_JS_LIB})`
75- // TODO: Consider declaring the C++ standard version
76- // `target_compile_features(${targetName} PRIVATE cxx_std_17)`
83+ `target_link_libraries(${ targetName } PRIVATE \${CMAKE_JS_LIB})` ,
84+ `target_compile_features(${ targetName } PRIVATE cxx_std_17)`
7785 // or
7886 // `set_target_properties(${targetName} PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO)`,
7987 ) ;
0 commit comments