Repository navigation
Expand file tree
/
Copy pathbuild.ts
More file actions
136 lines (112 loc) · 3.92 KB
/
Copy pathbuild.ts
File metadata and controls
136 lines (112 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import path from "node:path";
import process from "node:process";
import { createXCframework } from "react-native-node-api";
import { $ } from "./deps.ts";
$.setPrintCommand(true);
const monorepoRoot = path.resolve(import.meta.dirname!, "..");
// CWD is scripts/
Deno.chdir(new URL("./", import.meta.url));
// Process arguments and environment variables
const VALID_PLATFORMS = ["macos", "ios", "ios-sim", "ios-universal"];
const targetPlatform = Deno.args[0] ?? "macos";
if (!VALID_PLATFORMS.includes(targetPlatform)) {
throw new Error(
`Invalid platform: ${targetPlatform}. Valid platforms are: ${VALID_PLATFORMS.join(
", "
)}`
);
}
const platformDir = targetPlatform.startsWith("ios") ? "ios" : "macos";
const buildConfig = Deno.args.includes("debug") ? "Debug" : "Release";
// Ensure we have a build directory
await Deno.mkdir(`../packages/${platformDir}/build`).catch(() => {});
async function ensureTargetDir(targetPlatform: string) {
// Clean any previous target specific build
// await Deno.remove(`../packages/${platformDir}/build/${targetPlatform}`, { recursive: true }).catch(
// () => {},
// );
// Create a new target specific build directory
await Deno.mkdir(`../packages/${platformDir}/build/${targetPlatform}`).catch(
() => {}
);
}
const archs: Record<string, string[]> = {
ios: ["arm64"],
"ios-universal": ["arm64", "x86_64"],
"ios-sim": ["arm64", "x86_64"],
macos: ["x86_64", "arm64"],
};
async function build(targetPlatform: string) {
await ensureTargetDir(targetPlatform);
const maxMDSize = Math.max(
...archs[targetPlatform].map((arch) => {
return Deno.lstatSync(
new URL(
`../metadata/metadata.${platformDir}.${arch}.nsmd`,
import.meta.url
)
).size;
})
);
// Generate the build files
await $`cmake -S=../ -B=../packages/${platformDir}/build/${targetPlatform} -GXcode -DTARGET_PLATFORM=${targetPlatform} -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ${
targetPlatform.startsWith("ios-test") ? "" : `-DMETADATA_SIZE=${maxMDSize}`
}`;
// Build the project
await $`cmake --build ../packages/${platformDir}/build/${targetPlatform} --config ${buildConfig}`;
if (platformDir === "macos") {
await Deno.mkdir(`../packages/${platformDir}/dist/${targetPlatform}`, {
recursive: true,
}).catch(() => {});
// Will be empty-string or a version number depending on the value of
// FRAMEWORK in CMakeLists.txt:
// - TRUE: ".0.1.0"
// - FALSE: ""
const version = ".0.1.0";
await Deno.copyFile(
`../packages/${platformDir}/build/${targetPlatform}/${buildConfig}/libNativeScript${version}.dylib`,
`../packages/${platformDir}/dist/${targetPlatform}/NativeScript.node`
);
}
}
const TARGET_RELEASE_FOLDERS: Record<string, string> = {
ios: `${buildConfig}-iphoneos`,
"ios-sim": `${buildConfig}-iphonesimulator`,
macos: buildConfig,
};
if (import.meta.main) {
await ensureTargetDir(targetPlatform);
const targets = targetPlatform === 'ios-universal' ?
["ios", "ios-sim"] :
[targetPlatform];
for (const target of targets) {
await build(target);
}
const frameworkPaths = targets.map((targetPlatform) =>
path.resolve(
monorepoRoot,
`packages/${platformDir}/build/${targetPlatform}/${TARGET_RELEASE_FOLDERS[targetPlatform]}/NativeScript.framework`
)
);
const xcframeworkOutputPath = path.resolve(
monorepoRoot,
`packages/${platformDir}/build/${buildConfig}/NativeScript.apple.node`
);
try {
await createXCframework({
outputPath: xcframeworkOutputPath,
frameworkPaths,
autoLink: true,
});
} catch (error) {
const errorMessage = error instanceof Error ? error.message : "<unknown>";
console.error(`Failed to assemble XCFramework: ${errorMessage}`);
process.exit(1);
}
console.log(
`\x1b[32m✔\x1b[0m XCFramework assembled into \x1b[2m${path.relative(
monorepoRoot,
xcframeworkOutputPath
)}\x1b[22m`
);
}