修复构建脚本和WASM测试问题
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
|
||||
|
||||
# build.ps1 - 智能 WASM 构建脚本
|
||||
param(
|
||||
[switch]$Clean,
|
||||
@@ -13,16 +13,50 @@ Write-Host "=========================================" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
# 1. 设置环境变量
|
||||
$EmsdkPath = "E:\emsdk"
|
||||
if (-not (Test-Path $EmsdkPath)) {
|
||||
$emsdkCandidates = @()
|
||||
|
||||
if ($env:EMSDK) {
|
||||
$emsdkCandidates += $env:EMSDK
|
||||
}
|
||||
|
||||
$emsdkCandidates += "E:\emsdk"
|
||||
$emsdkCandidates += (Join-Path (Split-Path $PSScriptRoot -Parent) "..\emsdk")
|
||||
$emsdkCandidates += (Join-Path (Split-Path $PSScriptRoot -Parent) "emsdk")
|
||||
|
||||
$emccCommand = Get-Command emcc -ErrorAction SilentlyContinue
|
||||
if ($emccCommand -and $emccCommand.Source) {
|
||||
$emccSourceDir = Split-Path $emccCommand.Source -Parent
|
||||
if (Test-Path (Join-Path $emccSourceDir "emcc.ps1")) {
|
||||
$emsdkCandidates += (Split-Path (Split-Path $emccSourceDir -Parent) -Parent)
|
||||
}
|
||||
}
|
||||
|
||||
$EmsdkPath = $null
|
||||
foreach ($candidate in $emsdkCandidates | Select-Object -Unique) {
|
||||
if (-not [string]::IsNullOrWhiteSpace($candidate)) {
|
||||
try {
|
||||
if (Test-Path $candidate) {
|
||||
$resolvedCandidate = [System.IO.Path]::GetFullPath($candidate)
|
||||
if (Test-Path (Join-Path $resolvedCandidate "upstream\emscripten\emcc.ps1")) {
|
||||
$EmsdkPath = $resolvedCandidate
|
||||
break
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (-not $EmsdkPath) {
|
||||
Write-Host "[错误] 未找到 Emscripten" -ForegroundColor Red
|
||||
Write-Host "请确保 Emscripten 安装在: $EmsdkPath" -ForegroundColor Yellow
|
||||
Write-Host "请设置 EMSDK 环境变量,或确认 emsdk 已安装。" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 设置 PATH
|
||||
$env:PATH = "$EmsdkPath\upstream\emscripten;$env:PATH"
|
||||
Write-Host "[✓] 设置 Emscripten 环境" -ForegroundColor Green
|
||||
Write-Host "[✓] 设置 Emscripten 环境: $EmsdkPath" -ForegroundColor Green
|
||||
|
||||
# 2. 清理旧构建
|
||||
if ($Clean) {
|
||||
@@ -84,8 +118,8 @@ $compileResult = emcc `
|
||||
-lorocos-kdl `
|
||||
-std=c++17 `
|
||||
-s WASM=1 `
|
||||
-s EXPORTED_FUNCTIONS='["_init_func","_func","_smart_process_json","_smart_get_function_list","_smart_get_function_info","_smart_test_match","_smart_get_version","_smart_free_string","_wasm_malloc","_wasm_free","_malloc","_free","_add","_subtract","_multiply","_divide","_fibonacci","_create_buffer","_destroy_buffer","_compute_sum","_get_greeting","_main"]' `
|
||||
-s EXPORTED_RUNTIME_METHODS='["UTF8ToString","stringToUTF8","lengthBytesUTF8","ccall","cwrap","HEAPU8","HEAP8","HEAPF32","HEAPF64","allocateUTF8"]' `
|
||||
-s EXPORTED_FUNCTIONS='["_init_func","_func","_smart_process_json","_smart_get_function_list","_smart_get_function_info","_smart_test_match","_smart_get_version","_smart_free_string","_malloc","_free","_add","_subtract","_multiply","_divide","_fibonacci","_create_buffer","_destroy_buffer","_compute_sum","_get_greeting","_main"]' `
|
||||
-s EXPORTED_RUNTIME_METHODS='["UTF8ToString","stringToUTF8","lengthBytesUTF8","ccall","cwrap","HEAPU8","HEAP8","HEAPF32","HEAPF64"]' `
|
||||
-s MODULARIZE=1 `
|
||||
-s EXPORT_NAME='createSmartMathModule' `
|
||||
-s ALLOW_MEMORY_GROWTH=1 `
|
||||
@@ -125,28 +159,28 @@ $wasmSize = $wasmFile.Length / 1KB
|
||||
|
||||
# 6. 显示构建结果
|
||||
Write-Host ""
|
||||
Write-Host "🎉 构建成功!" -ForegroundColor Green
|
||||
Write-Host ("═" * 50) -ForegroundColor DarkGray
|
||||
Write-Host "📦 生成文件:" -ForegroundColor White
|
||||
Write-Host "├─ smart_math.js ($($jsSize.ToString('F1')) KB)" -ForegroundColor Gray
|
||||
Write-Host "└─ smart_math.wasm ($($wasmSize.ToString('F1')) KB)" -ForegroundColor Gray
|
||||
Write-Host "构建成功" -ForegroundColor Green
|
||||
Write-Host ("=" * 50) -ForegroundColor DarkGray
|
||||
Write-Host "生成文件:" -ForegroundColor White
|
||||
Write-Host " - smart_math.js ($($jsSize.ToString('F1')) KB)" -ForegroundColor Gray
|
||||
Write-Host " - smart_math.wasm ($($wasmSize.ToString('F1')) KB)" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
Write-Host "✨ 功能特性:" -ForegroundColor White
|
||||
Write-Host "├─ ✅ 机器人正逆解业务接口 (func/init_func)" -ForegroundColor Gray
|
||||
Write-Host "├─ ✅ SPC / 四足机器人 / 连杆机构业务接口" -ForegroundColor Gray
|
||||
Write-Host "├─ ✅ 智能 JSON 数学测试接口" -ForegroundColor Gray
|
||||
Write-Host "├─ ✅ Node / Web 双环境加载" -ForegroundColor Gray
|
||||
Write-Host "├─ ✅ 与 Emscripten 内存管理兼容" -ForegroundColor Gray
|
||||
Write-Host "└─ ✅ 批量回归测试可直接调用" -ForegroundColor Gray
|
||||
Write-Host "功能特性:" -ForegroundColor White
|
||||
Write-Host " - 机器人正逆解业务接口 (func/init_func)" -ForegroundColor Gray
|
||||
Write-Host " - SPC / 四足机器人 / 连杆机构业务接口" -ForegroundColor Gray
|
||||
Write-Host " - 智能 JSON 数学测试接口" -ForegroundColor Gray
|
||||
Write-Host " - Node / Web 双环境加载" -ForegroundColor Gray
|
||||
Write-Host " - 与 Emscripten 内存管理兼容" -ForegroundColor Gray
|
||||
Write-Host " - 批量回归测试可直接调用" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
Write-Host "📁 输出目录: $((Get-Location).Path)\public\wasm" -ForegroundColor Gray
|
||||
Write-Host "⏱️ 编译耗时: $($duration.ToString('F2')) 秒" -ForegroundColor Gray
|
||||
Write-Host "输出目录: $((Get-Location).Path)\public\wasm" -ForegroundColor Gray
|
||||
Write-Host "编译耗时: $($duration.ToString('F2')) 秒" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
|
||||
# 7. 生成 JavaScript 包装器(可选)
|
||||
Write-Host "[ ] 生成 JavaScript 包装器..." -ForegroundColor Cyan
|
||||
|
||||
$jsWrapper = @"
|
||||
$jsWrapper = @'
|
||||
// Smart WASM JSON API JavaScript 包装器
|
||||
class SmartWasmAPI {
|
||||
constructor() {
|
||||
@@ -154,6 +188,13 @@ class SmartWasmAPI {
|
||||
this.initialized = false;
|
||||
}
|
||||
|
||||
allocString(value) {
|
||||
const size = this.module.lengthBytesUTF8(value) + 1;
|
||||
const ptr = this.module._malloc(size);
|
||||
this.module.stringToUTF8(value, ptr, size);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
async initialize() {
|
||||
if (this.initialized) return;
|
||||
|
||||
@@ -192,7 +233,7 @@ class SmartWasmAPI {
|
||||
let responsePtr = null;
|
||||
|
||||
try {
|
||||
requestPtr = this.module.allocateUTF8(JSON.stringify(jsonRequest));
|
||||
requestPtr = this.allocString(JSON.stringify(jsonRequest));
|
||||
responsePtr = this.module._func(requestPtr);
|
||||
const responseStr = this.module.UTF8ToString(responsePtr);
|
||||
return JSON.parse(responseStr);
|
||||
@@ -220,7 +261,7 @@ class SmartWasmAPI {
|
||||
|
||||
try {
|
||||
// 将 JSON 字符串转换为 WASM 内存中的字符串
|
||||
requestPtr = this.module.allocateUTF8(JSON.stringify(jsonRequest));
|
||||
requestPtr = this.allocString(JSON.stringify(jsonRequest));
|
||||
|
||||
// 调用 WASM 函数
|
||||
responsePtr = this.module._smart_process_json(requestPtr);
|
||||
@@ -286,7 +327,7 @@ class SmartWasmAPI {
|
||||
let responsePtr = null;
|
||||
|
||||
try {
|
||||
requestPtr = this.module.allocateUTF8(JSON.stringify(jsonRequest));
|
||||
requestPtr = this.allocString(JSON.stringify(jsonRequest));
|
||||
responsePtr = this.module._smart_test_match(requestPtr);
|
||||
const responseStr = this.module.UTF8ToString(responsePtr);
|
||||
return JSON.parse(responseStr);
|
||||
@@ -354,7 +395,7 @@ if (typeof window !== 'undefined') {
|
||||
}
|
||||
|
||||
export default SmartWasmAPI;
|
||||
"@
|
||||
'@
|
||||
|
||||
$jsWrapper | Out-File -FilePath "public/wasm/smart_api_wrapper.js" -Encoding UTF8
|
||||
Write-Host "[✓] 生成 JavaScript 包装器" -ForegroundColor Green
|
||||
@@ -386,9 +427,10 @@ if ($Serve) {
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "🚀 使用提示:" -ForegroundColor White
|
||||
Write-Host "├─ 在 VS Code 中按 F5 启动调试" -ForegroundColor Gray
|
||||
Write-Host "├─ 使用 Ctrl+Shift+B 快速构建" -ForegroundColor Gray
|
||||
Write-Host "├─ 查看 public/wasm/smart_api_wrapper.js 了解 JavaScript API 使用" -ForegroundColor Gray
|
||||
Write-Host "└─ 运行 \`npm run serve\` 启动开发服务器" -ForegroundColor Gray
|
||||
Write-Host "使用提示:" -ForegroundColor White
|
||||
Write-Host " - 在 VS Code 中按 F5 启动调试" -ForegroundColor Gray
|
||||
Write-Host " - 使用 Ctrl+Shift+B 快速构建" -ForegroundColor Gray
|
||||
Write-Host " - 查看 public/wasm/smart_api_wrapper.js 了解 JavaScript API 使用" -ForegroundColor Gray
|
||||
Write-Host " - 运行 npm run serve 启动开发服务器" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user