forked from ccache/ccache
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compopt.c
155 lines (142 loc) · 4.46 KB
/
compopt.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
/*
* Copyright (C) 2010 Joel Rosdahl
*
* 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 3 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 "ccache.h"
#include "compopt.h"
#define TOO_HARD (1 << 0)
#define TOO_HARD_DIRECT (1 << 1)
#define TAKES_ARG (1 << 2)
#define TAKES_CONCAT_ARG (1 << 3)
#define TAKES_PATH (1 << 4)
#define AFFECTS_CPP (1 << 5)
struct compopt {
const char *name;
int type;
};
static const struct compopt compopts[] = {
{"--coverage", TOO_HARD},
{"--param", TAKES_ARG},
{"-A", TAKES_ARG},
{"-D", AFFECTS_CPP | TAKES_ARG | TAKES_CONCAT_ARG},
{"-G", TAKES_ARG},
{"-I", AFFECTS_CPP | TAKES_ARG | TAKES_CONCAT_ARG | TAKES_PATH},
{"-L", TAKES_ARG},
{"-M", TOO_HARD},
{"-MF", TAKES_ARG},
{"-MM", TOO_HARD},
{"-MQ", TAKES_ARG},
{"-MT", TAKES_ARG},
{"-U", AFFECTS_CPP | TAKES_ARG | TAKES_CONCAT_ARG},
{"-V", TAKES_ARG},
{"-Xassembler", TAKES_ARG},
{"-Xlinker", TAKES_ARG},
{"-Xpreprocessor", TOO_HARD_DIRECT | TAKES_ARG},
{"-aux-info", TAKES_ARG},
{"-b", TAKES_ARG},
{"-fbranch-probabilities", TOO_HARD},
{"-fprofile-arcs", TOO_HARD},
{"-fprofile-generate", TOO_HARD},
{"-fprofile-use", TOO_HARD},
{"-frepo", TOO_HARD},
{"-ftest-coverage", TOO_HARD},
{"-idirafter", AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
{"-imacros", AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
{"-imultilib", AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
{"-include", AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
{"-install_name", TAKES_ARG}, /* Darwin linker option */
{"-iprefix", AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
{"-iquote", AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
{"-isysroot", AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
{"-isystem", AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
{"-iwithprefix", AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
{"-iwithprefixbefore", AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
{"-nostdinc", AFFECTS_CPP},
{"-nostdinc++", AFFECTS_CPP},
{"-save-temps", TOO_HARD},
{"-u", TAKES_ARG},
};
static int
compare_compopts(const void *key1, const void *key2)
{
const struct compopt *opt1 = (const struct compopt *)key1;
const struct compopt *opt2 = (const struct compopt *)key2;
return strcmp(opt1->name, opt2->name);
}
static const struct compopt *
find(const char *option)
{
struct compopt key;
key.name = option;
return bsearch(
&key, compopts, sizeof(compopts) / sizeof(compopts[0]),
sizeof(compopts[0]), compare_compopts);
}
/* Runs fn on the first two characters of option. */
bool
compopt_short(bool (*fn)(const char *), const char *option)
{
char *short_opt = x_strndup(option, 2);
bool retval = fn(short_opt);
free(short_opt);
return retval;
}
/* For test purposes. */
bool
compopt_verify_sortedness(void)
{
size_t i;
for (i = 1; i < sizeof(compopts)/sizeof(compopts[0]); i++) {
if (strcmp(compopts[i-1].name, compopts[i].name) >= 0) {
fprintf(stderr,
"compopt_verify_sortedness: %s >= %s\n",
compopts[i-1].name,
compopts[i].name);
return false;
}
}
return true;
}
bool
compopt_affects_cpp(const char *option)
{
const struct compopt *co = find(option);
return co && (co->type & AFFECTS_CPP);
}
bool
compopt_too_hard(const char *option)
{
const struct compopt *co = find(option);
return co && (co->type & TOO_HARD);
}
bool
compopt_too_hard_for_direct_mode(const char *option)
{
const struct compopt *co = find(option);
return co && (co->type & TOO_HARD_DIRECT);
}
bool
compopt_takes_path(const char *option)
{
const struct compopt *co = find(option);
return co && (co->type & TAKES_PATH);
}
bool
compopt_takes_arg(const char *option)
{
const struct compopt *co = find(option);
return co && (co->type & TAKES_ARG);
}