最近RustでROSを使っていてクロスコンパイルしたいということがありました。
単純にcross
つかってクロスコンパイルしたらmsg
のパス解決がうまくいかなくてエラーが出ました。
どうすればうまくいったか
ROSRUST_MSG_PATH="/project/msgs" cross build --target arm-unknown-linux-musleabihf
どういう経緯か
こんなディレクトリ構成です。
.
├── msgs
│ ├── common_msgs
│ └── std_msgs
├── src
│ └── main.rs
└── Cargo.toml
普通にホスト向けでビルドするときは
ROSRUST_MSG_PATH="$(pwd)/msgs" cargo build
とかやります。
cross
をつかってクロスコンパイルするときには、Cross.toml
にROSRUST_MSG_PATH
環境変数をビルド環境に渡すように指示を書きます。
[build.env]
passthrough = [
"ROSRUST_MSG_PATH"
]
これでいけるかと思いきや、エラーが出ます。
$ ROSRUST_MSG_PATH="$(pwd)/msgs" cross build --target arm-unknown-linux-musleabihf
...
Compiling rosrust v0.9.5
error: proc macro panicked
--> /target/arm-unknown-linux-musleabihf/debug/build/rosrust_msg-8eeb2498a88c50fd/out/messages.rs:3:9
|
3 | rosrust::rosmsg_include!(,IGNORE_BAD);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: message: Package string needs to be in package/name format:
error: aborting due to previous error
error: could not compile `rosrust_msg`
To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed
これはcross
がDockerをつかってビルドするときに使用する環境とホスト環境とで、ソースツリーが置かれているディレクトリのパスが違っていることが原因です(まあ pwd
の結果はそのまま使えないのは明かですね)。
じゃあどこにマウントされているんだと云うはなしですが、cross
のREADMEをよく読むとそれらしき記述があります。
When running cross from inside a docker container, cross needs access to the hosts docker daemon itself. This is normally achieved by mounting the docker daemons socket /var/run/docker.sock. For example:
$ docker run -v /var/run/docker.sock:/var/run/docker.sock -v .:/project \ -w /project my/development-image:tag cross build --target mips64-unknown-linux-gnuabi64
The image running cross requires the rust development tools to be installed.
With this setup cross must find and mount the correct host paths into the container used for cross compilation. This includes the original project directory as well as the root path of the parent container to give access to the rust build tools.
/project
以下にマウントしろっていっていますね。
というわけで、最終的にこうしたらうまくいきました。
ROSRUST_MSG_PATH="/project/msgs" cross build --target arm-unknown-linux-musleabihf