Browse Source

Preliminar changes to support trimmed assemblies.

Vicente Penades 3 years ago
parent
commit
c0aed7c82c

+ 8 - 1
.editorconfig

@@ -56,6 +56,12 @@ dotnet_diagnostic.CA1707.severity = silent
 
 # CA1815: Override equals and operator equals on value types
 dotnet_diagnostic.CA1815.severity = silent
+
+# IL2026: Using dynamic types might cause types or members to be removed by trimmer.
+dotnet_diagnostic.IL2026.severity = error
+csharp_style_throw_expression = true:suggestion
+dotnet_diagnostic.IL2046.severity = error
+dotnet_diagnostic.IL2109.severity = error
 
 [*.{cs,vb}]
 #### Naming styles ####
@@ -139,4 +145,5 @@ dotnet_style_qualification_for_method = false:silent
 dotnet_style_qualification_for_event = false:silent
 
 # CA1014: Mark assemblies with CLSCompliant
-dotnet_diagnostic.CA1014.severity = silent
+dotnet_diagnostic.CA1014.severity = silent
+dotnet_style_namespace_match_folder = true:suggestion

+ 9 - 2
SharpGLTF.sln

@@ -1,7 +1,7 @@
 
 Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio Version 16
-VisualStudioVersion = 16.0.29709.97
+# Visual Studio Version 17
+VisualStudioVersion = 17.1.32228.430
 MinimumVisualStudioVersion = 10.0.40219.1
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{29566B60-311D-42A0-9E8D-C48DECDD587F}"
 	ProjectSection(SolutionItems) = preProject
@@ -49,6 +49,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpGLTF.Plotly", "example
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpGLTF.ThirdParty.Tests", "tests\SharpGLTF.ThirdParty.Tests\SharpGLTF.ThirdParty.Tests.csproj", "{38B27C0B-DB22-4CB7-A6DA-4A6C2A8385A8}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpGLTF.Trimmed.App", "tests\SharpGLTF.Trimmed.App\SharpGLTF.Trimmed.App.csproj", "{A09437F7-403C-44A2-B1FE-15CC535B64CA}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -107,6 +109,10 @@ Global
 		{38B27C0B-DB22-4CB7-A6DA-4A6C2A8385A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{38B27C0B-DB22-4CB7-A6DA-4A6C2A8385A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{38B27C0B-DB22-4CB7-A6DA-4A6C2A8385A8}.Release|Any CPU.Build.0 = Release|Any CPU
+		{A09437F7-403C-44A2-B1FE-15CC535B64CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{A09437F7-403C-44A2-B1FE-15CC535B64CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{A09437F7-403C-44A2-B1FE-15CC535B64CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{A09437F7-403C-44A2-B1FE-15CC535B64CA}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -125,6 +131,7 @@ Global
 		{7FEFC259-51D6-4409-8724-8DE0EA8D5CD9} = {0CBF510D-D836-40BA-95EC-E93FDBB90632}
 		{A63C2A2D-950F-4C76-9299-2B2D325A8653} = {83E7E49D-8A28-45E8-9DBD-1F3AEDEF3E42}
 		{38B27C0B-DB22-4CB7-A6DA-4A6C2A8385A8} = {0CBF510D-D836-40BA-95EC-E93FDBB90632}
+		{A09437F7-403C-44A2-B1FE-15CC535B64CA} = {0CBF510D-D836-40BA-95EC-E93FDBB90632}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {1D7BBAD9-834C-4981-AC96-0AA5226FC43F}

+ 6 - 0
src/SharpGLTF.Core/IO/JsonContent.Impl.cs

@@ -22,6 +22,9 @@ namespace SharpGLTF.IO
     {
         #region serialization
 
+        #if NET6_0_OR_GREATER
+        [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(JsonContent._JsonTrimmingError1)]
+        #endif
         public static string ToJson(Object obj, JSONOPTIONS options)
         {
             if (obj == null) return String.Empty;
@@ -54,6 +57,9 @@ namespace SharpGLTF.IO
             throw new ArgumentException($"Can't serialize {value.GetType().Name}", nameof(value));
         }
 
+        #if NET6_0_OR_GREATER
+        [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(JsonContent._JsonTrimmingError1)]
+        #endif
         public static Object Deserialize(Object obj, Type type, JSONOPTIONS options = null)
         {
             if (options == null)

+ 16 - 0
src/SharpGLTF.Core/IO/JsonContent.cs

@@ -22,13 +22,17 @@ namespace SharpGLTF.IO
     {
         #region debug
 
+        internal const string _JsonTrimmingError1 = "JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.";
+
         private string ToDebuggerDisplay()
         {
             if (_Content == null) return null;
 
             var options = new JSONOPTIONS();
             options.WriteIndented = true;
+            #pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
             return ToJson(options);
+            #pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
         }
 
         #endregion
@@ -132,6 +136,9 @@ namespace SharpGLTF.IO
         /// <param name="value">The value to convert.</param>
         /// <param name="options">Options to control the conversion behavior.</param>
         /// <returns>A <see cref="JsonContent"/> object.</returns>
+        #if NET6_0_OR_GREATER
+        [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(_JsonTrimmingError1)]
+        #endif
         public static JsonContent Serialize(Object value, JSONOPTIONS options = null)
         {
             if (value == null) return default;
@@ -170,16 +177,25 @@ namespace SharpGLTF.IO
             return root == null ? default : new JsonContent(_JsonStaticUtils.Deserialize(root.RootElement));
         }
 
+        #if NET6_0_OR_GREATER
+        [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(_JsonTrimmingError1)]
+        #endif
         public string ToJson(JSONOPTIONS options = null)
         {
             return _JsonStaticUtils.ToJson(_Content, options);
         }
 
+        #if NET6_0_OR_GREATER
+        [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(_JsonTrimmingError1)]
+        #endif
         public Object Deserialize(Type type, JSONOPTIONS options = null)
         {
             return _JsonStaticUtils.Deserialize(_Content, type, options);
         }
 
+        #if NET6_0_OR_GREATER
+        [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(_JsonTrimmingError1)]
+        #endif
         public T Deserialize<T>(JSONOPTIONS options = null)
         {
             return (T)_JsonStaticUtils.Deserialize(_Content, typeof(T), options);

+ 5 - 0
src/SharpGLTF.Core/SharpGLTF.Core.csproj

@@ -18,6 +18,11 @@
   <Import Project="..\Analyzers.props" />
   <Import Project="..\Testing.props" />
   <Import Project="..\SourceLink.props" />
+
+  <PropertyGroup>
+    <IsTrimmable>true</IsTrimmable>
+    <EnableTrimAnalyzer>true</EnableTrimAnalyzer>
+  </PropertyGroup>
   
   <ItemGroup>
     <Compile Include="..\Shared\Guard.cs" Link="Diagnostics\Guard.cs" />

+ 5 - 0
src/SharpGLTF.Toolkit/SharpGLTF.Toolkit.csproj

@@ -14,6 +14,11 @@
   <Import Project="..\Testing.props" />
   <Import Project="..\SourceLink.props" />
 
+  <PropertyGroup>
+    <IsTrimmable>true</IsTrimmable>
+    <EnableTrimAnalyzer>true</EnableTrimAnalyzer>
+  </PropertyGroup>
+
   <ItemGroup>
     <Compile Include="..\Shared\Guard.cs" Link="Diagnostics\Guard.cs" />
     <Compile Include="..\Shared\_Extensions.cs" Link="_Extensions.cs" />

+ 15 - 0
tests/SharpGLTF.Trimmed.App/Program.cs

@@ -0,0 +1,15 @@
+using System;
+using SharpGLTF.Schema2;
+
+namespace SharpGLTF
+{
+    class Program
+    {
+        public static void Main(string[] args)
+        {
+            ModelRoot
+                .Load(args[0])
+                .Save(args[1]);
+        }
+    }
+}

+ 17 - 0
tests/SharpGLTF.Trimmed.App/Properties/PublishProfiles/FolderProfile.pubxml

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+https://go.microsoft.com/fwlink/?LinkID=208121.
+-->
+<Project>
+  <PropertyGroup>
+    <Configuration>Release</Configuration>
+    <Platform>Any CPU</Platform>
+    <PublishDir>bin\publish\</PublishDir>
+    <PublishProtocol>FileSystem</PublishProtocol>
+    <TargetFramework>net6.0</TargetFramework>
+    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
+    <SelfContained>true</SelfContained>
+    <PublishSingleFile>true</PublishSingleFile>
+    <PublishReadyToRun>false</PublishReadyToRun>
+  </PropertyGroup>
+</Project>

+ 28 - 0
tests/SharpGLTF.Trimmed.App/SharpGLTF.Trimmed.App.csproj

@@ -0,0 +1,28 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net6.0</TargetFramework>    
+  </PropertyGroup>
+
+  <Import Project="..\..\src\Analyzers.props" />
+
+  <PropertyGroup>
+    <PublishTrimmed>true</PublishTrimmed>
+    <!-- Prevent warnings from unused code in dependencies -->
+    <TrimmerDefaultAction>link</TrimmerDefaultAction>
+    <RootNamespace>SharpGLTF</RootNamespace>
+  </PropertyGroup>
+
+  <!-- https://docs.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming -->
+
+  <ItemGroup>
+    <ProjectReference Include="..\..\src\SharpGLTF.Core\SharpGLTF.Core.csproj" />
+    <ProjectReference Include="..\..\src\SharpGLTF.Toolkit\SharpGLTF.Toolkit.csproj" />
+
+    <!-- Analyze the whole library, even if attributed with "IsTrimmable" -->
+    <TrimmerRootAssembly Include="SharpGLTF.Core" />
+    <TrimmerRootAssembly Include="SharpGLTF.Toolkit" />
+  </ItemGroup>
+
+</Project>

+ 1 - 0
tests/SharpGLTF.Trimmed.App/publish.cmd

@@ -0,0 +1 @@
+dotnet publish -c Release -r win-x64