Fix SDL_MAIN_USE_CALLBACKS with Android, introduce SDL_MAIN_EXPORTED

My simplification of the conditions for including SDL_main_impl.h
had one problem: I forgot that SDL_main_impl.h must be included
even on Android when SDL_MAIN_USE_CALLBACKS is used, because then a
SDL_main() function that makes sure the callbacks get called is needed,
and that function is implemented in SDL_main_impl.h

But OTOH, even when SDL_MAIN_USE_CALLBACKS is used, SDL_main_impl.h
should not implement a standard `int main(...)` function on Android
(because there the SDL-using native code is compiled as a library and
 the entry point is in SDLActivity.java, which calls SDL_main()
 in said library).

So the check for platforms that don't have *any* native main function
but just SDL_main() called from the outside should be handled in
SDL_main_impl.h, so both the normal and the callback case can avoid
generating a standard main() in the same way.
To do this, SDL_MAIN_EXPORTED is defined for platforms like Android,
where the real entry point (main() function) is outside of the code
that uses SDL, so
- SDL_main() must be visibly exported with SDL_DECLSPEC, so the outside
  code can call it
- SDL_main_impl.h must not implement a "real" main() function

Another small change based on this is defining SDLMAIN_DECLSPEC
at a more general place.

If another platform like Android (where the entry point is somewhere
else entirely, possibly implemented in a different programming language)
turns up, defining SDL_MAIN_NEEDED (so the users main() is renamed
to SDL_main()) and SDL_MAIN_EXPORTED should be all that's needed on the
SDL_main.h side - and if not then at least the implementation is
cleaner and clearer now, IMHO.

fixes #11162
This commit is contained in:
Daniel Gibson 2024-10-11 23:04:00 +02:00 committed by Sam Lantinga
parent 86ef790a23
commit 33366b0a4e
2 changed files with 21 additions and 15 deletions

View File

@ -86,8 +86,14 @@
*/ */
#define SDL_MAIN_NEEDED #define SDL_MAIN_NEEDED
/* We need to export SDL_main so it can be launched from Java */ /* As this is launched from Java, the real entry point (main() function)
#define SDLMAIN_DECLSPEC SDL_DECLSPEC is outside of the the binary built from this code.
This define makes sure that, unlike on other platforms, SDL_main.h
and SDL_main_impl.h export an `SDL_main()` function (to be called
from Java), but don't implement a native `int main(int argc, char* argv[])`
or similar.
*/
#define SDL_MAIN_EXPORTED
#elif defined(SDL_PLATFORM_EMSCRIPTEN) #elif defined(SDL_PLATFORM_EMSCRIPTEN)
/* On Emscripten, SDL provides a main function that converts URL /* On Emscripten, SDL provides a main function that converts URL
@ -134,9 +140,14 @@
#endif #endif
#endif /* SDL_MAIN_HANDLED */ #endif /* SDL_MAIN_HANDLED */
#ifndef SDLMAIN_DECLSPEC #ifdef SDL_MAIN_EXPORTED
/* We need to export SDL_main so it can be launched from external code,
like SDLActivity.java on Android */
#define SDLMAIN_DECLSPEC SDL_DECLSPEC
#else
/* usually this is empty */
#define SDLMAIN_DECLSPEC #define SDLMAIN_DECLSPEC
#endif #endif /* SDL_MAIN_EXPORTED */
#ifdef SDL_WIKI_DOCUMENTATION_SECTION #ifdef SDL_WIKI_DOCUMENTATION_SECTION
@ -562,14 +573,8 @@ extern SDL_DECLSPEC void SDLCALL SDL_GDKSuspendComplete(void);
#include <SDL3/SDL_close_code.h> #include <SDL3/SDL_close_code.h>
#if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL) #if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL)
/* include header-only SDL_main implementations /* include header-only SDL_main implementations */
* Note: currently Android is the only platform where we rename main() to SDL_main() but #if defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE)
* do *not* use SDL_main_impl.h (because SDL_main() is called from external Java code).
* If other platforms like that turn up, add them next to "defined(SDL_PLATFORM_ANDROID)"
*/
#if (defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE)) && \
!defined(SDL_PLATFORM_ANDROID)
/* platforms which main (-equivalent) can be implemented in plain C */ /* platforms which main (-equivalent) can be implemented in plain C */
#include <SDL3/SDL_main_impl.h> #include <SDL3/SDL_main_impl.h>
#endif #endif

View File

@ -29,7 +29,7 @@
#endif #endif
/* if someone wants to include SDL_main.h but doesn't want the main handing magic, /* if someone wants to include SDL_main.h but doesn't want the main handing magic,
(maybe to call SDL_RegisterApp()) they can #define SDL_MAIN_HANDLED first (maybe to call SDL_RegisterApp()) they can #define SDL_MAIN_HANDLED first.
SDL_MAIN_NOIMPL is for SDL-internal usage (only affects implementation, SDL_MAIN_NOIMPL is for SDL-internal usage (only affects implementation,
not definition of SDL_MAIN_AVAILABLE etc in SDL_main.h) and if the user wants not definition of SDL_MAIN_AVAILABLE etc in SDL_main.h) and if the user wants
to have the SDL_main implementation (from this header) in another source file to have the SDL_main implementation (from this header) in another source file
@ -64,8 +64,9 @@
#endif /* SDL_MAIN_USE_CALLBACKS */ #endif /* SDL_MAIN_USE_CALLBACKS */
/* set up the usual SDL_main stuff if we're not using callbacks or if we are but need the normal entry point. */ /* set up the usual SDL_main stuff if we're not using callbacks or if we are but need the normal entry point,
#if !defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD) unless the real entry point needs to be somewhere else entirely, like Android where it's in Java code */
#if (!defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD)) && !defined(SDL_MAIN_EXPORTED)
#if defined(SDL_PLATFORM_WINDOWS) #if defined(SDL_PLATFORM_WINDOWS)