Skip to content

Commit f228685

Browse files
feat: building aarch64 for darwin (#123)
1 parent b95667a commit f228685

File tree

1 file changed

+75
-48
lines changed

1 file changed

+75
-48
lines changed

script/build.ts

Lines changed: 75 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
import { ensureDir } from "https://deno.land/std@0.145.0/fs/ensure_dir.ts";
22

33
const decoder = new TextDecoder();
4-
5-
const archMap: { [k in typeof Deno.build.arch]: string } = {
6-
"x86_64": "x86_64",
7-
"aarch64": "arm64",
8-
} as const;
9-
10-
function indent(source: string, spaces = 2): string {
11-
return source.split("\n").map((line) => `${" ".repeat(spaces)}${line}\n`)
12-
.join("");
13-
}
4+
const architectures = [["x86_64", "x86_64"], ["aarch64", "arm64"]] as const;
145

156
const ExitType = {
167
Exit: "exit",
@@ -19,9 +10,22 @@ const ExitType = {
1910
} as const;
2011
type ExitType = typeof ExitType[keyof typeof ExitType];
2112

13+
const LogType = {
14+
Success: "success",
15+
Always: "always",
16+
Fail: "fail",
17+
Never: "never",
18+
} as const;
19+
type LogType = typeof LogType[keyof typeof LogType];
20+
21+
function indent(source: string, spaces = 2): string {
22+
return source.split("\n").map((line) => `${" ".repeat(spaces)}${line}\n`)
23+
.join("");
24+
}
25+
2226
async function spawn<T extends Deno.SpawnOptions>(
2327
cmd: string,
24-
{ opts, exit }: { opts?: T; exit?: ExitType } = { exit: ExitType.Never },
28+
{ opts, exit, log }: { opts?: T; exit?: ExitType; log?: LogType } = {},
2529
): Promise<{
2630
status: Deno.ChildStatus;
2731
stdout: string;
@@ -32,31 +36,55 @@ async function spawn<T extends Deno.SpawnOptions>(
3236
opts.stderr = "piped";
3337
}
3438

35-
const { status, stdout, stderr } = await Deno.spawn(cmd, opts);
39+
exit ??= ExitType.Never;
40+
log ??= LogType.Always;
41+
42+
const result = await Deno.spawn(cmd, opts);
43+
44+
const stdout = decoder.decode(result.stdout!);
45+
const stderr = decoder.decode(result.stderr!);
46+
47+
if (result.status.success) {
48+
if (log !== "never") {
49+
console.log(`Successfully ran "${cmd} ${(opts?.args ?? []).join(" ")}"`);
50+
}
3651

37-
if (status.success) {
38-
console.log(`Successfully ran "${cmd} ${(opts?.args ?? []).join(" ")}"`);
39-
console.log(`stdout:\n${indent(decoder.decode(stdout!))}`);
40-
console.log(`stderr:\n${indent(decoder.decode(stderr!))}`);
52+
if (log === "success" || log === "always") {
53+
if (stdout.length !== 0) {
54+
console.log(`stdout:\n${indent(stdout)}`);
55+
}
56+
if (stderr.length !== 0) {
57+
console.log(`stderr:\n${indent(stderr)}`);
58+
}
59+
}
4160
} else {
42-
console.log(`Failed run "${cmd}"`);
43-
console.log(`stdout:\n${indent(decoder.decode(stdout!))}`);
44-
console.log(`stderr:\n${indent(decoder.decode(stderr!))}`);
45-
console.log(`status: ${status.code}`);
61+
if (log !== "never") {
62+
console.log(`Failed run "${cmd}"`);
63+
}
64+
65+
if (log === "fail" || log === "always") {
66+
if (stdout.length !== 0) {
67+
console.log(`stdout:\n${indent(stdout)}`);
68+
}
69+
if (stderr.length !== 0) {
70+
console.log(`stderr:\n${indent(stderr)}`);
71+
}
72+
console.log(`status: ${result.status.code}`);
73+
}
4674

4775
if (exit === ExitType.Fail) {
48-
Deno.exit(status.code);
76+
Deno.exit(result.status.code);
4977
}
5078
}
5179

5280
if (exit === ExitType.Exit) {
53-
Deno.exit(status.code);
81+
Deno.exit(result.status.code);
5482
}
5583

5684
return {
57-
status,
58-
stdout: decoder.decode(stdout!),
59-
stderr: decoder.decode(stderr!),
85+
status: result.status,
86+
stdout,
87+
stderr,
6088
};
6189
}
6290

@@ -71,30 +99,29 @@ switch (Deno.build.os) {
7199
}
72100

73101
case "darwin": {
74-
await spawn("c++", {
75-
opts: {
76-
exit: ExitType.Fail,
77-
args: [
78-
"webview/webview.cc",
79-
"-dynamiclib",
80-
"-fpic",
81-
"-DWEBVIEW_COCOA",
82-
"-std=c++11",
83-
"-Wall",
84-
"-Wextra",
85-
"-pedantic",
86-
"-framework",
87-
"WebKit",
88-
"-arch",
89-
archMap[Deno.build.arch],
90-
"-o",
91-
`build/libwebview.${Deno.build.arch}.dylib`,
92-
],
93-
env: {
94-
"CFLAGS": "",
102+
for (const [denoArch, gccArch] of architectures) {
103+
await spawn("c++", {
104+
opts: {
105+
exit: ExitType.Fail,
106+
args: [
107+
"webview/webview.cc",
108+
"-dynamiclib",
109+
"-fpic",
110+
"-DWEBVIEW_COCOA",
111+
"-std=c++11",
112+
"-Wall",
113+
"-Wextra",
114+
"-pedantic",
115+
"-framework",
116+
"WebKit",
117+
"-arch",
118+
gccArch,
119+
"-o",
120+
`build/libwebview.${denoArch}.dylib`,
121+
],
95122
},
96-
},
97-
});
123+
});
124+
}
98125
Deno.exit(0);
99126
break;
100127
}

0 commit comments

Comments
 (0)