Skip to content

Fix MilCodeGen #6135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed

Conversation

ThomasGoulet73
Copy link
Contributor

@ThomasGoulet73 ThomasGoulet73 commented Feb 16, 2022

Description

This PR aims to fix MilCodeGen which seems to be very outdated and it doesn't seem like it ever worked in this repo (It probably worked in the internal repo for Microsoft).

Since it does not seem like it ever worked, I did these changes just by deducing how it's supposed to work so this PR might be totally wrong.

I noticed a couple of changes in the generated code other than white-space and license header where I don't know whether the source of truth is the new generated code or the existing generated code (It's mostly comments that differ between both).

This PR unblocks PRs like this one: #5651.

Customer Impact

None.

Regression

No.

Testing

I tested this fix locally using this command build -projects "src\Microsoft.DotNet.Wpf\src\WpfGfx\codegen\mcg\mcg.proj" and by looking at the changes and doing a normal build afterwards.

Risk

I think the risk is pretty low since it's only used for development in this repo and the worse that can happen is to be unable to run MilCodeGen (It didn't work before this PR so it can only be an improvement). All the generated code is checked out in Git so it's easier to validate that the generated code is good.

@ThomasGoulet73 ThomasGoulet73 requested a review from a team as a code owner February 16, 2022 04:37
@ghost ghost added the PR metadata: Label to tag PRs, to facilitate with triage label Feb 16, 2022
@ghost ghost requested review from dipeshmsft, singhashish-wpf and SamBent February 16, 2022 04:37
@@ -21,17 +21,8 @@ pushd %~dp0\..

call %~dp0\SetClrPath.cmd

copy %CspExePath%\csp.exe %clrpath%
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know the context for copying csp to clrpath but it seemed unnecessary.

@@ -1,6 +1 @@
set clrpath=%MANAGED_TOOLS_ROOT%\%MANAGED_REFS_VERSION%
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't know how to get clrpath automatically. It's the path used by sn.exe but I couldn't find where to get it.


:: Revert any generated files which haven't changed
if {%_NoRevertFlag%} EQU {} call %~dp0\tf_GeneratedFiles %OutputDir% %~dp0\GeneratedElements.txt revert -a
call %~dp0\InvokeCSP.cmd %Options% -rsp:main\Elements.rsp -enableCsPrime -r:"System.Xml.dll" -- %_SdFlag% -xmlFile:xml\Elements.xml -dataType:MS.Internal.MilCodeGen.ResourceModel.CG -o:"%OutputDir%"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the argument -clrdir and the full path in the argument -r. It works without it and avoids the need of finding the path for .Net Framework reference assemblies.

@@ -0,0 +1,19 @@
-s:main\SimpleGenerator.cs
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I created this file and Resources.rsp by hand because they didn't exist. They were probably missed when adding the code in this repo because *.rsp files are excluded in the gitignore.

Copy link
Member

Choose a reason for hiding this comment

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

There are 3 .rsp files in internal source, Elements Resources and SimpleGenerator.
I am still looking at these and trying to make sense, but in the meanwhile here are the contents of the 3 files :

  1. Elements.rsp
-rsp:main\SimpleGenerator.rsp
-s:Generators\Elements.cs
-s:Generators\IAnimatableHelper.cs
-s:ResourceModel\Elements.cs
-s:ResourceModel\Generated\Elements.cs
  1. Resources.rsp
-s:main\ResourceGenerator.cs
-s:main\cmdline\IndentedWriter.cs
-s:main\cmdline\Utility.cs
-s:main\cmdline\CommandLineArguments.cs
-s:Runtime\DelimitedList.cs
-s:Runtime\OrList.cs
-s:Runtime\CodeSink.cs
-s:Runtime\FileCodeSink.cs
-s:Runtime\StringCodeSink.cs
-s:Runtime\ParameterList.cs
-s:Runtime\XmlLoader.cs
-s:Runtime\GeneratorMethods.cs
-s:ResourceModel\Command.cs
-s:ResourceModel\Enum.cs
-s:ResourceModel\Field.cs
-s:ResourceModel\Primitive.cs
-s:ResourceModel\Realization.cs
-s:ResourceModel\RenderData.cs
-s:ResourceModel\Resource.cs
-s:ResourceModel\ResourceModel.cs
-s:ResourceModel\Struct.cs
-s:ResourceModel\Type.cs
-s:ResourceModel\Array.cs
-s:helpers\CodeGenHelpers.cs
-s:helpers\CollectionHelper.cs
-s:helpers\EmptyClassHelper.cs
-s:helpers\ManagedStyle.cs
-s:helpers\PaddedCommand.cs
-s:helpers\StringHelpers.cs
-s:helpers\Style.cs
-s:helpers\EnumHelper.cs
-s:helpers\StructHelper.cs
-s:generators\AnimatableTemplate.cs
-s:generators\AnimationBaseTemplate.cs
-s:generators\AnimationClockResource.cs
-s:generators\AnimationResourceTemplate.cs
-s:generators\AnimationTemplate.cs
-s:generators\AnimationUsingKeyFramesTemplate.cs
-s:generators\CommandProcessMessage.cs
-s:generators\CommandStructure.cs
-s:generators\CommandType.cs
-s:generators\DiscreteKeyFrameTemplate.cs
-s:generators\DuceResource.cs
-s:generators\EasingKeyFrameTemplate.cs
-s:generators\FrameworkElementTemplate.cs
-s:generators\IAnimatableHelper.cs
-s:generators\KeyFrameTemplate.cs
-s:generators\KeyFrameCollectionTemplate.cs
-s:generators\LinearKeyFrameTemplate.cs
-s:generators\ManagedEnum.cs
-s:generators\ManagedResource.cs
-s:generators\ManagedStruct.cs
-s:generators\PolySegmentTemplate.cs
-s:generators\RenderData.cs
-s:generators\ResourceFactory.cs
-s:generators\ResourceType.cs
-s:generators\SplineKeyFrameTemplate.cs
-s:generators\Template.cs
-s:generators\MiscDef.cs
-s:generators\MilRenderTypesGenerated.cs
-s:generators\WincodecPrivateGenerated.cs

  1. SimpleGenerator.rsp
-s:main\SimpleGenerator.cs
-s:main\cmdline\IndentedWriter.cs
-s:main\cmdline\Utility.cs
-s:main\cmdline\CommandLineArguments.cs
-s:Runtime\DelimitedList.cs
-s:Runtime\OrList.cs
-s:Runtime\CodeSink.cs
-s:Runtime\FileCodeSink.cs
-s:Runtime\StringCodeSink.cs
-s:Runtime\ParameterList.cs
-s:Runtime\GeneratorMethods.cs
-s:ResourceModel\CG.cs
-s:helpers\ManagedStyle.cs
-s:helpers\Style.cs

@kant2002
Copy link
Contributor

You stole my next target 😄

@ThomasGoulet73
Copy link
Contributor Author

I pushed a bunch of commit to try to reduce the diff but I couldn't reduce it much further because the bulk of the changes in generated code come from whitespace added because of conditional code which adds an empty line (Example: This code generates these whitespaces).

@omariom
Copy link
Contributor

omariom commented Mar 8, 2022

With 308 changed files it's almost impossible to understand what has changed :/

Copy link
Contributor

@kant2002 kant2002 left a comment

Choose a reason for hiding this comment

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

I think would be worth to keep build changes from style formatting changes separately. I have hopes that this allow speedup of the review, or at least schedule it for testing.

@@ -250,12 +250,15 @@ internal void RemoveAtWithoutFiringPublicEvents(int index)

WritePreamble();

if (!Object.ReferenceEquals(_collection[index], value))
if (!Object.ReferenceEquals(_collection[ index ], value))
Copy link
Contributor

Choose a reason for hiding this comment

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

These kind of changes is against current code style. I assume that it would be hard to convince WPF team on this kind of changes. Also they are reduce ability understand what's going on.

OnFreezablePropertyChanged(oldValue, value);

_collection[index] = value;
_collection[ index ] = value;

Copy link
Contributor

Choose a reason for hiding this comment

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

Do these empty lines needed?

@@ -236,8 +245,11 @@ void DUCE.IResource.ReleaseOnChannel(DUCE.Channel channel)

if (_duceResource.ReleaseOnChannel(channel))
{

Copy link
Contributor

Choose a reason for hiding this comment

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

Why these two lines? I see these a lot. That's confusing.

@ThomasGoulet73
Copy link
Contributor Author

@kant2002 @omariom Sorry about the delay, I forgot about this PR. All the files are generated using MilCodeGen without any modifications by hand so all the changes are necessary.

It looks like a lot of generated files were formatted when adding the files to this repo.
For example, TextDecorationCollection from referencesource.microsoft.com is a lot closer to this repo with the changes in this PR than without this PR. TextDecorationCollection in this repo with the changes in this PR vs TextDecorationCollection in this repo without the changes in this PR. TextDecorationCollection did not change since the initial commit of PresentationCore.

@dipeshmsft dipeshmsft self-assigned this Jun 18, 2022
@ghost ghost assigned ThomasGoulet73 Jun 18, 2022
@pchaurasia14 pchaurasia14 added the Community Contribution A label for all community Contributions label Jul 20, 2022
@ThomasGoulet73 ThomasGoulet73 force-pushed the fix-milcodegen-build branch from 3b939e3 to 4fdf462 Compare May 1, 2024 01:21
@ThomasGoulet73
Copy link
Contributor Author

ThomasGoulet73 commented May 1, 2024

I rebased, fixed the conflicts and synced MilCodeGen to the generated code since changes to generated files was merged into main.

@dotnet/wpf-developers: Could this be considered for the next test pass ? MilCodeGen is outdated compared to the generated files in the main branch because generated files get changed manually because we cannot run MilCodeGen.

@ThomasGoulet73 ThomasGoulet73 requested a review from a team as a code owner January 28, 2025 04:28
@ThomasGoulet73
Copy link
Contributor Author

ThomasGoulet73 commented Jan 28, 2025

I rebased to fix the conflicts.

I had to restore a bunch of usings that were removed because they were unused but the problem is that they were removed from files that were generated from the same template and some file didn't need the usings and some did so to keep it unified all files generated from the same template have the same usings. I did remove usings that none of the files generated from the same template needed.

@dipeshmsft
Copy link
Member

@ThomasGoulet73, I have started looking at this PR. I was going through the README files in this area, and my general understanding is that MilCodeGen uses the csp.exe tool to do the codegen.

It can basically be divided into three parts I guess :

  1. Fixing csp.csproj to get it to build and run.
  2. Fixing milcodegen to get it to build and run.
  3. Fixing templates and regenerating the code files.

If my understanding is correct, will it be possible to break the PR into smaller PRs that do the things above.

Also regarding fixing the csp.exe tool, there are some unit tests for the Csp tool were you able to get them to run ?


I will try to run mcg for .NET Framework codebase, I am guessing there it will be in a running state, as the targets files imported in csp.csproj and other files are present there.

@dipeshmsft
Copy link
Member

I will try to run mcg for .NET Framework codebase, I am guessing there it will be in a running state, as the targets files imported in csp.csproj and other files are present there.

I tried running MCG of Framework codebase and it works there. I will try to match the steps here with logs that are generated from the run. If possible, I will attach a log file for the run.

@ThomasGoulet73
Copy link
Contributor Author

@ThomasGoulet73, I have started looking at this PR. I was going through the README files in this area, and my general understanding is that MilCodeGen uses the csp.exe tool to do the codegen.

It can basically be divided into three parts I guess :

  1. Fixing csp.csproj to get it to build and run.
  2. Fixing milcodegen to get it to build and run.
  3. Fixing templates and regenerating the code files.

If my understanding is correct, will it be possible to break the PR into smaller PRs that do the things above.

Yes, @h3xds1nz also suggested spliting it here #10310 (comment).

What I think I'm going to do is:

  1. Open a PR to fix CSP and MilCodeGen, to be able to run the codegen (I don't see the value in splitting those in 2 PR).
  2. Open a PR where I update the CodeGen to align templates with the current generated code to reduce diff.
  3. Open a PR for each project that contains generated code where I only update the generated code.

The PRs 1 and 2 won't include changes to shipping code so it will be easy to review. The PRs in step 3 will include changes to shipping code but it shouldn't change the behavior of the code, and since it will be 1 PR per project it will be easier to review.

Also regarding fixing the csp.exe tool, there are some unit tests for the Csp tool were you able to get them to run ?

I can't seem to find those tests you're talking about, could you link me those tests ?

@ThomasGoulet73
Copy link
Contributor Author

I can't seem to find those tests you're talking about, could you link me those tests ?

Nevermind, I just found it and I'm working on fixing it. Could you check if you have the file CspProject.rsp in your internal repo, it seems to be missing from this repo (For the same reason as #6135 (comment)).

@dipeshmsft
Copy link
Member

dipeshmsft commented Feb 6, 2025

Could you check if you have the file CspProject.rsp in your internal repo, it seems to be missing from this repo.

@ThomasGoulet73 Here's the content of that file

-main:MS.Internal.Csp.Test.CspProjectTest
-s:CspProjectTest.cs
  1. Open a PR to fix CSP and MilCodeGen, to be able to run the codegen (I don't see the value in splitting those in 2 PR).
  2. Open a PR where I update the CodeGen to align templates with the current generated code to reduce diff.
  3. Open a PR for each project that contains generated code where I only update the generated code.

This sounds like a good division.

@ThomasGoulet73
Copy link
Contributor Author

Thanks @dipeshmsft, I was able to reverse engineer the content CspProject.rsp when I was working on #10417 but it helps to validate that what I did was correct.

This sounds like a good division.

I opened #10417 as the first part of this work, could you prioritize it ? Realistically it's very low to no risk since it doesn't affect shipping code and worse case scenario we can always revert it.

@dipeshmsft
Copy link
Member

I opened #10417 as the first part of this work, could you prioritize it ? Realistically it's very low to no risk since it doesn't affect shipping code and worse case scenario we can always revert it.

I am already on it. There is still some part of MCG that I haven't gone through, but I will be mostly done by tomorrow.

@h3xds1nz
Copy link
Member

h3xds1nz commented Feb 6, 2025

@dipeshmsft Great to see progress on this one, thanks :)

@dipeshmsft
Copy link
Member

@ThomasGoulet73 , I think we can go ahead and close the PR now as most of the other MilCodeGen PRs are already merged in main.

@ThomasGoulet73
Copy link
Contributor Author

@ThomasGoulet73 , I think we can go ahead and close the PR now as most of the other MilCodeGen PRs are already merged in main.

Yes, thanks a lot for reviewing all of this.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants