Skip to content
Merged
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
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 @@ -81,4 +81,47 @@ describe("gyp.assertRoot", () => {
});
}, /Expected all cflags to be strings/);
});

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

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

assert.throws(() => {
assertBinding({
targets: [
{
target_name: "addon",
sources: ["addon.cc"],
ldflags: ["-Wl,-dead_strip", 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,-dead_strip", "-framework CoreFoundation"],
},
],
},
});

assert(
output.includes(
"target_link_options(foo PRIVATE -Wl,-dead_strip -framework\\ CoreFoundation)",
),
`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,-dead_strip -pthread"],
},
],
},
});

assert(
output.includes(
"target_link_options(foo PRIVATE -Wl,-dead_strip -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