1 /*===- llvm-stub.c - Stub executable to run llvm bytecode files -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tool is used by the gccld program to enable transparent execution of
11 // bytecode files by the user. Specifically, gccld outputs two files when asked
12 // to compile a <program> file:
13 // 1. It outputs the LLVM bytecode 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 //===----------------------------------------------------------------------===*/
27 #include "llvm/Config/config.h"
29 #if defined(HAVE_UNISTD_H) && !defined(_MSC_VER)
38 int main(int argc
, char** argv
) {
39 const char *Interp
= getenv("LLVMINTERP");
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 */
49 /* Cygwin strips the .exe suffix off of argv[0] to "help" us. Put it back
52 argv
[0] = strcat(strcpy((char*)malloc(strlen(argv
[0])+5), argv
[0]), ".exe");
55 /* argv[1] is argv[0] + ".bc". */
56 Args
[1] = strcat(strcpy((char*)malloc(strlen(argv
[0])+4), argv
[0]), ".bc");
58 /* The rest of the args are as before. */
59 memcpy(Args
+2, argv
+1, sizeof(char*)*argc
);
62 execvp(Interp
, (char *const*)Args
);
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");