5
7

More than 3 years have passed since last update.

WSL2 + Rust + VSCode + CrossBuild

Last updated at Posted at 2020-06-27

以下の条件のRust開発環境を構築したときのメモ。

  • ホストOSは、Windows10 Professional (Homeでもたぶん大丈夫)

  • WSL2(Debian Buster)にRustビルド環境をインストール

  • VSCodeからWSL2にリモート接続してIDEのようにデバッグできるようにする

  • 別のCPU向けにクロスビルドもできるようにする

ちなみに、これを書いている時点でRustは全くの初心者で何も知らない。

Rust install in WSL2

WSL2 setup

このあたりを参考に。
https://qiita.com/tomokei5634/items/27504849bb4353d8fef8

ディストリビューションはDebian(Buster)を選択した。

WSL2-Debian setup

インストールしたWSL2にログインする。
いつもの初期化をしておく。他にも必要かもしれないがメモとるのを忘れた。

$ apt-get update
$ apt-get upgrade

gcc開発環境をインストールしていない場合は、以下をインストールしておく。

$ apt-get install build-essential

Rustup install

WSL2-DebianにRust開発環境をインストールする。
公式サイトとかで紹介されている通りにやる。

$ curl https://sh.rustup.rs -sSf | sh
$ source $HOME/.cargo/env
$ rustup update

クロス環境 install

WSL2-Debianにdockerかpodmanをインストールする。
Debianだとdockerが標準だが、podmanの方がデーモン不要で軽そうなのでpodmanにしておく。

参考
https://podman.io/getting-started/installation.html

$ echo 'deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/Debian_10/ /' > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
$ curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/Debian_10/Release.key | sudo apt-key add -

$ apt-get update
$ apt-get install podman

$ cargo install cross

※ echoのところはsudoでもパーミッションエラーになるかもしれない。その場合は、viで /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list を開き、echoしているテキストを書き込めばOK。

これでクロスターゲット向けビルドができるようになる。
例えば RaspberryPi なら下記のようにしてビルドできる。

$ cargo new hoge
$ cd hoge
$ cross build --target armv7-unknown-linux-gnueabihf

これでクロスターゲットのシェルに "Hello World" が表示されるコマンドが作成される。

RSL install

VSCodeでのデバッグ用に Rust Language Server (RLS) をインストールする。
VSCodeが自動的にインストールしてくれる場合もある。

$ rustup component add rls rust-analysis rust-src

Python3 install

デバッグしようとしたら以下のエラーが出たので、調べてみたら libpython3.7m.so.1.0 がないということらしい。このファイル、python3をインストールしただけではだめで、python3-devのインストールが必要になる。

Could not initialize Python interpreter - some features will be unavailable (e.g. debug visualizers).

$ apt-get python3 python3-dev

VSCode setup

プラグインインストール

  • Remote Development

    • Visual Studio Code Remote Development Extension Pack
  • Rust

    • The Rust Programming Language
  • CodeLLDB

    • Native debugger based on LLDB

これで、"Remote-WSL: New Window" でWSLに接続してデバッグまでできるようになる。(Windowsターゲットのバイナリ)
途中いろいろと聞かれてくるが、Yesを選んでいればそのうち動くようになる。

デバッグ

Rustプロジェクトディレクトリをオープンする。
最初のデバッグ時には、以下のような画面になる。

image.png

"create a launch.json file" をクリックして、"LLDB" を選択し、launch.jsonを自動生成する。

image.png

image.png

ビルド

Shift + Ctrl + B でビルドできる。
ビルドしようとすると、以下のようなエラーが出ることがあるかもしれない。

/bin/bash: cargo: command not found

その場合は、VSCodeの設定の terminal.integrated.inheritEnv を true にすると解決する。

参考
https://github.com/rust-lang/rls-vscode/issues/620

クロスビルド

キーボードショートカットでクロスビルドもできるようにする。
tasks.json と keybindings.json を設定する。

tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cargo",
            "subcommand": "build",
            "problemMatcher": [
                "$rustc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "label": "Rust: cargo build"
        },
        {
            "type": "process",
            "command": "cross",
            "args": ["build", "--target", "armv7-unknown-linux-gnueabihf"],
            "problemMatcher": [
                "$rustc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "label": "Rust: cross build"
        }
    ]
}

このtasks.jsonでは、タスクが2つ定義されている。
一つ目は、VSCodeにより自動生成されるタスク。
二つ目は、クロスビルド用に追加したタスク。

これに、Shift + Ctrl + C のキーボードショートカットを割り当てる。

keybindings.json
[
    {
        "key": "ctrl+shift+C",
        "command": "workbench.action.tasks.runTask",
        "args": "Rust: cross build"
    }
]

クロスデバッグ

クロスターゲットに、上記WSLと同様の方法でrustupとRSLをインストールして、VSCodeからSSHで接続すればクロス環境でのIDEとしてデバッグできる。

5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7