Skip to content

Commit 55895aa

Browse files
authored
Add transformation of "defines" to gyp-to-cmake (#97)
* Add test for command expansion * Emit "defines" via target_compile_definitions
1 parent d168ec6 commit 55895aa

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type GypTarget = {
77
target_name: string;
88
sources: string[];
99
include_dirs?: string[];
10+
defines?: string[];
1011
};
1112

1213
export type GypBinding = {

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,63 @@ describe("bindingGypToCmakeLists", () => {
6464

6565
assert(output.includes("add_library(foo SHARED file\\ with\\ spaces.cc"));
6666
});
67+
68+
describe("command expansions", () => {
69+
it("should expand", () => {
70+
const output = bindingGypToCmakeLists({
71+
projectName: "some-project",
72+
gyp: {
73+
targets: [
74+
{
75+
target_name: "foo",
76+
sources: ["<!echo bar baz"],
77+
},
78+
],
79+
},
80+
});
81+
82+
// Adding \ between bar and baz, as we expect the "bar baz" to be handled like a path with spaces
83+
assert(output.includes("add_library(foo SHARED bar\\ baz"));
84+
});
85+
86+
it("should expand into lists when prefixed with '@'", () => {
87+
const output = bindingGypToCmakeLists({
88+
projectName: "some-project",
89+
gyp: {
90+
targets: [
91+
{
92+
target_name: "foo",
93+
sources: ["<!@echo bar baz"],
94+
},
95+
],
96+
},
97+
});
98+
99+
assert(output.includes("add_library(foo SHARED bar baz"));
100+
});
101+
});
102+
103+
describe("defines", () => {
104+
it("should add defines as target-specific compile definitions", () => {
105+
const output = bindingGypToCmakeLists({
106+
projectName: "some-project",
107+
gyp: {
108+
targets: [
109+
{
110+
target_name: "foo",
111+
sources: ["foo.cc"],
112+
defines: ["FOO", "BAR=value"],
113+
},
114+
],
115+
},
116+
});
117+
118+
assert(
119+
output.includes(
120+
"target_compile_definitions(foo PRIVATE FOO BAR=value)"
121+
),
122+
`Expected output to include target_compile_definitions:\n${output}`
123+
);
124+
});
125+
});
67126
});

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function isCmdExpansion(value: string) {
1919
return trimmedValue.startsWith("<!");
2020
}
2121

22-
function escapePath(source: string) {
22+
function escapeSpaces(source: string) {
2323
return source.replace(/ /g, "\\ ");
2424
}
2525

@@ -65,27 +65,33 @@ export function bindingGypToCmakeLists({
6565
//"cmake_policy(SET CMP0042 NEW)",
6666
`project(${projectName})`,
6767
"",
68-
`add_compile_definitions(-DNAPI_VERSION=${napiVersion})`,
68+
// Declaring a project-wide NAPI_VERSION as a fallback for targets that don't explicitly set it
69+
`add_compile_definitions(NAPI_VERSION=${napiVersion})`,
6970
];
7071

7172
for (const target of gyp.targets) {
7273
const { target_name: targetName } = target;
7374

7475
// TODO: Handle "conditions"
75-
// TODO: Handle "defines"
7676
// TODO: Handle "cflags"
7777
// TODO: Handle "ldflags"
7878

7979
const escapedJoinedSources = target.sources
8080
.flatMap(mapExpansion)
8181
.map(transformPath)
82-
.map(escapePath)
82+
.map(escapeSpaces)
8383
.join(" ");
8484

8585
const escapedJoinedIncludes = (target.include_dirs || [])
8686
.flatMap(mapExpansion)
8787
.map(transformPath)
88-
.map(escapePath)
88+
.map(escapeSpaces)
89+
.join(" ");
90+
91+
const escapedJoinedDefines = (target.defines || [])
92+
.flatMap(mapExpansion)
93+
.map(transformPath)
94+
.map(escapeSpaces)
8995
.join(" ");
9096

9197
lines.push(
@@ -94,7 +100,12 @@ export function bindingGypToCmakeLists({
94100
`set_target_properties(${targetName} PROPERTIES PREFIX "" SUFFIX ".node")`,
95101
`target_include_directories(${targetName} PRIVATE ${escapedJoinedIncludes} \${CMAKE_JS_INC})`,
96102
`target_link_libraries(${targetName} PRIVATE \${CMAKE_JS_LIB})`,
97-
`target_compile_features(${targetName} PRIVATE cxx_std_17)`
103+
`target_compile_features(${targetName} PRIVATE cxx_std_17)`,
104+
...(escapedJoinedDefines
105+
? [
106+
`target_compile_definitions(${targetName} PRIVATE ${escapedJoinedDefines})`,
107+
]
108+
: [])
98109
// or
99110
// `set_target_properties(${targetName} PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO)`,
100111
);

0 commit comments

Comments
 (0)