简体   繁体   中英

How can I compile a .NET Standard 2.0 class library by directly invoking the C# complier using only the .NET Core SDK?

When compiling a .NET Standard 2.0 class library project in Visual Studio 2019, the C# compiler ( csc.exe ) gets invoked to compile the project. If I examine the build log, the command line used looks something like this (line breaks added for readability):

C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\csc.exe
/noconfig
/unsafe-
/checked-
/nowarn:1701,1702,1701,1702,2008
/nostdlib+
/errorreport:prompt
/warn:4
/define:TRACE;DEBUG;NETSTANDARD;NETSTANDARD2_0
/errorendlocation
/preferreduilang:en-US

<
A whole slew of /reference: switches that point to:
    %PROGRAMFILES%\dotnet\sdk\NuGetFallbackFolder\netstandard.library
    \2.0.3\build\netstandard2.0\ref
>

/debug+
/debug:portable
/filealign:512
/optimize-
/out:obj\Debug\netstandard2.0\ClassLibrary1.dll
/target:library
/warnaserror-
/utf8output
/deterministic+
Class1.cs
/warnaserror+:NU1605

Now, assume that I am on a machine without any version of Visual Studio. Further assume that I have the .NET Core SDK unzipped into a directory on that machine. How can I recreate the above list of /reference: switches with the proper list of referenced assemblies? Where is the magic manifest that I can read that will let me assemble that list?

The .csproj from which the above command line was generated has the following contents:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

</Project>

As one can see, there are no assembly reference tags in the project itself, so the list of references appears to be just the list of files located in the aforementioned directory. What I want to know is how that list of files was assembled to be placed in that directory for VS, and how to mimic it using only the .NET Core SDK.

Please note that there are technical reasons I need to invoke the C# compiler directly; using MSBuild in any way is not an acceptable solution to this particular problem. This means that using dotnet build against a .csproj file is not an option for this particular project.

In .NET Core, dependencies are either projects or NuGet packages. There are a couple of NuGet packages which are referenced implicitly. For example, if your target netstandard2.0 , you will reference version 1.6.1 of the NETStandard.Library .

All these rules are encoded in the dotnet/sdk repository. This repository contains code which is shared across the .NET CLI and Visual Studio, because both products require inimate knowledge about the .NET Core build process. For example, the dependency on NETStandard.Library 1.6.1 is encoded in Microsoft.NET.Sdk.DefaultItems.targets

The magic command you are looking for is dotnet restore . This will resolve all references (NuGet and project) and create files such as obj\\projects.assets.json , which lists, all compile time assembly references for your project. There are other files, such as {projectname}.nuget.* , which contain related information.

So for Bazel, there are a couple of paths you could take:

  • Run dotnet restore , and parse the output of project.assets.json .
  • Try consume the logic in dotnet/sdk from Bazel, just like Visual Studio, MSBuild and the .NET CLI do. This will be hard, since dotnet/sdk is written in C# and MSBuild, and Bazel probably has no notion of that.
  • Try to duplicate the logic in dotnet/sdk in Bazel. This will be an sisyphean task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM