diff --git a/packages/cmake-rn/src/weak-node-api.ts b/packages/cmake-rn/src/weak-node-api.ts index 4cf1982e..7dde4ba6 100644 --- a/packages/cmake-rn/src/weak-node-api.ts +++ b/packages/cmake-rn/src/weak-node-api.ts @@ -11,6 +11,10 @@ import { import { ANDROID_ARCHITECTURES } from "./android.js"; import { getNodeAddonHeadersPath, getNodeApiHeadersPath } from "./headers.js"; +export function toCmakePath(input: string) { + return input.split(path.win32.sep).join(path.posix.sep); +} + export function getWeakNodeApiPath(triplet: SupportedTriplet): string { const { pathname } = new URL( import.meta.resolve("react-native-node-api/weak-node-api") diff --git a/packages/gyp-to-cmake/src/cli.ts b/packages/gyp-to-cmake/src/cli.ts index 61ba11e0..77a4f92d 100644 --- a/packages/gyp-to-cmake/src/cli.ts +++ b/packages/gyp-to-cmake/src/cli.ts @@ -57,15 +57,17 @@ export function transformBindingGypsRecursively( export const program = new Command("gyp-to-cmake") .description("Transform binding.gyp to CMakeLists.txt") + .option("--no-path-transforms", "Don't transform output from command expansions (replacing '\\' with '/')") .argument( "[path]", "Path to the binding.gyp file or directory to traverse recursively", process.cwd() ) - .action((targetPath: string) => { + .action((targetPath: string, { pathTransforms }) => { const options: TransformOptions = { unsupportedBehaviour: "throw", disallowUnknownProperties: false, + transformWinPathsToPosix: pathTransforms, }; const stat = fs.statSync(targetPath); if (stat.isFile()) { diff --git a/packages/gyp-to-cmake/src/transformer.test.ts b/packages/gyp-to-cmake/src/transformer.test.ts index 47975995..9727cdcd 100644 --- a/packages/gyp-to-cmake/src/transformer.test.ts +++ b/packages/gyp-to-cmake/src/transformer.test.ts @@ -33,6 +33,22 @@ describe("bindingGypToCmakeLists", () => { assert(output.includes("add_library(bar SHARED bar.cc")); }); + it("transform \\ to / in source filenames", () => { + const output = bindingGypToCmakeLists({ + projectName: "some-project", + gyp: { + targets: [ + { + target_name: "foo", + sources: ["file\\with\\win32\\separator.cc"], + }, + ], + }, + }); + + assert(output.includes("add_library(foo SHARED file/with/win32/separator.cc")); + }); + it("escapes spaces in source filenames", () => { const output = bindingGypToCmakeLists({ projectName: "some-project", diff --git a/packages/gyp-to-cmake/src/transformer.ts b/packages/gyp-to-cmake/src/transformer.ts index 6a33c160..066511d9 100644 --- a/packages/gyp-to-cmake/src/transformer.ts +++ b/packages/gyp-to-cmake/src/transformer.ts @@ -1,4 +1,5 @@ import cp from "node:child_process"; +import path from "node:path"; import type { GypBinding } from "./gyp.js"; @@ -10,6 +11,7 @@ export type GypToCmakeListsOptions = { napiVersion?: number; executeCmdExpansions?: boolean; unsupportedBehaviour?: "skip" | "warn" | "throw"; + transformWinPathsToPosix?: boolean; }; function isCmdExpansion(value: string) { @@ -31,6 +33,7 @@ export function bindingGypToCmakeLists({ napiVersion = DEFAULT_NAPI_VERSION, executeCmdExpansions = true, unsupportedBehaviour = "skip", + transformWinPathsToPosix = true, }: GypToCmakeListsOptions): string { function mapExpansion(value: string): string[] { if (!isCmdExpansion(value)) { @@ -48,6 +51,14 @@ export function bindingGypToCmakeLists({ return [value]; } + function transformPath(input: string) { + if (transformWinPathsToPosix) { + return input.split(path.win32.sep).join(path.posix.sep); + } else { + return input; + } + } + const lines: string[] = [ "cmake_minimum_required(VERSION 3.15)", //"cmake_policy(SET CMP0091 NEW)", @@ -67,11 +78,13 @@ export function bindingGypToCmakeLists({ const escapedJoinedSources = target.sources .flatMap(mapExpansion) + .map(transformPath) .map(escapePath) .join(" "); const escapedJoinedIncludes = (target.include_dirs || []) .flatMap(mapExpansion) + .map(transformPath) .map(escapePath) .join(" ");