Misha Brukman | 8050e9e | 2008-12-31 17:40:52 +0000 | [diff] [blame] | 1 | /*===- llvm-stub.c - Stub executable to run llvm bitcode files ------------===// |
Michael J. Spencer | a71b4ba | 2010-10-07 18:51:10 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 4c6d124 | 2004-06-01 23:48:45 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | cf78659 | 2007-12-29 20:47:37 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Michael J. Spencer | a71b4ba | 2010-10-07 18:51:10 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 4c6d124 | 2004-06-01 23:48:45 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tool is used by the gccld program to enable transparent execution of |
Gabor Greif | db5565a | 2007-07-06 20:28:40 +0000 | [diff] [blame] | 11 | // bitcode files by the user. Specifically, gccld outputs two files when asked |
Chris Lattner | 4c6d124 | 2004-06-01 23:48:45 +0000 | [diff] [blame] | 12 | // to compile a <program> file: |
Gabor Greif | db5565a | 2007-07-06 20:28:40 +0000 | [diff] [blame] | 13 | // 1. It outputs the LLVM bitcode file to <program>.bc |
Chris Lattner | 4c6d124 | 2004-06-01 23:48:45 +0000 | [diff] [blame] | 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 Brukman | fff0ff8 | 2004-06-02 00:29:52 +0000 | [diff] [blame] | 18 | // shell script that does the forwarding. Windows does not like #!/bin/sh |
Chris Lattner | 4c6d124 | 2004-06-01 23:48:45 +0000 | [diff] [blame] | 19 | // 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 Spencer | 3000cbf | 2004-12-20 04:34:36 +0000 | [diff] [blame] | 26 | |
| 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 Lattner | 4c6d124 | 2004-06-01 23:48:45 +0000 | [diff] [blame] | 37 | |
| 38 | int 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 Lattner | f7e92b1 | 2004-06-02 00:04:54 +0000 | [diff] [blame] | 47 | |
Argyrios Kyrtzidis | e2bc1cb | 2008-06-15 12:07:01 +0000 | [diff] [blame] | 48 | #ifdef LLVM_ON_WIN32 |
Duncan Sands | 9954c76 | 2008-06-19 08:47:31 +0000 | [diff] [blame] | 49 | { |
| 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. |
| 54 | */ |
| 55 | argv[0] = strcat(strcpy((char*)malloc(len + 5), argv[0]), ".exe"); |
| 56 | } |
Argyrios Kyrtzidis | e2bc1cb | 2008-06-15 12:07:01 +0000 | [diff] [blame] | 57 | } |
Chris Lattner | f7e92b1 | 2004-06-02 00:04:54 +0000 | [diff] [blame] | 58 | #endif |
| 59 | |
Chris Lattner | 4c6d124 | 2004-06-01 23:48:45 +0000 | [diff] [blame] | 60 | /* argv[1] is argv[0] + ".bc". */ |
| 61 | Args[1] = strcat(strcpy((char*)malloc(strlen(argv[0])+4), argv[0]), ".bc"); |
| 62 | |
| 63 | /* The rest of the args are as before. */ |
Benjamin Kramer | 6b83198 | 2009-08-11 11:01:19 +0000 | [diff] [blame] | 64 | memcpy((char **)Args+2, argv+1, sizeof(char*)*argc); |
Chris Lattner | 4c6d124 | 2004-06-01 23:48:45 +0000 | [diff] [blame] | 65 | |
| 66 | /* Run the JIT. */ |
Benjamin Kramer | cb2caf7 | 2010-10-16 15:43:02 +0000 | [diff] [blame] | 67 | #ifndef _WIN32 |
| 68 | execvp(Interp, (char **)Args); /* POSIX execvp takes a char *const[]. */ |
| 69 | #else |
| 70 | execvp(Interp, Args); /* windows execvp takes a const char *const *. */ |
| 71 | #endif |
Chris Lattner | 4c6d124 | 2004-06-01 23:48:45 +0000 | [diff] [blame] | 72 | /* if _execv returns, the JIT could not be started. */ |
| 73 | fprintf(stderr, "Could not execute the LLVM JIT. Either add 'lli' to your" |
| 74 | " path, or set the\ninterpreter you want to use in the LLVMINTERP " |
| 75 | "environment variable.\n"); |
| 76 | return 1; |
| 77 | } |