Skip to content

Commit 275efcf

Browse files
committed
Add --namespaced-targets to gyp-to-cmake
1 parent 80ae73b commit 275efcf

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

.changeset/real-emus-jam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gyp-to-cmake": minor
3+
---
4+
5+
Add --namespaced-targets to allow a root project to add many sub-projects

packages/gyp-to-cmake/src/cli.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ export const program = new Command("gyp-to-cmake")
9090
"Disable emitting target properties to produce Apple frameworks",
9191
)
9292
.option("--cpp <version>", "C++ standard version", "17")
93+
.option(
94+
"--namespaced-targets",
95+
"Use namespaced targets, to allow multiple targets with the same name to be referenced from a single parent project",
96+
false,
97+
)
9398
.addOption(projectNameOption)
9499
.argument(
95100
"[path]",
@@ -107,6 +112,7 @@ export const program = new Command("gyp-to-cmake")
107112
weakNodeApi,
108113
appleFramework,
109114
projectName,
115+
namespacedTargets,
110116
},
111117
) => {
112118
const options: Omit<TransformOptions, "projectName"> = {
@@ -117,6 +123,7 @@ export const program = new Command("gyp-to-cmake")
117123
defineNapiVersion,
118124
weakNodeApi,
119125
appleFramework,
126+
namespacedTargets,
120127
};
121128
const stat = fs.statSync(targetPath);
122129
if (stat.isFile()) {

packages/gyp-to-cmake/src/transformer.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type GypToCmakeListsOptions = {
1616
defineNapiVersion?: boolean;
1717
weakNodeApi?: boolean;
1818
appleFramework?: boolean;
19+
namespacedTargets?: boolean;
1920
};
2021

2122
function isCmdExpansion(value: string) {
@@ -50,6 +51,7 @@ export function bindingGypToCmakeLists({
5051
weakNodeApi = false,
5152
appleFramework = true,
5253
compileFeatures = [],
54+
namespacedTargets = false,
5355
}: GypToCmakeListsOptions): string {
5456
function mapExpansion(value: string): string[] {
5557
if (!isCmdExpansion(value)) {
@@ -123,20 +125,26 @@ export function bindingGypToCmakeLists({
123125
escapedIncludes.push("${CMAKE_JS_INC}");
124126
}
125127

128+
const actualTargetName = namespacedTargets
129+
? `${projectName}-${targetName}`
130+
: targetName;
131+
126132
function setTargetPropertiesLines(
127133
properties: Record<string, string>,
128134
indent = "",
129135
): string[] {
130136
return [
131-
`${indent}set_target_properties(${targetName} PROPERTIES`,
137+
`${indent}set_target_properties(${actualTargetName} PROPERTIES`,
132138
...Object.entries(properties).map(
133139
([key, value]) => `${indent} ${key} ${value ? value : '""'}`,
134140
),
135141
`${indent} )`,
136142
];
137143
}
138144

139-
lines.push(`add_library(${targetName} SHARED ${escapedSources.join(" ")})`);
145+
lines.push(
146+
`add_library(${actualTargetName} SHARED ${escapedSources.join(" ")})`,
147+
);
140148

141149
if (appleFramework) {
142150
lines.push(
@@ -161,6 +169,8 @@ export function bindingGypToCmakeLists({
161169
{
162170
PREFIX: "",
163171
SUFFIX: ".node",
172+
// Ensure the final library use the non-namespaced target name
173+
...(actualTargetName ? { OUTPUT_NAME: targetName } : {}),
164174
},
165175
" ",
166176
),
@@ -178,31 +188,31 @@ export function bindingGypToCmakeLists({
178188

179189
if (libraries.length > 0) {
180190
lines.push(
181-
`target_link_libraries(${targetName} PRIVATE ${libraries.join(" ")})`,
191+
`target_link_libraries(${actualTargetName} PRIVATE ${libraries.join(" ")})`,
182192
);
183193
}
184194

185195
if (escapedIncludes.length > 0) {
186196
lines.push(
187-
`target_include_directories(${targetName} PRIVATE ${escapedIncludes.join(
197+
`target_include_directories(${actualTargetName} PRIVATE ${escapedIncludes.join(
188198
" ",
189199
)})`,
190200
);
191201
}
192202

193203
if (escapedDefines.length > 0) {
194204
lines.push(
195-
`target_compile_definitions(${targetName} PRIVATE ${escapedDefines.join(" ")})`,
205+
`target_compile_definitions(${actualTargetName} PRIVATE ${escapedDefines.join(" ")})`,
196206
);
197207
}
198208

199209
if (compileFeatures.length > 0) {
200210
lines.push(
201-
`target_compile_features(${targetName} PRIVATE ${compileFeatures.join(" ")})`,
211+
`target_compile_features(${actualTargetName} PRIVATE ${compileFeatures.join(" ")})`,
202212
);
203213
}
204214

205-
// `set_target_properties(${targetName} PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO)`,
215+
// `set_target_properties(${actualTargetName} PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO)`,
206216
}
207217

208218
if (!weakNodeApi) {

0 commit comments

Comments
 (0)