File: local.cpp

package info (click to toggle)
icecc 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,444 kB
  • sloc: cpp: 13,720; sh: 2,949; ansic: 767; xml: 744; makefile: 233; asm: 2
file content (388 lines) | stat: -rw-r--r-- 11,218 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 99; -*- */
/* vim: set ts=4 sw=4 et tw=99:  */
/*
    This file is part of Icecream.

    Copyright (c) 2004 Stephan Kulow <coolo@suse.de>
                  2002, 2003 by Martin Pool <mbp@samba.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "config.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <vector>
#include <signal.h>

#include <comm.h>
#include "client.h"

using namespace std;

/* Name of this program, for trace.c */
const char *rs_program_name = "icecc";

#define CLIENT_DEBUG 0

static string compiler_path_lookup_helper(const string &compiler, const string &compiler_path)
{
    if (compiler_path.find('/') != string::npos) {
        return compiler_path;
    }

    string path = ::getenv("PATH");
    string::size_type begin = 0;
    string::size_type end = 0;
    struct stat s;
    bool after_selflink = false;
    string best_match;

    while (end != string::npos) {
        end = path.find(':', begin);
        string part;

        if (end == string::npos) {
            part = path.substr(begin);
        } else {
            part = path.substr(begin, end - begin);
        }

        begin = end + 1;

        part = part + '/' + compiler;

        if (!lstat(part.c_str(), &s)) {
            if (S_ISLNK(s.st_mode)) {
                std::string buffer;
                const int ret = resolve_link(part, buffer);

                if (ret != 0) {
                    log_error() << "resolve_link failed " << strerror(ret) << endl;
                    continue;
                }

                string target = find_basename(buffer);

                if (target == rs_program_name
                        || (after_selflink
                            && (target == "tbcompiler" || target == "distcc"
                                || target == "colorgcc"))) {
                    // this is a link pointing to us, ignore it
                    after_selflink = true;
                    continue;
                }
            } else if (!S_ISREG(s.st_mode)) {
                // It's not a link and not a file, so just ignore it. We don't
                // want to accidentially attempt to execute directories.
                continue;
            }

            if( best_match.empty()) {
                best_match = part;
            }

            if (after_selflink) {
                return part;
            }
        }
    }

    if (best_match.empty()) {
        log_error() << "couldn't find any " << compiler << endl;
    }

    return best_match;
}

string compiler_path_lookup(const string& compiler)
{
    return compiler_path_lookup_helper(compiler, compiler);
}

/*
 * Get the name of the compiler depedant on the
 * language of the job and the environment
 * variable set. This is useful for native cross-compilers.
 * (arm-linux-gcc for example)
 */
string find_compiler(const CompileJob &job)
{
    if (job.language() == CompileJob::Lang_C) {
        if (const char *env = getenv("ICECC_CC")) {
            return env;
        }
    }

    if (job.language() == CompileJob::Lang_CXX) {
        if (const char *env = getenv("ICECC_CXX")) {
            return env;
        }
    }

    return compiler_path_lookup_helper(job.compilerName(), job.compilerPathname());
}

bool compiler_is_clang(const CompileJob &job)
{
    if (job.language() == CompileJob::Lang_Custom) {
        return false;
    }

    assert(job.compilerName().find('/') == string::npos);
    return job.compilerName().find("clang") != string::npos;
}

/*
Clang works suboptimally when handling an already preprocessed source file,
for example error messages quote (already preprocessed) parts of the source.
Therefore it is better to only locally merge all #include files into the source
file and do the actual preprocessing remotely together with compiling.
There exists a Clang patch to implement option -frewrite-includes that does
such #include rewritting, and it's been only recently merged upstream.

This is similar with newer gcc versions, and gcc has -fdirectives-only, which
works similarly to -frewrite-includes (although it's not exactly the same).
*/
bool compiler_only_rewrite_includes(const CompileJob &job)
{
    if( job.blockRewriteIncludes()) {
        return false;
    }
    if (const char *rewrite_includes = getenv("ICECC_REMOTE_CPP")) {
        return (*rewrite_includes != '\0') && (*rewrite_includes != '0');
    }
    if (!compiler_is_clang(job)) {
#ifdef HAVE_GCC_FDIRECTIVES_ONLY
        // gcc has had -fdirectives-only for a long time, but clang on macosx poses as gcc
        // and fails when given the option. Since we right now detect whether a compiler
        // is gcc merely by checking the binary name, enable usage only if the configure
        // check found the option working.
        return true;
#endif
    }
    if (compiler_is_clang(job)) {
        if (const char *rewrite_includes = getenv("ICECC_CLANG_REMOTE_CPP")) {
            return (*rewrite_includes != '\0') && (*rewrite_includes != '0');
        }

#ifdef HAVE_CLANG_REWRITE_INCLUDES
        // Assume that we use the same clang (as least as far as capabilities go)
        // as was available when icecream was built. ICECC_CLANG_REMOTE_CPP above
        // allows override, and the only case when this should realistically break
        // is if somebody downgrades their clang.
        return true;
#endif
    }

    return false;
}

string clang_get_default_target(const CompileJob &job)
{
    return read_command_output( find_compiler( job ) + " -dumpmachine" );
}

static volatile int user_break_signal = 0;
static volatile pid_t child_pid;

static void handle_user_break(int sig)
{
    dcc_unlock();

    user_break_signal = sig;

    if (child_pid != 0) {
        kill(child_pid, sig);
    }

    signal(sig, handle_user_break);
}

/**
 * Invoke a compiler locally.  This is, obviously, the alternative to
 * dcc_compile_remote().
 *
 * The server does basically the same thing, but it doesn't call this
 * routine because it wants to overlap execution of the compiler with
 * copying the input from the network.
 *
 * This routine used to exec() the compiler in place of distcc.  That
 * is slightly more efficient, because it avoids the need to create,
 * schedule, etc another process.  The problem is that in that case we
 * can't clean up our temporary files, and (not so important) we can't
 * log our resource usage.
 *
 **/
int build_local(CompileJob &job, MsgChannel *local_daemon, struct rusage *used)
{
    list<string> arguments;

    string compiler_name = find_compiler(job);

    if (compiler_name.empty()) {
        log_error() << "could not find " << job.compilerName() << " in PATH." << endl;
        return EXIT_NO_SUCH_FILE;
    }

    arguments.push_back(compiler_name);
    appendList(arguments, job.allFlags());

    if (!job.inputFile().empty()) {
        arguments.push_back(job.inputFile());
    }

    if (!job.outputFile().empty()) {
        arguments.push_back("-o");
        arguments.push_back(job.outputFile());
    }

    vector<char*> argv; 
    string argstxt;

    for (list<string>::const_iterator it = arguments.begin(); it != arguments.end(); ++it) {
        if( *it == "-fdirectives-only" )
            continue; // pointless locally, and it can break things
        argv.push_back(strdup(it->c_str()));
        argstxt += ' ';
        argstxt += *it;
    }

    argv.push_back(0);

    trace() << "invoking:" << argstxt << endl;

    if (!local_daemon) {
        if (!dcc_lock_host()) {
            log_error() << "can't lock for local job" << endl;
            return EXIT_DISTCC_FAILED;
        }
    }

    bool color_output = job.language() != CompileJob::Lang_Custom
                        && colorify_wanted(job);
    int pf[2];

    if (color_output && pipe(pf)) {
        color_output = false;
    }

    if (used || color_output) {
        flush_debug();
        child_pid = fork();
    }

    if (child_pid == -1){
        log_perror("fork failed");
    }

    if (!child_pid) {
        dcc_increment_safeguard(job.language() == CompileJob::Lang_Custom ? SafeguardStepCustom : SafeguardStepCompiler);

        if (color_output) {
            if ((-1 == close(pf[0])) && (errno != EBADF)){
                log_perror("close failed");
            }
            if ((-1 == close(2)) && (errno != EBADF)){
                log_perror("close failed");
            }
            if (-1 == dup2(pf[1], 2)){
                log_perror("dup2 failed");
            }
        }

        execv(argv[0], &argv[0]);
        int exitcode = ( errno == ENOENT ? 127 : 126 );
        ostringstream errmsg;
        errmsg << "execv " << argv[0] << " failed";
        log_perror(errmsg.str());

        dcc_unlock();

        {
            char buf[256];
            snprintf(buf, sizeof(buf), "ICECC[%d]: %s:", getpid(), argv[0]);
            log_perror(buf);
        }

        _exit(exitcode);
    }
    for(vector<char*>::const_iterator i = argv.begin(); i != argv.end(); ++i){
        free(*i);
    }
    argv.clear();

    if (color_output) {
        if ((-1 == close(pf[1])) && (errno != EBADF)){
            log_perror("close failed");
        }
    }

    // setup interrupt signals, so that the JobLocalBeginMsg will
    // have a matching JobLocalDoneMsg
    void (*old_sigint)(int) = signal(SIGINT, handle_user_break);
    void (*old_sigterm)(int) = signal(SIGTERM, handle_user_break);
    void (*old_sigquit)(int) = signal(SIGQUIT, handle_user_break);
    void (*old_sighup)(int) = signal(SIGHUP, handle_user_break);

    if (color_output) {
        string s_ccout;
        char buf[250];

        for (;;) {
	    int r;
            while ((r = read(pf[0], buf, sizeof(buf) - 1)) > 0) {
                buf[r] = '\0';
                s_ccout.append(buf);
            }

            if (r == 0) {
                break;
            }

            if (r < 0 && errno != EINTR) {
                break;
            }
        }

        colorify_output(s_ccout);
    }

    int status = 1;

    while (wait4(child_pid, &status, 0, used) < 0 && errno == EINTR) {}

    status = shell_exit_status(status);

    signal(SIGINT, old_sigint);
    signal(SIGTERM, old_sigterm);
    signal(SIGQUIT, old_sigquit);
    signal(SIGHUP, old_sighup);

    if (user_break_signal) {
        raise(user_break_signal);
    }

    dcc_unlock();

    return status;
}