3

编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!

 1 year ago
source link: https://www.51cto.com/article/719514.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!

作者:离北况归 2022-09-23 17:17:33
WSL2使用最新、最强大的虚拟化技术在轻量级实用工具虚拟机 (VM) 中运行 Linux 内核。 但是,WSL2不是传统的 VM 体验。
12f479186f9971b650864441d51d9e676e850b.png

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

笔者发现使用wsl(Windows Subsystem for Linux,适用于 Linux 的 Windows 子系统)编译openharmony镜像比传统虚拟机快3倍以上。编译环境换成wsl后,编译ohos只需要传统虚拟机上三分之一的时间。下面分享使用经验如下:

为什么使用WSL2比传统虚拟机编译OpenHarmony快?

以下是微软官方给的解释:https://learn.microsoft.com/zh-cn/windows/wsl/about。

  • Windows Subsystem for Linux(适用于 Linux 的 Windows 子系统)可让开发人员按原样运行 GNU/Linux 环境 - 包括大多数命令行工具、实用工具和应用程序 - 且不会产生传统虚拟机或双启动设置开销
  • WSL2使用最新、最强大的虚拟化技术在轻量级实用工具虚拟机 (VM) 中运行 Linux 内核。 但是,WSL2不是传统的 VM 体验。
  • 传统的 VM 体验可能启动速度慢,是独立的,消耗大量资源,需要你花费时间进行管理。 WSL 2 没有这些属性。

以下是笔者的感受:

  • 具体点说就是编译openharmony时cpu的利用率可以达到90%以上。而且编译直接占用的是电脑的内存,而不是给传统虚拟机上分配的内存。
#打卡不停更#编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!!-开源基础软件社区
#打卡不停更#编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!!-开源基础软件社区
  • 而传统虚拟机还要设置虚拟机参数,性能是受到制约的。
#打卡不停更#编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!!-开源基础软件社区

配置WSL环境

检查运行 WSL 2 的要求

若要更新到 WSL 2,需要运行 Windows 10。

  • 对于 x64 系统:版本 1903 或更高版本,内部版本为 18362 或更高版本。
  • 对于 ARM64 系统:版本 2004 或更高版本,内部版本为 19041 或更高版本。
    或 Windows 11。

若要检查 Windows 版本及内部版本号,选择 Windows 徽标键 + R,然后键入“winver”,选择“确定”。

安装 WSL 命令

现在,可以在管理员 PowerShell 或 Windows 命令提示符中输入此命令,然后重启计算机来安装运行适用于 Linux 的 Windows 子系统 (WSL) 所需的全部内容。

wsl --install

启用适用于 Linux 的 Windows 子系统

需要先启用“适用于 Linux 的 Windows 子系统”可选功能,然后才能在 Windows 上安装 Linux 分发。

以管理员身份打开 PowerShell(“开始”菜单 >“PowerShell” >单击右键 >“以管理员身份运行”),然后输入以下命令:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

启用虚拟机功能

安装 WSL 2 之前,必须启用“虚拟机平台”可选功能。 计算机需要虚拟化功能才能使用此功能。

以管理员身份打开 PowerShell 并运行:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

按要求重启使设置生效。

下载 Linux 内核更新包

1.下载最新包:

​适用于 x64 计算机的 WSL2 Linux 内核更新包​​。

2.运行上一步中下载的更新包。 (双击以运行 - 系统将提示你提供提升的权限,选择“是”以批准此安装。)

将 WSL 2 设置为默认版本

使用 wsl --install 命令安装的新 Linux 安装将默认设置为 WSL 2。

wsl --set-default-version 2

安装所选的 Linux 分发

打开 Microsoft Store,并选择你偏好的 Linux 分发版。

编译openharmony只能安装Ubuntu 20.04和Ubuntu 18.04

#打卡不停更#编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!!-开源基础软件社区
#打卡不停更#编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!!-开源基础软件社区

首次启动新安装的 Linux 分发版时,将打开一个控制台窗口,系统会要求你等待一分钟或两分钟,以便文件解压缩并存储到电脑上。 未来的所有启动时间应不到一秒。

然后,需要为新的 Linux 分发版创建用户帐户。

#打卡不停更#编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!!-开源基础软件社区
  • wsl2到底安装在windows系统的那个位置呢?
    C:\Users\用户名\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04LTS_79rhkp1fndgsc\LocalState

在Linux终端输入命令:

sudo vim /etc/apt/sources.list
#打卡不停更#编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!!-开源基础软件社区

把etc/apt/sources.list文件内容清楚,换下述其中一个源。(以下是vim的使用)。

  • 按一下字母「i」就可以进入「插入模式(Insert mode)」,就可以开始输入文字了。
  • 编辑好后按ESC键退出。
  • 最后键盘输入:wq,然后文件存盘并退出vim。
#163源
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
#中科大源
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
#阿里云源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
#清华源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

笔者换的是清华源。

#打卡不停更#编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!!-开源基础软件社区

最后更新源:

apt update
apt upgrade

在windowsx下的vscode里面下载好remote - wsl拓展

还可以通过使用 VS Code 中的快捷方式 CTRL+SHIFT+P 调出命令面板,以访问更多 VS Code 远程选项。

#打卡不停更#编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!!-开源基础软件社区
#打卡不停更#编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!!-开源基础软件社区

使用WSL1编译OpenHarmony可以吗?

编译不了,笔者已经踩过坑了。
比较 WSL 1 和 WSL 2:

#打卡不停更#编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!!-开源基础软件社区

使用wsl2编译openharmony镜像

源码获取和编译工具安装的部分不多作介绍了,社区有很多优秀的参考文章值得参考。

#打卡不停更#编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!!-开源基础软件社区
#打卡不停更#编译效率快三倍,使用WSL2编译OpenHarmony嘎嘎得劲!!-开源基础软件社区

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​​。

责任编辑:jianghua 来源: 51CTO开源基础软件社区

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK