4aff5e969c81315c68acde80cd6306fdd9a6dbdb
[oota-llvm.git] / lib / System / Win32 / Program.cpp
1 //===- Win32/Program.cpp - Win32 Program Implementation ------- -*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Jeff Cohen and is distributed under the 
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides the Win32 specific implementation of the Program class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Win32.h"
15 #include <malloc.h>
16
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only Win32 specific code 
19 //===          and must not be UNIX code
20 //===----------------------------------------------------------------------===//
21
22 namespace llvm {
23 using namespace sys;
24
25 // This function just uses the PATH environment variable to find the program.
26 Path
27 Program::FindProgramByName(const std::string& progName) {
28
29   // Check some degenerate cases
30   if (progName.length() == 0) // no program
31     return Path();
32   Path temp;
33   if (!temp.setFile(progName)) // invalid name
34     return Path();
35   if (temp.executable()) // already executable as is
36     return temp;
37
38   // At this point, the file name is valid and its not executable.
39   // Let Windows search for it.
40   char buffer[MAX_PATH];
41   char *dummy = NULL;
42   DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH,
43                          buffer, &dummy);
44
45   // See if it wasn't found.
46   if (len == 0)
47     return Path();
48
49   // See if we got the entire path.
50   if (len < MAX_PATH)
51     return Path(buffer);
52
53   // Buffer was too small; grow and retry.
54   while (true) {
55     char *b = reinterpret_cast<char *>(_alloca(len+1));
56     DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, b, &dummy);
57
58     // It is unlikely the search failed, but it's always possible some file
59     // was added or removed since the last search, so be paranoid...
60     if (len2 == 0)
61       return Path();
62     else if (len2 <= len)
63       return Path(b);
64
65     len = len2;
66   }
67 }
68
69 //
70 int 
71 Program::ExecuteAndWait(const Path& path, 
72                         const std::vector<std::string>& args,
73                         const char** env) {
74   if (!path.executable())
75     throw path.toString() + " is not executable"; 
76
77   // Windows wants a command line, not an array of args, to pass to the new
78   // process.  We have to concatenate them all, while quoting the args that
79   // have embedded spaces.
80
81   // First, determine the length of the command line.
82   std::string progname(path.getLast());
83   unsigned len = progname.length() + 1;
84   if (progname.find(' ') != std::string::npos)
85     len += 2;
86
87   for (unsigned i = 0; i < args.size(); i++) {
88     len += args[i].length() + 1;
89     if (args[i].find(' ') != std::string::npos)
90       len += 2;
91   }
92
93   // Now build the command line.
94   char *command = reinterpret_cast<char *>(_alloca(len));
95   char *p = command;
96
97   bool needsQuoting = progname.find(' ') != std::string::npos;
98   if (needsQuoting)
99     *p++ = '"';
100   memcpy(p, progname.c_str(), progname.length());
101   p += progname.length();
102   if (needsQuoting)
103     *p++ = '"';
104   *p++ = ' ';
105
106   for (unsigned i = 0; i < args.size(); i++) {
107     const std::string& arg = args[i];
108     needsQuoting = arg.find(' ') != std::string::npos;
109     if (needsQuoting)
110       *p++ = '"';
111     memcpy(p, arg.c_str(), arg.length());
112     p += arg.length();
113     if (needsQuoting)
114       *p++ = '"';
115     *p++ = ' ';
116   }
117
118   *p = 0;
119
120   // Create a child process.
121   STARTUPINFO si;
122   memset(&si, 0, sizeof(si));
123   si.cb = sizeof(si);
124
125   PROCESS_INFORMATION pi;
126   memset(&pi, 0, sizeof(pi));
127
128   LPVOID lpEnvironment = envp;
129   if (!CreateProcess(path.c_str(), command, NULL, NULL, FALSE, 0,
130                      lpEnvironment, NULL, &si, &pi))
131   {
132     ThrowError(std::string("Couldn't execute program '") + 
133                path.toString() + "'");
134   }
135
136   // Wait for it to terminate.
137   WaitForSingleObject(pi.hProcess, INFINITE);
138   
139   // Get its exit status.
140   DWORD status;
141   BOOL rc = GetExitCodeProcess(pi.hProcess, &status);
142
143   // Done with the handles; go close them.
144   CloseHandle(pi.hProcess);
145   CloseHandle(pi.hThread);
146
147   if (!rc)
148     ThrowError(std::string("Failed getting status for program '") + 
149                path.toString() + "'");
150
151   return status;
152 }
153
154 }
155 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab