Merge branch 'master' into msp430
[llvm/msp430.git] / tools / llvm-stub / llvm-stub.c
blobe5624a9c9b0948136969415ca91f8983cfc0cf89
1 /*===- llvm-stub.c - Stub executable to run llvm bitcode files ------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This tool is used by the gccld program to enable transparent execution of
11 // bitcode files by the user. Specifically, gccld outputs two files when asked
12 // to compile a <program> file:
13 // 1. It outputs the LLVM bitcode file to <program>.bc
14 // 2. It outputs a stub executable that runs lli on <program>.bc
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
18 // shell script that does the forwarding. Windows does not like #!/bin/sh
19 // programs in .exe files, so we make it an actual program, defined here.
21 //===----------------------------------------------------------------------===*/
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "llvm/Config/config.h"
29 #if defined(HAVE_UNISTD_H) && !defined(_MSC_VER)
30 #include <unistd.h>
31 #endif
33 #ifdef _WIN32
34 #include <process.h>
35 #include <io.h>
36 #endif
38 int main(int argc, char** argv) {
39 const char *Interp = getenv("LLVMINTERP");
40 const char **Args;
41 if (Interp == 0) Interp = "lli";
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;
48 #ifdef LLVM_ON_WIN32
50 int len = strlen(argv[0]);
51 if (len < 4 || strcmp(argv[0] + len - 4, ".exe") != 0) {
52 /* .exe suffix is stripped off of argv[0] if the executable was run on the
53 * command line without one. Put it back on.
55 argv[0] = strcat(strcpy((char*)malloc(len + 5), argv[0]), ".exe");
58 #endif
60 /* argv[1] is argv[0] + ".bc". */
61 Args[1] = strcat(strcpy((char*)malloc(strlen(argv[0])+4), argv[0]), ".bc");
63 /* The rest of the args are as before. */
64 memcpy(Args+2, argv+1, sizeof(char*)*argc);
66 /* Run the JIT. */
67 execvp(Interp, (char *const*)Args);
69 /* if _execv returns, the JIT could not be started. */
70 fprintf(stderr, "Could not execute the LLVM JIT. Either add 'lli' to your"
71 " path, or set the\ninterpreter you want to use in the LLVMINTERP "
72 "environment variable.\n");
73 return 1;