Selaa lähdekoodia

Update macOS build

Ruben 7 kuukautta sitten
vanhempi
sitoutus
1b8621fdf7

+ 23 - 9
.github/workflows/BuildMacOS.yml

@@ -40,13 +40,20 @@ jobs:
             -outputPath "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64" `
             -appVersion "${{steps.get-version.outputs.version}}"
         shell: pwsh
-        
-      # Step 6: Create DMG for arm64
+      
+      # Step 6: Run fix_dylibs.sh script for arm64
+      - name: Run fix_dylibs script for arm64
+        run: |
+          chmod +x "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64/PicView.app/Contents/MacOS/fix_dylibs.sh"
+          cd "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64/PicView.app/Contents/MacOS/"
+          ./fix_dylibs.sh
+      
+      # Step 7: Create DMG for arm64
       - name: Create DMG for arm64
         run: |
           hdiutil create -volname "PicView" -srcfolder "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64/PicView.app" -ov -format UDZO "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64.dmg"
-        
-      # Step 7: Upload arm64 artifacts
+      
+      # Step 8: Upload arm64 artifacts
       - name: Upload arm64 artifacts
         uses: actions/upload-artifact@v4
         with:
@@ -56,7 +63,7 @@ jobs:
             ${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64.dmg
           retention-days: 14
 
-      # Step 8: Build x64 version
+      # Step 9: Build x64 version
       - name: Build x64 version
         run: |
           pwsh -File "${{ github.workspace }}/Build/Build Avalonia.MacOS.ps1" `
@@ -64,13 +71,20 @@ jobs:
             -outputPath "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64" `
             -appVersion "${{steps.get-version.outputs.version}}"
         shell: pwsh
-        
-      # Step 9: Create DMG for x64
+      
+      # Step 10: Run fix_dylibs.sh script for x64
+      - name: Run fix_dylibs script for x64
+        run: |
+          chmod +x "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64/PicView.app/Contents/MacOS/fix_dylibs.sh"
+          cd "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64/PicView.app/Contents/MacOS/"
+          ./fix_dylibs.sh
+      
+      # Step 11: Create DMG for x64
       - name: Create DMG for x64
         run: |
           hdiutil create -volname "PicView" -srcfolder "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64/PicView.app" -ov -format UDZO "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64.dmg"
 
-      # Step 10: Upload x64 artifacts
+      # Step 12: Upload x64 artifacts
       - name: Upload x64 artifacts
         uses: actions/upload-artifact@v4
         with:
@@ -78,4 +92,4 @@ jobs:
           path: |
             ${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64/PicView.app
             ${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64.dmg
-          retention-days: 14
+          retention-days: 14

+ 115 - 15
Build/Build Avalonia.MacOS.ps1

@@ -14,25 +14,27 @@ $coreProjectPath = Join-Path -Path $PSScriptRoot -ChildPath "..\src\PicView.Core
 # Load the .csproj file as XML
 [xml]$coreCsproj = Get-Content $coreProjectPath
 
-# Define the package reference to replace
-$packageRefX64 = "Magick.NET-Q8-x64"
-$packageRefArm64 = "Magick.NET-Q8-arm64"
+# Define the package reference to replace based on the platform
+if ($Platform -eq "arm64") {
+    $packageRef = "Magick.NET-Q8-OpenMP-arm64"
+} else {
+    $packageRef = "Magick.NET-Q8-OpenMP-x64"
+}
 
-# Find the Magick.NET package reference and update it based on the platform
-$packageNodes = $coreCsproj.Project.ItemGroup.PackageReference | Where-Object { $_.Include -eq $packageRefX64 -or $_.Include -eq $packageRefArm64 }
+# Find the Magick.NET package reference and update it
+$packageNodes = $coreCsproj.Project.ItemGroup.PackageReference | Where-Object { $_.Include -like "Magick.NET-Q8*" }
 if ($packageNodes) {
     foreach ($packageNode in $packageNodes) {
-        if ($Platform -eq "arm64") {
-            $packageNode.Include = $packageRefArm64
-        } else {
-            $packageNode.Include = $packageRefX64
-        }
+        $packageNode.Include = $packageRef
     }
+    
+    # Save the updated .csproj file
+    $coreCsproj.Save($coreProjectPath)
+    Write-Host "Updated Magick.NET package reference to $packageRef"
+} else {
+    Write-Host "Warning: No Magick.NET package reference found to update"
 }
 
-# Save the updated .csproj file
-$coreCsproj.Save($coreProjectPath)
-
 # Define the project path for the actual build target
 $avaloniaProjectPath = Join-Path -Path $PSScriptRoot -ChildPath "../src/PicView.Avalonia.MacOS/PicView.Avalonia.MacOS.csproj"
 
@@ -40,12 +42,18 @@ $avaloniaProjectPath = Join-Path -Path $PSScriptRoot -ChildPath "../src/PicView.
 $tempBuildPath = Join-Path -Path $outputPath -ChildPath "temp"
 New-Item -ItemType Directory -Force -Path $tempBuildPath
 
+# Run dotnet restore to ensure we have the updated packages
+Write-Host "Restoring packages for $avaloniaProjectPath..."
+dotnet restore $avaloniaProjectPath
+
 # Run dotnet publish for the Avalonia project
+Write-Host "Publishing project for osx-$Platform..."
 dotnet publish $avaloniaProjectPath `
     --runtime "osx-$Platform" `
     --self-contained true `
     --configuration Release `
     -p:PublishSingleFile=false `
+    -p:MagickCopyNativeMacOS=true `
     --output $tempBuildPath
 
 # Create .app bundle structure
@@ -53,13 +61,15 @@ $appBundlePath = Join-Path -Path $outputPath -ChildPath "PicView.app"
 $contentsPath = Join-Path -Path $appBundlePath -ChildPath "Contents"
 $macOSPath = Join-Path -Path $contentsPath -ChildPath "MacOS"
 $resourcesPath = Join-Path -Path $contentsPath -ChildPath "Resources"
+$frameworksPath = Join-Path -Path $contentsPath -ChildPath "Frameworks"
 
 # Create directory structure
 New-Item -ItemType Directory -Force -Path $macOSPath
 New-Item -ItemType Directory -Force -Path $resourcesPath
+New-Item -ItemType Directory -Force -Path $frameworksPath
 
 # Create Info.plist
-$infoPlistPath = Join-Path -Path $contentsPath -ChildPath "Info.plist"  # Add this line to specify the correct path
+$infoPlistPath = Join-Path -Path $contentsPath -ChildPath "Info.plist"
 $infoPlistContent = @"
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
@@ -108,8 +118,51 @@ $utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $false
 [System.IO.File]::WriteAllText($infoPlistPath, $infoPlistContent, $utf8NoBomEncoding)
 
 # Copy build output to MacOS directory
+Write-Host "Copying build output to .app structure..."
 Copy-Item -Path "$tempBuildPath/*" -Destination $macOSPath -Recurse
 
+# Find and copy Magick.NET dylibs to the Frameworks directory
+Write-Host "Looking for Magick.NET dylibs..."
+$dylibs = Get-ChildItem -Path $tempBuildPath -Filter "*.dylib" -Recurse
+if ($dylibs) {
+    Write-Host "Found $($dylibs.Count) dylibs, copying to Frameworks directory"
+    foreach ($dylib in $dylibs) {
+        Copy-Item -Path $dylib.FullName -Destination $frameworksPath -Force
+    }
+} else {
+    Write-Host "Warning: No dylibs found in the build output"
+    
+    # Find Magick.NET package in the NuGet cache and copy dylibs if available
+    $nugetPackagesDir = if ($env:NUGET_PACKAGES) { 
+        $env:NUGET_PACKAGES 
+    } else {
+        Join-Path -Path $HOME -ChildPath ".nuget/packages"
+    }
+    
+    # Look for the Magick.NET package
+    $magickPackageDir = Join-Path -Path $nugetPackagesDir -ChildPath $packageRef
+    
+    if (Test-Path $magickPackageDir) {
+        Write-Host "Found Magick.NET package directory: $magickPackageDir"
+        $newestVersion = (Get-ChildItem $magickPackageDir | Sort-Object -Property Name -Descending)[0].Name
+        $runtimesDir = Join-Path -Path $magickPackageDir -ChildPath "$newestVersion/runtimes/osx-$Platform/native"
+        
+        if (Test-Path $runtimesDir) {
+            Write-Host "Copying dylibs from package cache: $runtimesDir"
+            $packageDylibs = Get-ChildItem -Path $runtimesDir -Filter "*.dylib"
+            foreach ($dylib in $packageDylibs) {
+                Copy-Item -Path $dylib.FullName -Destination $frameworksPath -Force
+                # Also copy to the MacOS directory as a fallback
+                Copy-Item -Path $dylib.FullName -Destination $macOSPath -Force
+            }
+        } else {
+            Write-Host "Couldn't find expected native runtimes in NuGet package: $runtimesDir"
+        }
+    } else {
+        Write-Host "Couldn't find Magick.NET package in NuGet cache"
+    }
+}
+
 # Copy icon if it exists
 $iconSource = Join-Path -Path $PSScriptRoot -ChildPath "../src/PicView.Avalonia.MacOS/Assets/AppIcon.icns"
 if (Test-Path $iconSource) {
@@ -119,18 +172,65 @@ if (Test-Path $iconSource) {
 # Remove PDB files
 Get-ChildItem -Path $macOSPath -Filter "*.pdb" -Recurse | Remove-Item -Force
 
+# Create a script to fix library paths in the main executable
+$fixDylibPath = Join-Path -Path $macOSPath -ChildPath "fix_dylibs.sh"
+$fixDylibScript = @"
+#!/bin/bash
+cd "\$(dirname "\$0")"
+
+# Process PicView executable
+EXECUTABLE="./PicView"
+
+# Process each dylib in the Frameworks directory
+for dylib in ../Frameworks/*.dylib; do
+    # Get just the filename
+    dylib_name=\$(basename \$dylib)
+    echo "Processing \$dylib_name"
+    
+    # For executable
+    install_name_tool -change "@rpath/\$dylib_name" "@executable_path/../Frameworks/\$dylib_name" \$EXECUTABLE
+    
+    # For each dylib that might depend on other dylibs
+    for target_dylib in ../Frameworks/*.dylib; do
+        install_name_tool -change "@rpath/\$dylib_name" "@executable_path/../Frameworks/\$dylib_name" \$target_dylib
+    done
+done
+
+echo "Library paths fixed successfully"
+"@
+
+# Save fix_dylibs script
+$utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $false
+[System.IO.File]::WriteAllText($fixDylibPath, $fixDylibScript, $utf8NoBomEncoding)
+
 # Remove temporary build directory
 Remove-Item -Path $tempBuildPath -Recurse -Force
 
 # Set proper permissions for the entire .app bundle
 if ($IsLinux -or $IsMacOS) {
+    # Make the fix_dylibs.sh script executable
+    chmod +x $fixDylibPath
+    
+    # Run the fix_dylibs script if we're on macOS
+    if ($IsMacOS) {
+        Write-Host "Running fix_dylibs.sh script"
+        & $fixDylibPath
+    }
+    
     # Set executable permissions on all binaries and dylibs
     Get-ChildItem -Path $macOSPath -Recurse | ForEach-Object {
-        if ($_.Extension -in @('.dylib', '') -or $_.Name -eq 'PicView.Avalonia.MacOS') {
+        if ($_.Extension -in @('.dylib', '') -or $_.Name -eq 'PicView') {
             chmod +x $_.FullName
         }
     }
     
+    # Set executable permissions on dylibs in Frameworks directory
+    Get-ChildItem -Path $frameworksPath -Filter "*.dylib" | ForEach-Object {
+        chmod +x $_.FullName
+    }
+    
     # Set proper ownership and permissions for the entire .app bundle
     chmod -R 755 $appBundlePath
 }
+
+Write-Host "Build completed successfully: $appBundlePath"

+ 20 - 14
src/PicView.Avalonia.MacOS/PicView.Avalonia.MacOS.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <OutputType>WinExe</OutputType>
-	<TargetFramework>net9.0</TargetFramework>
+    <TargetFramework>net9.0</TargetFramework>
     <Platforms>x64;arm64</Platforms>
     <Nullable>enable</Nullable>
     <BuiltInComInteropSupport>true</BuiltInComInteropSupport>
@@ -20,16 +20,15 @@
     <ImplicitUsings>enable</ImplicitUsings>
     <Product>PicView</Product>
     <AssemblyName>PicView</AssemblyName>
+    <!-- Native library handling -->
+    <MagickCopyNativeMacOS>true</MagickCopyNativeMacOS>
+    <NativeLibraryRelocator>true</NativeLibraryRelocator>
   </PropertyGroup>
+
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
     <DebugType>none</DebugType>
   </PropertyGroup>
 
-  <PropertyGroup>
-    <MagickCopyNativeMacOS>true</MagickCopyNativeMacOS>
-  </PropertyGroup>
-
-
   <PropertyGroup Condition="'$(Platform)' == 'x64'">
     <PlatformTarget>x64</PlatformTarget>
   </PropertyGroup>
@@ -37,7 +36,6 @@
   <PropertyGroup Condition="'$(Platform)' == 'arm64'">
     <PlatformTarget>ARM64</PlatformTarget>
   </PropertyGroup>
-  
 
   <ItemGroup>
     <ProjectReference Include="..\PicView.Avalonia\PicView.Avalonia.csproj" />
@@ -49,6 +47,14 @@
     <PackageReference Include="Avalonia.Themes.Simple" Version="11.2.5" />
   </ItemGroup>
 
+  <!-- Ensure native libraries are copied to output -->
+  <Target Name="CopyNativeLibraries" AfterTargets="Publish">
+    <ItemGroup>
+      <NativeLibs Include="$(OutDir)\*.dylib" />
+    </ItemGroup>
+    <Copy SourceFiles="@(NativeLibs)" DestinationFolder="$(PublishDir)" SkipUnchangedFiles="true" />
+  </Target>
+
   <ItemGroup>
     <AvaloniaXaml Update="Views\ExifWindow.axaml">
       <SubType>Designer</SubType>
@@ -73,11 +79,11 @@
     </Compile>
   </ItemGroup>
 
-	<ItemGroup>
-		<InternalsVisibleTo Include="PicView.Tests" />
-	</ItemGroup>
+  <ItemGroup>
+    <InternalsVisibleTo Include="PicView.Tests" />
+  </ItemGroup>
 
-	<ItemGroup>
-	  <UpToDateCheckInput Remove="Views\SettingsWindow.axaml" />
-	</ItemGroup>
-</Project>
+  <ItemGroup>
+    <UpToDateCheckInput Remove="Views\SettingsWindow.axaml" />
+  </ItemGroup>
+</Project>