Skip to content

Commit b24dbfc

Browse files
Use LTO build of libunwind if using -Clinker-plugin-lto
This greatly reduces binary sizes, at the expense of compilation speed. Normal builds still use a regular non-LTO build of libunwind.
1 parent 37e271c commit b24dbfc

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

libunwind/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
src
22
include
3-
libunwind.a
3+
libunwind*.a
44
*.o

libunwind/Makefile

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
SUFFIX ?=
2+
CPPFLAGS ?=
3+
4+
OUTLIB := libunwind$(SUFFIX).a
5+
16
CC := clang
27
CXX := clang++
38
AR := llvm-ar
@@ -9,20 +14,25 @@ S_OBJLIST := UnwindRegistersRestore.o UnwindRegistersSave.o
914

1015
CFLAGS := -std=c99
1116
CXXFLAGS := -std=c++11 -nostdinc++ -fno-exceptions -fno-rtti
12-
CPPFLAGS := -target mipsel-unknown-unknown -mcpu=mips2 -msingle-float \
13-
-fstrict-aliasing -funwind-tables -O3 \
17+
CPPFLAGS := $(CPPFLAGS) -target mipsel-unknown-unknown -mcpu=mips2 \
18+
-msingle-float -fstrict-aliasing -funwind-tables -O3 \
1419
-D __LITTLE_ENDIAN__ -D __ELF__ -D _LIBUNWIND_IS_BAREMETAL \
1520
-D _LIBUNWIND_HAS_NO_THREADS -D _LIBUNWIND_IS_NATIVE_ONLY \
1621
-DNDEBUG \
1722
-I /usr/local/pspdev/psp/include/ \
1823
-I include
1924

20-
../psp/libunwind.a: libunwind.a
21-
cp libunwind.a ../psp/
25+
all:
26+
make ../psp/libunwind.a
27+
SUFFIX=_lto CPPFLAGS=-flto make ../psp/libunwind_lto.a
28+
29+
../psp/$(OUTLIB): $(OUTLIB)
30+
cp $^ ../psp/
2231
touch ../psp/build.rs
32+
make clean
2333

24-
libunwind.a: $(CPP_OBJLIST) $(C_OBJLIST) $(S_OBJLIST)
25-
$(AR) $(ARFLAGS) libunwind.a $^
34+
$(OUTLIB): $(CPP_OBJLIST) $(C_OBJLIST) $(S_OBJLIST)
35+
$(AR) $(ARFLAGS) $(OUTLIB) $^
2636

2737
$(CPP_OBJLIST): %.o: src/%.cpp
2838
$(COMPILE.cc) $^
@@ -32,7 +42,7 @@ $(S_OBJLIST): %.o: src/%.S
3242
$(C_OBJLIST) $(S_OBJLIST):
3343
$(COMPILE.c) $^
3444

35-
.PHONY: clean patch
45+
.PHONY: all clean patch
3646

3747
patch:
3848
git submodule update --init --depth 1 -- ./rustc
@@ -41,4 +51,4 @@ patch:
4151
patch -p0 < ./no-sdc1.patch
4252

4353
clean:
44-
rm -r *.o libunwind.a
54+
rm -r *.o

psp/build.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1-
use std::{env, path::Path};
1+
use std::{env, path::Path, os::unix::prelude::OsStrExt};
22

33
fn main() {
44
println!("cargo:rerun-if-changed=build.rs");
55
println!("cargo:rerun-if-changed=libunwind.a");
6+
println!("cargo:rerun-if-env-changed=RUSTFLAGS");
67

78
if env::var("CARGO_FEATURE_STUB_ONLY").is_ok() {
89
return;
910
}
1011

12+
// Figure out whether to use the LTO libunwind, or the regular one.
13+
let libunwind = if env::var_os("CARGO_ENCODED_RUSTFLAGS")
14+
.expect("could not get `CARGO_ENCODED_RUSTFLAGS` variable")
15+
.as_bytes()
16+
.split(|b| *b == 0x1f)
17+
.any(|flags| flags.starts_with(b"-Clinker-plugin-lto"))
18+
{
19+
"./libunwind_lto.a"
20+
} else {
21+
"./libunwind.a"
22+
};
23+
1124
// TODO: Do we even need to copy the library over? Maybe we can just link
1225
// directly from the current directory.
1326
let out_dir = env::var("OUT_DIR").unwrap();
1427
let out_file = Path::new(&out_dir).join("libunwind.a");
15-
std::fs::copy("./libunwind.a", out_file).unwrap();
28+
std::fs::copy(libunwind, out_file).unwrap();
1629

1730
println!("cargo:rustc-link-lib=static=unwind");
1831
println!("cargo:rustc-link-search=native={}", out_dir);

psp/libunwind.a

-6.01 KB
Binary file not shown.

psp/libunwind_lto.a

62.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)