Skip to content

ndk-macro: Reexport android_logger from ndk-glue and use it in macros#88

Merged
MarijnS95 merged 5 commits intorust-mobile:masterfrom
MarijnS95:provide-android_logger-crate-for-macro
May 19, 2021
Merged

ndk-macro: Reexport android_logger from ndk-glue and use it in macros#88
MarijnS95 merged 5 commits intorust-mobile:masterfrom
MarijnS95:provide-android_logger-crate-for-macro

Conversation

@MarijnS95
Copy link
Copy Markdown
Member

Hi all,

Upfront apologies for my bluntness in this "RFC": I'm usually rather disappointed after running into undeclared type or module errors when a macro generates code in my (programs) scope with a hard dependency on some crate or symbol there.

This PR is a suggested "fix" for addressing the missing android_logger logger when enabling the logging feature, without importing that crate into the target package. It is not how I envision a final solution so I'm opening this for feedback, read on for more questions. Note that tests were intentionally not updated until a proper discussion how to best implement this.

I haven't really understood the purpose of providing the path to android_logger in the macro; it is already linked by ndk-glue and is best used from there. If this PR finds its way to master one way or another, how about removing this "path override" feature, as there should now be one consistent place to reference android_logger from? We might give log the same treatment, even though this is generally available in the same scope to print log messages.

ndk-glue doesn't seem to use android_logger at all, does it make more sense to move this dependency into ndk-macro so that it can be used on its own? There's no $crate::android_logger in proc-macro to make this any easier though.

@msiglreith msiglreith self-requested a review November 3, 2020 16:04
@msiglreith
Copy link
Copy Markdown
Contributor

Relates to #53
On the topic I'm not sure yet how to approach this, I see that's not optimal but haven't looked into these parts very much so far. Maybe we can also minimize the feature set of the macro.

@msiglreith msiglreith added status: needs discussion Direction must be ironed out status: needs review A maintainer must review this code labels Nov 3, 2020
@august64
Copy link
Copy Markdown
Contributor

august64 commented Nov 3, 2020

Thanks for calling this out and making a PR! We always want to improve user experience. I'm definitely in favor of macros being self-contained; implicitly requiring the user to add some deps really sucks.

how about removing this "path override" feature

If we're re-exporting android_logger/etc. ourselves, then I agree that we don't need path overrides for it. We'll still need one for ndk-glue though; #53 explains the general motive for that feature in depth.

ndk-glue doesn't seem to use android_logger

The only reason ndk-macro is separate from ndk-glue is because of the requirements of proc macro crates. They're still conceptually merged though; end-users only directly depend on ndk-glue, since ndk-glue exports the contents of ndk-macro. Therefore, I think it's very reasonable for ndk-glue to contain dependencies for ndk-macro, since the separation between the two crates is just an implementation detail. I'd even argue that it's the job of the crate that re-exports a macro to also export its dependencies.

does it make more sense to move this dependency into ndk-macro so that it can be used on its own?

I don't think that's possible? Proc macro crates are pretty limited in what they can export.

There's no $crate::android_logger in proc-macro to make this any easier though.

We already have prior art in ndk-macro for working around this (also discussed in #53), as un-fun as it is.

@MarijnS95
Copy link
Copy Markdown
Member Author

how about removing this "path override" feature

If we're re-exporting android_logger/etc. ourselves, then I agree that we don't need path overrides for it. We'll still need one for ndk-glue though; #53 explains the general motive for that feature in depth.

I'll remove the path to android_logger as it is not necessary, but what do you think about log? It's probably safest to re-export a known-compatible version and remove the path too. Crate users can then leverage ndk_glue::log or add the crate to Cargo.toml.
(A specific example: what if a user wants to configure logging level without printing any logs themselves, like when attempting to trace something in winit which utilizes trace! a bunch?)

ndk-glue doesn't seem to use android_logger

The only reason ndk-macro is separate from ndk-glue is because of the requirements of proc macro crates. They're still conceptually merged though; end-users only directly depend on ndk-glue, since ndk-glue exports the contents of ndk-macro. Therefore, I think it's very reasonable for ndk-glue to contain dependencies for ndk-macro, since the separation between the two crates is just an implementation detail. I'd even argue that it's the job of the crate that re-exports a macro to also export its dependencies.

Right, they are already supposed to be used together if the rest of the code is believed.

does it make more sense to move this dependency into ndk-macro so that it can be used on its own?

I don't think that's possible? Proc macro crates are pretty limited in what they can export.

Indeed, I wasn't aware of that. Since you've just made the case for the two crates being heavily tied together there is no reason to move this - it would in fact be impossible which is why this structure is in place already 😊

There's no $crate::android_logger in proc-macro to make this any easier though.

We already have prior art in ndk-macro for working around this (also discussed in #53), as un-fun as it is.

I see, that was clarified there as well. crate_path is already used for ndk-glue so that's a breeze.

@MarijnS95 MarijnS95 force-pushed the provide-android_logger-crate-for-macro branch from afd08aa to b285c07 Compare November 3, 2020 22:38
@MarijnS95
Copy link
Copy Markdown
Member Author

Pretty glad all these nice tests are now actually ran by the CI following #89, makes me feel a lot safer even though I made sure to run most of them locally before pushing :)

@MarijnS95 MarijnS95 force-pushed the provide-android_logger-crate-for-macro branch from b285c07 to d1b883f Compare November 3, 2020 22:46
@august64
Copy link
Copy Markdown
Contributor

august64 commented Nov 4, 2020

what do you think about log? It's probably safest to re-export a known-compatible version and remove the path too. Crate users can then leverage ndk_glue::log or add the crate to Cargo.toml.

I think that seems reasonable. Is compatibility much of a problem here? log seems super stable.

(A specific example: what if a user wants to configure logging level without printing any logs themselves, like when attempting to trace something in winit which utilizes trace! a bunch?)

That wouldn't actually involve using the log crate, right? Either way, I'm glad to hear winit produces a lot of logging! Maybe this issue should be marked as resolved...

@MarijnS95 MarijnS95 force-pushed the provide-android_logger-crate-for-macro branch from d1b883f to 771e574 Compare November 4, 2020 10:50
@MarijnS95
Copy link
Copy Markdown
Member Author

what do you think about log? It's probably safest to re-export a known-compatible version and remove the path too. Crate users can then leverage ndk_glue::log or add the crate to Cargo.toml.

I think that seems reasonable. Is compatibility much of a problem here? log seems super stable.

Agreed there should be no problem in android-ndk using a "random" log version from the surrounding program/scope, leaving usage ergonomics as the sole argument.

(A specific example: what if a user wants to configure logging level without printing any logs themselves [...])

That wouldn't actually involve using the log crate, right?

It doesn't involve the log crate if one merely wants to configure the logger with logger(level = "trace", tag = "my-tag"), suffering the same problem as android_logger outlined above. Pushed the change necessary to pull log from ndk_glue as well.

Either way, I'm glad to hear winit produces a lot of logging! Maybe this issue should be marked as resolved...

Unfortunately nope. Turns out that's merely a trace! call in ndk_glue::wake, printed for every event that arrives.

@MarijnS95
Copy link
Copy Markdown
Member Author

@francesca64 @msiglreith Is anything left to be discussed here? If I remember correctly (it's been a while) this should be ready for review and merging actually :)

@august64
Copy link
Copy Markdown
Contributor

if I remember correctly (it's been a while) this should be ready for review and merging actually :)

That's my recollection too!

MarijnS95 added 4 commits May 18, 2021 21:57
This crate does not contain executables nor libraries, just the examples
in the example directory.  This is already confusing [1] [2] as they can
only be run with something like:

    cargo apk run -p ndk-examples --example hello_world

Or cd-ing into `ndk-examples` before being able to run these.

Unfortunately `cargo apk run -p ndk-examples` keeps working fine,
resulting in:

    Error: Path "target/aarch64-linux-android/debug/libndk_examples.so" doesn't exist.

Despite the crate clearly not exposing a library target.  The same
happens without this change because `cdylib` is not set for `[lib]`,
but time is still spent compiling all the dependencies instead of
erroring out straight away.

[1]: rust-mobile#48 (comment)
[2]: rust-mobile#29 (comment)
It is more common to use a logging framework instead of printing raw
text to stdout that ends up in the logs; lead by example.

More importantly this exemplifies a current issue with android_logger
not being exported by the relevant macro crate.
When enabling the logger feature:

    error[E0433]: failed to resolve: use of undeclared type or module `android_logger`
     --> ndk-examples/examples/hello_world.rs:6:5
      |
    6 |     ndk_glue::main(backtrace = "on", logger(level = "debug", tag = "hello-world"))
      |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared type or module `android_logger`
      |
      = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

This means the crate user is responsible for importing the
android_logger crate in their namespace where this macro is used.  That
is not ideal, especially when ndk-glue already imports it but doesn't
seem to use it anywhere.
In accordance with a prior, identical change for android_logger:
reexport the log crate from ndk-glue and use it in the macro instead of
relying on crate users to provide log in this scope or override the
path.
This is especially useful if a crate wishes to configure the log level
or tag without needing the log crate in the scope where ndk_glue::main
is called.
@MarijnS95 MarijnS95 force-pushed the provide-android_logger-crate-for-macro branch from 59114b9 to 7f9eeb8 Compare May 18, 2021 19:58
@MarijnS95
Copy link
Copy Markdown
Member Author

That's my recollection too!

Cool! Are you in a position to give this a quick read-through and review?

@MarijnS95 MarijnS95 changed the title [RFC] ndk-macro: Reexport android_logger from ndk-glue and use it in macros ndk-macro: Reexport android_logger from ndk-glue and use it in macros May 19, 2021
@MarijnS95 MarijnS95 requested a review from august64 May 19, 2021 07:34
@MarijnS95
Copy link
Copy Markdown
Member Author

Documented the changes in CHANGELOG.md, I think we're almost good to go 🎉!

Copy link
Copy Markdown
Contributor

@august64 august64 left a comment

Choose a reason for hiding this comment

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

Thanks so much!

@MarijnS95 MarijnS95 removed the status: needs discussion Direction must be ironed out label May 19, 2021
@MarijnS95 MarijnS95 removed the status: needs review A maintainer must review this code label May 19, 2021
@MarijnS95 MarijnS95 merged commit 197d491 into rust-mobile:master May 19, 2021
@MarijnS95 MarijnS95 deleted the provide-android_logger-crate-for-macro branch May 19, 2021 18:09
@MarijnS95
Copy link
Copy Markdown
Member Author

Thanks for the review all!

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