Skip to content

Commit 9eb2b2f

Browse files
kraenhansenCopilot
andauthored
Transform paths from win32 seps to posix as expected by CMake (#132)
* Transform paths from win32 seps to posix as expected by CMake * Update packages/gyp-to-cmake/src/transformer.test.ts Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent ece778a commit 9eb2b2f

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

packages/cmake-rn/src/weak-node-api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import {
1111
import { ANDROID_ARCHITECTURES } from "./android.js";
1212
import { getNodeAddonHeadersPath, getNodeApiHeadersPath } from "./headers.js";
1313

14+
export function toCmakePath(input: string) {
15+
return input.split(path.win32.sep).join(path.posix.sep);
16+
}
17+
1418
export function getWeakNodeApiPath(triplet: SupportedTriplet): string {
1519
const { pathname } = new URL(
1620
import.meta.resolve("react-native-node-api/weak-node-api")

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,17 @@ export function transformBindingGypsRecursively(
5757

5858
export const program = new Command("gyp-to-cmake")
5959
.description("Transform binding.gyp to CMakeLists.txt")
60+
.option("--no-path-transforms", "Don't transform output from command expansions (replacing '\\' with '/')")
6061
.argument(
6162
"[path]",
6263
"Path to the binding.gyp file or directory to traverse recursively",
6364
process.cwd()
6465
)
65-
.action((targetPath: string) => {
66+
.action((targetPath: string, { pathTransforms }) => {
6667
const options: TransformOptions = {
6768
unsupportedBehaviour: "throw",
6869
disallowUnknownProperties: false,
70+
transformWinPathsToPosix: pathTransforms,
6971
};
7072
const stat = fs.statSync(targetPath);
7173
if (stat.isFile()) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ describe("bindingGypToCmakeLists", () => {
3333
assert(output.includes("add_library(bar SHARED bar.cc"));
3434
});
3535

36+
it("transform \\ to / in source filenames", () => {
37+
const output = bindingGypToCmakeLists({
38+
projectName: "some-project",
39+
gyp: {
40+
targets: [
41+
{
42+
target_name: "foo",
43+
sources: ["file\\with\\win32\\separator.cc"],
44+
},
45+
],
46+
},
47+
});
48+
49+
assert(output.includes("add_library(foo SHARED file/with/win32/separator.cc"));
50+
});
51+
3652
it("escapes spaces in source filenames", () => {
3753
const output = bindingGypToCmakeLists({
3854
projectName: "some-project",

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cp from "node:child_process";
2+
import path from "node:path";
23

34
import type { GypBinding } from "./gyp.js";
45

@@ -10,6 +11,7 @@ export type GypToCmakeListsOptions = {
1011
napiVersion?: number;
1112
executeCmdExpansions?: boolean;
1213
unsupportedBehaviour?: "skip" | "warn" | "throw";
14+
transformWinPathsToPosix?: boolean;
1315
};
1416

1517
function isCmdExpansion(value: string) {
@@ -31,6 +33,7 @@ export function bindingGypToCmakeLists({
3133
napiVersion = DEFAULT_NAPI_VERSION,
3234
executeCmdExpansions = true,
3335
unsupportedBehaviour = "skip",
36+
transformWinPathsToPosix = true,
3437
}: GypToCmakeListsOptions): string {
3538
function mapExpansion(value: string): string[] {
3639
if (!isCmdExpansion(value)) {
@@ -48,6 +51,14 @@ export function bindingGypToCmakeLists({
4851
return [value];
4952
}
5053

54+
function transformPath(input: string) {
55+
if (transformWinPathsToPosix) {
56+
return input.split(path.win32.sep).join(path.posix.sep);
57+
} else {
58+
return input;
59+
}
60+
}
61+
5162
const lines: string[] = [
5263
"cmake_minimum_required(VERSION 3.15)",
5364
//"cmake_policy(SET CMP0091 NEW)",
@@ -67,11 +78,13 @@ export function bindingGypToCmakeLists({
6778

6879
const escapedJoinedSources = target.sources
6980
.flatMap(mapExpansion)
81+
.map(transformPath)
7082
.map(escapePath)
7183
.join(" ");
7284

7385
const escapedJoinedIncludes = (target.include_dirs || [])
7486
.flatMap(mapExpansion)
87+
.map(transformPath)
7588
.map(escapePath)
7689
.join(" ");
7790

0 commit comments

Comments
 (0)