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

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

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

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

背景:最近接手一个华为某型号的嵌入式设备,需要在上面搭建 .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


方法一:给你的服务器FQ

方法二:下载我上传的包

链接: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月前
|
消息中间件 前端开发 小程序
一个基于.NET Core构建的简单、跨平台、模块化的商城系统
今天大姚给大家分享一个基于.NET Core构建的简单、跨平台、模块化、完全开源免费(MIT License)的商城系统:Module Shop。
|
4月前
|
开发框架 JSON JavaScript
ASP.NET Core3.1实战教程---基于Jquery单文件上传
ASP.NET Core3.1实战教程---基于Jquery单文件上传
27 0
|
7月前
|
Go
Go 使用标准库 net/http 包构建服务器
Go 使用标准库 net/http 包构建服务器
27 0
|
16天前
|
开发框架 缓存 前端开发
利用Visual Basic构建高效的ASP.NET Web应用
【4月更文挑战第27天】本文探讨使用Visual Basic与ASP.NET创建高效Web应用的策略,包括了解两者基础、项目规划、MVC架构、数据访问与缓存、代码优化、异步编程、安全性、测试及部署维护。通过这些步骤,开发者能构建出快速、可靠且安全的Web应用,适应不断进步的技术环境。
|
14天前
|
机器学习/深度学习 自然语言处理 安全
【专栏】.NET 开发:构建智能应用的关键
【4月更文挑战第29天】本文探讨了.NET开发在构建智能应用中的关键作用,强调了其强大的框架、工具集、高效性能和跨平台支持。通过实例展示了.NET在人工智能、物联网及企业级应用中的应用。同时,指出了.NET开发面临的挑战,如技术更新的学习成本、性能优化、资源管理和安全隐私保护,并提出了应对策略。随着技术进步,.NET将在智能应用领域发挥更大作用,推动创新与便利。
|
15天前
|
中间件 Go API
Golang深入浅出之-Go语言标准库net/http:构建Web服务器
【4月更文挑战第25天】Go语言的`net/http`包是构建高性能Web服务器的核心,提供创建服务器和发起请求的功能。本文讨论了使用中的常见问题和解决方案,包括:使用第三方路由库改进路由设计、引入中间件处理通用逻辑、设置合适的超时和连接管理以防止资源泄露。通过基础服务器和中间件的代码示例,展示了如何有效运用`net/http`包。掌握这些最佳实践,有助于开发出高效、易维护的Web服务。
28 1
|
2月前
|
开发框架 .NET 物联网
.NET从入门到精通,零基础也能搞定的基础知识教程
.NET从入门到精通,零基础也能搞定的基础知识教程
26 0
|
9月前
|
存储 开发框架 .NET
ASP.NET Core SignalR系列之Hub教程
ASP.NET Core SignalR系列之Hub教程
119 0
|
5月前
|
应用服务中间件 nginx
Angular打包构建项目服务器运行runtime.js、polyfills.js、vendor.js报错net::ERR_ABORTED 404 (Not Found),build修改为相对路径./
Angular打包构建项目服务器运行runtime.js、polyfills.js、vendor.js报错net::ERR_ABORTED 404 (Not Found),build修改为相对路径./
|
5月前
|
定位技术 C#
.NET微信网页开发相关文章教程
.NET微信网页开发相关文章教程