forked from CapSoftware/Cap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
245 lines (207 loc) · 7.8 KB
/
Copy pathsetup.js
File metadata and controls
245 lines (207 loc) · 7.8 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// @ts-check
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { exec as execCb } from "node:child_process";
import { env } from "node:process";
import { promisify } from "node:util";
const exec = promisify(execCb);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const __root = path.resolve(path.join(__dirname, ".."));
const targetDir = path.join(__root, "target");
const arch =
process.env.RUST_TARGET_TRIPLE?.split("-")[0] ??
(process.arch === "arm64" ? "aarch64" : "x86_64");
const BASE_CARGO_TOML = `[env]
FFMPEG_DIR = { relative = true, force = true, value = "target/native-deps" }
`;
async function main() {
await fs.mkdir(targetDir, { recursive: true });
let cargoConfigContents = BASE_CARGO_TOML;
if (process.platform === "darwin") {
const NATIVE_DEPS_VERSION = "v0.25";
const NATIVE_DEPS_URL = `https://github.com/spacedriveapp/native-deps/releases/download/${NATIVE_DEPS_VERSION}`;
const NATIVE_DEPS_ASSETS = {
x86_64: "native-deps-x86_64-darwin-apple.tar.xz",
aarch64: "native-deps-aarch64-darwin-apple.tar.xz",
};
const nativeDepsTar = NATIVE_DEPS_ASSETS[arch];
const nativeDepsTarPath = path.join(targetDir, nativeDepsTar);
let downloadedNativeDeps = false;
if (!(await fileExists(nativeDepsTarPath))) {
console.log(`Downloading ${nativeDepsTar}`);
const nativeDepsBytes = await fetch(`${NATIVE_DEPS_URL}/${nativeDepsTar}`)
.then((r) => r.blob())
.then((b) => b.arrayBuffer());
await fs.writeFile(nativeDepsTarPath, Buffer.from(nativeDepsBytes));
console.log("Downloaded native deps");
downloadedNativeDeps = true;
} else console.log(`Using cached ${nativeDepsTar}`);
const nativeDepsFolder = `native-deps`;
const nativeDepsDir = path.join(targetDir, nativeDepsFolder);
const frameworkDir = path.join(nativeDepsDir, "Spacedrive.framework");
if (downloadedNativeDeps || !(await fileExists(nativeDepsDir))) {
await fs.mkdir(nativeDepsDir, { recursive: true });
await exec(`tar xf ${nativeDepsTarPath} -C ${nativeDepsDir}`);
console.log(`Extracted ${nativeDepsFolder}`);
} else console.log(`Using cached ${nativeDepsFolder}`);
await trimMacOSFramework(frameworkDir);
console.log("Trimmed .framework");
console.log("Signing .framework libraries");
await signMacOSFrameworkLibs(frameworkDir);
console.log("Signed .framework libraries");
const frameworkTargetDir = path.join(
targetDir,
"Frameworks",
"Spacedrive.framework"
);
await fs.rm(frameworkTargetDir, { recursive: true }).catch(() => {});
await fs.cp(
frameworkDir,
path.join(targetDir, "Frameworks", "Spacedrive.framework"),
{ recursive: true }
);
// alternative to specifying dylibs as linker args
await fs.mkdir(path.join(targetDir, "/debug"), { recursive: true });
for (const name of await fs.readdir(path.join(nativeDepsDir, "lib"))) {
await fs.copyFile(
path.join(nativeDepsDir, "lib", name),
path.join(targetDir, "debug", name)
);
}
console.log("Copied ffmpeg dylibs to target/debug");
} else if (process.platform === "win32") {
const FFMPEG_VERSION = "7.1";
const FFMPEG_ZIP_NAME = `ffmpeg-${FFMPEG_VERSION}-full_build-shared`;
const FFMPEG_ZIP_URL = `https://github.com/GyanD/codexffmpeg/releases/download/${FFMPEG_VERSION}/${FFMPEG_ZIP_NAME}.zip`;
await fs.mkdir(targetDir, { recursive: true });
let downloadedFfmpeg = false;
const ffmpegZip = `ffmpeg-${FFMPEG_VERSION}.zip`;
const ffmpegZipPath = path.join(targetDir, ffmpegZip);
if (!(await fileExists(ffmpegZipPath))) {
const ffmpegZipBytes = await fetch(FFMPEG_ZIP_URL)
.then((r) => r.blob())
.then((b) => b.arrayBuffer());
await fs.writeFile(ffmpegZipPath, Buffer.from(ffmpegZipBytes));
console.log(`Downloaded ${ffmpegZip}`);
downloadedFfmpeg = true;
} else console.log(`Using cached ${ffmpegZip}`);
const ffmpegDir = path.join(targetDir, "ffmpeg");
if (!(await fileExists(ffmpegDir)) || downloadedFfmpeg) {
await exec(`tar xf ${ffmpegZipPath} -C ${targetDir}`);
await fs.rm(ffmpegDir, { recursive: true, force: true }).catch(() => {});
await fs.rename(path.join(targetDir, FFMPEG_ZIP_NAME), ffmpegDir);
console.log("Extracted ffmpeg");
} else console.log("Using cached ffmpeg");
// alternative to adding ffmpeg/bin to PATH
await fs.mkdir(path.join(targetDir, "debug"), { recursive: true });
for (const name of await fs.readdir(path.join(ffmpegDir, "bin"))) {
await fs.copyFile(
path.join(ffmpegDir, "bin", name),
path.join(targetDir, "debug", name)
);
}
console.log("Copied ffmpeg dylibs to target/debug");
if (!(await fileExists(path.join(targetDir, "native-deps"))))
await fs.mkdir(path.join(targetDir, "native-deps"), { recursive: true });
await fs.cp(
path.join(ffmpegDir, "lib"),
path.join(targetDir, "native-deps", "lib"),
{
recursive: true,
force: true,
}
);
await fs.cp(
path.join(ffmpegDir, "include"),
path.join(targetDir, "native-deps", "include"),
{
recursive: true,
force: true,
}
);
console.log("Copied ffmpeg/lib and ffmpeg/include to target/native-deps");
const { stdout: vcInstallDir } = await exec(
'$(& "${env:ProgramFiles(x86)}/Microsoft Visual Studio/Installer/vswhere.exe" -latest -property installationPath)',
{ shell: "powershell.exe" }
);
const libclangPath = path.join(
vcInstallDir.trim(),
"VC/Tools/LLVM/x64/bin/libclang.dll"
);
cargoConfigContents += `LIBCLANG_PATH = "${libclangPath.replaceAll(
"\\",
"/"
)}"\n`;
}
await fs.mkdir(path.join(__root, ".cargo"), { recursive: true });
await fs.writeFile(
path.join(__root, ".cargo/config.toml"),
cargoConfigContents
);
}
main();
async function trimMacOSFramework(frameworkDir) {
const headersDir = path.join(frameworkDir, "Headers");
const librariesDir = path.join(frameworkDir, "Libraries");
const libraries = await fs.readdir(librariesDir);
const unnecessaryLibraries = libraries.filter(
(v) =>
!(
v.startsWith("libav") ||
v.startsWith("libsw") ||
v.startsWith("libpostproc")
)
);
for (const lib of unnecessaryLibraries) {
await fs.rm(path.join(librariesDir, lib), { recursive: true });
}
const headers = await fs.readdir(headersDir);
const unnecessaryHeaders = headers.filter(
(v) =>
!(
v.startsWith("libav") ||
v.startsWith("libsw") ||
v.startsWith("libpostproc")
)
);
for (const header of unnecessaryHeaders) {
await fs.rm(path.join(headersDir, header), { recursive: true });
}
const modelsPath = path.join(frameworkDir, "Resources", "Models");
if (await fileExists(modelsPath))
await fs.rm(modelsPath, {
recursive: true,
});
}
async function signMacOSFrameworkLibs(frameworkDir) {
const signId = env.APPLE_SIGNING_IDENTITY || "-";
const keychain = env.APPLE_KEYCHAIN ? `--keychain ${env.APPLE_KEYCHAIN}` : "";
// Sign dylibs (Required for them to work on macOS 13+)
await fs
.readdir(path.join(frameworkDir, "Libraries"), {
recursive: true,
withFileTypes: true,
})
.then((files) =>
Promise.all(
files
.filter((entry) => entry.isFile() && entry.name.endsWith(".dylib"))
.map((entry) =>
exec(
`codesign ${keychain} -s "${signId}" -f "${path.join(
entry.path,
entry.name
)}"`
)
)
)
);
}
async function fileExists(path) {
return await fs
.access(path)
.then(() => true)
.catch(() => false);
}