blob: 7e123cf607435946acd8a492192d75b5a925daae [file] [log] [blame]
Gabor Greifdb5565a2007-07-06 20:28:40 +00001/*===- llvm-stub.c - Stub executable to run llvm bitcode files -----------===//
Chris Lattner4c6d1242004-06-01 23:48:45 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnercf786592007-12-29 20:47:37 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4c6d1242004-06-01 23:48:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tool is used by the gccld program to enable transparent execution of
Gabor Greifdb5565a2007-07-06 20:28:40 +000011// bitcode files by the user. Specifically, gccld outputs two files when asked
Chris Lattner4c6d1242004-06-01 23:48:45 +000012// to compile a <program> file:
Gabor Greifdb5565a2007-07-06 20:28:40 +000013// 1. It outputs the LLVM bitcode file to <program>.bc
Chris Lattner4c6d1242004-06-01 23:48:45 +000014// 2. It outputs a stub executable that runs lli on <program>.bc
15//
16// This allows the end user to just say ./<program> and have the JIT executed
17// automatically. On unix, the stub executable emitted is actually a bourne
Misha Brukmanfff0ff82004-06-02 00:29:52 +000018// shell script that does the forwarding. Windows does not like #!/bin/sh
Chris Lattner4c6d1242004-06-01 23:48:45 +000019// programs in .exe files, so we make it an actual program, defined here.
20//
21//===----------------------------------------------------------------------===*/
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Reid Spencer3000cbf2004-12-20 04:34:36 +000026
27#include "llvm/Config/config.h"
28
29#if defined(HAVE_UNISTD_H) && !defined(_MSC_VER)
30#include <unistd.h>
31#endif
32
33#ifdef _WIN32
34#include <process.h>
35#include <io.h>
36#endif
Chris Lattner4c6d1242004-06-01 23:48:45 +000037
38int main(int argc, char** argv) {
39 const char *Interp = getenv("LLVMINTERP");
40 const char **Args;
41 if (Interp == 0) Interp = "lli";
42
43 /* Set up the command line options to pass to the JIT. */
44 Args = (const char**)malloc(sizeof(char*) * (argc+2));
45 /* argv[0] is the JIT */
46 Args[0] = Interp;
Chris Lattnerf7e92b12004-06-02 00:04:54 +000047
48#ifdef __CYGWIN32__
49 /* Cygwin strips the .exe suffix off of argv[0] to "help" us. Put it back
50 * on.
51 */
52 argv[0] = strcat(strcpy((char*)malloc(strlen(argv[0])+5), argv[0]), ".exe");
53#endif
54
Chris Lattner4c6d1242004-06-01 23:48:45 +000055 /* argv[1] is argv[0] + ".bc". */
56 Args[1] = strcat(strcpy((char*)malloc(strlen(argv[0])+4), argv[0]), ".bc");
57
58 /* The rest of the args are as before. */
59 memcpy(Args+2, argv+1, sizeof(char*)*argc);
60
61 /* Run the JIT. */
62 execvp(Interp, (char *const*)Args);
63
64 /* if _execv returns, the JIT could not be started. */
65 fprintf(stderr, "Could not execute the LLVM JIT. Either add 'lli' to your"
66 " path, or set the\ninterpreter you want to use in the LLVMINTERP "
67 "environment variable.\n");
68 return 1;
69}