Skip to content

Commit

Permalink
ndk-build: Use LLVM binutils when GNU binutils are removed (r23+) (#137)
Browse files Browse the repository at this point in the history
* ndk-build: Use LLVM binutils when GNU binutils are removed (r23+)

Since [r21] LLVM binutils are included _for testing_;
Since [r22] GNU binutils are deprecated in favour of LLVM's;
Since [r23] GNU binutils have been removed.

When GNU binutils are not available (`<triple>-<bin_name>`) "fall back"
to `llvm-<bin_name>` binaries from LLVM binutils that are now included
with the NDK.

To maintain stability with the current ndk-build crate release, prefer
GNU binutils for as long as it is provided by the NDK instead of trying
to use `llvm-<bin_name>` from r21 onwards.

[r21]: https://github.com/android/ndk/wiki/Changelog-r21
[r22]: https://github.com/android/ndk/wiki/Changelog-r22
[r23]: https://github.com/android/ndk/wiki/Changelog-r23

Fixes #135

* ndk-build/error: Wrap variable arguments in backticks
  • Loading branch information
MarijnS95 committed Apr 15, 2021
1 parent ca87ad8 commit dd54b81
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
16 changes: 11 additions & 5 deletions ndk-build/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,30 @@ pub enum NdkError {
environment variable."
)]
NdkNotFound,
#[error("Path {0:?} doesn't exist.")]
#[error("GNU toolchain binary `{gnu_bin}` nor LLVM toolchain binary `{llvm_bin}` found in `{toolchain_path:?}`.")]
ToolchainBinaryNotFound {
toolchain_path: PathBuf,
gnu_bin: String,
llvm_bin: String,
},
#[error("Path `{0:?}` doesn't exist.")]
PathNotFound(PathBuf),
#[error("Command {0} not found.")]
#[error("Command `{0}` not found.")]
CmdNotFound(String),
#[error("Android SDK has no build tools.")]
BuildToolsNotFound,
#[error("Android SDK has no platforms installed.")]
NoPlatformFound,
#[error("Platform {0} is not installed.")]
#[error("Platform `{0}` is not installed.")]
PlatformNotFound(u32),
#[error("Target is not supported.")]
UnsupportedTarget,
#[error("Host {0} is not supported.")]
#[error("Host `{0}` is not supported.")]
UnsupportedHost(String),
#[error(transparent)]
Io(#[from] IoError),
#[error("Invalid semver")]
InvalidSemver,
#[error("Command '{}' had a non-zero exit code.", format!("{:?}", .0).replace('"', ""))]
#[error("Command `{}` had a non-zero exit code.", format!("{:?}", .0).replace('"', ""))]
CmdFailed(Command),
}
33 changes: 23 additions & 10 deletions ndk-build/src/ndk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,35 @@ impl Ndk {
Ok((clang, clang_pp))
}

pub fn toolchain_bin(&self, bin: &str, target: Target) -> Result<PathBuf, NdkError> {
pub fn toolchain_bin(&self, name: &str, target: Target) -> Result<PathBuf, NdkError> {
#[cfg(target_os = "windows")]
let ext = ".exe";
#[cfg(not(target_os = "windows"))]
let ext = "";

let bin = self.toolchain_dir()?.join("bin").join(format!(
"{}-{}{}",
target.ndk_triple(),
bin,
ext
));
if !bin.exists() {
return Err(NdkError::PathNotFound(bin));
let toolchain_path = self.toolchain_dir()?.join("bin");

// Since r21 (https://github.com/android/ndk/wiki/Changelog-r21) LLVM binutils are included _for testing_;
// Since r22 (https://github.com/android/ndk/wiki/Changelog-r22) GNU binutils are deprecated in favour of LLVM's;
// Since r23 (https://github.com/android/ndk/wiki/Changelog-r23) GNU binutils have been removed.
// To maintain stability with the current ndk-build crate release, prefer GNU binutils for
// as long as it is provided by the NDK instead of trying to use llvm-* from r21 onwards.
let gnu_bin = format!("{}-{}{}", target.ndk_triple(), name, ext);
let gnu_path = toolchain_path.join(&gnu_bin);
if gnu_path.exists() {
Ok(gnu_path)
} else {
let llvm_bin = format!("llvm-{}{}", name, ext);
let llvm_path = toolchain_path.join(&llvm_bin);
llvm_path
.exists()
.then(|| llvm_path)
.ok_or_else(|| NdkError::ToolchainBinaryNotFound {
toolchain_path,
gnu_bin,
llvm_bin,
})
}
Ok(bin)
}

pub fn android_dir(&self) -> Result<PathBuf, NdkError> {
Expand Down

0 comments on commit dd54b81

Please sign in to comment.