File: cpp.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 (207 lines) | stat: -rw-r--r-- 6,570 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
/* -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 99; -*- */
/* vim: set ts=4 sw=4 et tw=99:  */
/*
 * distcc -- A simple distributed compiler system
 *
 * Copyright (C) 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.
 */

/**
 * @file
 *
 * Run the preprocessor.  Client-side only.
 **/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#include "client.h"

using namespace std;

bool dcc_is_preprocessed(const string &sfile)
{
    if (sfile.size() < 3) {
        return false;
    }

    int last = sfile.size() - 1;

    if ((sfile[last - 1] == '.') && (sfile[last] == 'i')) {
        return true;    // .i
    }

    if ((sfile[last - 2] == '.') && (sfile[last - 1] == 'i') && (sfile[last] == 'i')) {
        return true;    // .ii
    }

    return false;
}

/**
 * If the input filename is a plain source file rather than a
 * preprocessed source file, then preprocess it to a temporary file
 * and return the name in @p cpp_fname.
 *
 * The preprocessor may still be running when we return; you have to
 * wait for @p cpp_fid to exit before the output is complete.  This
 * allows us to overlap opening the TCP socket, which probably doesn't
 * use many cycles, with running the preprocessor.
 **/
pid_t call_cpp(CompileJob &job, int fdwrite, int fdread)
{
    flush_debug();
    pid_t pid = fork();

    if (pid == -1) {
        log_perror("failed to fork:");
        return -1; /* probably */
    }

    if (pid != 0) {
        /* Parent.  Close the write fd.  */
        if (fdwrite > -1) {
            if ((-1 == close(fdwrite)) && (errno != EBADF)){
                log_perror("close() failed");
            }
        }

        return pid;
    }

    /* Child.  Close the read fd, in case we have one.  */
    if (fdread > -1) {
        if ((-1 == close(fdread)) && (errno != EBADF)){
            log_perror("close failed");
        }
    }

    int ret = dcc_ignore_sigpipe(0);

    if (ret) {  /* set handler back to default */
        _exit(ret);
    }

    char **argv;

    if (dcc_is_preprocessed(job.inputFile())) {
        /* already preprocessed, great.
           write the file to the fdwrite (using cat) */
        argv = new char*[2 + 1];
        argv[0] = strdup("/bin/cat");
        argv[1] = strdup(job.inputFile().c_str());
        argv[2] = 0;
    } else {
        list<string> flags = job.localFlags();
        appendList(flags, job.restFlags());

        for (list<string>::iterator it = flags.begin(); it != flags.end();) {
            /* This has a duplicate meaning. it can either include a file
               for preprocessing or a precompiled header. decide which one.  */
            if ((*it) == "-include") {
                ++it;

                if (it != flags.end()) {
                    std::string p = (*it);

                    if (access(p.c_str(), R_OK) < 0 && access((p + ".gch").c_str(), R_OK) == 0) {
                        // PCH is useless for preprocessing, ignore the flag.
                        list<string>::iterator o = --it;
                        it++;
                        flags.erase(o);
                        o = it++;
                        flags.erase(o);
                    }
                }
            } else if ((*it) == "-include-pch") {
                list<string>::iterator o = it;
                ++it;
                if (it != flags.end()) {
                    std::string p = (*it);
                    if (access(p.c_str(), R_OK) == 0) {
                        // PCH is useless for preprocessing (and probably slows things down), ignore the flag.
                        flags.erase(o);
                        o = it++;
                        flags.erase(o);
                    }
                }
            } else if ((*it) == "-fpch-preprocess") {
                // This would add #pragma GCC pch_preprocess to the preprocessed output, which would make
                // the remote GCC try to load the PCH directly and fail. Just drop it. This may cause a build
                // failure if the -include check above failed to detect usage of a PCH file (e.g. because
                // it needs to be found in one of the -I paths, which we don't check) and the header file
                // itself doesn't exist.
                flags.erase(it++);
            } else {
                ++it;
            }
        }

        int argc = flags.size();
        argc++; // the program
        argc += 2; // -E file.i
        argc += 1; // -frewrite-includes / -fdirectives-only
        argv = new char*[argc + 1];
        argv[0] = strdup(find_compiler(job).c_str());
        int i = 1;

        for (list<string>::const_iterator it = flags.begin(); it != flags.end(); ++it) {
            argv[i++] = strdup(it->c_str());
        }

        argv[i++] = strdup("-E");
        argv[i++] = strdup(job.inputFile().c_str());

        if (compiler_only_rewrite_includes(job)) {
            if( compiler_is_clang(job)) {
                argv[i++] = strdup("-frewrite-includes");
            } else { // gcc
                argv[i++] = strdup("-fdirectives-only");
            }
        }

        argv[i++] = 0;
    }

    string argstxt = argv[ 0 ];
    for( int i = 1; argv[ i ] != NULL; ++i ) {
        argstxt += ' ';
        argstxt += argv[ i ];
    }
    trace() << "preparing source to send: " << argstxt << endl;

    if (fdwrite != STDOUT_FILENO) {
        /* Ignore failure */
        close(STDOUT_FILENO);
        dup2(fdwrite, STDOUT_FILENO);
        close(fdwrite);
    }

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