Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions packages/cmake-rn/src/weak-node-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion packages/gyp-to-cmake/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
16 changes: 16 additions & 0 deletions packages/gyp-to-cmake/src/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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\\seperator.cc"],
},
],
},
});

assert(output.includes("add_library(foo SHARED file/with/win32/seperator.cc"));
});

it("escapes spaces in source filenames", () => {
const output = bindingGypToCmakeLists({
projectName: "some-project",
Expand Down
13 changes: 13 additions & 0 deletions packages/gyp-to-cmake/src/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cp from "node:child_process";
import path from "node:path";

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

Expand All @@ -10,6 +11,7 @@ export type GypToCmakeListsOptions = {
napiVersion?: number;
executeCmdExpansions?: boolean;
unsupportedBehaviour?: "skip" | "warn" | "throw";
transformWinPathsToPosix?: boolean;
};

function isCmdExpansion(value: string) {
Expand All @@ -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)) {
Expand All @@ -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)",
Expand All @@ -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(" ");

Expand Down
Loading