Skip to content
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

android: Migrate from ndk-glue to ndk-context #641

Merged
merged 1 commit into from
Feb 16, 2022

Conversation

MarijnS95
Copy link
Contributor

ndk-glue suffers one fatal flaw: it's "only" supposed to be used by the crate providing fn main() and only supposed to end up in the dependency graph once as it has static globals which get duplicated across versions.

In the current case with winit 0.26 still on ndk-glue 0.5 but cpal on ndk-glue 0.6 it'll always panic in fn native_activity() as the static globals on this version is not initialized.

Introducing ndk-context: a crate that holds these statics, with the intention/premise to not see a breaking release /ever/ and make this a problem of the past. The crate is currently initialized with the VM and Android Context on ndk-glue 0.5.1 and 0.6.1 (0.4.1 pending) making it compatible with whatever is current, and the possibility for backporting to older ndk-glue versions too.

See also:
rust-mobile/ndk#211
rust-mobile/ndk#223

Cargo.toml Show resolved Hide resolved
`ndk-glue` suffers one fatal flaw: it's "only" supposed to be used by
the crate providing `fn main()` and only supposed to end up in the
dependency graph once as it has `static` globals which get duplicated
across versions.

In the current case with `winit 0.26` still on `ndk-glue 0.5` but
`cpal` on `ndk-glue 0.6` it'll always panic in `fn native_activity()` as
the `static` globals on this version is not initialized.

Introducing `ndk-context`: a crate that holds these `static`s, with the
intention/premise to not see a breaking release /ever/ and make this a
problem of the past.  The crate is currently initialized with the VM and
Android Context on `ndk-glue` 0.5.1 and 0.6.1 (0.4.1 pending) making it
compatible with whatever is current, and the possibility for backporting
to older `ndk-glue` versions too.

See also:
rust-mobile/ndk#211
rust-mobile/ndk#223
Copy link
Member

@est31 est31 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@est31
Copy link
Member

est31 commented Feb 15, 2022

Hmm wait it failed in CI:

error: custom attribute panicked
 --> examples/android.rs:8:35
  |
8 | #[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "full"))]
  |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: `"ndk-glue"` is not a valid identifier

warning: unused import: `HostTrait`
 --> examples/android.rs:6:33
  |
6 | use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
  |                                 ^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: `cpal` (example "android") generated 1 warning
error: could not compile `cpal` due to previous error; 1 warning emitted

@MarijnS95
Copy link
Contributor Author

It seems that target.'cfg(target_os = "android")' doesn't work for dev-dependencies? Removing it makes the build succeed again (but breaks other targets for obvious reasons). https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies contains such an example with cfg() for dev-dependencies, any idea what I'm missing?

@MarijnS95
Copy link
Contributor Author

This looks to be an odd issues inside our macros, investigating...

@est31
Copy link
Member

est31 commented Feb 15, 2022

This might be because cpal is still on the 2015 edition? The macro might be emitting a wrong span that points to cpal's sources so rustc treats it as edition 2015 code.

@MarijnS95
Copy link
Contributor Author

The issue is that proc-macro-crate doesn't lookup target.xy.dev-dependencies, only target.xy.dependencies:

https://github.com/bkchr/proc-macro-crate/blob/9ddaaac13fc04182a525c85e89edc1c29f3d4456/src/lib.rs#L178-L189

And our fallback in ndk-macro is wrong in that it doesn't replace the dash in the crate_path("ndk-glue", ...) call with an underscore, hence we get `"ndk-glue"` is not a valid identifier:

https://github.com/rust-windowing/android-ndk-rs/blob/9060d5dbea8bdd9aee9e42ef8e8375c35adbc271/ndk-macro/src/helper.rs#L64

I'm fixing both and will get back to you when it's done, sorry for the noise!

MarijnS95 added a commit to MarijnS95/proc-macro-crate that referenced this pull request Feb 15, 2022
In [RustAudio/cpal#641] we're seeing a [failure in `ndk-macro`] to turn
a crate name with dashes into underscores as fallback, with the
underlying issue that `proc-macro-crate` failed to find our crate in
`Cargo.toml` when limited to a specific target cfg.  According to [the
Rust docs] it is perfectly possible to have `dev-dependencies` behind
such a target predicate too.

[RustAudio/cpal#641]: RustAudio/cpal#641 (comment)
[failure in `ndk-macro`]: https://github.com/rust-windowing/android-ndk-rs/blob/9060d5dbea8bdd9aee9e42ef8e8375c35adbc271/ndk-macro/src/helper.rs#L64
[the Rust docs]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies
@MarijnS95
Copy link
Contributor Author

bkchr/proc-macro-crate#15

I'll think about removing the erroneous fallback in ndk-macro too.

MarijnS95 added a commit to MarijnS95/proc-macro-crate that referenced this pull request Feb 15, 2022
In [RustAudio/cpal#641] we're seeing a [failure in `ndk-macro`] to turn
a crate name with dashes into underscores as fallback, with the
underlying issue that `proc-macro-crate` failed to find our crate in
`Cargo.toml` when limited to a specific target cfg.  According to [the
Rust docs] it is perfectly possible to have `dev-dependencies` behind
such a target predicate too.

[RustAudio/cpal#641]: RustAudio/cpal#641 (comment)
[failure in `ndk-macro`]: https://github.com/rust-windowing/android-ndk-rs/blob/9060d5dbea8bdd9aee9e42ef8e8375c35adbc271/ndk-macro/src/helper.rs#L64
[the Rust docs]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies
MarijnS95 added a commit to rust-mobile/ndk that referenced this pull request Feb 15, 2022
`proc_macro_crate::crate_name` fails to find our `ndk-glue`
crate when it sits inside `[target.'cfg(target_os =
"android")'.dev-dependencies]` as shown in [RustAudio/cpal#641], with a
fix pending in [bkchr/proc-macro-crate#15].  This function is only ever
called with `"ndk-glue"` which is an invalid identifier and should have
its dash replaced with an underscore to be valid.  However, since this
fallback doesn't seem to have been hit before (we've never received
reports of `` `"ndk-glue"` is not a valid identifier ``) it is safe and
desired to enforce the crate name to reside in `Cargo.toml` and
`proc-macro-crate` being able to find it.

[RustAudio/cpal#641]: https://github.com/RustAudio/cpal/runs/5203107529?check_suite_focus=true
[bkchr/proc-macro-crate#15]: bkchr/proc-macro-crate#15
MarijnS95 added a commit to rust-mobile/ndk that referenced this pull request Feb 15, 2022
`proc_macro_crate::crate_name` fails to find our `ndk-glue`
crate when it sits inside `[target.'cfg(target_os =
"android")'.dev-dependencies]` as shown in [RustAudio/cpal#641], with a
fix pending in [bkchr/proc-macro-crate#15].  This function is only ever
called with `"ndk-glue"` which is an invalid identifier and should have
its dash replaced with an underscore to be valid.  However, since this
fallback doesn't seem to have been hit before (we've never received
reports of `` `"ndk-glue"` is not a valid identifier ``) it is safe and
desired to enforce the crate name to reside in `Cargo.toml` and
`proc-macro-crate` being able to find it.

[RustAudio/cpal#641]: https://github.com/RustAudio/cpal/runs/5203107529?check_suite_focus=true
[bkchr/proc-macro-crate#15]: bkchr/proc-macro-crate#15
MarijnS95 added a commit to rust-mobile/ndk that referenced this pull request Feb 15, 2022
`proc_macro_crate::crate_name` fails to find our `ndk-glue`
crate when it sits inside `[target.'cfg(target_os =
"android")'.dev-dependencies]` as shown in [RustAudio/cpal#641], with a
fix pending in [bkchr/proc-macro-crate#15].  This function is only ever
called with `"ndk-glue"` which is an invalid identifier and should have
its dash replaced with an underscore to be valid.  However, since this
fallback doesn't seem to have been hit before (we've never received
reports of `` `"ndk-glue"` is not a valid identifier ``) it is safe and
desired to enforce the crate name to reside in `Cargo.toml` and
`proc-macro-crate` being able to find it.

[RustAudio/cpal#641]: https://github.com/RustAudio/cpal/runs/5203107529?check_suite_focus=true
[bkchr/proc-macro-crate#15]: bkchr/proc-macro-crate#15
@MarijnS95
Copy link
Contributor Author

@est31 the fix to proc-macro-crate has been merged and released, re-running the CI should now succeed!

(The workaround has also been removed from ndk-macro, but since it's not critical I haven't bothered to create a release yet)

@MarijnS95
Copy link
Contributor Author

Well, I'm not sure how this happened: I made sure to test the fix locally, yet it seems we're now hit by bkchr/proc-macro-crate#16... Not sure how I got it working without that at some point.

@est31
Copy link
Member

est31 commented Feb 15, 2022

@MarijnS95 it's alright, take your time. I'm impressed by how quickly you figured out the cause of the problem and came up with a solution!

@MarijnS95
Copy link
Contributor Author

@est31 I just don't like making PRs (to proc-macro-crate) that are "half finished" and didn't actually fix the issue, shows that my testing methodology was/is messed up somewhere 😳

Anyway, the second fix is merged and released now too. I double checked it locally and the CI should not fail on it now 🤞

@MarijnS95
Copy link
Contributor Author

Looks like we're all set now, the current issues also appear on master. Perhaps these CI steps should be made infallible so that it looks a bit nicer in the git log, instead of seeing everything failed?

@est31 est31 merged commit 6825084 into RustAudio:master Feb 16, 2022
@est31
Copy link
Member

est31 commented Feb 16, 2022

Thanks again, please write if you want a release. As for the CI failures, yeah maybe the three failling jobs should be commented out or be allowed to fail. Ideally at least the windows job would be fixed tho. PRs welcome :).

@MarijnS95 MarijnS95 deleted the ndk-context branch February 16, 2022 22:25
@MarijnS95
Copy link
Contributor Author

No need for a release from my side yet, just propagating our android-ndk-rs changes through the most popular crates in the ecosystem. Perhaps @zarik5?

@zmerp
Copy link

zmerp commented Feb 16, 2022

I don't have any hurry, I'm keeping my cpal and oboe forks for my project. But once both this PR and the oboe one hit a new release I will be able to remove some duplicate code, thanks to ndk_context::initialize_android_context() (my project can't use ndk-glue yet)

rib pushed a commit to rust-mobile/ndk-glue that referenced this pull request Dec 6, 2022
`proc_macro_crate::crate_name` fails to find our `ndk-glue`
crate when it sits inside `[target.'cfg(target_os =
"android")'.dev-dependencies]` as shown in [RustAudio/cpal#641], with a
fix pending in [bkchr/proc-macro-crate#15].  This function is only ever
called with `"ndk-glue"` which is an invalid identifier and should have
its dash replaced with an underscore to be valid.  However, since this
fallback doesn't seem to have been hit before (we've never received
reports of `` `"ndk-glue"` is not a valid identifier ``) it is safe and
desired to enforce the crate name to reside in `Cargo.toml` and
`proc-macro-crate` being able to find it.

[RustAudio/cpal#641]: https://github.com/RustAudio/cpal/runs/5203107529?check_suite_focus=true
[bkchr/proc-macro-crate#15]: bkchr/proc-macro-crate#15
toptalhook pushed a commit to toptalhook/proc-macro-crate that referenced this pull request Jun 1, 2024
In [RustAudio/cpal#641] we're seeing a [failure in `ndk-macro`] to turn
a crate name with dashes into underscores as fallback, with the
underlying issue that `proc-macro-crate` failed to find our crate in
`Cargo.toml` when limited to a specific target cfg.  According to [the
Rust docs] it is perfectly possible to have `dev-dependencies` behind
such a target predicate too.

[RustAudio/cpal#641]: RustAudio/cpal#641 (comment)
[failure in `ndk-macro`]: https://github.com/rust-windowing/android-ndk-rs/blob/9060d5dbea8bdd9aee9e42ef8e8375c35adbc271/ndk-macro/src/helper.rs#L64
[the Rust docs]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies
toptalhook added a commit to toptalhook/proc-macro-crate that referenced this pull request Jun 1, 2024
In [RustAudio/cpal#641] we're seeing a [failure in `ndk-macro`] to turn
a crate name with dashes into underscores as fallback, with the
underlying issue that `proc-macro-crate` failed to find our crate in
`Cargo.toml` when limited to a specific target cfg.  According to [the
Rust docs] it is perfectly possible to have `dev-dependencies` behind
such a target predicate too.

[RustAudio/cpal#641]: RustAudio/cpal#641 (comment)
[failure in `ndk-macro`]: https://github.com/rust-windowing/android-ndk-rs/blob/9060d5dbea8bdd9aee9e42ef8e8375c35adbc271/ndk-macro/src/helper.rs#L64
[the Rust docs]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies
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

Successfully merging this pull request may close these issues.

3 participants