-
Notifications
You must be signed in to change notification settings - Fork 379
/
ldlogger-tool-gcc.c
556 lines (478 loc) · 14.5 KB
/
ldlogger-tool-gcc.c
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/**
* -------------------------------------------------------------------------
* The CodeChecker Infrastructure
* This file is distributed under the University of Illinois Open Source
* License. See LICENSE.TXT for details.
* -------------------------------------------------------------------------
*/
#include "ldlogger-tool.h"
#include "ldlogger-util.h"
#include <sys/types.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
/**
* List of file extensions accepted as source file. Binaries can also be
* sources of linker actions.
*/
static const char* const srcExts[] = {
"c", "cc", "cp", "cpp", "cxx", "c++", "o", "so", "a", NULL
};
/**
* List of file extensions accepted as object file.
*/
static const char* const objExts[] = {
"o", "so", "a", NULL
};
/**
* List of compiler name infixes belonging to C compilers.
*/
static const char* const cCompiler[] = {
"gcc", "cc", "clang", NULL
};
/**
* List of compiler name infixes belonging to C++ compilers.
*/
static const char* const cppCompiler[] = {
"g++", "c++", "clang++", NULL
};
/**
* Check if the given path is a gcc libpath or not.
*
* @param path_ an absolute directory path.
* @return true if the given path is a gcc lib path, false if not.
*/
static int isGccLibPath(const char* path_)
{
/* FIXME: it could be lib32 or lib64??? */
const char* gccStart = strstr(path_, "/lib/gcc");
if (!gccStart)
{
return 0;
}
/* We want to filter paths like:
* /usr/lib/gcc/x86_64-linux-gnu/4.8/include
* /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed */
return strstr(gccStart, "include") != NULL;
}
/**
* Tries to get the default header includes from a gcc(like) command and stores
* the result into the given vector.
*
* @param prog_ the gcc like program / command.
* @param args_ a vector for the arguments.
*/
static void getDefaultArguments(const char* prog_, LoggerVector* args_)
{
char command[PATH_MAX];
FILE* cmdOut;
char* line = NULL;
size_t lineSize = 0;
ssize_t readSize;
int incStarted = 0;
strcpy(command, prog_);
/* WARNING: this always gets the C++ compiler include
* dirs even if we are compiling C file.
* */
strcat(command, " -xc++ -E -v - < /dev/null 2>&1");
cmdOut = popen(command, "r");
if (!cmdOut)
{
return;
}
while ((readSize = getline(&line, &lineSize, cmdOut)) >= 0)
{
char fullPath[PATH_MAX] = "-I";
char* pathEnd;
char* pathStart;
if (!incStarted)
{
if (strstr(line, "#include <...> search starts here"))
{
incStarted = 1;
}
continue;
}
else if (strstr(line, "End of search list"))
{
break;
}
/* Drop the new line character from the end of the line and the leading
whitespaces. */
for (pathStart = line; *pathStart && isspace(*pathStart); ++pathStart) {}
for (pathEnd = pathStart; *pathEnd && !isspace(*pathEnd); ++pathEnd) {}
*pathEnd = 0;
if (pathStart[0] == 0)
{
/* WTF??? */
continue;
}
if (!loggerMakePathAbs(pathStart, fullPath + 2, 0))
{
/* Invalid path, skip */
continue;
}
if (isGccLibPath(fullPath))
{
/* We have to skip builtin gcc headers, we only need the paths to the
stdlib */
continue;
}
loggerVectorAdd(args_, loggerStrDup(fullPath));
}
free(line);
pclose(cmdOut);
}
/**
* This function inserts the paths from the given environment variable to the
* vector.
*
* Implementation details: This function is used to fetch the value in any of
* CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJC_INCLUDE_PATH variables.
* These contain paths separated by colon. An empty path means the current
* working directory
* (see https://gcc.gnu.org/onlinedocs/cpp/Environment-Variables.html).
*
* @param paths_ A vector in which the items from envVar_ are added.
* @param envVar_ An environment variable which contains paths separated by
* color (:) character. If no such environment variable is set then the vector
* remains untouched.
* @param flag_ A flag which is also inserted before each element in the vector
* as another element (e.g. -I or -isystem). If this is a NULL pointer then
* this flag will not be inserted in the vector.
*/
void getPathsFromEnvVar(
LoggerVector* paths_,
const char* envVar_,
const char* flag_)
{
char* env;
env = getenv(envVar_);
if (!env)
return;
const char* from = env;
const char* to = strchr(env, ':');
while (to)
{
char token[PATH_MAX];
size_t length = to - from;
if (length > PATH_MAX - 1){
// If the string is too long, skip it.
from = to + 1;
to = strchr(from, ':');
continue;
}
strncpy(token, from, length);
token[length] = 0;
from = to + 1;
to = strchr(from, ':');
if (flag_)
loggerVectorAdd(paths_, loggerStrDup(flag_));
if (strcmp(token, "") == 0)
loggerVectorAdd(paths_, loggerStrDup("."));
else
loggerVectorAdd(paths_, loggerStrDup(token));
}
loggerVectorAdd(paths_, loggerStrDup(flag_));
if (*from == 0)
loggerVectorAdd(paths_, loggerStrDup("."));
else
loggerVectorAdd(paths_, loggerStrDup(from));
}
char* findFullPath(const char* executable, char* fullpath) {
char* path;
char* dir;
path = strdup(getenv("PATH"));
for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":")) {
strcpy(fullpath, dir);
strcpy(fullpath + strlen(dir), "/");
strcpy(fullpath + strlen(dir) + 1, executable);
if (access(fullpath, F_OK ) != -1 ) {
free(path);
return fullpath;
}
}
free(path);
return 0;
}
int isObjectFile(const char* filename_)
{
char* ext = loggerGetFileExt(filename_, 1);
int i;
for (i = 0; objExts[i]; ++i)
if (strcmp(ext, objExts[i]) == 0)
{
free(ext);
return 1;
}
free(ext);
return 0;
}
/**
* Compilers (clang) support passing extra flags using so-called response
* files that you specify with the @file syntax.
* This function returns the response file path if it can be found in the
* given arguments or NULL if is doesn't.
* The returned string has to be deallocated by the caller.
*/
char* getResponseFile(const LoggerVector* arguments_)
{
int i;
for (i = 0; i < arguments_->size; ++i)
{
const char* arg = arguments_->data[i];
if (arg != NULL && arg[0] == '@')
return loggerStrDup(arg);
}
return NULL;
}
void transformSomePathsAbsolute(LoggerVector* args_)
{
/* TODO: The argument of -I, -idirafter, -isystem and -iquote may
* start with = sign which means that the following path is relative to
* --sysroot (see: man gcc). This logic is not implemented here. */
static const char* const absFlags[] = {
"-I", "-idirafter", "-imultilib", "-iquote", "-isysroot", "-isystem",
"-iwithprefix", "-iwithprefixbefore", "-sysroot", "--sysroot", NULL};
int pathComing = 0;
for (size_t i = 0; i < args_->size; ++i)
{
if (pathComing)
{
char newPath[PATH_MAX];
loggerMakePathAbs(args_->data[i], newPath, 0);
loggerVectorReplace(args_, i, loggerStrDup(newPath));
pathComing = 0;
}
else
{
const char* const* flag;
for (flag = absFlags;
*flag && !startsWith(args_->data[i], *flag);
++flag)
;
if (!*flag)
continue;
const char* path = (const char*)args_->data[i] + strlen(*flag);
if (*path)
{
char newPath[PATH_MAX];
strcpy(newPath, *flag);
int hasEqual = *path == '=';
if (hasEqual)
{
strcat(newPath, "=");
++path;
}
loggerMakePathAbs(path, newPath + strlen(*flag) + hasEqual, 0);
loggerVectorReplace(args_, i, loggerStrDup(newPath));
}
else
pathComing = 1;
}
}
}
int loggerGccParserCollectActions(
const char* prog_,
const char* const argv_[],
LoggerVector* actions_)
{
enum Language { C, CPP, OBJC } lang = CPP;
size_t i;
/* Position of the last include path + 1 */
char full_prog_path[PATH_MAX+1];
char *path_ptr = NULL;
char* responseFile = NULL;
size_t lastIncPos = 1;
size_t lastSysIncPos = 1;
LoggerAction* action = loggerActionNew();
char* keepLinkVar = getenv("CC_LOGGER_KEEP_LINK");
int keepLink = keepLinkVar && strcmp(keepLinkVar, "true") == 0;
const char* toolName = strrchr(prog_, '/');
if (toolName)
++toolName;
else
toolName = prog_;
/* If prog_ is not an absolute path, we try to find it as an
* executable in the PATH.
* Earlier there was an approach to use realpath() in order to fetch
* the absolute path of the binary. However, realpath() resolves the
* symlinks in the path and it is not good for us.
* The build environment can be set so "g++" is a symlink to
* /usr/bin/ccache. CCache can detect whether it was run through this
* symlink or it was run directly. In the former case CCache forwards
* the command line arguments to the original g++ compiler. This way
* we can query the implicit include paths from the compiler later in
* CodeChecker. If we resolve the symlink, then the implicit include
* path getter command line arguments go to CCache binary. The solution
* is not to resolve the symlinks in the logger.
*/
if (prog_ && prog_[0] != '/')
path_ptr = findFullPath(prog_, full_prog_path);
if (path_ptr) /* Log compiler with full path. */
loggerVectorAdd(&action->arguments, loggerStrDup(full_prog_path));
else /* Compiler was not found in path, log the binary name only. */
loggerVectorAdd(&action->arguments, loggerStrDup(prog_));
/* Determine programming language based on compiler name. */
for (i = 0; cCompiler[i]; ++i)
if (strstr(toolName, cCompiler[i]))
lang = C;
for (i = 0; cppCompiler[i]; ++i)
if (strstr(toolName, cppCompiler[i]))
lang = CPP;
for (i = 1; argv_[i]; ++i)
{
const char* current = argv_[i];
if (current[0])
loggerVectorAdd(&action->arguments, loggerStrDup(current));
if (current[0] == '-')
{
/* Determine the position of the last -I and -isystem flags.
* Depending on whether the parameter of -I or -isystem is separated
* from the flag by a space character.
* 2 == strlen("-I") && 8 == strlen("-isystem")
*/
if (startsWith(current, "-I"))
lastIncPos = action->arguments.size + (current[2] ? 0 : 1);
else if (startsWith(current, "-isystem"))
lastSysIncPos = action->arguments.size + (current[8] ? 0 : 1);
/* Determine the programming language based on -x flag.
*/
else if (startsWith(current, "-x"))
{
/* TODO: The language value after -x can be others too. See the man
* page of GCC.
* TODO: According to a GCC warning the -x flag has no effect when it
* is placed after the last input file to be compiled.
*/
const char* l = current[2] ? current + 2 : argv_[i + 1];
if (strcmp(l, "c") == 0 || strcmp(l, "c-header") == 0)
lang = C;
else if (strcmp(l, "c++") == 0 || strcmp(l, "c++-header") == 0)
lang = CPP;
}
/* Determining the output of the build command. In some cases some .o or
* or other files may be considered a source file when they are a
* parameter of a flag other than -o, such as -MT. The only usage of
* collecting action->output is to remove it from action->sources later.
*/
else if (startsWith(current, "-o"))
{
loggerFileInitFromPath(
&action->output,
current[2] ? current + 2 : argv_[i + 1]);
}
}
else
{
char* ext = loggerGetFileExt(current, 1);
if (ext)
{
int i;
for (i = 0; srcExts[i]; ++i)
{
if (strcmp(srcExts[i], ext) == 0)
{
char newPath[PATH_MAX];
if (getenv("CC_LOGGER_ABS_PATH"))
{
loggerMakePathAbs(current, newPath, 0);
}
else
{
strcpy(newPath, current);
}
loggerVectorAddUnique(&action->sources, loggerStrDup(newPath),
(LoggerCmpFuc) &strcmp);
break;
}
}
}
free(ext);
}
}
if (getenv("CC_LOGGER_DEF_DIRS"))
{
LoggerVector defIncludes;
loggerVectorInit(&defIncludes);
getDefaultArguments(prog_, &defIncludes);
if (defIncludes.size)
{
loggerVectorAddFrom(&action->arguments, &defIncludes,
&lastIncPos, (LoggerDupFuc) &loggerStrDup);
if (lastSysIncPos > lastIncPos)
lastSysIncPos += defIncludes.size;
lastIncPos += defIncludes.size;
}
loggerVectorClear(&defIncludes);
}
if (getenv("CPATH"))
{
LoggerVector includes;
loggerVectorInit(&includes);
getPathsFromEnvVar(&includes, "CPATH", "-I");
if (includes.size)
{
loggerVectorAddFrom(&action->arguments, &includes,
&lastIncPos, (LoggerDupFuc) &loggerStrDup);
if (lastSysIncPos > lastIncPos)
lastSysIncPos += includes.size;
lastIncPos += includes.size;
}
loggerVectorClear(&includes);
}
if (lang == CPP && getenv("CPLUS_INCLUDE_PATH"))
{
LoggerVector includes;
loggerVectorInit(&includes);
getPathsFromEnvVar(&includes, "CPLUS_INCLUDE_PATH", "-isystem");
if (includes.size)
{
loggerVectorAddFrom(&action->arguments, &includes,
&lastSysIncPos, (LoggerDupFuc) &loggerStrDup);
}
loggerVectorClear(&includes);
}
else if (lang == C && getenv("C_INCLUDE_PATH"))
{
LoggerVector includes;
loggerVectorInit(&includes);
getPathsFromEnvVar(&includes, "C_INCLUDE_PATH", "-isystem");
if (includes.size)
{
loggerVectorAddFrom(&action->arguments, &includes,
&lastSysIncPos, (LoggerDupFuc) &loggerStrDup);
}
loggerVectorClear(&includes);
}
if (getenv("CC_LOGGER_ABS_PATH"))
transformSomePathsAbsolute(&action->arguments);
/*
* Workaround for -MT and friends: if the source set contains the output,
* then we have to remove it from the set.
*/
i = loggerVectorFind(&action->sources, action->output.path,
(LoggerCmpFuc) &strcmp);
if (i != SIZE_MAX)
{
loggerVectorErase(&action->sources, i);
}
if (!keepLink)
do {
i = loggerVectorFindIf(&action->sources, (LoggerPredFuc) &isObjectFile);
loggerVectorErase(&action->sources, i);
} while (i != SIZE_MAX);
if (action->sources.size != 0)
{
loggerVectorAdd(actions_, action);
}
else if (responseFile = getResponseFile(&action->arguments))
{
loggerVectorAdd(&action->sources, responseFile);
loggerVectorAdd(actions_, action);
}
return 1;
}