Skip to content

Commit 1f5049a

Browse files
authored
fix(build): restore SDK packages and signed macOS app builds (#118833)
* fix(build): unblock SDK declarations and macOS packaging * fix(test): satisfy strict SDK declaration group indexing
1 parent 3c678ac commit 1f5049a

6 files changed

Lines changed: 156 additions & 13 deletions

File tree

‎scripts/package-mac-app.sh‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@ helper_bin_for_arch() {
8585
echo "$(helper_build_path_for_arch "$1")/$BUILD_CONFIG/$MLX_TTS_HELPER_PRODUCT"
8686
}
8787

88+
build_mlx_tts_helper() {
89+
local arch="$1"
90+
local swift_path
91+
local toolchain_metal
92+
local swift_args=(build)
93+
94+
swift_path="$(xcrun --find swift)"
95+
toolchain_metal="$(dirname "$swift_path")/metal"
96+
97+
if [[ -x "$toolchain_metal" ]] &&
98+
! "$toolchain_metal" --version >/dev/null 2>&1 &&
99+
xcrun metal --version >/dev/null 2>&1; then
100+
echo "⚠️ Xcode's default Metal shim cannot use the installed toolchain; using the native SwiftPM backend"
101+
swift_args+=(--build-system native)
102+
fi
103+
104+
swift "${swift_args[@]}" \
105+
--package-path "$MLX_TTS_HELPER_ROOT" \
106+
-c "$BUILD_CONFIG" \
107+
--product "$MLX_TTS_HELPER_PRODUCT" \
108+
--build-path "$(helper_build_path_for_arch "$arch")" \
109+
--arch "$arch"
110+
}
111+
88112
sparkle_framework_for_arch() {
89113
echo "$(build_path_for_arch "$1")/$BUILD_CONFIG/Sparkle.framework"
90114
}
@@ -373,7 +397,7 @@ for arch in "${BUILD_ARCHS[@]}"; do
373397
run_with_locked_swift_packages swift build -c "$BUILD_CONFIG" --product "$PRODUCT" --build-path "$BUILD_PATH" --arch "$arch" -Xlinker -rpath -Xlinker @executable_path/../Frameworks
374398
restore_swiftpm_resource_sources
375399
echo "🔨 Building $MLX_TTS_HELPER_PRODUCT ($BUILD_CONFIG) [$arch]"
376-
swift build --package-path "$MLX_TTS_HELPER_ROOT" -c "$BUILD_CONFIG" --product "$MLX_TTS_HELPER_PRODUCT" --build-path "$(helper_build_path_for_arch "$arch")" --arch "$arch"
400+
build_mlx_tts_helper "$arch"
377401
done
378402

379403
BIN_PRIMARY="$(bin_for_arch "$PRIMARY_ARCH")"

‎scripts/write-cli-startup-metadata.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Write Cli Startup Metadata script supports OpenClaw repository automation.
22
import { spawn, spawnSync } from "node:child_process";
33
import { createHash } from "node:crypto";
4-
import {
4+
import fs, {
55
existsSync,
66
mkdirSync,
77
mkdtempSync,
88
readdirSync,
99
readFileSync,
10-
rmSync,
1110
writeFileSync,
1211
} from "node:fs";
1312
import { availableParallelism, tmpdir } from "node:os";
@@ -329,7 +328,7 @@ function createRootHelpRenderStateDir(): string {
329328
}
330329

331330
function cleanupRootHelpRenderStateDir(stateDir: string): void {
332-
rmSync(stateDir, { force: true, recursive: true });
331+
fs.rmSync(stateDir, { force: true, recursive: true, maxRetries: 6, retryDelay: 25 });
333332
}
334333

335334
function withIsolatedRootHelpRenderContext<T>(

‎test/scripts/package-mac-app.test.ts‎

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ function makePlist(): string {
2929
return plist;
3030
}
3131

32-
function runHelper(script: string) {
33-
return spawnSync("bash", ["-lc", script], {
32+
function runHelper(script: string, shell = "bash") {
33+
return spawnSync(shell, ["-lc", script], {
3434
cwd: process.cwd(),
3535
encoding: "utf8",
3636
});
@@ -69,6 +69,17 @@ function getSparkleBuildHelperBlock(): string {
6969
return script.slice(start, end);
7070
}
7171

72+
function getMLXTTSHelperBuildBlock(): string {
73+
const script = readFileSync(scriptPath, "utf8");
74+
const start = script.indexOf("build_mlx_tts_helper() {");
75+
const end = script.indexOf("sparkle_framework_for_arch()", start);
76+
77+
expect(start).toBeGreaterThanOrEqual(0);
78+
expect(end).toBeGreaterThan(start);
79+
80+
return script.slice(start, end);
81+
}
82+
7283
function getSwiftPackageResolutionBlock(): string {
7384
const script = readFileSync(scriptPath, "utf8");
7485
const start = script.indexOf("run_with_locked_swift_packages()");
@@ -495,6 +506,7 @@ describe("package-mac-app plist stamping", () => {
495506

496507
it("builds and bundles the MLX TTS helper for every requested architecture", () => {
497508
const script = readFileSync(scriptPath, "utf8");
509+
const helperBlock = getMLXTTSHelperBuildBlock();
498510
const buildLoop = script.slice(
499511
script.indexOf('for arch in "${BUILD_ARCHS[@]}"; do'),
500512
script.indexOf('BIN_PRIMARY="$(bin_for_arch "$PRIMARY_ARCH")"'),
@@ -504,16 +516,95 @@ describe("package-mac-app plist stamping", () => {
504516
script.indexOf("SPARKLE_FRAMEWORK_PRIMARY="),
505517
);
506518

507-
expect(buildLoop).toContain('swift build --package-path "$MLX_TTS_HELPER_ROOT"');
508-
expect(buildLoop).toContain('--product "$MLX_TTS_HELPER_PRODUCT"');
509-
expect(buildLoop).toContain('--arch "$arch"');
519+
expect(buildLoop).toContain('build_mlx_tts_helper "$arch"');
520+
expect(helperBlock).toContain('--package-path "$MLX_TTS_HELPER_ROOT"');
521+
expect(helperBlock).toContain('--product "$MLX_TTS_HELPER_PRODUCT"');
522+
expect(helperBlock).toContain('--arch "$arch"');
510523
expect(helperCopy).toContain(
511524
'cp "$(helper_bin_for_arch "$PRIMARY_ARCH")" "$APP_ROOT/Contents/MacOS/$MLX_TTS_HELPER_PRODUCT"',
512525
);
513526
expect(helperCopy).toContain('/usr/bin/lipo -create "${HELPER_BIN_INPUTS[@]}"');
514527
expect(helperCopy).toContain('chmod +x "$APP_ROOT/Contents/MacOS/$MLX_TTS_HELPER_PRODUCT"');
515528
});
516529

530+
it.each([
531+
{ title: "keeps the default backend when Xcode's Metal shim works", shimExit: 0, xcrunExit: 0 },
532+
{
533+
title: "uses the native backend when xcrun can run Metal but Xcode's shim is broken",
534+
shimExit: 1,
535+
xcrunExit: 0,
536+
},
537+
{
538+
title: "keeps the default backend when no working Metal compiler is installed",
539+
shimExit: 1,
540+
xcrunExit: 1,
541+
},
542+
])("$title", ({ shimExit, xcrunExit }) => {
543+
const helperBlock = getMLXTTSHelperBuildBlock();
544+
const tempRoot = tempDirs.make("openclaw-package-mlx-metal-");
545+
const toolsDir = path.join(tempRoot, "tools");
546+
const toolchainDir = path.join(tempRoot, "xcode-toolchain");
547+
const invocationPath = path.join(tempRoot, "swift-args");
548+
549+
mkdirSync(toolsDir, { recursive: true });
550+
mkdirSync(toolchainDir, { recursive: true });
551+
552+
const tools: Array<[string, string]> = [
553+
[
554+
path.join(toolsDir, "xcrun"),
555+
[
556+
"#!/usr/bin/env bash",
557+
'case "$*" in',
558+
' "--find swift") printf "%s\\n" "$MOCK_SWIFT_PATH" ;;',
559+
' "metal --version") exit "$MOCK_XCRUN_EXIT" ;;',
560+
" *) exit 1 ;;",
561+
"esac",
562+
"",
563+
].join("\n"),
564+
],
565+
[
566+
path.join(toolsDir, "swift"),
567+
'#!/usr/bin/env bash\nprintf "%s\\n" "$@" > "$MOCK_SWIFT_ARGS"\n',
568+
],
569+
[path.join(toolchainDir, "swift"), "#!/usr/bin/env bash\nexit 0\n"],
570+
[path.join(toolchainDir, "metal"), `#!/usr/bin/env bash\nexit ${shimExit}\n`],
571+
];
572+
573+
for (const [toolPath, contents] of tools) {
574+
writeFileSync(toolPath, contents, "utf8");
575+
chmodSync(toolPath, 0o755);
576+
}
577+
578+
const result = runHelper(
579+
`
580+
set -euo pipefail
581+
export PATH=${JSON.stringify(`${toolsDir}:/usr/bin:/bin`)}
582+
export MOCK_SWIFT_PATH=${JSON.stringify(path.join(toolchainDir, "swift"))}
583+
export MOCK_XCRUN_EXIT=${JSON.stringify(String(xcrunExit))}
584+
export MOCK_SWIFT_ARGS=${JSON.stringify(invocationPath)}
585+
MLX_TTS_HELPER_ROOT=${JSON.stringify(path.join(tempRoot, "helper"))}
586+
MLX_TTS_HELPER_PRODUCT=openclaw-mlx-tts
587+
BUILD_CONFIG=debug
588+
helper_build_path_for_arch() { printf '%s\\n' ${JSON.stringify(path.join(tempRoot, "build"))}/"$1"; }
589+
${helperBlock}
590+
build_mlx_tts_helper arm64
591+
`,
592+
"/bin/bash",
593+
);
594+
595+
expect(result.status, result.stderr).toBe(0);
596+
const swiftArgs = readFileSync(invocationPath, "utf8").trim().split("\n");
597+
expect(swiftArgs[0]).toBe("build");
598+
expect(swiftArgs).toContain("--package-path");
599+
expect(swiftArgs).toContain("--arch");
600+
expect(swiftArgs).toContain("arm64");
601+
if (shimExit === 0 || xcrunExit !== 0) {
602+
expect(swiftArgs).not.toContain("--build-system");
603+
} else {
604+
expect(swiftArgs.slice(1, 3)).toEqual(["--build-system", "native"]);
605+
}
606+
});
607+
517608
it("falls back to corepack pnpm when the pnpm shim is absent", () => {
518609
const helperBlock = getPackageManagerHelperBlock();
519610
const tempRoot = tempDirs.make("openclaw-package-pnpm-root-");

‎test/scripts/tsdown-config.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import fs from "node:fs";
33
import path from "node:path";
44
import { describe, expect, it } from "vitest";
5+
import { publicPluginSdkEntrypoints } from "../../scripts/lib/plugin-sdk-entries.mjs";
56
import {
67
TSDOWN_PACKAGE_CONFIG_GROUP,
78
TSDOWN_UNIFIED_CONFIG_GROUP,
@@ -68,6 +69,22 @@ describe("tsdown config", () => {
6869
expect(new Set(declarationSources).size).toBe(declarationSources.length);
6970
});
7071

72+
it("keeps public SDK declarations together and isolates private runtime declarations", () => {
73+
const [publicDeclarationSources = [], privateDeclarationSources = []] =
74+
TSDOWN_UNIFIED_DTS_CONFIG_GROUPS.filter((name) =>
75+
name.startsWith("openclaw-dts-plugin-sdk-"),
76+
).map((name) => {
77+
const dts = configs.find((entry) => entry.name === name)?.dts;
78+
return dts && typeof dts === "object" && Array.isArray(dts.entry) ? dts.entry : [];
79+
});
80+
const publicSources = publicPluginSdkEntrypoints.map((entry) => `src/plugin-sdk/${entry}.ts`);
81+
const publicSourceSet = new Set(publicSources);
82+
83+
expect(publicDeclarationSources.toSorted()).toEqual(publicSources.toSorted());
84+
expect(privateDeclarationSources.some((source) => publicSourceSet.has(source))).toBe(false);
85+
expect(privateDeclarationSources).toContain("src/plugin-sdk/tts-runtime.ts");
86+
});
87+
7188
it("keeps node package artifacts on the declared js and dts extensions", () => {
7289
const nodePackageConfigs = configs.filter((entry) => entry.fixedExtension === false);
7390
expect(nodePackageConfigs).not.toHaveLength(0);

‎test/scripts/write-cli-startup-metadata.test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Write Cli Startup Metadata tests cover write cli startup metadata script behavior.
22
import { spawn, spawnSync } from "node:child_process";
33
import { EventEmitter } from "node:events";
4-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
4+
import fs, { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
55
import path from "node:path";
66
import { PassThrough } from "node:stream";
77
import { pathToFileURL } from "node:url";
@@ -561,6 +561,7 @@ describe("write-cli-startup-metadata", () => {
561561
{ title: "after successful rendering", failRender: false },
562562
{ title: "when rendering fails", failRender: true },
563563
])("removes isolated root-help state $title", async ({ failRender }) => {
564+
const removeState = vi.spyOn(fs, "rmSync");
564565
const tempRoot = createTempDir("openclaw-startup-metadata-cleanup-");
565566
const distDir = path.join(tempRoot, "dist");
566567
const extensionsDir = path.join(tempRoot, "extensions");
@@ -615,6 +616,13 @@ describe("write-cli-startup-metadata", () => {
615616
expect(stateDir).not.toBe("");
616617
expect(statePresentDuringSiblingRender).toBe(true);
617618
expect(existsSync(stateDir)).toBe(false);
619+
expect(removeState).toHaveBeenCalledWith(stateDir, {
620+
force: true,
621+
recursive: true,
622+
maxRetries: 6,
623+
retryDelay: 25,
624+
});
625+
removeState.mockRestore();
618626
});
619627

620628
it("regenerates nodes help when bundled canvas CLI help sources change", async () => {

‎tsdown.config.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
buildPluginSdkEntrySources,
1111
pluginSdkEntrypoints,
1212
productionPluginSdkEntrypoints,
13+
publicPluginSdkEntrypoints,
1314
} from "./scripts/lib/plugin-sdk-entries.mjs";
1415
import {
1516
createStateSchemaInlinePlugin,
@@ -581,10 +582,13 @@ function buildUnifiedDeclarationPartitions(
581582
extensionEntriesById.set(extensionId, extensionEntries);
582583
}
583584

584-
const pluginSdkPartitions = partitionUnifiedEntryGroups(
585-
pluginSdkEntries.map((entry) => [entry]),
586-
2,
585+
const publicPluginSdkEntryNames = new Set(
586+
publicPluginSdkEntrypoints.map((entry) => `plugin-sdk/${entry}`),
587587
);
588+
const pluginSdkPartitions = [
589+
pluginSdkEntries.filter(([name]) => publicPluginSdkEntryNames.has(name)),
590+
pluginSdkEntries.filter(([name]) => !publicPluginSdkEntryNames.has(name)),
591+
];
588592
const extensionPartitions = partitionUnifiedEntryGroups(
589593
[...extensionEntriesById.entries()]
590594
.toSorted(([left], [right]) => left.localeCompare(right))

0 commit comments

Comments
 (0)