Skip to content

Commit 2ea8ebb

Browse files
authored
Merging ref assemblies feature into master
2 parents 722f011 + 89ad231 commit 2ea8ebb

File tree

122 files changed

+4611
-676
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+4611
-676
lines changed

build/NuGetAdditionalFiles/Microsoft.Net.Compilers.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
AssemblyFile="$(MSBuildThisFileDirectory)..\tools\Microsoft.Build.Tasks.CodeAnalysis.dll" />
2424
<UsingTask TaskName="Microsoft.CodeAnalysis.BuildTasks.Vbc"
2525
AssemblyFile="$(MSBuildThisFileDirectory)..\tools\Microsoft.Build.Tasks.CodeAnalysis.dll" />
26+
<UsingTask TaskName="Microsoft.CodeAnalysis.BuildTasks.CopyRefAssembly"
27+
AssemblyFile="$(MSBuildThisFileDirectory)..\tools\Microsoft.Build.Tasks.CodeAnalysis.dll" />
2628
<PropertyGroup>
2729
<!-- By default don't use the compiler server in Visual Studio. -->
2830
<UseSharedCompilation Condition="'$(UseSharedCompilation)' == ''">false</UseSharedCompilation>

build/Targets/Versions.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
<!-- The release moniker for our packages. Developers should use "dev" and official builds pick the branch
1616
moniker listed below -->
1717
<RoslynNuGetMoniker Condition="'$(RoslynNuGetMoniker)' == ''">dev</RoslynNuGetMoniker>
18-
<RoslynNuGetMoniker Condition="'$(OfficialBuild)' == 'true'">beta2</RoslynNuGetMoniker>
18+
<!-- PROTOTYPE(refout) This needs to be reverted before merging to master -->
19+
<RoslynNuGetMoniker Condition="'$(OfficialBuild)' == 'true'">refout</RoslynNuGetMoniker>
1920
<!-- This is the base of the NuGet versioning for prerelease packages -->
2021
<NuGetPreReleaseVersion>$(RoslynFileVersionBase)-$(RoslynNuGetMoniker)</NuGetPreReleaseVersion>
2122

docs/compilers/CSharp/CommandLine.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| ---- | ---- |
55
| **OUTPUT FILES** |
66
| `/out:`*file* | Specify output file name (default: base name of file with main class or first file)
7+
| `/refout:`*file* | Specify the reference assembly's output file name
78
| `/target:exe` | Build a console executable (default) (Short form: `/t:exe`)
89
| `/target:winexe` | Build a Windows executable (Short form: `/t:winexe` )
910
| `/target:library` | Build a library (Short form: `/t:library`)
@@ -36,6 +37,7 @@
3637
| `/sourcelink`:*file* | [Source link](https://github.com/dotnet/core/blob/master/Documentation/diagnostics/source_link.md) info to embed into PDB.
3738
| `/optimize`{`+`&#124;`-`} | Enable optimizations (Short form: `/o`)
3839
| `/deterministic` | Produce a deterministic assembly (including module version GUID and timestamp)
40+
| `/refonly | Produce a reference assembly, instead of a full assembly, as the primary output
3941
| **ERRORS AND WARNINGS**
4042
| `/warnaserror`{`+`&#124;`-`} | Report all warnings as errors
4143
| `/warnaserror`{`+`&#124;`-`}`:`*warn list* | Report specific warnings as errors

docs/compilers/Visual Basic/CommandLine.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| ---- | ---- |
55
| **OUTPUT FILE**
66
| `/out:`*file* | Specifies the output file name.
7+
| `/refout:`*file* | Specify the reference assembly's output file name
78
| `/target:exe` | Create a console application (default). (Short form: `/t`)
89
| `/target:winexe` | Create a Windows application.
910
| `/target:library` | Create a library assembly.
@@ -39,6 +40,7 @@
3940
| `/debug:portable` | Emit debugging information in the portable format.
4041
| `/debug:pdbonly` | Emit PDB file only.
4142
| `/deterministic` | Produce a deterministic assembly (including module version GUID and timestamp)
43+
| `/refonly | Produce a reference assembly, instead of a full assembly, as the primary output
4244
| **ERRORS AND WARNINGS**
4345
| `/nowarn` | Disable all warnings.
4446
| `/nowarn:`*number_list* | Disable a list of individual warnings.

docs/features/refout.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Reference assemblies
2+
3+
Reference assemblies are metadata-only assemblies with the minimum amount of metadata to preserve the compile-time behavior of consumers (diagnostics may be affected, though).
4+
The compiler may choose to remove more metadata in later versions, if it is determined to be safe (ie. respects the principle above).
5+
6+
## Scenarios
7+
There are 4 scenarios:
8+
9+
1. The traditional one, where an assembly is emitted as the primary output (`/out` command-line parameter, or `peStream` parameter in `Compilation.Emit` APIs).
10+
2. The IDE scenario, where the metadata-only assembly is emitted (via `Emit` API), still as the primary output. Later on, the IDE is interested to get metadata-only assemblies even when there are errors in the compilation.
11+
3. The CoreFX scenario, where only the ref assembly is emitted, still as the primary output (`/refonly` command-line parameter)
12+
4. The MSBuild scenario, which is the new scenario, where both a real assembly is emitted as the primary output, and a ref assembly is emitted as the secondary output (`/refout` command-line parameter, or `metadataPeStream` parameter in `Emit`).
13+
14+
15+
## Definition of ref assemblies
16+
Metadata-only assembly have their method bodies replaced with a single `throw null` body, but include all members except anonymous types. The reason for using `throw null` bodies (as opposed to no bodies) is so that PEVerify could run and pass (thus validating the completeness of the metadata).
17+
Ref assemblies will include an assembly-level `ReferenceAssembly` attribute. This attribute may be specified in source (then we won't need to synthesize it). Because of this attribute, runtimes will refuse to load ref assemblies for execution (but they can still be loaded Reflection-only mode).
18+
Ref assemblies further remove metadata (private members) from metadata-only assemblies:
19+
20+
- A ref assembly will only have references for what it needs in the API surface. The real assembly may have additional references related to specific implementations. For instance, the ref assembly for `class C { private void M() { dynamic d = 1; ... } }` will not reference any types required for `dynamic`.
21+
- Private function-members (methods, properties and events) will be removed. If there are no `InternalsVisibleTo` attributes, do the same for internal function-members
22+
- But all types (including private or nested types) must be kept in ref assemblies. All attributes must be kept (even internal ones).
23+
- All virtual methods will be kept. Explicit interface implementations will be kept.
24+
- All fields of a struct will be kept. (This is a candidate for post-C#-7.1 refinement)
25+
26+
## API changes
27+
28+
### Command-line
29+
Two mutually exclusive command-line parameters will be added to `csc.exe` and `vbc.exe`:
30+
- `/refout`
31+
- `/refonly`
32+
33+
The `/refout` parameter specifies a file path where the ref assembly should be output. This translates to `metadataPeStream` in the `Emit` API (see details below). The filename for the ref assembly should generally match that of the primary assembly (we will warn when they don't match). The recommended convention (used by MSBuild) is to place the ref assembly in a "ref/" sub-folder relative to the primary assembly.
34+
35+
The `/refonly` parameter is a flag that indicates that a ref assembly should be output instead of an implementation assembly, as the primary output.
36+
The `/refonly` parameter is not allowed together with the `/refout` parameter, as it doesn't make sense to have both the primary and secondary outputs be ref assemblies. Also, the `/refonly` parameter silently disables outputting PDBs, as ref assemblies cannot be executed.
37+
The `/refonly` parameter translates to `EmitMetadataOnly` being `true`, and `IncludePrivateMembers` being `false` in the `Emit` API (see details below).
38+
Neither `/refonly` nor `/refout` are permitted with net modules (`/target:module`, `/addmodule` options).
39+
40+
The compilation from the command-line will either produce both assemblies (implementation and ref) or neither. There is no "partial success" scenario.
41+
When the compiler produces documentation, it is un-affected by either the `/refonly` or `/refout` parameters. This may change in the future.
42+
The main purpose of the `/refout` option is to speed up incremental build scenarios. The current implementation for this flag can produce a ref assembly with more metadata than `/refonly` (for instance, anonymous types). This is a candidate for post-C#-7.1 refinement.
43+
44+
### CscTask/CoreCompile
45+
The `CoreCompile` target will support a new output, called `IntermediateRefAssembly`, which parallels the existing `IntermediateAssembly`.
46+
The `Csc` task will support a new output, called `OutputRefAssembly`, which parallels the existing `OutputAssembly`.
47+
Both of those basically map to the `/refout` command-line parameter.
48+
49+
An additional task, called `CopyRefAssembly`, will be provided along with the existing `Csc` task. It takes a `SourcePath` and a `DestinationPath` and generally copies the file from the source over to the destination. But if it can determine that the contents of those two files match (by comparing their MVIDs, see details below), then the destination file is left untouched.
50+
51+
As a side-note, `CopyRefAssembly` uses the same assembly resolution/redirection trick as `Csc` and `Vbc`, to avoid type loading problems with `System.IO.FileSystem`.
52+
53+
### CodeAnalysis APIs
54+
It is already possible to produce metadata-only assemblies by using `EmitOptions.EmitMetadataOnly`, which is used in IDE scenarios with cross-language dependencies.
55+
The compiler will be updated to honour the `EmitOptions.IncludePrivateMembers` flag as well. When combined with `EmitMetadataOnly` or a `metadataPeStream` in `Emit`, a ref assembly will be produced.
56+
The diagnostic check for emitting methods lacking a body (`void M();`) will be filtered from declaration diagnostics, so that code will successfully emit with `EmitMetadataOnly`.
57+
Later on, the `EmitOptions.TolerateErrors` flag will allow emitting error types as well.
58+
`Emit` is also modified to produce a new PE section called ".mvid" containing a copy of the MVID, when producing ref assemblies. This makes it easy for `CopyRefAssembly` to extract and compare MVIDs from ref assemblies.
59+
60+
Going back to the 4 driving scenarios:
61+
1. For a regular compilation, `EmitMetadataOnly` is left to `false` and no `metadataPeStream` is passed into `Emit`.
62+
2. For the IDE scenario, `EmitMetadataOnly` is set to `true`, but `IncludePrivateMembers` is left to `true`.
63+
3. For the CoreFX scenario, ref assembly source code is used as input, `EmitMetadataOnly` is set to `true`, and `IncludePrivateMembers` is set to `false`.
64+
4. For the MSBuild scenario, `EmitMetadataOnly` is left to `false`, a `metadataPeStream` is passed in and `IncludePrivateMembers` is set to `false`.
65+
66+
## Future
67+
As mentioned above, there may be further refinements after C# 7.1:
68+
- Further reduce the metadata in ref assemblies produced by `/refout`, to match those produced by `/refonly`.
69+
- Controlling internals (producing public ref assemblies)
70+
- Produce ref assemblies even when there are errors outside method bodies (emitting error types when `EmitOptions.TolerateErrors` is set)
71+
- When the compiler produces documentation, the contents produced could be filtered down to match the APIs that go into the primary output. In other words, the documentation could be filtered down when using the `/refonly` parameter.
72+
73+
## Open questions
74+
75+
## Related issues
76+
- Produce ref assemblies from command-line and msbuild (https://github.com/dotnet/roslyn/issues/2184)
77+
- Refine what is in reference assemblies and what diagnostics prevent generating one (https://github.com/dotnet/roslyn/issues/17612)
78+
- [Are private members part of the API surface?](http://blog.paranoidcoding.com/2016/02/15/are-private-members-api-surface.html)

src/Compilers/CSharp/Portable/CSharpCodeAnalysis.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@
272272
<Compile Include="Emitter\EditAndContinue\EmitHelpers.cs" />
273273
<Compile Include="Emitter\EditAndContinue\PEDeltaAssemblyBuilder.cs" />
274274
<Compile Include="Emitter\EditAndContinue\CSharpSymbolMatcher.cs" />
275+
<Compile Include="Emitter\Model\SourceAssemblySymbolAdapter.cs" />
275276
<Compile Include="Emitter\Model\ArrayTypeSymbolAdapter.cs" />
276277
<Compile Include="Emitter\Model\AssemblyReference.cs" />
277278
<Compile Include="Emitter\Model\AttributeDataAdapter.cs" />

src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/CSharp/Portable/CSharpResources.resx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,6 +2297,12 @@ If such a class is used as a base class and if the deriving class defines a dest
22972297
<data name="ERR_Merge_conflict_marker_encountered" xml:space="preserve">
22982298
<value>Merge conflict marker encountered</value>
22992299
</data>
2300+
<data name="ERR_NoRefOutWhenRefOnly" xml:space="preserve">
2301+
<value>Do not use refout when using refonly.</value>
2302+
</data>
2303+
<data name="ERR_NoNetModuleOutputWhenRefOutOrRefOnly" xml:space="preserve">
2304+
<value>Cannot compile net modules when using /refout or /refonly.</value>
2305+
</data>
23002306
<data name="ERR_OvlOperatorExpected" xml:space="preserve">
23012307
<value>Overloadable operator expected</value>
23022308
</data>
@@ -4402,6 +4408,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
44024408
/target:winmdobj Build a Windows Runtime intermediate file that
44034409
is consumed by WinMDExp (Short form: /t:winmdobj)
44044410
/doc:&lt;file&gt; XML Documentation file to generate
4411+
/refout:&lt;file&gt; Reference assembly output to generate
44054412
/platform:&lt;string&gt; Limit which platforms this code can run on: x86,
44064413
Itanium, x64, arm, anycpu32bitpreferred, or
44074414
anycpu. The default is anycpu.
@@ -4445,6 +4452,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
44454452
/optimize[+|-] Enable optimizations (Short form: /o)
44464453
/deterministic Produce a deterministic assembly
44474454
(including module version GUID and timestamp)
4455+
/refonly Produce a reference assembly in place of the main output
44484456
/instrument:TestCoverage Produce an assembly instrumented to collect
44494457
coverage information
44504458
/sourcelink:&lt;file&gt; Source link info to embed into PDB.

src/Compilers/CSharp/Portable/CommandLine/CSharpCommandLineParser.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
6767
string outputDirectory = baseDirectory;
6868
ImmutableArray<KeyValuePair<string, string>> pathMap = ImmutableArray<KeyValuePair<string, string>>.Empty;
6969
string outputFileName = null;
70+
string outputRefFilePath = null;
71+
bool refOnly = false;
7072
string documentationPath = null;
7173
string errorLogPath = null;
7274
bool parseDocumentationComments = false; //Don't just null check documentationFileName because we want to do this even if the file name is invalid.
@@ -381,6 +383,7 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
381383
}
382384

383385
continue;
386+
384387
case "out":
385388
if (string.IsNullOrWhiteSpace(value))
386389
{
@@ -393,6 +396,26 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
393396

394397
continue;
395398

399+
case "refout":
400+
value = RemoveQuotesAndSlashes(value);
401+
if (string.IsNullOrEmpty(value))
402+
{
403+
AddDiagnostic(diagnostics, ErrorCode.ERR_NoFileSpec, arg);
404+
}
405+
else
406+
{
407+
outputRefFilePath = ParseGenericPathToFile(value, diagnostics, baseDirectory);
408+
}
409+
410+
continue;
411+
412+
case "refonly":
413+
if (value != null)
414+
break;
415+
416+
refOnly = true;
417+
continue;
418+
396419
case "t":
397420
case "target":
398421
if (value == null)
@@ -1150,6 +1173,16 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
11501173
diagnosticOptions[o.Key] = o.Value;
11511174
}
11521175

1176+
if (refOnly && outputRefFilePath != null)
1177+
{
1178+
AddDiagnostic(diagnostics, diagnosticOptions, ErrorCode.ERR_NoRefOutWhenRefOnly);
1179+
}
1180+
1181+
if (outputKind == OutputKind.NetModule && (refOnly || outputRefFilePath != null))
1182+
{
1183+
AddDiagnostic(diagnostics, diagnosticOptions, ErrorCode.ERR_NoNetModuleOutputWhenRefOutOrRefOnly);
1184+
}
1185+
11531186
if (!IsScriptRunner && !sourceFilesSpecified && (outputKind.IsNetModule() || !resourcesOrModulesSpecified))
11541187
{
11551188
AddDiagnostic(diagnostics, diagnosticOptions, ErrorCode.WRN_NoSources);
@@ -1261,7 +1294,8 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
12611294

12621295
var emitOptions = new EmitOptions
12631296
(
1264-
metadataOnly: false,
1297+
metadataOnly: refOnly,
1298+
includePrivateMembers: !refOnly && outputRefFilePath == null,
12651299
debugInformationFormat: debugInformationFormat,
12661300
pdbFilePath: null, // to be determined later
12671301
outputNameOverride: null, // to be determined later
@@ -1287,8 +1321,9 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
12871321
Utf8Output = utf8output,
12881322
CompilationName = compilationName,
12891323
OutputFileName = outputFileName,
1324+
OutputRefFilePath = outputRefFilePath,
12901325
PdbPath = pdbPath,
1291-
EmitPdb = emitPdb,
1326+
EmitPdb = emitPdb && !refOnly, // silently ignore emitPdb when refOnly is set
12921327
SourceLink = sourceLink,
12931328
RuleSetPath = ruleSetPath,
12941329
OutputDirectory = outputDirectory,

0 commit comments

Comments
 (0)