Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/gyp-cflags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"gyp-to-cmake": minor
---

Translate target-level `cflags` from `binding.gyp` into private CMake compile
options. The parser now validates that `cflags` is an array of strings, and the
generated options preserve command expansion and escaped spaces.
43 changes: 43 additions & 0 deletions packages/gyp-to-cmake/src/gyp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,47 @@ describe("gyp.assertRoot", () => {
assertBinding(input);
assert(Array.isArray(input.targets));
});

it("should accept target cflags", () => {
assert.doesNotThrow(() => {
assertBinding(
{
targets: [
{
target_name: "addon",
sources: ["addon.cc"],
cflags: ["-fPIC", "-Wall"],
},
],
},
true,
);
});
});

it("should reject malformed target cflags", () => {
assert.throws(() => {
assertBinding({
targets: [
{
target_name: "addon",
sources: ["addon.cc"],
cflags: "-fPIC",
},
],
});
}, /Expected 'cflags' to be an array/);

assert.throws(() => {
assertBinding({
targets: [
{
target_name: "addon",
sources: ["addon.cc"],
cflags: ["-fPIC", 42],
},
],
});
}, /Expected all cflags to be strings/);
});
});
16 changes: 15 additions & 1 deletion packages/gyp-to-cmake/src/gyp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type GypTarget = {
sources: string[];
include_dirs?: string[];
defines?: string[];
cflags?: string[];
};

export type GypBinding = {
Expand Down Expand Up @@ -49,8 +50,21 @@ export function assertTarget(
"Expected all include_dirs to be strings",
);
}
if ("cflags" in target) {
const { cflags } = target;
assert(Array.isArray(cflags), "Expected 'cflags' to be an array");
assert(
cflags.every((flag) => typeof flag === "string"),
"Expected all cflags to be strings",
);
}
if (disallowUnknownProperties) {
assertNoExtraProperties(target, ["target_name", "sources", "include_dirs"]);
assertNoExtraProperties(target, [
"target_name",
"sources",
"include_dirs",
"cflags",
]);
}
}

Expand Down
46 changes: 46 additions & 0 deletions packages/gyp-to-cmake/src/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,50 @@ describe("bindingGypToCmakeLists", () => {
});
});

describe("cflags", () => {
it("should add cflags as target-specific compile options", () => {
const output = bindingGypToCmakeLists({
projectName: "some-project",
gyp: {
targets: [
{
target_name: "foo",
sources: ["foo.cc"],
cflags: ["-fPIC", "-Wall", "-DNAME=value with space"],
},
],
},
});

assert(
output.includes(
"target_compile_options(foo PRIVATE -fPIC -Wall -DNAME=value\\ with\\ space)",
),
`Expected output to include target_compile_options:\n${output}`,
);
});

it("should expand cflags command output into compile options", () => {
const output = bindingGypToCmakeLists({
projectName: "some-project",
gyp: {
targets: [
{
target_name: "foo",
sources: ["foo.cc"],
cflags: ["<!@echo -fPIC -Wall"],
},
],
},
});

assert(
output.includes("target_compile_options(foo PRIVATE -fPIC -Wall)"),
`Expected expanded cflags in target_compile_options:\n${output}`,
);
});
});

describe("namespaced targets", () => {
const gyp = {
targets: [{ target_name: "addon", sources: ["addon.cc"] }],
Expand Down Expand Up @@ -174,6 +218,7 @@ describe("bindingGypToCmakeLists", () => {
sources: ["addon.cc"],
include_dirs: ["include"],
defines: ["FOO"],
cflags: ["-fPIC"],
},
],
},
Expand All @@ -186,6 +231,7 @@ describe("bindingGypToCmakeLists", () => {
"target_link_libraries(some-project-addon PRIVATE weak-node-api)",
"target_include_directories(some-project-addon PRIVATE include)",
"target_compile_definitions(some-project-addon PRIVATE FOO)",
"target_compile_options(some-project-addon PRIVATE -fPIC)",
"target_compile_features(some-project-addon PRIVATE cxx_std_17)",
]) {
assert(
Expand Down
11 changes: 9 additions & 2 deletions packages/gyp-to-cmake/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,9 @@ export function bindingGypToCmakeLists({
}

for (const target of gyp.targets) {
const { target_name: targetName, defines = [] } = target;
const { target_name: targetName, defines = [], cflags = [] } = target;

// TODO: Handle "conditions"
// TODO: Handle "cflags"
// TODO: Handle "ldflags"

const escapedSources = target.sources
Expand All @@ -116,6 +115,8 @@ export function bindingGypToCmakeLists({
.map(transformPath)
.map(escapeSpaces);

const escapedCflags = cflags.flatMap(mapExpansion).map(escapeSpaces);

const libraries = [];
if (weakNodeApi) {
libraries.push("weak-node-api");
Expand Down Expand Up @@ -216,6 +217,12 @@ export function bindingGypToCmakeLists({
);
}

if (escapedCflags.length > 0) {
lines.push(
`target_compile_options(${actualTargetName} PRIVATE ${escapedCflags.join(" ")})`,
);
}

if (compileFeatures.length > 0) {
lines.push(
`target_compile_features(${actualTargetName} PRIVATE ${compileFeatures.join(" ")})`,
Expand Down