From da44ade80303dc60240289ae8d00caa3af4240b9 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Fri, 23 Oct 2026 17:29:05 -0700 Subject: [PATCH] Build-system cleanup --- App_Resources/Android/app.gradle | 40 ++++++++++++++------------ scripts/kotlin-build.cjs | 23 ++++++++------- tests/evenhub-extensions-shim.test.cjs | 2 +- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/App_Resources/Android/app.gradle b/App_Resources/Android/app.gradle index 28bf7c45..997e1f4d 100644 --- a/App_Resources/Android/app.gradle +++ b/App_Resources/Android/app.gradle @@ -46,6 +46,17 @@ def faceclawNdkHostTag = { return "linux-x86_64" } +// Host tools carry .exe on Windows. Call the binaries themselves: the NDK's +// target-prefixed clang wrappers are bash scripts (or .cmd files) that only add +// --target. +def faceclawHostExecutable = { File dir, String name -> + return file("$dir/$name${Os.isFamily(Os.FAMILY_WINDOWS) ? ".exe" : ""}") +} + +def faceclawNdkTool = { File ndkRoot, String name -> + return faceclawHostExecutable(file("$ndkRoot/toolchains/llvm/prebuilt/${faceclawNdkHostTag()}/bin"), name) +} + def faceclawFindNdkRoot = { def explicitNdk = System.getenv("ANDROID_NDK_HOME") if (explicitNdk != null && file(explicitNdk).exists()) { @@ -111,12 +122,7 @@ tasks.register("prepareFaceclawNativeLibs") { if (ndkRoot == null || !ndkRoot.exists()) { throw new GradleException("Android NDK is required to build libfaceclaw_lc3.so") } - // The NDK ships the clang driver twice: extensionless (a Unix shell script) - // and as .cmd (the CreateProcess-launchable one Windows needs). - def clangBin = "$ndkRoot/toolchains/llvm/prebuilt/${faceclawNdkHostTag()}/bin/aarch64-linux-android24-clang" - def clang = Os.isFamily(Os.FAMILY_WINDOWS) - ? file("${clangBin}.cmd") - : file(clangBin) + def clang = faceclawNdkTool(ndkRoot, "clang") if (!clang.exists()) { throw new GradleException("Could not find Android clang at $clang") } @@ -130,6 +136,7 @@ tasks.register("prepareFaceclawNativeLibs") { exec { commandLine([ clang.absolutePath, + "--target=aarch64-linux-android24", "-shared", "-fPIC", "-O3", @@ -197,8 +204,8 @@ tasks.register("prepareFaceclawLlama") { if (cmakeDir == null) { throw new GradleException("Android SDK cmake package is required to build libfaceclaw_llama.so") } - def cmake = file("$cmakeDir/bin/cmake") - def ninja = file("$cmakeDir/bin/ninja") + def cmake = faceclawHostExecutable(file("$cmakeDir/bin"), "cmake") + def ninja = faceclawHostExecutable(file("$cmakeDir/bin"), "ninja") def march = "-march=armv8.2-a+dotprod+fp16" // Configure once; afterwards the ninja no-op build keeps this cheap while @@ -232,18 +239,13 @@ tasks.register("prepareFaceclawLlama") { } // The static-linked lib carries llama.cpp's full debug info (~50MB); // strip it so the APK only pays for the code (~8MB). - def ndkRootForStrip = faceclawFindNdkRoot() - if (ndkRootForStrip != null) { - // Windows wants llvm-strip.exe; the extensionless file is a Unix script. - def llvmStripBin = "$ndkRootForStrip/toolchains/llvm/prebuilt/${faceclawNdkHostTag()}/bin/llvm-strip" - def llvmStrip = Os.isFamily(Os.FAMILY_WINDOWS) && file("${llvmStripBin}.exe").exists() - ? file("${llvmStripBin}.exe") - : file(llvmStripBin) - if (llvmStrip.exists()) { - exec { - commandLine([llvmStrip.absolutePath, "--strip-unneeded", llamaSo.absolutePath]) - } + def llvmStrip = faceclawNdkTool(ndkRoot, "llvm-strip") + if (llvmStrip.exists()) { + exec { + commandLine([llvmStrip.absolutePath, "--strip-unneeded", llamaSo.absolutePath]) } + } else { + logger.warn("Could not find llvm-strip at $llvmStrip; packaging an unstripped libfaceclaw_llama.so (~50MB)") } } } diff --git a/scripts/kotlin-build.cjs b/scripts/kotlin-build.cjs index 5e216181..cff94868 100644 --- a/scripts/kotlin-build.cjs +++ b/scripts/kotlin-build.cjs @@ -30,19 +30,20 @@ async function buildKotlin(platform, configuration = 'debug') { if (!['debug', 'release'].includes(configuration)) throw new Error(`Unsupported Kotlin configuration: ${configuration}`); const variant = configuration === 'release' ? 'Release' : 'Debug'; const task = platform === 'android' ? ':shared:assembleAndroidMain' : `:shared:assembleFaceclawKit${variant}XCFramework`; - const wrapper = path.join(root, 'wear', process.platform === 'win32' ? 'gradlew.bat' : 'gradlew'); + const windows = process.platform === 'win32'; + const wrapper = path.join(root, 'wear', windows ? 'gradlew.bat' : 'gradlew'); + // Through cmd.exe a missing wrapper is only "not recognized" and exit 1. + if (!existsSync(wrapper)) throw new Error(`Gradle wrapper not found: ${wrapper}`); + const args = [task, '--console=plain']; console.log(`[kotlin] Building ${platform} (${configuration})`); await new Promise((resolve, reject) => { - // A .bat cannot be exec'd directly: Node passes it to CreateProcess, which - // rejects it with EINVAL. cmd.exe has to run it, so the Windows path goes - // through a shell, quoted because the checkout may sit under a path with - // spaces in it. - const windows = process.platform === 'win32'; - const child = spawn(windows ? `"${wrapper}"` : wrapper, [task, '--console=plain'], { - cwd: kotlin, - stdio: 'inherit', - shell: windows, - }); + // Node refuses to spawn a .bat directly (EINVAL, since the CVE-2024-27980 + // fix), so cmd.exe runs it. It gets one command string because args + // alongside shell: true are deprecated (DEP0190); the wrapper is quoted for + // checkouts under paths with spaces. + const child = windows + ? spawn(`"${wrapper}" ${args.join(' ')}`, { cwd: kotlin, stdio: 'inherit', shell: true }) + : spawn(wrapper, args, { cwd: kotlin, stdio: 'inherit' }); child.on('error', reject); child.on('exit', (code, signal) => { if (code === 0) resolve(); diff --git a/tests/evenhub-extensions-shim.test.cjs b/tests/evenhub-extensions-shim.test.cjs index ef70aff3..bb43d0bb 100644 --- a/tests/evenhub-extensions-shim.test.cjs +++ b/tests/evenhub-extensions-shim.test.cjs @@ -7,7 +7,7 @@ const vm = require('node:vm'); // The injected shim (app/apps/evenhub/session.ts) and the published types // (evenhub-extensions/src/index.ts) describe the same API from two sides. const source = fs.readFileSync(path.join(__dirname, '../app/apps/evenhub/session.ts'), 'utf8'); -const template = source.match(/export function buildFaceclawExtensionsScript[^`]*`([\s\S]*?)`;\r?\n}/)[1]; +const template = source.match(/export function buildFaceclawExtensionsScript[^`]*`([\s\S]*?)`;\n}/)[1]; const shim = template.replace('${JSON.stringify(versionString)}', JSON.stringify('Faceclaw/9.9.9')); const types = fs.readFileSync(path.join(__dirname, '../evenhub-extensions/src/index.ts'), 'utf8');