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/ldflags-target-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"gyp-to-cmake": minor
---

Translate target-level `ldflags` from `binding.gyp` into private CMake link
options. The parser now validates that `ldflags` 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 @@ -56,6 +56,23 @@ describe("gyp.assertRoot", () => {
});
});

it("should accept target ldflags", () => {
assert.doesNotThrow(() => {
assertBinding(
{
targets: [
{
target_name: "addon",
sources: ["addon.cc"],
ldflags: ["-Wl,--as-needed", "-pthread"],
},
],
},
true,
);
});
});

it("should reject malformed target cflags", () => {
assert.throws(() => {
assertBinding({
Expand All @@ -81,4 +98,30 @@ describe("gyp.assertRoot", () => {
});
}, /Expected all cflags to be strings/);
});

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

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

export type GypBinding = {
Expand Down Expand Up @@ -58,12 +59,21 @@ export function assertTarget(
"Expected all cflags to be strings",
);
}
if ("ldflags" in target) {
const { ldflags } = target;
assert(Array.isArray(ldflags), "Expected 'ldflags' to be an array");
assert(
ldflags.every((flag) => typeof flag === "string"),
"Expected all ldflags to be strings",
);
}
if (disallowUnknownProperties) {
assertNoExtraProperties(target, [
"target_name",
"sources",
"include_dirs",
"cflags",
"ldflags",
]);
}
}
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 @@ -170,6 +170,52 @@ describe("bindingGypToCmakeLists", () => {
});
});

describe("ldflags", () => {
it("should add ldflags as target-specific link options", () => {
const output = bindingGypToCmakeLists({
projectName: "some-project",
gyp: {
targets: [
{
target_name: "foo",
sources: ["foo.cc"],
ldflags: ["-Wl,--as-needed", "-Lpath with spaces"],
},
],
},
});

assert(
output.includes(
"target_link_options(foo PRIVATE -Wl,--as-needed -Lpath\\ with\\ spaces)",
),
`Expected output to include target_link_options:\n${output}`,
);
});

it("should expand ldflags command output into link options", () => {
const output = bindingGypToCmakeLists({
projectName: "some-project",
gyp: {
targets: [
{
target_name: "foo",
sources: ["foo.cc"],
ldflags: ["<!@echo -Wl,--as-needed -pthread"],
},
],
},
});

assert(
output.includes(
"target_link_options(foo PRIVATE -Wl,--as-needed -pthread)",
),
`Expected expanded ldflags in target_link_options:\n${output}`,
);
});
});

describe("namespaced targets", () => {
const gyp = {
targets: [{ target_name: "addon", sources: ["addon.cc"] }],
Expand Down
15 changes: 13 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,14 @@ export function bindingGypToCmakeLists({
}

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

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

const escapedSources = target.sources
.flatMap(mapExpansion)
Expand All @@ -116,6 +120,7 @@ export function bindingGypToCmakeLists({
.map(escapeSpaces);

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

const libraries = [];
if (weakNodeApi) {
Expand Down Expand Up @@ -223,6 +228,12 @@ export function bindingGypToCmakeLists({
);
}

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

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