Skip to content

Commit 950c064

Browse files
authored
[rcore][android] Replace android_fopen() with linker --wrap=fopen (#5605)
* ANDROID: replace android_fopen with linker --wrap=fopen * ANDROID: add --wrap=fopen linker flag to src/Makefile
1 parent f583674 commit 950c064

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
lines changed

src/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ if (${PLATFORM} MATCHES "Web")
7575
endif()
7676
endif()
7777

78+
if (${PLATFORM} MATCHES "Android")
79+
# Wrap fopen at link time so all code (including third-party libs) goes
80+
# through __wrap_fopen, which handles Android APK asset loading
81+
target_link_options(raylib INTERFACE -Wl,--wrap=fopen)
82+
endif()
83+
7884
set_target_properties(raylib PROPERTIES
7985
PUBLIC_HEADER "${raylib_public_headers}"
8086
VERSION ${PROJECT_VERSION}

src/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,9 @@ ifeq ($(TARGET_PLATFORM),PLATFORM_ANDROID)
614614
LDFLAGS += -Lsrc
615615
# Avoid unresolved symbol pointing to external main()
616616
LDFLAGS += -Wl,-undefined,dynamic_lookup
617+
# Wrap fopen at link time so all code (including third-party libs) goes
618+
# through __wrap_fopen, which handles Android APK asset loading
619+
LDFLAGS += -Wl,--wrap=fopen
617620
endif
618621

619622
# Define libraries required on linking: LDLIBS

src/platforms/rcore_android.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,12 @@ static int android_write(void *cookie, const char *buf, int size);
275275
static fpos_t android_seek(void *cookie, fpos_t offset, int whence);
276276
static int android_close(void *cookie);
277277

278-
FILE *android_fopen(const char *fileName, const char *mode); // Replacement for fopen() -> Read-only!
278+
FILE *__real_fopen(const char *fileName, const char *mode); // Real fopen, provided by the linker (--wrap=fopen)
279+
FILE *__wrap_fopen(const char *fileName, const char *mode); // Replacement for fopen()
280+
279281
FILE *funopen(const void *cookie, int (*readfn)(void *, char *, int), int (*writefn)(void *, const char *, int),
280282
fpos_t (*seekfn)(void *, fpos_t, int), int (*closefn)(void *));
281283

282-
#define fopen(name, mode) android_fopen(name, mode)
283-
284284
//----------------------------------------------------------------------------------
285285
// Module Functions Declaration
286286
//----------------------------------------------------------------------------------
@@ -1524,25 +1524,20 @@ static void SetupFramebuffer(int width, int height)
15241524
}
15251525
}
15261526

1527-
// Replacement for fopen()
1527+
// Replacement for fopen(), used as linker wrap entry point (-Wl,--wrap=fopen)
15281528
// REF: https://developer.android.com/ndk/reference/group/asset
1529-
FILE *android_fopen(const char *fileName, const char *mode)
1529+
FILE *__wrap_fopen(const char *fileName, const char *mode)
15301530
{
15311531
FILE *file = NULL;
1532-
1532+
1533+
// NOTE: AAsset provides access to read-only asset, write operations use regular fopen
15331534
if (mode[0] == 'w')
15341535
{
1535-
// NOTE: fopen() is mapped to android_fopen() that only grants read access to
1536-
// assets directory through AAssetManager but it could be required to write data
1537-
// using the standard stdio FILE access functions
1538-
// REF: https://stackoverflow.com/questions/11294487/android-writing-saving-files-from-native-code-only
1539-
#undef fopen
1540-
file = fopen(TextFormat("%s/%s", platform.app->activity->internalDataPath, fileName), mode);
1541-
#define fopen(name, mode) android_fopen(name, mode)
1536+
file = __real_fopen(TextFormat("%s/%s", platform.app->activity->internalDataPath, fileName), mode);
1537+
if (file == NULL) file = __real_fopen(fileName, mode);
15421538
}
15431539
else
15441540
{
1545-
// NOTE: AAsset provides access to read-only asset
15461541
AAsset *asset = AAssetManager_open(platform.app->activity->assetManager, fileName, AASSET_MODE_UNKNOWN);
15471542

15481543
if (asset != NULL)
@@ -1552,14 +1547,12 @@ FILE *android_fopen(const char *fileName, const char *mode)
15521547
}
15531548
else
15541549
{
1555-
#undef fopen
15561550
// Just do a regular open if file is not found in the assets
1557-
file = fopen(TextFormat("%s/%s", platform.app->activity->internalDataPath, fileName), mode);
1558-
if (file == NULL) file = fopen(fileName, mode);
1559-
#define fopen(name, mode) android_fopen(name, mode)
1551+
file = __real_fopen(TextFormat("%s/%s", platform.app->activity->internalDataPath, fileName), mode);
1552+
if (file == NULL) file = __real_fopen(fileName, mode);
15601553
}
15611554
}
1562-
1555+
15631556
return file;
15641557
}
15651558

src/raudio.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,6 @@ typedef struct tagBITMAPINFOHEADER {
180180
#include <stdio.h> // Required for: FILE, fopen(), fclose(), fread()
181181
#include <string.h> // Required for: strcmp() [Used in IsFileExtension(), LoadWaveFromMemory(), LoadMusicStreamFromMemory()]
182182

183-
#if defined(PLATFORM_ANDROID)
184-
FILE *android_fopen(const char *fileName, const char *mode);
185-
#define fopen(name, mode) android_fopen(name, mode)
186-
#endif
187-
188183
#if defined(RAUDIO_STANDALONE)
189184
#ifndef TRACELOG
190185
#define TRACELOG(level, ...) printf(__VA_ARGS__)

0 commit comments

Comments
 (0)