<commit_before>
template sdlMain*() =
    when defined(ios) or defined(android):
        when not compileOption("noMain"):
            {.error: "Please run Nim with --noMain flag.".}

        import nimx/system_logger
        {.push stackTrace: off.}
        proc setupLogger() {.cdecl.} =
            errorMessageWriter = proc(msg: string) =
                logi msg
        {.pop.}

        when defined(ios):
            {.emit: "#define __IPHONEOS__".}

        {.emit: """
// The following piece of code is a copy-paste from SDL/SDL_main.h
// It is required to avoid dependency on SDL headers
////////////////////////////////////////////////////////////////////////////////


/**
 *  \file SDL_main.h
 *
 *  Redefine main() on some platforms so that it is called by SDL.
 */

#ifndef SDL_MAIN_HANDLED
#if defined(__WIN32__)
/* On Windows SDL provides WinMain(), which parses the command line and passes
   the arguments to your main function.

   If you provide your own WinMain(), you may define SDL_MAIN_HANDLED
 */
#define SDL_MAIN_AVAILABLE

#elif defined(__WINRT__)
/* On WinRT, SDL provides a main function that initializes CoreApplication,
   creating an instance of IFrameworkView in the process.

   Please note that #include'ing SDL_main.h is not enough to get a main()
   function working.  In non-XAML apps, the file,
   src/main/winrt/SDL_WinRT_main_NonXAML.cpp, or a copy of it, must be compiled
   into the app itself.  In XAML apps, the function, SDL_WinRTRunApp must be
   called, with a pointer to the Direct3D-hosted XAML control passed in.
*/
#define SDL_MAIN_NEEDED

#elif defined(__IPHONEOS__)
/* On iOS SDL provides a main function that creates an application delegate
   and starts the iOS application run loop.

   See src/video/uikit/SDL_uikitappdelegate.m for more details.
 */
#define SDL_MAIN_NEEDED

#elif defined(__ANDROID__)
/* On Android SDL provides a Java class in SDLActivity.java that is the
   main activity entry point.

   See README-android.txt for more details on extending that class.
 */
#define SDL_MAIN_NEEDED

#endif
#endif /* SDL_MAIN_HANDLED */

#ifdef __cplusplus
#define C_LINKAGE   "C"
#else
#define C_LINKAGE
#endif /* __cplusplus */

/**
 *  \file SDL_main.h
 *
 *  The application's main() function must be called with C linkage,
 *  and should be declared like this:
 *  \code
 *  #ifdef __cplusplus
 *  extern "C"
 *  #endif
 *  int main(int argc, char *argv[])
 *  {
 *  }
 *  \endcode
 */

#if defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE)
#define main    SDL_main
#endif




//#include <SDL2/SDL_main.h>

extern int cmdCount;
extern char** cmdLine;
extern char** gEnv;

N_CDECL(void, NimMain)(void);

int main(int argc, char** args) {
    cmdLine = args;
    cmdCount = argc;
    gEnv = NULL;
    `setupLogger`();
    NimMain();
    return nim_program_result;
}

""".}

when not defined(emscripten):
    import macros

    when defined(macosx) or defined(ios):
        macro passToCAndL(s: string): typed =
            result = newNimNode(nnkStmtList)
            result.add parseStmt("{.passL: \"" & s.strVal & "\".}\n")
            result.add parseStmt("{.passC: \"" & s.strVal & "\".}\n")

        macro useFrameworks(n: varargs[string]): typed =
            result = newNimNode(nnkStmtList, n)
            for i in 0..n.len-1:
                result.add parseStmt("passToCAndL(\"-framework " & n[i].strVal & "\")")

    when defined(ios):
        useFrameworks("OpenGLES", "UIKit", "GameController", "CoreMotion")
        when not defined(simulator):
            when hostCPU == "arm":
                {.passC:"-arch armv7".}
                {.passL:"-arch armv7".}
            elif hostCPU == "arm64":
                {.passC:"-arch arm64".}
                {.passL:"-arch arm64".}
    elif defined(macosx):
        useFrameworks("OpenGL", "AppKit", "AudioUnit", "ForceFeedback", "IOKit", "Carbon", "CoreServices", "ApplicationServices")

    when defined(macosx) or defined(ios):
        useFrameworks("AudioToolbox", "CoreAudio", "CoreGraphics", "QuartzCore")
<commit_msg>Fix issue #342 where SDL failed to link to some libraries<commit_after>
template sdlMain*() =
    when defined(ios) or defined(android):
        when not compileOption("noMain"):
            {.error: "Please run Nim with --noMain flag.".}

        import nimx/system_logger
        {.push stackTrace: off.}
        proc setupLogger() {.cdecl.} =
            errorMessageWriter = proc(msg: string) =
                logi msg
        {.pop.}

        when defined(ios):
            {.emit: "#define __IPHONEOS__".}

        {.emit: """
// The following piece of code is a copy-paste from SDL/SDL_main.h
// It is required to avoid dependency on SDL headers
////////////////////////////////////////////////////////////////////////////////


/**
 *  \file SDL_main.h
 *
 *  Redefine main() on some platforms so that it is called by SDL.
 */

#ifndef SDL_MAIN_HANDLED
#if defined(__WIN32__)
/* On Windows SDL provides WinMain(), which parses the command line and passes
   the arguments to your main function.

   If you provide your own WinMain(), you may define SDL_MAIN_HANDLED
 */
#define SDL_MAIN_AVAILABLE

#elif defined(__WINRT__)
/* On WinRT, SDL provides a main function that initializes CoreApplication,
   creating an instance of IFrameworkView in the process.

   Please note that #include'ing SDL_main.h is not enough to get a main()
   function working.  In non-XAML apps, the file,
   src/main/winrt/SDL_WinRT_main_NonXAML.cpp, or a copy of it, must be compiled
   into the app itself.  In XAML apps, the function, SDL_WinRTRunApp must be
   called, with a pointer to the Direct3D-hosted XAML control passed in.
*/
#define SDL_MAIN_NEEDED

#elif defined(__IPHONEOS__)
/* On iOS SDL provides a main function that creates an application delegate
   and starts the iOS application run loop.

   See src/video/uikit/SDL_uikitappdelegate.m for more details.
 */
#define SDL_MAIN_NEEDED

#elif defined(__ANDROID__)
/* On Android SDL provides a Java class in SDLActivity.java that is the
   main activity entry point.

   See README-android.txt for more details on extending that class.
 */
#define SDL_MAIN_NEEDED

#endif
#endif /* SDL_MAIN_HANDLED */

#ifdef __cplusplus
#define C_LINKAGE   "C"
#else
#define C_LINKAGE
#endif /* __cplusplus */

/**
 *  \file SDL_main.h
 *
 *  The application's main() function must be called with C linkage,
 *  and should be declared like this:
 *  \code
 *  #ifdef __cplusplus
 *  extern "C"
 *  #endif
 *  int main(int argc, char *argv[])
 *  {
 *  }
 *  \endcode
 */

#if defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE)
#define main    SDL_main
#endif




//#include <SDL2/SDL_main.h>

extern int cmdCount;
extern char** cmdLine;
extern char** gEnv;

N_CDECL(void, NimMain)(void);

int main(int argc, char** args) {
    cmdLine = args;
    cmdCount = argc;
    gEnv = NULL;
    `setupLogger`();
    NimMain();
    return nim_program_result;
}

""".}

when not defined(emscripten):
    import macros

    when defined(macosx) or defined(ios):
        macro passToCAndL(s: string): typed =
            result = newNimNode(nnkStmtList)
            result.add parseStmt("{.passL: \"" & s.strVal & "\".}\n")
            result.add parseStmt("{.passC: \"" & s.strVal & "\".}\n")

        macro useFrameworks(n: varargs[string]): typed =
            result = newNimNode(nnkStmtList, n)
            for i in 0..n.len-1:
                result.add parseStmt("passToCAndL(\"-framework " & n[i].strVal & "\")")

    when defined(ios):
        useFrameworks("OpenGLES", "UIKit", "GameController", "CoreMotion", "Metal", "AVFoundation")
        when not defined(simulator):
            when hostCPU == "arm":
                {.passC:"-arch armv7".}
                {.passL:"-arch armv7".}
            elif hostCPU == "arm64":
                {.passC:"-arch arm64".}
                {.passL:"-arch arm64".}
    elif defined(macosx):
        useFrameworks("OpenGL", "AppKit", "AudioUnit", "ForceFeedback", "IOKit", "Carbon", "CoreServices", "ApplicationServices")

    when defined(macosx) or defined(ios):
        useFrameworks("AudioToolbox", "CoreAudio", "CoreGraphics", "QuartzCore")

{'fraction_non_alphanumeric': 0.13537972361501774, 'fraction_numerical': 0.0037911214381802618, 'mean_word_length': 2.642761692650334, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6404094', 'n_tokens_mistral': 2687, 'n_tokens_neox': 2537, 'n_words': 792}