-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtarget.c
406 lines (352 loc) · 7.8 KB
/
target.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
/*
* Process name, rule, command and prerequisite structures
*/
#include "make.h"
/*
* Add a prerequisite to the end of the supplied list.
* Return the new head pointer for that list.
*/
struct depend *
newdep(struct name *np, struct depend *dphead)
{
struct depend *dpnew;
struct depend *dp;
dpnew = xmalloc(sizeof(struct depend));
dpnew->d_next = NULL;
dpnew->d_name = np;
dpnew->d_refcnt = 0;
if (dphead == NULL)
return dpnew;
for (dp = dphead; dp->d_next; dp = dp->d_next)
;
dp->d_next = dpnew;
return dphead;
}
void
freedeps(struct depend *dp)
{
struct depend *nextdp;
if (dp && --dp->d_refcnt <= 0) {
for (; dp; dp = nextdp) {
nextdp = dp->d_next;
free(dp);
}
}
}
/*
* Add a command to the end of the supplied list of commands.
* Return the new head pointer for that list.
*/
struct cmd *
newcmd(char *str, struct cmd *cphead)
{
struct cmd *cpnew;
struct cmd *cp;
while (isspace(*str))
str++;
cpnew = xmalloc(sizeof(struct cmd));
cpnew->c_next = NULL;
cpnew->c_cmd = xstrdup(str);
cpnew->c_refcnt = 0;
cpnew->c_makefile = xstrdup(makefile);
cpnew->c_dispno = dispno;
if (cphead == NULL)
return cpnew;
for (cp = cphead; cp->c_next; cp = cp->c_next)
;
cp->c_next = cpnew;
return cphead;
}
void
freecmds(struct cmd *cp)
{
struct cmd *nextcp;
if (cp && --cp->c_refcnt <= 0) {
for (; cp; cp = nextcp) {
nextcp = cp->c_next;
free(cp->c_cmd);
free((void *)cp->c_makefile);
free(cp);
}
}
}
struct name *namehead[HTABSIZE];
struct name *firstname;
struct name *
findname(const char *name)
{
struct name *np;
for (np = namehead[getbucket(name)]; np; np = np->n_next) {
if (strcmp(name, np->n_name) == 0)
return np;
}
return NULL;
}
static int
check_name(const char *name)
{
const char *s;
#if ENABLE_FEATURE_MAKE_EXTENSIONS
# if defined(__CYGWIN__)
if (!posix || (pragma & P_WINDOWS)) {
if (isalpha(name[0]) && name[1] == ':' && name[2] == '/') {
name += 3;
}
}
# endif
if (!posix) {
for (s = name; *s; ++s) {
if (*s == '=')
return FALSE;
}
return TRUE;
}
#endif
for (s = name; *s; ++s) {
if ((
#if ENABLE_FEATURE_MAKE_EXTENSIONS
(pragma & P_TARGET_NAME) ||
#endif
#if ENABLE_FEATURE_MAKE_POSIX_2024
!POSIX_2017
#else
FALSE
#endif
) ? !(isfname(*s) || *s == '/') : !ispname(*s))
return FALSE;
}
return TRUE;
}
int
is_valid_target(const char *name)
{
char *archive, *member = NULL;
int ret;
/* Names of the form 'lib(member)' are referred to as 'expressions'
* in POSIX and are subjected to special treatment. The 'lib'
* and 'member' elements must each be a valid target name. */
archive = splitlib(name, &member);
ret = check_name(archive) && (member == NULL || check_name(member));
free(archive);
return ret;
}
#if ENABLE_FEATURE_MAKE_EXTENSIONS
static int
potentially_valid_target(const char *name)
{
int ret = FALSE;
if (!(pragma & P_TARGET_NAME)) {
pragma |= P_TARGET_NAME;
ret = is_valid_target(name);
pragma &= ~P_TARGET_NAME;
}
return ret;
}
#endif
/*
* Intern a name. Return a pointer to the name struct
*/
struct name *
newname(const char *name)
{
struct name *np = findname(name);
if (np == NULL) {
unsigned int bucket;
if (!is_valid_target(name))
#if ENABLE_FEATURE_MAKE_EXTENSIONS
error("invalid target name '%s'%s", name,
potentially_valid_target(name) ?
": allow with pragma target_name" : "");
#else
error("invalid target name '%s'", name);
#endif
bucket = getbucket(name);
np = xmalloc(sizeof(struct name));
np->n_next = namehead[bucket];
namehead[bucket] = np;
np->n_name = xstrdup(name);
np->n_rule = NULL;
np->n_tim = (struct timespec){0, 0};
np->n_flag = 0;
}
return np;
}
/*
* Return the commands on the first rule that has them or NULL.
*/
struct cmd *
getcmd(struct name *np)
{
struct rule *rp;
if (np == NULL)
return NULL;
for (rp = np->n_rule; rp; rp = rp->r_next)
if (rp->r_cmd)
return rp->r_cmd;
return NULL;
}
#if ENABLE_FEATURE_CLEAN_UP
void
freenames(void)
{
int i;
struct name *np, *nextnp;
for (i = 0; i < HTABSIZE; i++) {
for (np = namehead[i]; np; np = nextnp) {
nextnp = np->n_next;
free(np->n_name);
freerules(np->n_rule);
free(np);
}
}
}
#endif
void
freerules(struct rule *rp)
{
struct rule *nextrp;
for (; rp; rp = nextrp) {
nextrp = rp->r_next;
freedeps(rp->r_dep);
freecmds(rp->r_cmd);
free(rp);
}
}
static void *
inc_ref(void *vp)
{
if (vp) {
struct depend *dp = vp;
if (dp->d_refcnt == INT_MAX)
error("out of memory");
dp->d_refcnt++;
}
return vp;
}
#if ENABLE_FEATURE_MAKE_EXTENSIONS
// Order must match constants in make.h
// POSIX levels must be last and in increasing order
static const char *p_name[] = {
"macro_name",
"target_name",
"command_comment",
"empty_suffix",
#if defined(__CYGWIN__)
"windows",
#endif
"posix_2017",
"posix_2024",
"posix_202x"
};
void
set_pragma(const char *name)
{
int i;
// posix_202x is an alias for posix_2024
for (i = 0; i < sizeof(p_name)/sizeof(p_name[0]); ++i) {
if (strcmp(name, p_name[i]) == 0) {
#if !ENABLE_FEATURE_MAKE_POSIX_2024
if (i == BIT_POSIX_2024 || i == BIT_POSIX_202X) {
break;
}
#endif
if (i >= BIT_POSIX_2017) {
// POSIX level is stored in a separate variable.
// No bits in 'pragma' are used.
if (posix_level == DEFAULT_POSIX_LEVEL) {
posix_level = i - BIT_POSIX_2017;
if (posix_level > STD_POSIX_2024)
posix_level = STD_POSIX_2024;
} else if (posix_level != i - BIT_POSIX_2017)
warning("unable to change POSIX level");
} else {
pragma |= 1 << i;
}
return;
}
}
warning("invalid pragma '%s'", name);
}
void
pragmas_to_env(void)
{
int i;
char *val = NULL;
for (i = 0; i < BIT_POSIX_2017; ++i) {
if ((pragma & (1 << i)))
val = xappendword(val, p_name[i]);
}
if (posix_level != DEFAULT_POSIX_LEVEL)
val = xappendword(val, p_name[BIT_POSIX_2017 + posix_level]);
if (val) {
setenv("PDPMAKE_PRAGMAS", val, 1);
free(val);
}
}
#endif
/*
* Add a new rule to a target. This checks to see if commands already
* exist for the target. If flag is TRUE the target can have multiple
* rules with commands (double-colon rules).
*
* i) If the name is a special target and there are no prerequisites
* or commands to be added remove all prerequisites and commands.
* This is necessary when clearing a built-in inference rule.
* ii) If name is a special target and has commands, replace them.
* This is for redefining commands for an inference rule.
*/
void
addrule(struct name *np, struct depend *dp, struct cmd *cp, int flag)
{
struct rule *rp;
struct rule **rpp;
#if ENABLE_FEATURE_MAKE_EXTENSIONS
// Can't mix single-colon and double-colon rules
if (!posix && (np->n_flag & N_TARGET)) {
if (!(np->n_flag & N_DOUBLE) != !flag) // like xor
error("inconsistent rules for target %s", np->n_name);
}
#endif
// Clear out prerequisites and commands
if ((np->n_flag & N_SPECIAL) && !dp && !cp) {
#if ENABLE_FEATURE_MAKE_POSIX_2024
if (strcmp(np->n_name, ".PHONY") == 0)
return;
#endif
freerules(np->n_rule);
np->n_rule = NULL;
return;
}
if (cp && !(np->n_flag & N_DOUBLE) && getcmd(np)) {
// Handle the inference rule redefinition case
// .DEFAULT rule can also be redefined (as an extension).
if ((np->n_flag & N_INFERENCE)
#if ENABLE_FEATURE_MAKE_EXTENSIONS
&& !(posix && (np->n_flag & N_SPECIAL))
#endif
) {
freerules(np->n_rule);
np->n_rule = NULL;
} else {
error("commands defined twice for target %s", np->n_name);
}
}
rpp = &np->n_rule;
while (*rpp)
rpp = &(*rpp)->r_next;
*rpp = rp = xmalloc(sizeof(struct rule));
rp->r_next = NULL;
rp->r_dep = inc_ref(dp);
rp->r_cmd = inc_ref(cp);
np->n_flag |= N_TARGET;
if (flag)
np->n_flag |= N_DOUBLE;
#if ENABLE_FEATURE_MAKE_EXTENSIONS
if (strcmp(np->n_name, ".PRAGMA") == 0) {
for (; dp; dp = dp->d_next) {
set_pragma(dp->d_name->n_name);
}
pragmas_to_env();
}
#endif
}