|
| 1 | +# PowerShell script to compile Java sources and create litecam.jar |
| 2 | +param( |
| 3 | + [string]$BuildDir = "build", |
| 4 | + # Optional: additional directories containing prebuilt natives to merge (each should contain platform libs) |
| 5 | + [string[]]$ExtraNativeDirs = @() |
| 6 | +) |
| 7 | + |
| 8 | +$ErrorActionPreference = 'Stop' |
| 9 | + |
| 10 | +$projectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 11 | +Set-Location $projectRoot |
| 12 | + |
| 13 | +$javaSrc = Join-Path $projectRoot 'java-src' |
| 14 | +$outDir = Join-Path $projectRoot 'java-build' |
| 15 | +if (Test-Path $outDir) { Remove-Item -Recurse -Force $outDir } |
| 16 | +New-Item -ItemType Directory -Force -Path $outDir | Out-Null |
| 17 | + |
| 18 | +Write-Host "Compiling Java sources..." |
| 19 | + |
| 20 | +$javaFiles = Get-ChildItem -Recurse $javaSrc -Filter *.java | ForEach-Object { $_.FullName } |
| 21 | +& javac -d $outDir $javaFiles |
| 22 | +if ($LASTEXITCODE -ne 0) { throw "javac failed" } |
| 23 | + |
| 24 | +$jarPath = Join-Path $projectRoot 'litecam.jar' |
| 25 | +if (Test-Path $jarPath) { Remove-Item $jarPath } |
| 26 | + |
| 27 | +# Collect native libraries by platform/arch |
| 28 | +$nativeMap = @{} |
| 29 | +$dllCandidates = @() |
| 30 | +$dllCandidates += (Join-Path $projectRoot 'build/Release/litecam.dll') |
| 31 | +$dllCandidates += (Join-Path $projectRoot 'build/litecam.dll') |
| 32 | +$dllCandidates += (Join-Path $projectRoot 'build/Release/liblitecam.dylib') |
| 33 | +$dllCandidates += (Join-Path $projectRoot 'build/liblitecam.dylib') |
| 34 | +$dllCandidates += (Join-Path $projectRoot 'build/Release/liblitecam.so') |
| 35 | +$dllCandidates += (Join-Path $projectRoot 'build/liblitecam.so') |
| 36 | + |
| 37 | +function Get-OsToken { |
| 38 | + if ([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)) { return 'windows' } |
| 39 | + elseif ([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::OSX)) { return 'macos' } |
| 40 | + else { return 'linux' } |
| 41 | +} |
| 42 | +function Get-ArchToken { if ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture -in @('Arm64')) { 'arm64' } else { 'x86_64' } } |
| 43 | + |
| 44 | +$osToken = Get-OsToken |
| 45 | +$archToken = Get-ArchToken |
| 46 | + |
| 47 | +# Primary host build native bundling |
| 48 | +$nativeRoot = Join-Path $outDir 'natives' |
| 49 | +New-Item -ItemType Directory -Force -Path $nativeRoot | Out-Null |
| 50 | +$nativeDir = Join-Path $nativeRoot "$osToken-$archToken" |
| 51 | +New-Item -ItemType Directory -Force -Path $nativeDir | Out-Null |
| 52 | + |
| 53 | +$picked = $false |
| 54 | +foreach ($c in $dllCandidates) { |
| 55 | + if (Test-Path $c) { |
| 56 | + $destName = if ($osToken -eq 'windows') { 'litecam.dll' } elseif ($osToken -eq 'macos') { 'liblitecam.dylib' } else { 'liblitecam.so' } |
| 57 | + Copy-Item $c (Join-Path $nativeDir $destName) -Force |
| 58 | + Write-Host "Bundling native library: $c -> natives/$osToken-$archToken/$destName" |
| 59 | + $picked = $true |
| 60 | + break |
| 61 | + } |
| 62 | +} |
| 63 | +if (-not $picked) { Write-Warning "No native library found to bundle for host." } |
| 64 | + |
| 65 | +# Merge additional prebuilt native sets |
| 66 | +foreach ($dir in $ExtraNativeDirs) { |
| 67 | + if (-not (Test-Path $dir)) { Write-Warning "Extra native dir not found: $dir"; continue } |
| 68 | + # Expect structure like <dir>/windows-x86_64/litecam.dll etc. Copy recursively. |
| 69 | + Get-ChildItem -Directory $dir | ForEach-Object { |
| 70 | + $platformFolder = $_.Name |
| 71 | + $target = Join-Path $nativeRoot $platformFolder |
| 72 | + New-Item -ItemType Directory -Force -Path $target | Out-Null |
| 73 | + Get-ChildItem -File -Recurse $_.FullName | ForEach-Object { |
| 74 | + Copy-Item $_.FullName (Join-Path $target ($_.Name)) -Force |
| 75 | + } |
| 76 | + Write-Host "Merged extra natives: $platformFolder" |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +Write-Host "Creating JAR with bundled natives..." |
| 81 | +Push-Location $outDir |
| 82 | +& jar cf $jarPath . |
| 83 | +Pop-Location |
| 84 | + |
| 85 | +Write-Host "Created $jarPath (includes natives/* for packaged platforms)" |
| 86 | + |
| 87 | +# Convenience run script |
| 88 | +$runScript = @' |
| 89 | +@echo off |
| 90 | +setlocal |
| 91 | +set JAR=%~dp0litecam.jar |
| 92 | +java -cp "%JAR%" com.example.litecam.LiteCamViewer %* |
| 93 | +endlocal |
| 94 | +'@ |
| 95 | +Set-Content -Path (Join-Path $projectRoot 'run-litecam.bat') -Value $runScript -Encoding ASCII |
| 96 | +Write-Host "Created run-litecam.bat" |
0 commit comments