Search code examples
linuxmacosrustcnosdb

Compile the rust program with cross-rs


Some unexpected events occurred when using cross-rs to compile rust

The source code is here:

https://github.com/IvanGao01/cnosdb/tree/2d870495aa65e3432755ad0bbc6a02bd957cd922

Execute the following command to compile

  1. Build image
docker build -t cnosdb/cross:aarch64-unknown-linux-gnu -f ./scripts/cross/aarch64-unknown-linux-gnu.dockerfile .
  1. Compile using the specified image
cross build --target aarch64-unknown-linux-gnu

But every time something unexpected happens:

error: failed to run custom build command for `protos v2.1.0 (/project/common/protos)`

Caused by:
  process didn't exit successfully: `/target/debug/build/protos-23a6f50301f268ec/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-changed=/project/common/protos/proto/kv_service.proto
  cargo:rerun-if-changed=/project/common/protos/proto

  --- stderr
  thread 'main' panicked at 'Could not find `protoc` installation and this build crate cannot proceed without
      this knowledge. If `protoc` is installed and this crate had trouble finding
      it, you can set the `PROTOC` environment variable with the specific path to your
      installed `protoc` binary.If you're on debian, try `apt-get install protobuf-compiler` or download it from https://github.com/protocolbuffers/protobuf/releases

  For more information: https://docs.rs/prost-build/#sourcing-protoc
  ', /cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/prost-build-0.11.6/src/lib.rs:1387:10
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

But I checked the protoc program in the image. It is in/usr/bin/protoc, It doesn't work even if I add it to PROTOC=/usr/bin/protoc.


Solution

  • Cross executes in the same user context as it was invoked.

    When you copy protoc from the temp dir to /usr/bin here you're not setting the permission so that all users can read and execute it, which means prost-build can not see protoc in path.

    the solution is to do chmod 755 /usr/bin/protoc or equivalent as a user that can access the file (in this case it's owned by root with permission 750)