Description
I'm not 100% sure if this is a cargo bug or a rustc bug, but it looks like a rustc bug.
I tested using rustc 1.41.0-nightly (412f43ac5 2019-11-24)
running on MacOSX Catalina.
I put my files in a small repository, which you can find here: https://github.com/davehylands/simple-lib
What I noticed is that if I build using cargo normally (i.e. without using --release) then the object files for external crates do not get bitcode embedded. If I build using cargo with --release when the object files for external creates do get bitcode embedded. In either case, cargo is being run by using RUSTFLAGS="-Z embed-bitcode"
You can pass --release
to the build.sh script to get the --release behaviour, and pass no flags to get the debug behaviour. The build.sh script runs otool over the objects included in libsimple_lib.a to show whether bitcode is included or not.
If you modify the for loop to use
for obj in rand_core*.o; do
rather than:
for obj in *.o; do
then you'll get simpler output.
Normally, I use a rust toolchain that has the rust standard libraries built with bitcode, so I realize that these objects won't have bitcode using the nightly compiler.
Cargo.toml
[package]
name = "simple_lib"
version = "0.1.0"
authors = ["Dave Hylands <davehylands@fullstory.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.7"
[lib]
name = "simple_lib"
# rustc cannont create dylibs for iOS.
# https://github.com/rust-lang/rust/issues/21727#issuecomment-424026535
crate-type = ["staticlib"]
src/lib.rs
use rand;
pub fn test_func2() -> String {
let x = rand::random::<u16>();
format!("Your number is {}", x)
}
build.sh
#!/bin/sh
set -e
if [ "$1" == "--release" ]; then
BUILD_TYPE="release"
CARGO_ARGS="--release"
else
BUILD_TYPE="debug"
fi
TARGET_DIR=target
rm -rf ${TARGET_DIR}
RUSTFLAGS="-Z embed-bitcode" cargo build -v ${CARGO_ARGS}
cd ${TARGET_DIR}/${BUILD_TYPE}
ar x libsimple_lib.a
for obj in *.o; do
if otool -l ${obj} | grep bitcode > /dev/null ; then
echo "${obj} has bitcode"
else
echo "${obj} DOES NOT have bitcode"
fi
done