1 // Copyright (C) 2002 Andrew Tridgell
2 // Copyright (C) 2011-2018 Joel Rosdahl
4 // This program is free software; you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License as published by the Free
6 // Software Foundation; either version 3 of the License, or (at your option)
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 // You should have received a copy of the GNU General Public License along with
15 // this program; if not, write to the Free Software Foundation, Inc., 51
16 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 extern struct conf *conf;
23 find_executable_in_path(const char *name, const char *exclude_name, char *path);
26 // Re-create a win32 command line string based on **argv.
27 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
29 win32argvtos(char *prefix, char **argv)
33 char *arg = prefix ? prefix : argv[i++];
36 for (int j = 0; arg[j]; j++) {
49 } while ((arg = argv[i++]));
51 char *ptr = malloc(k + 1);
58 arg = prefix ? prefix : argv[i++];
62 for (int j = 0; arg[j]; j++) {
82 } while ((arg = argv[i++]));
89 win32getshell(char *path)
93 const char *ext = get_extension(path);
94 if (ext && strcasecmp(ext, ".sh") == 0 && (path_env = getenv("PATH"))) {
95 sh = find_executable_in_path("sh.exe", NULL, path_env);
97 if (!sh && getenv("CCACHE_DETECT_SHEBANG")) {
99 FILE *fp = fopen(path, "r");
102 fgets(buf, sizeof(buf), fp);
104 if (str_eq(buf, "#!/bin/sh") && (path_env = getenv("PATH"))) {
105 sh = find_executable_in_path("sh.exe", NULL, path_env);
114 void add_exe_ext_if_no_to_fullpath(char *full_path_win_ext, size_t max_size,
115 const char *ext, const char *path) {
116 if (!ext || (!str_eq(".exe", ext)
117 && !str_eq(".bat", ext)
118 && !str_eq(".EXE", ext)
119 && !str_eq(".BAT", ext))) {
120 snprintf(full_path_win_ext, max_size, "%s.exe", path);
122 snprintf(full_path_win_ext, max_size, "%s", path);
127 win32execute(char *path, char **argv, int doreturn,
128 int fd_stdout, int fd_stderr)
130 PROCESS_INFORMATION pi;
131 memset(&pi, 0x00, sizeof(pi));
134 memset(&si, 0x00, sizeof(si));
136 char *sh = win32getshell(path);
141 si.cb = sizeof(STARTUPINFO);
142 if (fd_stdout != -1) {
143 si.hStdOutput = (HANDLE)_get_osfhandle(fd_stdout);
144 si.hStdError = (HANDLE)_get_osfhandle(fd_stderr);
145 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
146 si.dwFlags = STARTF_USESTDHANDLES;
147 if (si.hStdOutput == INVALID_HANDLE_VALUE
148 || si.hStdError == INVALID_HANDLE_VALUE) {
152 // Redirect subprocess stdout, stderr into current process.
153 si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
154 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
155 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
156 si.dwFlags = STARTF_USESTDHANDLES;
157 if (si.hStdOutput == INVALID_HANDLE_VALUE
158 || si.hStdError == INVALID_HANDLE_VALUE) {
163 char *args = win32argvtos(sh, argv);
164 const char *ext = strrchr(path, '.');
165 char full_path_win_ext[MAX_PATH] = {0};
166 add_exe_ext_if_no_to_fullpath(full_path_win_ext, MAX_PATH, ext, path);
168 CreateProcess(full_path_win_ext, args, NULL, NULL, 1, 0, NULL, NULL,
170 if (fd_stdout != -1) {
177 DWORD dw = GetLastError();
179 FORMAT_MESSAGE_ALLOCATE_BUFFER |
180 FORMAT_MESSAGE_FROM_SYSTEM |
181 FORMAT_MESSAGE_IGNORE_INSERTS,
182 NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf,
185 LPVOID lpDisplayBuf =
186 (LPVOID) LocalAlloc(LMEM_ZEROINIT,
187 (lstrlen((LPCTSTR) lpMsgBuf)
188 + lstrlen((LPCTSTR) __FILE__) + 200)
190 _snprintf((LPTSTR) lpDisplayBuf,
191 LocalSize(lpDisplayBuf) / sizeof(TCHAR),
192 TEXT("%s failed with error %lu: %s"), __FILE__, dw,
193 (const char *)lpMsgBuf);
195 cc_log("can't execute %s; OS returned error: %s",
196 full_path_win_ext, (char *)lpDisplayBuf);
199 LocalFree(lpDisplayBuf);
203 WaitForSingleObject(pi.hProcess, INFINITE);
206 GetExitCodeProcess(pi.hProcess, &exitcode);
207 CloseHandle(pi.hProcess);
208 CloseHandle(pi.hThread);
217 // Execute a compiler backend, capturing all output to the given paths the full
218 // path to the compiler to run is in argv[0].
220 execute(char **argv, int fd_out, int fd_err, pid_t *pid)
222 cc_log_argv("Executing ", argv);
229 fatal("Failed to fork: %s", strerror(errno));
238 x_exit(execv(argv[0], argv));
245 if (waitpid(*pid, &status, 0) != *pid) {
246 fatal("waitpid failed: %s", strerror(errno));
253 if (WEXITSTATUS(status) == 0 && WIFSIGNALED(status)) {
257 return WEXITSTATUS(status);
261 // Find an executable by name in $PATH. Exclude any that are links to
264 find_executable(const char *name, const char *exclude_name)
266 if (is_absolute_path(name)) {
267 return x_strdup(name);
270 char *path = conf->path;
271 if (str_eq(path, "")) {
272 path = getenv("PATH");
275 cc_log("No PATH variable");
279 return find_executable_in_path(name, exclude_name, path);
283 find_executable_in_path(const char *name, const char *exclude_name, char *path)
285 path = x_strdup(path);
287 // Search the path looking for the first compiler of the right name that
289 char *saveptr = NULL;
290 for (char *tok = strtok_r(path, PATH_DELIM, &saveptr);
292 tok = strtok_r(NULL, PATH_DELIM, &saveptr)) {
294 char namebuf[MAX_PATH];
295 int ret = SearchPath(tok, name, NULL, sizeof(namebuf), namebuf, NULL);
297 char *exename = format("%s.exe", name);
298 ret = SearchPath(tok, exename, NULL, sizeof(namebuf), namebuf, NULL);
304 return x_strdup(namebuf);
307 struct stat st1, st2;
308 char *fname = format("%s/%s", tok, name);
309 // Look for a normal executable file.
310 if (access(fname, X_OK) == 0 &&
311 lstat(fname, &st1) == 0 &&
312 stat(fname, &st2) == 0 &&
313 S_ISREG(st2.st_mode)) {
314 if (S_ISLNK(st1.st_mode)) {
315 char *buf = x_realpath(fname);
317 char *p = basename(buf);
318 if (str_eq(p, exclude_name)) {
319 // It's a link to "ccache"!
342 print_command(FILE *fp, char **argv)
344 for (int i = 0; argv[i]; i++) {
345 fprintf(fp, "%s%s", (i == 0) ? "" : " ", argv[i]);