ctru-rs icon indicating copy to clipboard operation
ctru-rs copied to clipboard

Use `userAppInit` and `userAppExit` for runtime setup and takedown

Open FenrirWolf opened this issue 3 years ago • 8 comments

Fun fact, libctru supports life before and after main in the appInit and appExit functions. You can hook into this system by defining extern "C" fn userAppInit() and extern "C" fn userAppExit() functions and putting any* library-level setup/teardown code you want in them.

*Well, some things in the Rust stdlib aren't safe to call before or after main. But 3DS service initialization and even panic hook registration should be fine to do there.

FenrirWolf avatar Mar 19 '22 07:03 FenrirWolf

Oh, this means we could avoid the call to ctru::init at the start of programs.

Edit: seems like we can't. The issue is obviously with linking, the symbols aren't escalated since believed to be unused... having #[used(linker)] available for functions too is a priority, most of our toolchain is based on that.

Meziu avatar Mar 19 '22 10:03 Meziu

Hmm, that's unfortunate. It's also possible to override appInit and appExit themselves, and I wonder if that would work better since libctru calls those unconditionally. I'd try it out myself but I haven't worked out what the build process for the project is yet

FenrirWolf avatar Mar 19 '22 23:03 FenrirWolf

One downside of using this feature is that then users can't use this, since the symbol would already be defined.

AzureMarker avatar Mar 20 '22 01:03 AzureMarker

I'd try it out myself but I haven't worked out what the build process for the project is yet

It's pretty much take the rust-horizon rustc fork, compile locally and add to rustup, then use cargo-3ds for building and stuff. Plus the devkitpro installation. We should probably write this up somewhere.

AzureMarker avatar Mar 20 '22 02:03 AzureMarker

We should probably write this up somewhere.

That would be good. I've already run into issues getting the thing to build so a place to talk about that would be nice too

FenrirWolf avatar Mar 20 '22 02:03 FenrirWolf

One downside of using this feature is that then users can't use this, since the symbol would already be defined.

We could add our own safe hook, though for now it doesn’t matter.

Meziu avatar Mar 20 '22 10:03 Meziu

We should probably write this up somewhere.

That would be good. I've already run into issues getting the thing to build so a place to talk about that would be nice too

We were waiting for Rust to merge our changes before making wikis, this way people would be able to just use cargo 3ds on a nightly toolchain and be able to compile.

Meziu avatar Mar 20 '22 10:03 Meziu

We could enable and use the GitHub discussions feature in the meantime.

AzureMarker avatar Mar 20 '22 22:03 AzureMarker

It may work now, but due to the uncertainty of using the std outside the main and wanting to leave the possibility of customization to the user, this issue is now obsolete. Didn't think I'd ever close this! 😄

Meziu avatar Feb 15 '23 14:02 Meziu