Build the ring rust crate with a stable build path
kpcyrd at archlinux.org
Mon May 8 00:08:09 UTC 2023
I was using github actions to compile my project but had trouble
matching the binary, even when using podman with a ubuntu:22.04
container to match the ubuntu used by my github actions.
I was attempting to use --remap-path-prefix with RUSTFLAGS to prevent
rustc from recording build paths, but using diffoscope I discovered the
binary still contains paths like
from the github environment or
from my build container.
After some research I discovered this is a known issue in the ring
crate with a pending patch, but also found a workaround I want to
share with this list.
I setup a script called build.sh that looks like this:
mkdir -p -- "$CARGO_HOME"
unshare -Umr sh -xe <<EOF
mount -t tmpfs tmpfs /mnt
mkdir /mnt/src /mnt/cargo
mount --bind "$PWD" /mnt/src
mount --bind "$CARGO_HOME" /mnt/cargo
CARGO_HOME=/mnt/cargo cargo build --release --verbose
This can be run without root privileges if user namespaces are enabled.
The script sets up two directories in /mnt to provide the source code,
build directory and $CARGO_HOME folder at stable locations.
This view on the file system is exclusive to the compiler process and
doesn't interfere with any other processes making use of the /mnt
directory, but this approach obviously only works if the input
directories are located in e.g. /home and not /mnt itself. You may need
to delete the target/ directory to get rid of any embedded build paths
in your build cache that aren't /mnt/src or /mnt/cargo.
This allowed me to match the binary built by github actions with one
built in my ubuntu:22.04 container. You still need to match all
compilers used or you may run into "GNU AS 2.38" vs "GNU AS 2.40.0" or
"GCC: (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0" vs "GCC: (GNU) 13.1.1
20230429". And obviously to change the binary output is the whole point
of releasing a new compiler version. Linux distributions are using
buildinfo files for this, I'm not aware of any github native solutions
I hope somebody considers this useful.
More information about the rb-general