Skip to content

Commit c4aa102

Browse files
committed
prepare for ios and windows
1 parent 33d9a90 commit c4aa102

File tree

5 files changed

+96
-34
lines changed

5 files changed

+96
-34
lines changed

build/build.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
import { bold, cyan, yellow } from "jsr:@std/fmt@1/colors";
22
import { ensureDeps } from "./deps.ts";
33
import {
4+
BuildTool,
5+
buildToolToCommand,
46
exec,
57
getEnvVars,
68
getHostTriple,
79
getSysrootEnv,
8-
needsCross,
10+
needsCrossCompile,
911
stripBinary,
1012
} from "./util.ts";
1113

1214
// Build library
1315
export async function buildLib(target?: string, debug = false) {
1416
const host = getHostTriple();
15-
const useCross = needsCross(host, target);
17+
const buildTool = needsCrossCompile(host, target);
1618

1719
// Ensure dependencies are set up
1820
await ensureDeps(target);
1921

2022
console.log(
2123
cyan(bold("Building")) +
22-
` libdivvun_runtime ${debug ? yellow("DEBUG") : bold("release")} for target: ${
23-
bold(target || host)
24-
}` +
25-
(useCross ? " " + yellow("(cross)") : ""),
24+
` libdivvun_runtime ${
25+
debug ? yellow("DEBUG") : bold("release")
26+
} for target: ${bold(target || host)}` +
27+
(buildTool !== BuildTool.Cargo ? " " + yellow(`(${buildTool})`) : ""),
2628
);
2729

28-
const command = useCross ? "cross" : "cargo";
29-
const args = [command, "build", "--features", "ffi"];
30+
const baseCmd = buildToolToCommand(buildTool);
31+
const args = [...baseCmd, "build", "--features", "ffi"];
3032

3133
if (!debug) {
3234
args.push("--release");
@@ -38,7 +40,7 @@ export async function buildLib(target?: string, debug = false) {
3840

3941
// Add sysroot env vars if cross-compiling
4042
const env = { ...getEnvVars(target) };
41-
if (useCross && target) {
43+
if (buildTool !== BuildTool.Cargo && target) {
4244
Object.assign(env, getSysrootEnv(target));
4345
}
4446

@@ -48,7 +50,7 @@ export async function buildLib(target?: string, debug = false) {
4850
// Build CLI
4951
export async function build(target?: string, debug = false) {
5052
const host = getHostTriple();
51-
const useCross = needsCross(host, target);
53+
const buildTool = needsCrossCompile(host, target);
5254

5355
// Ensure dependencies are set up
5456
await ensureDeps(target);
@@ -58,12 +60,12 @@ export async function build(target?: string, debug = false) {
5860
` CLI (${debug ? yellow("debug") : bold("release")}) for target: ${
5961
bold(target || host)
6062
}` +
61-
(useCross ? " " + yellow("(cross)") : ""),
63+
(buildTool !== BuildTool.Cargo ? " " + yellow(`(${buildTool})`) : ""),
6264
);
6365

64-
const command = useCross ? "cross" : "cargo";
66+
const baseCmd = buildToolToCommand(buildTool);
6567
const args = [
66-
command,
68+
...baseCmd,
6769
"build",
6870
"-p",
6971
"divvun-runtime-cli",
@@ -86,7 +88,7 @@ export async function build(target?: string, debug = false) {
8688

8789
// Add sysroot env vars if cross-compiling
8890
Object.assign(env, getEnvVars(target));
89-
if (useCross && target) {
91+
if (buildTool !== BuildTool.Cargo && target) {
9092
Object.assign(env, getSysrootEnv(target));
9193
}
9294

@@ -99,7 +101,7 @@ export async function build(target?: string, debug = false) {
99101
// Check CLI
100102
export async function check(target?: string, debug = false) {
101103
const host = getHostTriple();
102-
const useCross = needsCross(host, target);
104+
const buildTool = needsCrossCompile(host, target);
103105

104106
// Ensure dependencies are set up
105107
await ensureDeps(target);
@@ -109,12 +111,12 @@ export async function check(target?: string, debug = false) {
109111
` CLI (${debug ? yellow("debug") : bold("release")}) for target: ${
110112
bold(target || host)
111113
}` +
112-
(useCross ? " " + yellow("(cross)") : ""),
114+
(buildTool !== BuildTool.Cargo ? " " + yellow(`(${buildTool})`) : ""),
113115
);
114116

115-
const command = useCross ? "cross" : "cargo";
117+
const baseCmd = buildToolToCommand(buildTool);
116118
const args = [
117-
command,
119+
...baseCmd,
118120
"check",
119121
"-p",
120122
"divvun-runtime-cli",
@@ -137,7 +139,7 @@ export async function check(target?: string, debug = false) {
137139

138140
// Add sysroot env vars if cross-compiling
139141
Object.assign(env, getEnvVars(target));
140-
if (useCross && target) {
142+
if (buildTool !== BuildTool.Cargo && target) {
141143
Object.assign(env, getSysrootEnv(target));
142144
}
143145

build/deps.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ function isWindows(target: string): boolean {
1717
return target.includes("windows");
1818
}
1919

20+
function isIOS(target: string): boolean {
21+
return target.includes("ios");
22+
}
23+
2024
// Progress state for a single package
2125
interface ProgressState {
2226
completed: number;
@@ -57,13 +61,19 @@ function getPlatformDeps(
5761
if (isWindows(target)) {
5862
// Windows only needs icu4c and pytorch
5963
return {
60-
icu4c: DEPS.icu4c,
61-
pytorch: DEPS.pytorch,
64+
...DEPS,
6265
protobuf: null,
6366
libomp: null,
6467
};
6568
}
6669

70+
if (isIOS(target)) {
71+
return {
72+
...DEPS,
73+
libomp: null,
74+
};
75+
}
76+
6777
// Linux/macOS need all dependencies
6878
return { ...DEPS };
6979
}
@@ -151,7 +161,8 @@ async function downloadPackage(
151161
// URL pattern: https://github.com/divvun/static-lib-build/releases/download/{name}%2Fv{version}/{name}_v{version}_{target}.tar.gz
152162
const tag = `${name}%2Fv${version}`; // URL-encoded {name}/v{version}
153163
const filename = `${name}_v${version}_${target}.tar.gz`;
154-
const url = `https://github.com/${GITHUB_REPO}/releases/download/${tag}/${filename}`;
164+
const url =
165+
`https://github.com/${GITHUB_REPO}/releases/download/${tag}/${filename}`;
155166

156167
// Download tarball
157168
const response = await fetch(url);
@@ -310,9 +321,8 @@ export async function setupDeps(target?: string): Promise<void> {
310321
const platformDeps = getPlatformDeps(actualTarget);
311322

312323
console.log(
313-
"\n" + cyan(bold("Setting up dependencies")) + ` for ${
314-
bold(actualTarget)
315-
}\n`,
324+
"\n" + cyan(bold("Setting up dependencies")) +
325+
` for ${bold(actualTarget)}\n`,
316326
);
317327

318328
// Create multi-progress bar for parallel downloads

build/install.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export async function install(target?: string, debug = false) {
66
await build(target, debug);
77

88
console.log(
9-
cyan("Installing") + ` divvun-runtime for target: ${bold(target || "host")}`,
9+
cyan("Installing") +
10+
` divvun-runtime for target: ${bold(target || "host")}`,
1011
);
1112

1213
const buildType = debug ? "debug" : "release";

build/main.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ const parser = or(
107107
);
108108

109109
const VERSION = (() => {
110-
const p = `${import.meta.filename?.split('/').slice(0, -1).join("/")}/../Cargo.toml`
110+
const p = `${
111+
import.meta.filename?.split("/").slice(0, -1).join("/")
112+
}/../Cargo.toml`;
111113
const data = new TextDecoder().decode(Deno.readFileSync(p));
112-
const r= /version = "([^"]+)"/.exec(data);
114+
const r = /version = "([^"]+)"/.exec(data);
113115
return r ? r[1] : "unknown";
114-
})()
116+
})();
115117

116118
const config = run(parser, {
117119
help: "both",

build/util.ts

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
import { cyan, dim } from "jsr:@std/fmt@1/colors";
22

3+
// Build tool to use for compilation
4+
export enum BuildTool {
5+
Cargo = "cargo",
6+
Cross = "cross",
7+
CargoXwin = "cargo-xwin",
8+
CargoNdk = "cargo-ndk",
9+
}
10+
11+
// Convert BuildTool to command array
12+
export function buildToolToCommand(tool: BuildTool): string[] {
13+
switch (tool) {
14+
case BuildTool.Cargo:
15+
return ["cargo"];
16+
case BuildTool.Cross:
17+
return ["cross"];
18+
case BuildTool.CargoXwin:
19+
return ["cargo", "xwin"];
20+
case BuildTool.CargoNdk:
21+
return ["cargo", "ndk"];
22+
}
23+
}
24+
325
// Get the host platform triple
426
export function getHostTriple(): string {
527
const os = Deno.build.os;
@@ -23,7 +45,9 @@ export function getEnvVars(target?: string): Record<string, string> {
2345
return {
2446
LZMA_API_STATIC: "1",
2547
LIBTORCH_BYPASS_VERSION_CHECK: "1",
26-
LIBTORCH: Deno.realPathSync(`${import.meta.dirname}/../.x/sysroot/${actualTarget}`),
48+
LIBTORCH: Deno.realPathSync(
49+
`${import.meta.dirname}/../.x/sysroot/${actualTarget}`,
50+
),
2751
LIBTORCH_STATIC: "1",
2852
};
2953
}
@@ -59,10 +83,33 @@ export async function stripBinary(
5983
await exec(["strip", "-x", "-S", binaryPath]);
6084
}
6185

62-
// Check if cross-compilation tooling is needed
63-
export function needsCross(host: string, target?: string): boolean {
64-
if (!target) return false;
65-
return host !== target;
86+
// Determine which build tool to use for the target
87+
export function needsCrossCompile(host: string, target?: string): BuildTool {
88+
if (!target) {
89+
return BuildTool.Cargo;
90+
}
91+
92+
// Android targets use cargo-ndk
93+
if (target.includes("android")) {
94+
return BuildTool.CargoNdk;
95+
}
96+
97+
// Windows targets from Unix hosts use cargo-xwin
98+
if (!host.includes("windows") && target.includes("windows")) {
99+
return BuildTool.CargoXwin;
100+
}
101+
102+
// Apple-to-apple cross-compilation (x86_64 ↔ aarch64) uses cargo
103+
if (host.includes("apple") && target.includes("apple")) {
104+
return BuildTool.Cargo;
105+
}
106+
107+
// Different architectures use cross
108+
if (host !== target) {
109+
return BuildTool.Cross;
110+
}
111+
112+
return BuildTool.Cargo;
66113
}
67114

68115
// Get sysroot path for target

0 commit comments

Comments
 (0)