Skip to content

Expose AR_...=llvm-ar with Android NDK 23+ toolchains #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
montekki opened this issue Mar 28, 2022 · 11 comments
Closed

Expose AR_...=llvm-ar with Android NDK 23+ toolchains #91

montekki opened this issue Mar 28, 2022 · 11 comments

Comments

@montekki
Copy link

montekki commented Mar 28, 2022

I am trying to build a project that has ring as a dependency that uses cc-rs which among other things requires invoking custom CC and ARs among other things.

Since r23 GNU binutils including stuff like x86_64-linux-android-ar have been removed so what I am trying to do is to point the build to the llvm-ar like this:

cargo {
	targets = ["x86_64"]
	apiLevel = 21
	exec { spec, toolchain ->
		if (toolchain.target == "x86_64-linux-android") {
			spec.environment("AR_x86_64_linux_android", "llvm-ar")
		}
	}
}

But apparently it has no effect and falls back with an error message

error occurred: Failed to find tool. Is `/home/$USER/Android/Sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android-ar` installed?

@ncalexan
Copy link
Member

ncalexan commented Mar 28, 2022

Hmm, interesting. So the exec block should impact those variables for the actual invocation, but it won't impact the checking by the rust-android-gradle plugin itself -- and that's what you're seeing.

It rather sounds like we need to accommodate these changes to NDK 23+ in

data class Toolchain(val platform: String,
val type: ToolchainType,
val target: String,
val compilerTriple: String,
val binutilsTriple: String,
val folder: String) {
fun cc(apiLevel: Int): File =
if (System.getProperty("os.name").startsWith("Windows")) {
if (type == ToolchainType.ANDROID_PREBUILT) {
File("bin", "$compilerTriple$apiLevel-clang.cmd")
} else {
File("$platform-$apiLevel/bin", "$compilerTriple-clang.cmd")
}
} else {
if (type == ToolchainType.ANDROID_PREBUILT) {
File("bin", "$compilerTriple$apiLevel-clang")
} else {
File("$platform-$apiLevel/bin", "$compilerTriple-clang")
}
}
fun cxx(apiLevel: Int): File =
if (System.getProperty("os.name").startsWith("Windows")) {
if (type == ToolchainType.ANDROID_PREBUILT) {
File("bin", "$compilerTriple$apiLevel-clang++.cmd")
} else {
File("$platform-$apiLevel/bin", "$compilerTriple-clang++.cmd")
}
} else {
if (type == ToolchainType.ANDROID_PREBUILT) {
File("bin", "$compilerTriple$apiLevel-clang++")
} else {
File("$platform-$apiLevel/bin", "$compilerTriple-clang++")
}
}
fun ar(apiLevel: Int): File =
if (type == ToolchainType.ANDROID_PREBUILT) {
File("bin", "$binutilsTriple-ar")
} else {
File("$platform-$apiLevel/bin", "$binutilsTriple-ar")
}
}

Patches wanted!

@montekki
Copy link
Author

montekki commented Mar 30, 2022

I was able to solve this with specifying an absolute path to ndk's llvm-ar

def ndkDir = android.ndkDirectory

exec { spec, toolchain ->
	if (toolchain.target == "x86_64-linux-android") {
		spec.environment("AR_x86_64-linux-android", "$ndkDir/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar")
	}
	if (toolchain.target == "armv7-linux-androideabi") {
		spec.environment("AR_armv7-linux-androideabi", "$ndkDir/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar")
	}
	if (toolchain.target == "aarch64-linux-android") {
		spec.environment("AR_aarch64-linux-android", "$ndkDir/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar")
	}
}

@ncalexan
Copy link
Member

I'm going to keep this open -- if the modern NDKs have changed layout, we want to accommodate in the code.

@ncalexan ncalexan reopened this Mar 30, 2022
@Will5
Copy link

Will5 commented May 2, 2022

I'm facing the same problem.

I was able to solve this with specifying an absolute path to ndk's llvm-ar

def ndkDir = android.ndkDirectory

exec { spec, toolchain ->
	if (toolchain.target == "x86_64-linux-android") {
		spec.environment("AR_x86_64-linux-android", "$ndkDir/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar")
	}
	if (toolchain.target == "armv7-linux-androideabi") {
		spec.environment("AR_armv7-linux-androideabi", "$ndkDir/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar")
	}
	if (toolchain.target == "aarch64-linux-android") {
		spec.environment("AR_aarch64-linux-android", "$ndkDir/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar")
	}
}

@montekki, where did you add this?

I added it to the cargo block in my build.gradle file, but the error is still there

@montekki
Copy link
Author

montekki commented May 3, 2022

I added it to the cargo block in my build.gradle file, but the error is still there

check out our gradle file here

@ncalexan ncalexan changed the title Builds with newer Android NDK toolchains Expose AR_...=llvm-ar with Android NDK 23+ toolchains May 5, 2022
ncalexan added a commit to ncalexan/rust-android-gradle that referenced this issue May 5, 2022
…Fixes mozilla#91.

I'd prefer to do this by interrogating the binaries on disk, using
something like `which`, but the current expression makes that a little
hard to achieve, so we'll feed the NDK version into the relevant
helper function for now.
ncalexan added a commit to ncalexan/rust-android-gradle that referenced this issue May 5, 2022
…Fixes mozilla#91.

I'd prefer to do this by interrogating the binaries on disk, using
something like `which`, but the current expression makes that a little
hard to achieve, so we'll feed the NDK version into the relevant
helper function for now.
ncalexan added a commit to ncalexan/rust-android-gradle that referenced this issue May 6, 2022
…Fixes mozilla#91.

I'd prefer to do this by interrogating the binaries on disk, using
something like `which`, but the current expression makes that a little
hard to achieve, so we'll feed the NDK version into the relevant
helper function for now.
@ncalexan
Copy link
Member

ncalexan commented May 6, 2022

This should be fixed in the just published 0.9.3. Testing appreciated!

@Will5
Copy link

Will5 commented May 9, 2022

It seems the llvm-ar error is resolved, but now I am getting an lgcc error:

  = note: ld: error: unable to find library -lgcc
          clang-12: error: linker command failed with exit code 1 (use -v to see invocation)

The release notes mention lgcc, so perhaps it is related.

@ncalexan
Copy link
Member

ncalexan commented May 9, 2022

It seems the llvm-ar error is resolved, but now I am getting an lgcc error:

  = note: ld: error: unable to find library -lgcc
          clang-12: error: linker command failed with exit code 1 (use -v to see invocation)

The release notes mention lgcc, so perhaps it is related.

Can I get more logs? Including some details on your build environment? The plugin takes care to use a linker invocation that rewrites -lgcc to -lunwind, perhaps we are somehow missing it when invoked within a build script? That seems possible.

@Will5
Copy link

Will5 commented May 9, 2022

I created a secret gist

@ncalexan
Copy link
Member

ncalexan commented May 9, 2022

I created a secret gist

Well, this is a first: those logs strongly suggest that you're building with cargo ndk, which isn't this plugin. In particular, you'll need the equivalent of #83 in your setup.

@Will5
Copy link

Will5 commented May 9, 2022

I created a secret gist

Well, this is a first: those logs strongly suggest that you're building with cargo ndk, which isn't this plugin. In particular, you'll need the equivalent of #83 in your setup.

You're right. I see someone added cargoNdk as a dependency of another task. I'm not sure what the solution is, but we can stay on NDK 22.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants