@@ -81,6 +81,19 @@ protected override void GenerateBuildScript(BuildPartition buildPartition, Artif
8181
8282 [ SuppressMessage ( "ReSharper" , "StringLiteralTypo" ) ] // R# complains about $variables$
8383 protected override void GenerateProject ( BuildPartition buildPartition , ArtifactsPaths artifactsPaths , ILogger logger )
84+ {
85+ File . WriteAllText ( artifactsPaths . ProjectFilePath ,
86+ GenerateProject ( "CsProj.txt" , buildPartition , artifactsPaths , logger )
87+ ) ;
88+
89+ // Integration tests are built without dependencies, so we skip gathering dlls.
90+ if ( ! buildPartition . ForcedNoDependenciesForIntegrationTests )
91+ {
92+ GatherReferences ( buildPartition , artifactsPaths , logger ) ;
93+ }
94+ }
95+
96+ private string GenerateProject ( string template , BuildPartition buildPartition , ArtifactsPaths artifactsPaths , ILogger logger )
8497 {
8598 var benchmark = buildPartition . RepresentativeBenchmarkCase ;
8699 var projectFile = GetProjectFilePath ( benchmark . Descriptor . Type , logger ) ;
@@ -89,7 +102,7 @@ protected override void GenerateProject(BuildPartition buildPartition, Artifacts
89102 xmlDoc . Load ( projectFile . FullName ) ;
90103 var ( customProperties , sdkName ) = GetSettingsThatNeedToBeCopied ( xmlDoc , projectFile ) ;
91104
92- var content = new StringBuilder ( ResourceHelper . LoadTemplate ( "CsProj.txt" ) )
105+ return new StringBuilder ( ResourceHelper . LoadTemplate ( template ) )
93106 . Replace ( "$PLATFORM$" , buildPartition . Platform . ToConfig ( ) )
94107 . Replace ( "$CODEFILENAME$" , Path . GetFileName ( artifactsPaths . ProgramCodePath ) )
95108 . Replace ( "$CSPROJPATH$" , projectFile . FullName )
@@ -99,33 +112,26 @@ protected override void GenerateProject(BuildPartition buildPartition, Artifacts
99112 . Replace ( "$COPIEDSETTINGS$" , customProperties )
100113 . Replace ( "$SDKNAME$" , sdkName )
101114 . ToString ( ) ;
102-
103- File . WriteAllText ( artifactsPaths . ProjectFilePath , content ) ;
104-
105- // Integration tests are built without dependencies, so we skip gathering dlls.
106- if ( ! buildPartition . ForcedNoDependenciesForIntegrationTests )
107- {
108- GatherReferences ( buildPartition , artifactsPaths , logger ) ;
109- }
110115 }
111116
112117 private static string GetDllGathererPath ( string filePath )
113118 => Path . Combine ( Path . GetDirectoryName ( filePath ) , $ "DllGatherer{ Path . GetExtension ( filePath ) } ") ;
114119
115120 protected void GatherReferences ( BuildPartition buildPartition , ArtifactsPaths artifactsPaths , ILogger logger )
116121 {
117- // Copy csproj template without the generated C# file to build the original project for all necessary runtime dlls.
122+ // Create a project using the default template to build the original project for all necessary runtime dlls.
118123 // We can't just build the original project directly because it could be a library project, so we need an exe project to reference it.
124+ string gathererProject = GetDllGathererPath ( artifactsPaths . ProjectFilePath ) ;
119125 var xmlDoc = new XmlDocument ( ) ;
120- xmlDoc . Load ( artifactsPaths . ProjectFilePath ) ;
126+ xmlDoc . LoadXml ( GenerateProject ( "CsProj.txt" , buildPartition , artifactsPaths , logger ) ) ;
121127 var projectElement = xmlDoc . DocumentElement ;
128+
129+ // Replace the default C# file with an empty Main method to satisfy the exe build.
122130 var compileNode = projectElement . SelectSingleNode ( "ItemGroup/Compile" ) ;
123131 string emptyMainFile = GetDllGathererPath ( artifactsPaths . ProgramCodePath ) ;
124132 compileNode . Attributes [ "Include" ] . Value = emptyMainFile ;
125- string gathererProject = GetDllGathererPath ( artifactsPaths . ProjectFilePath ) ;
126133 xmlDoc . Save ( gathererProject ) ;
127134
128- // Generate a C# file with an empty Main method to satisfy the exe build.
129135 File . WriteAllText ( emptyMainFile , """
130136 namespace BenchmarkDotNet.Autogenerated
131137 {
0 commit comments