annotate lib/swig/swigwin-2.0.11/CCache/execute.c @ 2553:48708da03b7f

Party Creation UI logic separated from MainMenu
author a.parshin
date Wed, 13 May 2015 02:20:05 +0200
parents b3009adc0e2f
children
rev   line source
1899
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
1 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
2 Copyright (C) Andrew Tridgell 2002
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
3
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
6 the Free Software Foundation; either version 2 of the License, or
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
7 (at your option) any later version.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
8
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
12 GNU General Public License for more details.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
13
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
15 along with this program; if not, write to the Free Software
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
17 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
18
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
19 #include "ccache.h"
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
20
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
21 #ifdef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
22 char *argvtos(char **argv)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
23 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
24 int i, len;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
25 char *ptr, *str;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
26
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
27 for (i = 0, len = 0; argv[i]; i++) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
28 len += strlen(argv[i]) + 3;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
29 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
30
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
31 str = ptr = (char *)malloc(len + 1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
32 if (str == NULL)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
33 return NULL;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
34
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
35 for (i = 0; argv[i]; i++) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
36 len = strlen(argv[i]);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
37 *ptr++ = '"';
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
38 memcpy(ptr, argv[i], len);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
39 ptr += len;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
40 *ptr++ = '"';
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
41 *ptr++ = ' ';
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
42 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
43 *ptr = 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
44
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
45 return str;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
46 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
47 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
48
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
49 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
50 execute a compiler backend, capturing all output to the given paths
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
51 the full path to the compiler to run is in argv[0]
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
52 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
53 int execute(char **argv,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
54 const char *path_stdout,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
55 const char *path_stderr)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
56 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
57 #ifdef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
58
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
59 #if 1
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
60 PROCESS_INFORMATION pinfo;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
61 STARTUPINFO sinfo;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
62 BOOL ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
63 DWORD exitcode;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
64 char *args;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
65 HANDLE fd_out, fd_err;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
66 SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
67
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
68 /* TODO: needs moving after possible exit() below, but before stdout is redirected */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
69 if (ccache_verbose) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
70 display_execute_args(argv);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
71 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
72
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
73 fd_out = CreateFile(path_stdout, GENERIC_WRITE, 0, &sa, CREATE_ALWAYS,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
74 FILE_ATTRIBUTE_NORMAL, NULL);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
75 if (fd_out == INVALID_HANDLE_VALUE) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
76 return STATUS_NOCACHE;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
77 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
78
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
79 fd_err = CreateFile(path_stderr, GENERIC_WRITE, 0, &sa, CREATE_ALWAYS,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
80 FILE_ATTRIBUTE_NORMAL, NULL);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
81 if (fd_err == INVALID_HANDLE_VALUE) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
82 return STATUS_NOCACHE;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
83 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
84
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
85 ZeroMemory(&pinfo, sizeof(PROCESS_INFORMATION));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
86 ZeroMemory(&sinfo, sizeof(STARTUPINFO));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
87
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
88 sinfo.cb = sizeof(STARTUPINFO);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
89 sinfo.hStdError = fd_err;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
90 sinfo.hStdOutput = fd_out;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
91 sinfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
92 sinfo.dwFlags |= STARTF_USESTDHANDLES;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
93
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
94 args = argvtos(argv);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
95
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
96 ret = CreateProcessA(argv[0], args, NULL, NULL, TRUE, 0, NULL, NULL,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
97 &sinfo, &pinfo);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
98
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
99 free(args);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
100 CloseHandle(fd_out);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
101 CloseHandle(fd_err);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
102
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
103 if (ret == 0)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
104 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
105
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
106 WaitForSingleObject(pinfo.hProcess, INFINITE);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
107 GetExitCodeProcess(pinfo.hProcess, &exitcode);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
108 CloseHandle(pinfo.hProcess);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
109 CloseHandle(pinfo.hThread);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
110
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
111 return exitcode;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
112 #else /* possibly slightly faster */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
113 /* needs fixing to quote commandline options to handle spaces in CCACHE_DIR etc */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
114 int status = -2;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
115 int fd, std_od = -1, std_ed = -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
116
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
117 /* TODO: needs moving after possible exit() below, but before stdout is redirected */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
118 if (ccache_verbose) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
119 display_execute_args(argv);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
120 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
121
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
122 unlink(path_stdout);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
123 std_od = _dup(1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
124 fd = _open(path_stdout, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
125 if (fd == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
126 exit(STATUS_NOCACHE);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
127 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
128 _dup2(fd, 1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
129 _close(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
130
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
131 unlink(path_stderr);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
132 fd = _open(path_stderr, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
133 std_ed = _dup(2);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
134 if (fd == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
135 exit(STATUS_NOCACHE);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
136 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
137 _dup2(fd, 2);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
138 _close(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
139
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
140 /* Spawn process (_exec* familly doesn't return) */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
141 status = _spawnv(_P_WAIT, argv[0], (const char **)argv);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
142
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
143 /* Restore descriptors */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
144 if (std_od != -1) _dup2(std_od, 1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
145 if (std_ed != -1) _dup2(std_ed, 2);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
146 _flushall();
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
147
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
148 return (status>0);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
149
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
150 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
151
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
152 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
153 pid_t pid;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
154 int status;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
155
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
156 pid = fork();
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
157 if (pid == -1) fatal("Failed to fork");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
158
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
159 if (pid == 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
160 int fd;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
161
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
162 /* TODO: needs moving after possible exit() below, but before stdout is redirected */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
163 if (ccache_verbose) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
164 display_execute_args(argv);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
165 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
166
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
167 unlink(path_stdout);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
168 fd = open(path_stdout, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
169 if (fd == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
170 exit(STATUS_NOCACHE);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
171 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
172 dup2(fd, 1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
173 close(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
174
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
175 unlink(path_stderr);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
176 fd = open(path_stderr, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
177 if (fd == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
178 exit(STATUS_NOCACHE);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
179 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
180 dup2(fd, 2);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
181 close(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
182
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
183 exit(execv(argv[0], argv));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
184 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
185
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
186 if (waitpid(pid, &status, 0) != pid) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
187 fatal("waitpid failed");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
188 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
189
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
190 if (WEXITSTATUS(status) == 0 && WIFSIGNALED(status)) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
191 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
192 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
193
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
194 return WEXITSTATUS(status);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
195 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
196 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
197
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
198
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
199 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
200 find an executable by name in $PATH. Exclude any that are links to exclude_name
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
201 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
202 char *find_executable(const char *name, const char *exclude_name)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
203 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
204 #if _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
205 (void)exclude_name;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
206 DWORD ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
207 char namebuf[MAX_PATH];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
208
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
209 ret = SearchPathA(getenv("CCACHE_PATH"), name, ".exe",
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
210 sizeof(namebuf), namebuf, NULL);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
211 if (ret != 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
212 return x_strdup(namebuf);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
213 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
214
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
215 return NULL;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
216 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
217 char *path;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
218 char *tok;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
219 struct stat st1, st2;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
220
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
221 if (*name == '/') {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
222 return x_strdup(name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
223 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
224
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
225 path = getenv("CCACHE_PATH");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
226 if (!path) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
227 path = getenv("PATH");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
228 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
229 if (!path) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
230 cc_log("no PATH variable!?\n");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
231 stats_update(STATS_ENVIRONMMENT);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
232 return NULL;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
233 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
234
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
235 path = x_strdup(path);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
236
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
237 /* search the path looking for the first compiler of the right name
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
238 that isn't us */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
239 for (tok=strtok(path,":"); tok; tok = strtok(NULL, ":")) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
240 char *fname;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
241 x_asprintf(&fname, "%s/%s", tok, name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
242 /* look for a normal executable file */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
243 if (access(fname, X_OK) == 0 &&
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
244 lstat(fname, &st1) == 0 &&
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
245 stat(fname, &st2) == 0 &&
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
246 S_ISREG(st2.st_mode)) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
247 /* if its a symlink then ensure it doesn't
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
248 point at something called exclude_name */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
249 if (S_ISLNK(st1.st_mode)) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
250 char *buf = x_realpath(fname);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
251 if (buf) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
252 char *p = str_basename(buf);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
253 if (strcmp(p, exclude_name) == 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
254 /* its a link to "ccache" ! */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
255 free(p);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
256 free(buf);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
257 continue;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
258 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
259 free(buf);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
260 free(p);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
261 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
262 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
263
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
264 /* found it! */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
265 free(path);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
266 return fname;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
267 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
268 free(fname);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
269 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
270
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
271 return NULL;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
272 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
273 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
274
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
275 void display_execute_args(char **argv)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
276 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
277 if (argv) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
278 printf("ccache executing: ");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
279 while (*argv) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
280 printf("%s ", *argv);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
281 ++argv;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
282 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
283 printf("\n");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
284 fflush(stdout);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
285 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
286 }