Skip to content

Commit

Permalink
Support NativeAOT
Browse files Browse the repository at this point in the history
This enables support for NativeAOT (Ahead-of-time native compilation during publish). Doing so required adding a `net8.0` target, which also highlighted a few nullable annotations that needed updating.
  • Loading branch information
drewnoakes committed Feb 5, 2024
1 parent 8395f02 commit d9dedbb
Show file tree
Hide file tree
Showing 17 changed files with 4,597 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
run: dotnet test --verbosity normal --configuration Release --no-build -f net8.0 MetadataExtractor.Tests/MetadataExtractor.Tests.csproj
- name: Test net8.0 (Debug)
run: dotnet test --verbosity normal --configuration Debug --no-build -f net8.0 MetadataExtractor.Tests/MetadataExtractor.Tests.csproj
- name: Publish NativeAOT
run: dotnet publish --verbosity normal --configuration Release --no-build -f net8.0 MetadataExtractor.Tools.FileProcessor/MetadataExtractor.Tools.FileProcessor.csproj

windows:
runs-on: windows-latest
Expand All @@ -48,3 +50,5 @@ jobs:
run: dotnet test --verbosity normal --configuration Release --no-build -f net8.0 MetadataExtractor.Tests\MetadataExtractor.Tests.csproj
- name: Test net462
run: dotnet test --verbosity normal --configuration Release --no-build -f net462 MetadataExtractor.Tests\MetadataExtractor.Tests.csproj
- name: Publish NativeAOT
run: dotnet publish --verbosity normal --configuration Release --no-build -f net8.0 -r win-x64 MetadataExtractor.Tools.FileProcessor/MetadataExtractor.Tools.FileProcessor.csproj
2 changes: 0 additions & 2 deletions MetadataExtractor.Tests/RationalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ public void TypeConverter()
Assert.Equal(new Rational(12, 34), converter.ConvertFrom(new[] { 12, 34 }));
Assert.Equal(new Rational(12, 34), converter.ConvertFrom(new[] { 12u, 34u }));
Assert.Equal(new Rational(13, 35), converter.ConvertFrom(new[] { 12.9, 34.9 })); // rounding

Assert.Throws<NotSupportedException>(() => converter.ConvertFrom(null!));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net48;net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;net48</TargetFrameworks>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PublishTrimmed>true</PublishTrimmed>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion MetadataExtractor/Formats/Exif/ExifTiffHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,9 @@ public override void EndingIfd(in TiffReaderContext context)
base.EndingIfd(context);
}

#if NET7_0_OR_GREATER
[UnconditionalSuppressMessage("Aot", "IL3050:RequiresDynamicCode", Justification = "Array.CreateInstance is only called with known element types")]
#endif
private void ProcessGeoTiff(ushort[] geoKeys, ExifIfd0Directory sourceDirectory)
{
if (geoKeys.Length < 4)
Expand Down Expand Up @@ -448,7 +451,8 @@ private void ProcessGeoTiff(ushort[] geoKeys, ExifIfd0Directory sourceDirectory)
{
if (valueOffset + valueCount <= sourceArray.Length)
{
var array = Array.CreateInstance(sourceArray.GetType().GetElementType(), valueCount);
Type? elementType = sourceArray.GetType().GetElementType();
var array = Array.CreateInstance(elementType!, valueCount);
Array.Copy(sourceArray, valueOffset, array, 0, valueCount);
geoTiffDirectory.Set(keyId, array);
}
Expand Down
6 changes: 3 additions & 3 deletions MetadataExtractor/Formats/Jpeg/JpegProcessingException.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
using System.Runtime.Serialization;
#endif

namespace MetadataExtractor.Formats.Jpeg
{
/// <summary>An exception class thrown upon unexpected and fatal conditions while processing a JPEG file.</summary>
/// <author>Drew Noakes https://drewnoakes.com</author>
#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
[Serializable]
#endif
public class JpegProcessingException : ImageProcessingException
Expand All @@ -28,7 +28,7 @@ public JpegProcessingException(Exception? innerException)
{
}

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
protected JpegProcessingException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Expand Down
4 changes: 4 additions & 0 deletions MetadataExtractor/Formats/Jpeg/JpegSegmentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ public static bool CanContainMetadata(this JpegSegmentType type)
}

/// <summary>Gets JPEG segment types that might contain metadata.</summary>
#if NET5_0_OR_GREATER
public static IReadOnlyList<JpegSegmentType> CanContainMetadataTypes { get; } = Enum.GetValues<JpegSegmentType>().Cast<JpegSegmentType>().Where(type => type.CanContainMetadata()).ToList();
#else
public static IReadOnlyList<JpegSegmentType> CanContainMetadataTypes { get; } = Enum.GetValues(typeof(JpegSegmentType)).Cast<JpegSegmentType>().Where(type => type.CanContainMetadata()).ToList();
#endif

/// <summary>Gets whether this JPEG segment type's marker is followed by a length indicator.</summary>
public static bool ContainsPayload(this JpegSegmentType type)
Expand Down
6 changes: 3 additions & 3 deletions MetadataExtractor/Formats/Png/PngProcessingException.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
using System.Runtime.Serialization;
#endif

namespace MetadataExtractor.Formats.Png
{
/// <summary>An exception class thrown upon unexpected and fatal conditions while processing a JPEG file.</summary>
/// <author>Drew Noakes https://drewnoakes.com</author>
#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
[Serializable]
#endif
public class PngProcessingException : ImageProcessingException
Expand All @@ -28,7 +28,7 @@ public PngProcessingException(Exception? innerException)
{
}

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
protected PngProcessingException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Expand Down
6 changes: 3 additions & 3 deletions MetadataExtractor/Formats/Riff/RiffProcessingException.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
using System.Runtime.Serialization;
#endif

namespace MetadataExtractor.Formats.Riff
{
/// <summary>An exception class thrown upon unexpected and fatal conditions while processing a RIFF file.</summary>
/// <author>Drew Noakes https://drewnoakes.com</author>
#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
[Serializable]
#endif
public class RiffProcessingException : ImageProcessingException
Expand All @@ -28,7 +28,7 @@ public RiffProcessingException(Exception? innerException)
{
}

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
protected RiffProcessingException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Expand Down
6 changes: 3 additions & 3 deletions MetadataExtractor/Formats/Tiff/TiffProcessingException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
using System.Runtime.Serialization;
#endif

Expand All @@ -9,7 +9,7 @@ namespace MetadataExtractor.Formats.Tiff
/// <summary>An exception class thrown upon unexpected and fatal conditions while processing a TIFF file.</summary>
/// <author>Drew Noakes https://drewnoakes.com</author>
/// <author>Darren Salomons</author>
#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
[Serializable]
#endif
public class TiffProcessingException : ImageProcessingException
Expand All @@ -29,7 +29,7 @@ public TiffProcessingException(Exception? innerException)
{
}

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
protected TiffProcessingException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Expand Down
6 changes: 3 additions & 3 deletions MetadataExtractor/IO/BufferBoundsException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
using System.Runtime.Serialization;
#endif

Expand All @@ -10,7 +10,7 @@ namespace MetadataExtractor.IO
/// Thrown when the index provided to an <see cref="IndexedReader"/> is invalid.
/// </summary>
/// <author>Drew Noakes https://drewnoakes.com</author>
#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
[Serializable]
#endif
public class BufferBoundsException : IOException
Expand Down Expand Up @@ -43,7 +43,7 @@ private static string GetMessage(int index, int bytesRequested, long bufferLengt
return $"Attempt to read from beyond end of underlying data source (requested index: {index}, requested count: {bytesRequested}, max index: {bufferLength - 1})";
}

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
protected BufferBoundsException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Expand Down
6 changes: 3 additions & 3 deletions MetadataExtractor/ImageProcessingException.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
using System.Runtime.Serialization;
#endif

namespace MetadataExtractor
{
/// <summary>An exception class thrown upon an unexpected condition that was fatal for the processing of an image.</summary>
/// <author>Drew Noakes https://drewnoakes.com</author>
#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
[Serializable]
#endif
public class ImageProcessingException : Exception
Expand All @@ -28,7 +28,7 @@ public ImageProcessingException(Exception? innerException)
{
}

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
protected ImageProcessingException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Expand Down
6 changes: 3 additions & 3 deletions MetadataExtractor/MetadataException.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
using System.Runtime.Serialization;
#endif

namespace MetadataExtractor
{
/// <summary>Base class for all metadata specific exceptions.</summary>
/// <author>Drew Noakes https://drewnoakes.com</author>
#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
[Serializable]
#endif
public class MetadataException : Exception
Expand All @@ -28,7 +28,7 @@ public MetadataException(string? msg, Exception? innerException)
{
}

#if !NETSTANDARD1_3
#if !NETSTANDARD1_3 && !NET8_0_OR_GREATER
protected MetadataException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Expand Down
7 changes: 6 additions & 1 deletion MetadataExtractor/MetadataExtractor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ MOV and related QuickTime video formats such as MP4, M4V, 3G2, 3GP are supported

Camera manufacturer specific support exists for Agfa, Canon, Casio, DJI, Epson, Fujifilm, Kodak, Kyocera, Leica, Minolta, Nikon, Olympus, Panasonic, Pentax, Reconyx, Sanyo, Sigma/Foveon and Sony models.</Description>
<AssemblyTitle>Metadata Extractor</AssemblyTitle>
<TargetFrameworks>netstandard1.3;netstandard2.1;net462</TargetFrameworks>
<TargetFrameworks>net8.0;netstandard1.3;netstandard2.1;net462</TargetFrameworks>
<NoWarn>$(NoWarn);1591</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>Metadata;Exif;IPTC;XMP;ICC;Photoshop;WebP;PNG;BMP;ICO;PCX;JPEG;TIFF;PSD;Photography;QuickTime;MOV;MP4;M4V;Video;MP3;WAV;Imaging;Video;Audio</PackageTags>
Expand All @@ -22,6 +22,11 @@ Camera manufacturer specific support exists for Agfa, Canon, Casio, DJI, Epson,
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<IsAotCompatible>true</IsAotCompatible>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
<AdditionalFiles Include="PublicAPI/$(TargetFramework)/PublicAPI.Shipped.txt" />
<AdditionalFiles Include="PublicAPI/$(TargetFramework)/PublicAPI.Unshipped.txt" />
Expand Down
Loading

0 comments on commit d9dedbb

Please sign in to comment.