[rcore][android] Replace android_fopen() with linker --wrap=fopen#5605
Merged
raysan5 merged 2 commits intoraysan5:masterfrom Mar 2, 2026
Merged
[rcore][android] Replace android_fopen() with linker --wrap=fopen#5605raysan5 merged 2 commits intoraysan5:masterfrom
android_fopen() with linker --wrap=fopen#5605raysan5 merged 2 commits intoraysan5:masterfrom
Conversation
Contributor
|
Hi @ghera, do not forget the Makefile if ray accepts it. |
android_fopen() with linker --wrap=fopen
Owner
|
@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 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
This is a follow-up to #5589, which fixed
LoadMusicStreamfailing on Android for OGG, WAV, and MP3 formats. That fix was accepted by @raysan5 as a temporary patch — adding a#define fopen android_fopenoverride directly insideraudio.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=fopenoption to redirect allfopencalls at link time. This PR implements that approach.What changed
src/CMakeLists.txt: Addedtarget_link_options(raylib INTERFACE -Wl,--wrap=fopen)for Android builds, so the linker redirects allfopencalls to__wrap_fopentransparently.src/Makefile: AddedLDFLAGS += -Wl,--wrap=fopen.src/platforms/rcore_android.c: Renamedandroid_fopen→__wrap_fopenand added a__real_fopendeclaration. Removed all#undef fopeninside the function body. Also fixes a missing fallback in write mode ('w'): the old code only triedinternalDataPath/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_fopenblock 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=fopencovers all source files and third-party libraries simultaneously:LoadMusicStream()for OGG/WAV/MP3 — already fixed in via macro; now covered cleanlyLoadOBJ()andLoadMaterials()for OBJ/MTL files — when loading the companion.mtlfile,tinyobj_loader_c.hcallsfopendirectly (tinyobj_parse_and_index_mtl_file) and was not covered by the previous patch__wrap_fopenis cleaner than the old android_fopen without the#undef/#definetrick.Testing
Tested on Android with:
LoadMusicStream(read) for OGG, WAV, and MP3 — loading from APK assets via relative pathiostream(write) to external storage via absolute path — which in the Android NDK internally usesfopen— confirming third-party code is correctly interceptedAbsolute path write support also benefits from the new fallback added in
__wrap_fopen: wheninternalDataPath/fileNamefails, the function now retries with the path as-is, which was previously missing.