Generate the C main() function when linking a binary (fixes #5373)
[ghc.git] / rts / RtsMain.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team 1998-2000
4 *
5 * Main function for a standalone Haskell program.
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #define COMPILING_RTS_MAIN
10
11 #include "PosixSource.h"
12 #include "Rts.h"
13 #include "RtsAPI.h"
14
15 #include "RtsUtils.h"
16 #include "Prelude.h"
17 #include "Task.h"
18 #if defined(mingw32_HOST_OS)
19 #include "win32/seh_excn.h"
20 #endif
21
22 #ifdef DEBUG
23 # include "Printer.h" /* for printing */
24 #endif
25
26 #ifdef HAVE_WINDOWS_H
27 # include <windows.h>
28 #endif
29
30 /* Annoying global vars for passing parameters to real_main() below
31 * This is to get around problem with Windows SEH, see hs_main(). */
32 static int progargc;
33 static char **progargv;
34 static StgClosure *progmain_closure; /* This will be ZCMain_main_closure */
35 static RtsConfig rtsconfig;
36
37 /* Hack: we assume that we're building a batch-mode system unless
38 * INTERPRETER is set
39 */
40 #ifndef INTERPRETER /* Hack */
41 static void real_main(void) GNUC3_ATTRIBUTE(__noreturn__);
42 static void real_main(void)
43 {
44 int exit_status;
45 SchedulerStatus status;
46
47 hs_init_ghc(&progargc, &progargv, rtsconfig);
48
49 /* kick off the computation by creating the main thread with a pointer
50 to mainIO_closure representing the computation of the overall program;
51 then enter the scheduler with this thread and off we go;
52
53 the same for GranSim (we have only one instance of this code)
54
55 in a parallel setup, where we have many instances of this code
56 running on different PEs, we should do this only for the main PE
57 (IAmMainThread is set in startupHaskell)
58 */
59
60 /* ToDo: want to start with a larger stack size */
61 {
62 Capability *cap = rts_lock();
63 cap = rts_evalLazyIO(cap,progmain_closure, NULL);
64 status = rts_getSchedStatus(cap);
65 taskTimeStamp(myTask());
66 rts_unlock(cap);
67 }
68
69 /* check the status of the entire Haskell computation */
70 switch (status) {
71 case Killed:
72 errorBelch("main thread exited (uncaught exception)");
73 exit_status = EXIT_KILLED;
74 break;
75 case Interrupted:
76 errorBelch("interrupted");
77 exit_status = EXIT_INTERRUPTED;
78 break;
79 case HeapExhausted:
80 exit_status = EXIT_HEAPOVERFLOW;
81 break;
82 case Success:
83 exit_status = EXIT_SUCCESS;
84 break;
85 default:
86 barf("main thread completed with invalid status");
87 }
88 shutdownHaskellAndExit(exit_status);
89 }
90
91 /* The rts entry point from a compiled program using a Haskell main
92 * function. This gets called from a tiny main function generated by
93 * GHC and linked into each compiled Haskell program that uses a
94 * Haskell main function.
95 *
96 * We expect the caller to pass ZCMain_main_closure for
97 * main_closure. The reason we cannot refer to this symbol directly
98 * is because we're inside the rts and we do not know for sure that
99 * we'll be using a Haskell main function.
100 */
101 int hs_main (int argc, char *argv[], // program args
102 StgClosure *main_closure, // closure for Main.main
103 RtsConfig rts_config) // RTS configuration
104 {
105 /* We do this dance with argc and argv as otherwise the SEH exception
106 stuff (the BEGIN/END CATCH below) on Windows gets confused */
107 progargc = argc;
108 progargv = argv;
109 progmain_closure = main_closure;
110 rtsconfig = rts_config;
111
112 #if defined(mingw32_HOST_OS)
113 BEGIN_CATCH
114 #endif
115 real_main();
116 #if defined(mingw32_HOST_OS)
117 END_CATCH
118 #endif
119 }
120 # endif /* BATCH_MODE */