Skip to content

Commit 7ca378b

Browse files
committed
Improve cross compilation
1 parent 807dd02 commit 7ca378b

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
sudo apt-get update -y
4747
sudo apt-get install -y --no-install-recommends gcc-mips64-linux-gnuabi64 libc6-dev-mips64-cross
4848
shell: bash
49-
- name: Build ${{ matrix.lua }}
49+
- name: Build for ${{ matrix.target }}
5050
run: |
5151
cargo build --manifest-path testcrate/Cargo.toml --target ${{ matrix.target }} --release
5252
shell: bash
@@ -72,7 +72,7 @@ jobs:
7272
- uses: dtolnay/rust-toolchain@stable
7373
with:
7474
target: ${{ matrix.target }}
75-
- name: Run ${{ matrix.lua }} tests
75+
- name: Run tests
7676
run: |
7777
cargo test --manifest-path testcrate/Cargo.toml --release
7878
cargo test --manifest-path testcrate/Cargo.toml --release --features lua52compat

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Sources of LuaJIT 2.1 (OpenResty's branch) and logic to build it.
1313

1414
[dependencies]
1515
cc = "1.0"
16+
which = "4.4"

src/lib.rs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,54 @@ impl Build {
112112
// Infer ar/ranlib tools from cross compilers if the it looks like
113113
// we're doing something like `foo-gcc` route that to `foo-ranlib`
114114
// as well.
115-
if compiler_path.ends_with("-gcc") && !target.contains("unknown-linux-musl") {
116-
let prefix = &compiler_path[..compiler_path.len() - 3];
117-
make.env("CROSS", prefix);
115+
let prefix = if compiler_path.ends_with("-gcc") {
116+
&compiler_path[..compiler_path.len() - 3]
117+
} else if compiler_path.ends_with("-clang") {
118+
&compiler_path[..compiler_path.len() - 5]
119+
} else {
120+
""
121+
};
122+
123+
let compiler_path =
124+
which::which(compiler_path).expect(&format!("cannot find {compiler_path}"));
125+
let bindir = compiler_path.parent().unwrap();
126+
make.env("STATIC_CC", &compiler_path);
127+
make.env("TARGET_LD", &compiler_path);
128+
129+
// Find ar
130+
if bindir.join(format!("{prefix}ar")).is_file() {
131+
let mut ar = bindir.join(format!("{prefix}ar")).into_os_string();
132+
ar.push(" rcus");
133+
make.env("TARGET_AR", ar);
134+
} else if compiler.is_like_clang() {
135+
if bindir.join("llvm-ar").is_file() {
136+
let mut ar = bindir.join("llvm-ar").into_os_string();
137+
ar.push(" rcus");
138+
make.env("TARGET_AR", ar);
139+
} else {
140+
panic!("cannot find {prefix}ar or llvm-ar");
141+
}
142+
} else if compiler.is_like_gnu() && bindir.join("ar").is_file() {
143+
let mut ar = bindir.join("ar").into_os_string();
144+
ar.push(" rcus");
145+
make.env("TARGET_AR", ar);
146+
} else {
147+
panic!("cannot find {prefix}ar");
148+
}
149+
150+
// Find strip
151+
if bindir.join(format!("{prefix}strip")).is_file() {
152+
make.env("TARGET_STRIP", bindir.join(format!("{prefix}strip")));
153+
} else if compiler.is_like_clang() {
154+
if bindir.join("llvm-strip").is_file() {
155+
make.env("TARGET_STRIP", bindir.join("llvm-strip"));
156+
} else {
157+
panic!("cannot find {prefix}strip or llvm-strip");
158+
}
159+
} else if compiler.is_like_gnu() && bindir.join("strip").is_file() {
160+
make.env("TARGET_STRIP", bindir.join("strip"));
161+
} else {
162+
panic!("cannot find {prefix}strip");
118163
}
119164

120165
let mut xcflags = vec!["-fPIC"];

0 commit comments

Comments
 (0)