Windows 上编译 corefx 源码生成 Linux 上可用的 System.Data.SqlClient.dll

最近在排查一个奇怪的 EF Core 查询速度慢的问题,需要在 corefx 2.2.3 的 System.Data.SqlClient 源码中打点。

github 上签出 corefx 的源代码,运行 build.cmd 命令,然后用 VS2017 打开 System.Data.SqlClient.sln ,添加 Console.WriteLine 打点代码,用 VS 进行 build 。

build 之后 corefx 根路径下 bin\Windows_NT.AnyCPU.Debug\System.Data.SqlClient\netstandard 文件夹中会生成 System.Data.SqlClient.dll 文件,但这个 dll 在 Linux 上无法使用(错误如下)。

The type initializer for 'System.Data.SqlClient.SNILoadHandle' threw an exception. 
---> System.DllNotFoundException: Unable to load shared library 'sni.dll' or one of its dependencies. 
In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: 
libsni.dll: cannot open shared object file: No such file or directory
   at System.Data.SqlClient.SNINativeMethodWrapper.SNIInitialize(IntPtr pmo)

而 bin\AnyOS.AnyCPU.Debug 中没有生成 System.Data.SqlClient.dll 。

改用 build-managed.cmd 命令 build :

build-managed.cmd -Project=src\System.Data.SqlClient\src

出现下面的错误提示

error CS1069: The type name 'Console' could not be found in the namesp
ace 'System'. This type has been forwarded to assembly 'System.Console, Version=0.0.0.0, Culture=neutral, PublicKeyToke
n=b03f5f7f11d50a3a' Consider adding a reference to that assembly.

在 System.Data.SqlClient.csproj 中添加 System.Console 的引用后消除了上面的错误提示。

<ItemGroup Condition="'$(TargetGroup)' == 'netcoreapp'">
  <Reference Include="System.Console" />
  <Reference Include="System.Runtime.Extensions" />
  <Reference Include="System.Data.Common" />
  <Reference Include="System.ComponentModel.Primitives" />
  <Reference Include="System.Xml.ReaderWriter" />
  <Reference Include="System.Runtime.InteropServices" />
</ItemGroup>

但 build 结果与 Visual Stuido 一样,得不到 Linux 版的 System.Data.SqlClient.dll 。

到 Linux 机器上试了试,运行 ./build-managed.sh 命令后会在 bin/AnyOS.AnyCPU.Debug 文件夹中生成 System.Data.SqlClient.dll ,原来要得到 Linux 上可用的 System.Data.SqlClient.dll 需要 build 输出到 bin/AnyOS.AnyCPU.Debug 文件夹。

但 Linux 上 build 有个问题,每次修改代码后 build 总会出现下面的错误:

/root/corefx/Tools/sign.targets(113,5): error : /root/corefx/bin/obj/ref/Microsoft.CSharp/4.0.0.0/netstandard/Microsoft.CSharp.dll: PE file is already strong-name signed. [/root/corefx/src/Microsoft.CSharp/ref/Microsoft.CSharp.csproj]

需要运行 ./build-managed.sh -CleanAllProjects 命令才能消除这个错误。

继续回到 Windows 上,运行 build-managed.cmd -? 命令仔细看了一下命令参数,发现了 -OSGroup 参数

[-OSGroup]                        Sets the OS for the BuildConfigurtation you want to build.
                                  => Default value: ${OSName}
                                  => Legal values: [Windows_NT, Unix, Linux, OSX, FreeBSD, NetBSD].

于是使用下面的命令进行 build

build-managed.cmd -OSGroup=Linux

这样 build 后会在 bin 中多了 Linux.AnyCPU.Debug 与 Unix.AnyCPU.Debug 文件夹, Linux 上可用的 System.Data.SqlClient.dll 就在 Unix.AnyCPU.Debug 文件夹中。

posted @ 2019-04-13 17:27  dudu  阅读(851)  评论(0编辑  收藏  举报