blob: 8d5175af1009be4a25fc82fe02ba71751c8bc6f9 [file] [log] [blame]
Chris Lattner4c6d1242004-06-01 23:48:45 +00001/*===- llvm-stub.c - Stub executable to run llvm bytecode files -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
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
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 Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Config/unistd.h" /* provides definition of execve */
Chris Lattner4c6d1242004-06-01 23:48:45 +000027
28int main(int argc, char** argv) {
29 const char *Interp = getenv("LLVMINTERP");
30 const char **Args;
31 if (Interp == 0) Interp = "lli";
32
33 /* Set up the command line options to pass to the JIT. */
34 Args = (const char**)malloc(sizeof(char*) * (argc+2));
35 /* argv[0] is the JIT */
36 Args[0] = Interp;
Chris Lattnerf7e92b12004-06-02 00:04:54 +000037
38#ifdef __CYGWIN32__
39 /* Cygwin strips the .exe suffix off of argv[0] to "help" us. Put it back
40 * on.
41 */
42 argv[0] = strcat(strcpy((char*)malloc(strlen(argv[0])+5), argv[0]), ".exe");
43#endif
44
Chris Lattner4c6d1242004-06-01 23:48:45 +000045 /* argv[1] is argv[0] + ".bc". */
46 Args[1] = strcat(strcpy((char*)malloc(strlen(argv[0])+4), argv[0]), ".bc");
47
48 /* The rest of the args are as before. */
49 memcpy(Args+2, argv+1, sizeof(char*)*argc);
50
51 /* Run the JIT. */
52 execvp(Interp, (char *const*)Args);
53
54 /* if _execv returns, the JIT could not be started. */
55 fprintf(stderr, "Could not execute the LLVM JIT. Either add 'lli' to your"
56 " path, or set the\ninterpreter you want to use in the LLVMINTERP "
57 "environment variable.\n");
58 return 1;
59}