Use ranlib instead of ar x

This commit is contained in:
bjorn3 2018-10-20 15:19:07 +02:00
parent 6bf9444f3e
commit 3f574e9f7b
2 changed files with 30 additions and 29 deletions

View file

@ -12,26 +12,8 @@ else
exit 1
fi
extract_data() {
pushd target/out/
ar x $1 data.o
chmod +rw data.o
mv data.o $2
popd
}
link_and_run() {
target=$1
shift
pushd target/out
gcc $@ -o $target
sh -c ./$target || true
popd
}
build_lib() {
SHOULD_CODEGEN=1 $RUSTC $2 --crate-name $1 --crate-type lib
extract_data lib$1.rlib $1.o
}
run_bin() {
@ -40,9 +22,11 @@ run_bin() {
build_example_bin() {
$RUSTC $2 --crate-name $1 --crate-type bin
extract_data $1 $1.o
link_and_run $1 mini_core.o $1.o
pushd target/out
gcc libmini_core.rlib $1 -o $1_bin
sh -c ./$1_bin || true
popd
}
if [[ "$1" == "--release" ]]; then
@ -80,4 +64,3 @@ time SHOULD_CODEGEN=1 xargo build --color always
popd
cat target/out/log.txt | sort | uniq -c
#extract_data libcore.rlib core.o

View file

@ -298,6 +298,21 @@ impl CodegenBackend for CraneliftCodegenBackend {
);
let file = File::create(&output_name).unwrap();
let mut builder = ar::Builder::new(file);
if should_codegen(sess) {
// Add main object file
let obj = artifact.emit().unwrap();
builder
.append(
&ar::Header::new(b"data.o".to_vec(), obj.len() as u64),
::std::io::Cursor::new(obj),
)
.unwrap();
}
// Non object files need to be added after object files, because ranlib will
// try to read the native architecture from the first file, even if it isn't
// an object file
builder
.append(
&ar::Header::new(
@ -307,14 +322,17 @@ impl CodegenBackend for CraneliftCodegenBackend {
::std::io::Cursor::new(metadata.clone()),
)
.unwrap();
if should_codegen(sess) {
let obj = artifact.emit().unwrap();
builder
.append(
&ar::Header::new(b"data.o".to_vec(), obj.len() as u64),
::std::io::Cursor::new(obj),
)
.unwrap();
// Finalize archive
std::mem::drop(builder);
// Run ranlib to be able to link the archive
let status = std::process::Command::new("ranlib")
.arg(output_name)
.status()
.expect("Couldn't run ranlib");
if !status.success() {
sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));
}
}
_ => sess.fatal(&format!("Unsupported crate type: {:?}", crate_type)),