Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Support Reference Assemblies in SGEN. #24491

Merged
merged 10 commits into from
Nov 6, 2017

Conversation

huanwu
Copy link
Contributor

@huanwu huanwu commented Oct 6, 2017

Customer can define the reference assemblies as well as the related types that need pregenerated serializer as the followings.

<ItemGroup>
  <SerializationAssembly Include="MyData06">
    <SerializationType>MyData06.Class1;MyData06.Class3</SerializationType>
  </SerializationAssembly>
  <SerializationAssembly Include="MyData07">
    <SerializationType>MyData07.Class5;MyData07.Class6</SerializationType>
  </SerializationAssembly>
</ItemGroup>

Fix #22937

@shmao @mconnew @zhenlan

@huanwu huanwu added area-Serialization enhancement Product code improvement that does NOT require public API changes/additions labels Oct 6, 2017
@huanwu huanwu self-assigned this Oct 6, 2017
<TargetSerializationAssembly>%(SerializationAssembly.Identity)</TargetSerializationAssembly>
<TargetSerializationType>@(SerializationAssembly-&gt;Metadata('SerializationType'))</TargetSerializationType>
</TargetSerializationAssemblyAndType>
</ItemGroup>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ItemGroup above can be simplified as,

    <ItemGroup Condition="@(SerializationAssembly)!=''" Label="Parse SerializationAssembly">
      <SearchSerializationAssembly Include="@(Reference)">
        <AssemblyName>%(SerializationAssembly.Identity)</AssemblyName>
        <SerializationTypes>%(SerializationAssembly.SerializationType)</SerializationTypes>
      </SearchSerializationAssembly>
      <TargetSerializationAssembly Include="@(SearchSerializationAssembly)" Condition="$([System.String]::new('%(SearchSerializationAssembly.Identity)').EndsWith('%(SearchSerializationAssembly.AssemblyName).dll'))" />
    </ItemGroup>

Accordingly, the <Exec> at line 53 can be simplified as,

<Exec Command="dotnet Microsoft.XmlSerializer.Generator /force /quiet /assembly:%(TargetSerializationAssembly.Identity) /type:%(TargetSerializationAssembly.SerializationTypes) /out:$(IntermediateOutputPath)" ContinueOnError="true" />

<ReferenceSerializerName Include="%(SerializationAssembly.Identity).XmlSerializers" />
<ReferenceSerializerDllImmediatePath Include="$(IntermediateOutputPath)%(ReferenceSerializerName.Identity).dll" />
<ReferenceSerializerPdbImmediatePath Include="$(IntermediateOutputPath)%(ReferenceSerializerName.Identity).pdb" />
<ReferenceSerializerCsImmediatePath Include="$(IntermediateOutputPath)%(ReferenceSerializerName.Identity).cs" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can keep only one ReferenceSerializerImmediatePath. It's not easy to distinguish the above 3 items. Besides, it seems in some case we cannot use them.

@@ -79,7 +79,18 @@ private int Run(string[] args)
}
else if (ArgumentMatch(arg, "type"))
{
types.Add(value);
if (value.Contains(";"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to special case value.Contains(";") == true. If value is not empty, we can just do value.Split(';').

@@ -368,7 +379,16 @@ private void ImportType(Type type, ArrayList mappings, ArrayList importedTypes,
private static Assembly LoadAssembly(string assemblyName, bool throwOnFail)
{
Assembly assembly = null;
string path = Path.GetFullPath(assemblyName);
string path;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be written as,

string path = Path.IsPathRooted(assemblyName) ? assemblyName : Path.GetFullPath(assemblyName);

@@ -173,6 +173,11 @@ internal static Assembly LoadGeneratedAssembly(Type type, string defaultNamespac
name.CodeBase = null;
name.CultureInfo = CultureInfo.InvariantCulture;
string serializerPath = Path.Combine(Path.GetDirectoryName(type.Assembly.Location), serializerName + ".dll");
if(!File.Exists(serializerPath))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: space after if

@huanwu
Copy link
Contributor Author

huanwu commented Oct 9, 2017

@dotnet-bot test OSX x64 Debug Build

<Target Name="CopySerializer" AfterTargets="PrepareForPublish">
<Copy Condition="Exists('$(OutputPath)\$(AssemblyName).XmlSerializers.dll')=='true'" SourceFiles="$(OutputPath)\$(AssemblyName).XmlSerializers.dll" DestinationFolder="$(PublishDir)" SkipUnchangedFiles="false" />
</Target>

<Target Name="GenerateSerializationAssemblyForReferenceAssembly" AfterTargets="GenerateSerializationAssembly" Condition="@(SerializationAssembly)!=''">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please correct the indent for the changes?

<Csc Condition="Exists('$(IntermediateOutputPath)%(ReferenceSerializerName.Identity).cs') == 'true'" ContinueOnError="true" OutputAssembly="$(IntermediateOutputPath)%(ReferenceSerializerName.Identity).dll" References="@(ReferencePath);@(IntermediateAssembly)" EmitDebugInformation="$(DebugSymbols)" Sources="$(IntermediateOutputPath)%(ReferenceSerializerName.Identity).cs" TargetType="Library" ToolExe="$(CscToolExe)" ToolPath="$(CscToolPath)" />
<Warning Condition="Exists('$(IntermediateOutputPath)%(ReferenceSerializerName.Identity).dll') != 'true' And Exists('$(IntermediateOutputPath)%(ReferenceSerializerName.Identity).cs') == 'true'" Text="SGEN : warning SGEN1: Fail to compile %(ReferenceSerializerName.Identity).cs. Please follow the instructions in https://go.microsoft.com/fwlink/?linkid=858594 and try again." />
<Copy Condition="Exists('%(ReferenceSerializeImmediatePath.Identity).dll') == 'true'" SourceFiles="%(ReferenceSerializeImmediatePath.Identity).dll" DestinationFolder="$(OutputPath)" />
</Target>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add a target, like the target CopySerializer above, for publishing the generated assemblies?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add CopySerializerForReferenceAssemblies

@@ -79,7 +79,14 @@ private int Run(string[] args)
}
else if (ArgumentMatch(arg, "type"))
{
types.Add(value);
if(value != string.Empty)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add space after if

types.Add(value);
if(value != string.Empty)
{
var typelist = value.Split(';');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'var' -> string[]

types.Add(value);
if(value != string.Empty)
{
string[] typelist = value.Split(';');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

';' [](start = 60, length = 3)

Is making sgen handle the semi-colon the way we wish to handle this as this is different from sg? If it is, that's fine but if we want to use a comma in the tool, then we can do the change in msbuild.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is different from sg desktop version, which is using /type: type1 /type:type2

@karelz karelz removed the enhancement Product code improvement that does NOT require public API changes/additions label Oct 11, 2017
@mconnew
Copy link
Member

mconnew commented Oct 25, 2017

<SerializerDllImmediatePath>$(IntermediateOutputPath)$(SerializerName).dll</SerializerDllImmediatePath>

Where you say ImmediatePath do you mean IntermediatePath?


Refers to: src/Microsoft.XmlSerializer.Generator/pkg/build/Microsoft.XmlSerializer.Generator.targets:4 in c37e15f. [](commit_id = c37e15f, deletion_comment = False)

@mconnew
Copy link
Member

mconnew commented Oct 25, 2017

<SGenWarningText>SGEN : warning SGEN1: Fail to generate the serializer for $(AssemblyName).dll. Please follow the instructions in https://go.microsoft.com/fwlink/?linkid=858594 and try again.</SGenWarningText>

at


Refers to: src/Microsoft.XmlSerializer.Generator/pkg/build/Microsoft.XmlSerializer.Generator.targets:7 in c37e15f. [](commit_id = c37e15f, deletion_comment = False)

<Target Name="CleanSerializationAssembly" AfterTargets="CoreClean">
<Message Text="Cleaning serialization files..." Importance="normal"/>
<Delete Condition="Exists('$(OutputPath)\$(SerializerName).dll') == 'true'" Files="$(OutputPath)\$(SerializerName).dll" />
</Target>

<Target Name="CopySerializer" AfterTargets="PrepareForPublish">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CopySerializer [](start = 16, length = 14)

To keep consistent naming with the other targets, this should be CopySerializationAssembly

@mconnew
Copy link
Member

mconnew commented Oct 25, 2017

<SerializerDllImmediatePath>$(IntermediateOutputPath)$(SerializerName).dll</SerializerDllImmediatePath>

Any properties that an end user would not manipulate/specify in their csproj should be prefixed with _


Refers to: src/Microsoft.XmlSerializer.Generator/pkg/build/Microsoft.XmlSerializer.Generator.targets:4 in c37e15f. [](commit_id = c37e15f, deletion_comment = False)

<SerializationTypes>%(SerializationAssembly.SerializationType)</SerializationTypes>
</SearchSerializationAssembly>
<TargetSerializationAssembly Include="@(SearchSerializationAssembly)" Condition="$([System.String]::new('%(SearchSerializationAssembly.Identity)').EndsWith('%(SearchSerializationAssembly.AssemblyName).dll'))" />
<ReferenceSerializerName Include="%(SerializationAssembly.Identity).XmlSerializers" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReferenceSerializerName [](start = 7, length = 23)

Inconsistent naming. Should this be ReferenceSerializationAssemblyName?

@mconnew
Copy link
Member

mconnew commented Oct 25, 2017

<Delete Condition="Exists('$(SerializerPdbImmediatePath)') == 'true'" Files="$(SerializerPdbImmediatePath)" />

If deleting the dll fails, will the build continue of fail? We should fail otherwise later checks with conditions might not get an error and act like it was successful. This can happen if the application is still running.


Refers to: src/Microsoft.XmlSerializer.Generator/pkg/build/Microsoft.XmlSerializer.Generator.targets:11 in c37e15f. [](commit_id = c37e15f, deletion_comment = False)

@karelz karelz added this to the 2.1.0 milestone Oct 28, 2017
@huanwu huanwu force-pushed the SupportReferenceAssemblyInSgen branch from c37e15f to 2d913b5 Compare October 30, 2017 20:26
@huanwu
Copy link
Contributor Author

huanwu commented Oct 31, 2017

@dotnet-bot test OSX x64 Debug Build

@huanwu
Copy link
Contributor Author

huanwu commented Oct 31, 2017

@dotnet-bot test Windows x64 Debug Build

@huanwu
Copy link
Contributor Author

huanwu commented Oct 31, 2017

@dotnet-bot test Windows x86 Release Build

<SGenWarningText>SGEN : warning SGEN1: Fail to generate the serializer for $(AssemblyName).dll. Please follow the instructions in https://go.microsoft.com/fwlink/?linkid=858594 and try again.</SGenWarningText>
<_SerializerName>$(AssemblyName).XmlSerializers</_SerializerName>
<_SerializerDllIntermediatePath>$(IntermediateOutputPath)$(_SerializerName).dll</_SerializerDllIntermediatePath>
<_SerializerPdbIntermediatePath>$(IntermediateOutputPath)$(_SerializerName).pdb</_SerializerPdbIntermediatePath>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, Path = folder, Filename = file

<SerializerCsImmediatePath>$(IntermediateOutputPath)$(SerializerName).cs</SerializerCsImmediatePath>
<SGenWarningText>SGEN : warning SGEN1: Fail to generate the serializer for $(AssemblyName).dll. Please follow the instructions in https://go.microsoft.com/fwlink/?linkid=858594 and try again.</SGenWarningText>
<_SerializerName>$(AssemblyName).XmlSerializers</_SerializerName>
<_SerializerDllIntermediatePath>$(IntermediateOutputPath)$(_SerializerName).dll</_SerializerDllIntermediatePath>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SerializationAssemblyName?

@huanwu
Copy link
Contributor Author

huanwu commented Nov 2, 2017

@dotnet-bot test OSX x64 Debug Build

Copy link
Member

@mconnew mconnew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@huanwu
Copy link
Contributor Author

huanwu commented Nov 6, 2017

@dotnet-bot test OSX x64 Debug Build

@huanwu huanwu merged commit f7ed59d into dotnet:master Nov 6, 2017
shmao pushed a commit to shmao/corefx that referenced this pull request Dec 6, 2017
* Support Reference Assemblies in SGEN.

* Several fixes based on Shin's feedback.

* minor change.

* Publish serializer for reference assemblies.

* Add publish

* correct the assembly name to be copied

* small format and naming change.

* Add a space.

* Rename some variables.

* Rename some variables.
shmao pushed a commit to shmao/corefx that referenced this pull request Jan 3, 2018
* Support Reference Assemblies in SGEN.

* Several fixes based on Shin's feedback.

* minor change.

* Publish serializer for reference assemblies.

* Add publish

* correct the assembly name to be copied

* small format and naming change.

* Add a space.

* Rename some variables.

* Rename some variables.
@Lxiamail Lxiamail modified the milestones: 2.1.0, 3.0, 2.1.x Nov 14, 2018
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* Support Reference Assemblies in SGEN.

* Several fixes based on Shin's feedback.

* minor change.

* Publish serializer for reference assemblies.

* Add publish

* correct the assembly name to be copied

* small format and naming change.

* Add a space.

* Rename some variables.

* Rename some variables.


Commit migrated from dotnet/corefx@f7ed59d
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants