瞎折腾实录:构建 Armel 版本的 .NET Core 教程和资料资源

内容纲要

[TOC]

首先我要说明,我失败了~

我把我的进度和经验放出来,希望能够帮助别人完成编译工作~

背景:最近接手一个华为某型号的嵌入式设备,需要在上面搭建 .NET Core 环境。

设备是 Armel 架构的,Linux 内核 3.10;.NET Core ARM 只有 Armhf。

因此编译出来的二进制文件无法在此设备下运行。

然后想尝试在 Git 上下载源码,手动编译出 Armel 版本的 .NET Core SDK/Runtime。

感谢张队提供了大量的参考资料。

一,工作开始前

.NET Core SDK/Runtime 并不是只有一个 Git 库,而是多个:

  • CoreCLR 最小化的运行时
  • CoreFX 类库和框架功能
  • CLI .NET SDK 核心
  • Core-SetUp 被分割为 CoreCLR、CoreFX

Core-SetUp 最终生成的是 https://dotnet.microsoft.com/download#core

在编译工作开始前,需要确认要编译的库。

源码地址:

https://github.com/dotnet/coreclr/

https://github.com/dotnet/corecfx/

https://github.com/dotnet/cli/

你需要一台 Linux x86/x64 的服务器,内存大于 1G,使用 Ubuntu/Debian 系统。

可以安装的便利工具:

apt install lrzsz 一个用于跨终端传输文件的工具,如果使用xshell远程终端,可以直接通过拖拉文件上传到Linux服务器中。

apt install tree 以树的形式显示目录下的文件。

二,编译工具组件

安装必备工具链

  • cmake
  • llvm-3.9
  • clang-3.9
  • lldb-3.9
  • liblldb-3.9-dev
  • libunwind8
  • libunwind8-dev
  • gettext
  • libicu-dev
  • liblttng-ust-dev
  • libcurl4-openssl-dev
  • libssl-dev
  • libkrb5-dev
  • libnuma-dev (optional, enables numa support)

一键安装命令:

sudo apt-get install cmake llvm-3.9 clang-3.9 lldb-3.9 liblldb-3.9-dev libunwind8 libunwind8-dev gettext libicu-dev liblttng-ust-dev libcurl4-openssl-dev libssl-dev libnuma-dev libkrb5-dev

克隆 CoreCLR 仓库

git clone https://github.com/dotnet/coreclr.git

cross 文件夹可以看到:

[root@instance-wxxixh4k cross]# tree -L 1
.
├── android
├── arm
├── arm64
├── armel
├── build-android-rootfs.sh
├── build-rootfs.sh
├── toolchain.cmake
├── tryrun.cmake
└── x86
编译前配置检查

①文件句柄最大数量

查看支持的文件句柄最大数量

sysctl fs.file-max

如果显示小于100000,那么要设置超过这个数字,例如

#打开文件
nano /etc/sysctl.conf
#添加一行
fs.file-max = 100000

然后

sudo sysctl -p

②内存要大于 1G

如果服务器内存不足 1G ,是无法开始编译的,因为脚本限制必须大于 1G 内存才可以进行编译。

还好,工良找到了一个办法。

创建交换分区

sudo dd if=/dev/zero of=/swapfile bs=64M count=16
sudo mkswap /swapfile
sudo swapon /swapfile

bs:每个分区大小,count:分区个数。

在编译完成后,记得删除交换分区:

sudo swapoff /swapfile
sudo rm /swapfile

三,编译 MUSL

安装需要的库

sudo apt-get install qemu qemu-user-static binfmt-support debootstrap
sudo apt-get install binutils-arm-linux-gnueabihf

下载 Git 库

git clone https://github.com/richfelker/musl-cross-make.git

打开 MUSL 库的目录,创建 config.mak 文件

touch config.mak

在里面添加如下内容

TARGET = armv7-alpine-linux-musleabihel
OUTPUT = /usr
BINUTILS_CONFIG=--enable-gold=yes

关于 config.mak 文件,第一行的格式 arm[eb]-linux-musleabi[hf]

CPU 架构/指令集版本可以通过 cat /proc/cpuinfo 命令获取。

四,构建 Armel 的 Rootfs

sudo ./cross/build-rootfs.sh armel 
#或者
sudo ./cross/build-rootfs.sh armel tizen

编译完毕,可以在 ./coreclr/cross/rootfs/下看到相应版本的 rootfs。

使用 第一种命令,虽然出现相应的 rootfs,但是最终出现了问题,另外我无法 使用 sudo ./cross/build-rootfs.sh armel tizen 命令。

问题:

umount: /var/test/0828/coreclr/cross/rootfs/armel/bin: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/boot: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/dev: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/etc: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/home: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/lib: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/media: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/mnt: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/opt: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/proc: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/root: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/run: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/sbin: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/srv: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/sys: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/tmp: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/usr: not mounted.
umount: /var/test/0828/coreclr/cross/rootfs/armel/var: not mounted.

但是既没有出现 Error、faild,也没有出现异常提示,不确定是否失败。查看目录,发现 Rootfts 是完整的,没毛病呀。

五,交叉编译 CoreCLR

./build.sh armel debug verbose cross
#或者 
./build.sh armel release verbose cross

示例:

root@ubuntu:/var/netcore/0828/coreclr# ./build.sh armel debug verbose cross
Commencing CoreCLR Repo build
__DistroRid: debian.8-armel
__RuntimeId: debian.8-armel
Setting up directories for build
Checking prerequisites...

执行命令后,会出现 Checking prerequisites... (检查先决条件中),说明离成功近了一步。

当然,可能会报这个错误:

dotnet_install: Error: Could not find/download: .NET Core SDK with version = 3.0.100-preview6-012264
dotnet_install: Error: Refer to: https://aka.ms/dotnet-os-lifecycle for information on .NET Core support
Failed to install dotnet SDK (exit code '1').
Failed to get PGO data package version.

原因是无法下载 .NET Core SDK。

打开 /tmp 目录,可以看到很多下载 SDK失败的缓存,因为是下载源在国外,国内下载速度可能只有 几kb。

而要下载的文件大小 120MB 左右,下载时间长达数个小时,可能会出现中断或超时,因此会失败。

root@ubuntu:/tmp# ls -lah
total 90M
drwxrwxrwt 10 root    root    4.0K Aug 28 01:48 .
drwxr-xr-x 24 root    root    4.0K Aug 27 09:37 ..
-rw-rw----  1 netdata netdata  240 Aug 20 05:28 as.log
-rw-------  1 root    root     34M Aug 27 06:30 dotnet.9TuQ60VOK
-rw-------  1 root    root    3.2M Aug 28 01:18 dotnet.a3WI49nPx
-rw-------  1 root    root       0 Aug 27 03:30 dotnet.CxR2scA4h
-rw-------  1 root    root       0 Aug 27 01:38 dotnet.DuAMYKCT4
-rw-------  1 root    root     16K Aug 27 01:38 dotnet.gEYeUSXYy
-rw-------  1 root    root       0 Aug 28 00:06 dotnet.HWdDnC5Hl
-rw-------  1 root    root       0 Aug 27 10:03 dotnet.hX7K3ac76
-rw-------  1 root    root       0 Aug 28 00:08 dotnet.ijQuG82B1
-rw-------  1 root    root     34M Aug 28 01:47 dotnet.ImT2CM9gt
-rw-------  1 root    root    3.9M Aug 27 10:03 dotnet.KnQlzjA4g
-rw-------  1 root    root    1.4M Aug 28 01:49 dotnet.o3nnapWHT
-rw-------  1 root    root     14M Aug 27 03:30 dotnet.XKVdUNje0
-rw-------  1 root    root       0 Aug 28 01:47 dotnet.xVEb3C7Bl

方法一:给你的服务器翻墙

方法二:下载我上传的包

链接:https://eyun.baidu.com/s/3dGnzGC5 密码:GWuV

把下载的包,放到 ./coreclr/.dotnet/ 里面解压。

再重新执行 ./build.sh armel debug verbose cross

构建成功?

若是成功,会出现提示。

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.64
/var/test/0828/coreclr/bin/obj/Linux.armel.Debug /var/test/0828/coreclr
Invoking "/var/test/0828/coreclr/src/pal/tools/gen-buildsys-clang.sh" "/var/test/0828/coreclr" 5 "0" armel /var/test/0828/coreclr/src/pal/tools Debug    -DCLR_CMAKE_TARGET_OS=Linux -DCLR_CMAKE_PACKAGES_DIR=/var/test/0828/coreclr/.packages -DCLR_CMAKE_PGO_INSTRUMENT=0 -DCLR_CMAKE_OPTDATA_VERSION=99.99.99-master-20190716.1 -DCLR_CMAKE_PGO_OPTIMIZE=1 
Unable to locate llvm-ar

虽然提示成功,但是高兴得太早。

官方文档说:

As usual, the resulting binaries will be found inbin/Product/BuildOS.BuildArch.BuildType/

意思是,构建成功的话, 在 bin/Product/目录下,会出现 [系统类型].[架构版本].[构建版本] 的文件夹。

打开目录 ./bin/Product

drwxr-xr-x 3 root root 4.0K Aug 28 01:12 Linux.armel.Debug
drwxr-xr-x 2 root root 4.0K Aug 28 00:06 Linux.x64.Debug

嗯,的确出现了!

但是…

Linux.armel.Debug 下没有文件,打开 ./coreclr/cross/rootfs/ armel 目录,看看里面的文件。

root@ubuntu:/var/test/0828/coreclr/bin/Product# tree -L 3
.
├── Linux.armel.Debug
│   └── x64
├── Linux.armel.Release
│   └── x64
├── Linux.x64.Debug
│   ├── coreconsole
│   ├── corerun
│   ├── createdump
│   ├── crossgen
│   ├── gcinfo
│   │   └── gcinfoencoder.cpp
│   ├── IL
│   │   ├── System.Private.CoreLib.deps.json
│   │   ├── System.Private.CoreLib.dll
│   │   └── System.Private.CoreLib.xml
... ...

参考 x64,编译成功目录下会出现这些文件,而 armel 下面,没有文件。。。

所以,最终确定失败。

因为时间问题和复杂程度,工良已经放弃了治疗… …

六,参考资料

需要使用到的 Git 库或文件,我已上传到网盘

链接:https://eyun.baidu.com/s/3qZVI8sk 密码:u5Xd

Enabling Linux ARM32 for .NET Core(推荐):https://www.bountysource.com/issues/39759217-enabling-linux-arm32-for-net-core

Build .NET Core from source:https://docs.microsoft.com/en-us/dotnet/core/build/

CoreCLR 构建:https://github.com/dotnet/coreclr/blob/master/Documentation/building/linux-instructions.md#Environment

CoreCLR交叉编译:https://github.com/dotnet/coreclr/blob/master/Documentation/building/cross-building.md

官方已经收录的 .NET Core SDK/Runtime 版本:https://github.com/dotnet/core-setup

MUSL编译:https://github.com/richfelker/musl-cross-make

三个很有帮助的 Issue

Enabling Linux ARM32 for .NET Core:https://github.com/dotnet/core-setup/issues/725

Can’t build 2.0.0 for armel (debian rootfs) :https://github.com/dotnet/core-setup/issues/3100

Enabling Linux ARM32 for SDK:https://github.com/dotnet/cli/issues/5289

点赞
  1. I simply wished to thank you very much yet again. I do not know what I would have worked on without those tricks shown by you relating to this topic. This was the troublesome situation in my view, nevertheless witnessing your well-written way you treated the issue took me to weep with joy. Now i am thankful for your work as well as wish you recognize what an amazing job your are undertaking training the rest with the aid of your websites. Probably you haven't met any of us.

  2. cialis no rx说道:

    I enjoy you because of all of the effort on this blog. Debby delights in carrying out internet research and it's easy to see why. A number of us hear all about the compelling form you convey both useful and interesting guidelines by means of the blog and boost contribution from other individuals about this area of interest and my child has been becoming educated a great deal. Enjoy the remaining portion of the new year. You're carrying out a dazzling job.

  3. cheap viagra说道:

    I enjoy you because of all of your effort on this blog. Ellie enjoys doing internet research and it's easy to see why. All of us hear all about the compelling manner you create functional ideas by means of the blog and as well cause contribution from other ones about this article and my child is being taught a great deal. Enjoy the remaining portion of the new year. You're the one carrying out a first class job.

发表评论

邮箱地址不会被公开。 必填项已用*标注

You must enable javascript to see captcha here!