Skip to content

[rcore][android] Replace android_fopen() with linker --wrap=fopen#5605

Merged
raysan5 merged 2 commits intoraysan5:masterfrom
ghera:fix/android-fopen-raudio-rmodel
Mar 2, 2026
Merged

[rcore][android] Replace android_fopen() with linker --wrap=fopen#5605
raysan5 merged 2 commits intoraysan5:masterfrom
ghera:fix/android-fopen-raudio-rmodel

Conversation

@ghera
Copy link
Contributor

@ghera ghera commented Feb 28, 2026

Context

This is a follow-up to #5589, which fixed LoadMusicStream failing on Android for OGG, WAV, and MP3 formats. That fix was accepted by @raysan5 as a temporary patch — adding a #define fopen android_fopen override directly inside raudio.c — with the hope of finding a cleaner solution.

As discussed in the comments of #5589, @maiconpintoabreu proposed this alternative approach: using the linker --wrap=fopen option to redirect all fopen calls at link time. This PR implements that approach.

What changed

  • src/CMakeLists.txt: Added target_link_options(raylib INTERFACE -Wl,--wrap=fopen) for Android builds, so the linker redirects all fopen calls to __wrap_fopen transparently.
  • src/Makefile: Added LDFLAGS += -Wl,--wrap=fopen.
  • src/platforms/rcore_android.c: Renamed android_fopen__wrap_fopen and added a __real_fopen declaration. Removed all #undef fopen inside the function body. Also fixes a missing fallback in write mode ('w'): the old code only tried internalDataPath/fileName; now falls back to the literal path if that fails, allowing absolute paths to work correctly.
  • src/raudio.c: Removed the platform-specific #define fopen android_fopen block added in the previous patch.

Why this is better than the temporary patch

The macro approach only covered the source files that explicitly included the override. The linker --wrap=fopen covers all source files and third-party libraries simultaneously:

  • LoadMusicStream() for OGG/WAV/MP3 — already fixed in via macro; now covered cleanly
  • LoadOBJ() and LoadMaterials() for OBJ/MTL files — when loading the companion .mtl file, tinyobj_loader_c.h calls fopen directly (tinyobj_parse_and_index_mtl_file) and was not covered by the previous patch
  • Any future file-loading code (new formats, new decoders) — automatically covered with no extra changes needed
  • __wrap_fopen is cleaner than the old android_fopen without the #undef / #define trick.

Testing

Tested on Android with:

  • LoadMusicStream (read) for OGG, WAV, and MP3 — loading from APK assets via relative path
  • C++ iostream (write) to external storage via absolute path — which in the Android NDK internally uses fopen — confirming third-party code is correctly intercepted

Absolute path write support also benefits from the new fallback added in __wrap_fopen: when internalDataPath/fileName fails, the function now retries with the path as-is, which was previously missing.

@maiconpintoabreu
Copy link
Contributor

Hi @ghera, do not forget the Makefile if ray accepts it.

@raysan5 raysan5 changed the title ANDROID: replace android_fopen with linker --wrap=fopen [rcore][android] Replace android_fopen() with linker --wrap=fopen Mar 1, 2026
@raysan5 raysan5 added the platform: Android Android platform label Mar 1, 2026
@raysan5 raysan5 merged commit 950c064 into raysan5:master Mar 2, 2026
16 checks passed
@raysan5
Copy link
Owner

raysan5 commented Mar 2, 2026

@ghera @maiconpintoabreu thanks for the improvement and review, I like the code simplification but I'm a but scary about moving that feature control to the build system instead of keeping it inside code, what about build.zig? what about other custom build systems on user side...

I'm merging it and let's see if it generates further issues on that regards... I also think it should be better documented in the code with some // WARNING: ... messages.

@ghera ghera deleted the fix/android-fopen-raudio-rmodel branch March 2, 2026 23:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform: Android Android platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants