1 | /* Internals of variables for GNU Make.
|
---|
2 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997,
|
---|
3 | 2002 Free Software Foundation, Inc.
|
---|
4 | This file is part of GNU Make.
|
---|
5 |
|
---|
6 | GNU Make is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Make is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Make; see the file COPYING. If not, write to
|
---|
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
---|
19 | Boston, MA 02111-1307, USA. */
|
---|
20 |
|
---|
21 | #include "make.h"
|
---|
22 | #include "dep.h"
|
---|
23 | #include "filedef.h"
|
---|
24 | #include "job.h"
|
---|
25 | #include "commands.h"
|
---|
26 | #include "variable.h"
|
---|
27 | #include "rule.h"
|
---|
28 | #ifdef WINDOWS32
|
---|
29 | #include "pathstuff.h"
|
---|
30 | #endif
|
---|
31 | #include "hash.h"
|
---|
32 |
|
---|
33 | /* Chain of all pattern-specific variables. */
|
---|
34 |
|
---|
35 | static struct pattern_var *pattern_vars;
|
---|
36 |
|
---|
37 | /* Pointer to last struct in the chain, so we can add onto the end. */
|
---|
38 |
|
---|
39 | static struct pattern_var *last_pattern_var;
|
---|
40 |
|
---|
41 | /* Create a new pattern-specific variable struct. */
|
---|
42 |
|
---|
43 | struct pattern_var *
|
---|
44 | create_pattern_var (char *target, char *suffix)
|
---|
45 | {
|
---|
46 | register struct pattern_var *p
|
---|
47 | = (struct pattern_var *) xmalloc (sizeof (struct pattern_var));
|
---|
48 |
|
---|
49 | if (last_pattern_var != 0)
|
---|
50 | last_pattern_var->next = p;
|
---|
51 | else
|
---|
52 | pattern_vars = p;
|
---|
53 | last_pattern_var = p;
|
---|
54 | p->next = 0;
|
---|
55 |
|
---|
56 | p->target = target;
|
---|
57 | p->len = strlen (target);
|
---|
58 | p->suffix = suffix + 1;
|
---|
59 |
|
---|
60 | return p;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /* Look up a target in the pattern-specific variable list. */
|
---|
64 |
|
---|
65 | static struct pattern_var *
|
---|
66 | lookup_pattern_var (struct pattern_var *start, char *target)
|
---|
67 | {
|
---|
68 | struct pattern_var *p;
|
---|
69 | unsigned int targlen = strlen(target);
|
---|
70 |
|
---|
71 | for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
|
---|
72 | {
|
---|
73 | char *stem;
|
---|
74 | unsigned int stemlen;
|
---|
75 |
|
---|
76 | if (p->len > targlen)
|
---|
77 | /* It can't possibly match. */
|
---|
78 | continue;
|
---|
79 |
|
---|
80 | /* From the lengths of the filename and the pattern parts,
|
---|
81 | find the stem: the part of the filename that matches the %. */
|
---|
82 | stem = target + (p->suffix - p->target - 1);
|
---|
83 | stemlen = targlen - p->len + 1;
|
---|
84 |
|
---|
85 | /* Compare the text in the pattern before the stem, if any. */
|
---|
86 | if (stem > target && !strneq (p->target, target, stem - target))
|
---|
87 | continue;
|
---|
88 |
|
---|
89 | /* Compare the text in the pattern after the stem, if any.
|
---|
90 | We could test simply using streq, but this way we compare the
|
---|
91 | first two characters immediately. This saves time in the very
|
---|
92 | common case where the first character matches because it is a
|
---|
93 | period. */
|
---|
94 | if (*p->suffix == stem[stemlen]
|
---|
95 | && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
|
---|
96 | break;
|
---|
97 | }
|
---|
98 |
|
---|
99 | return p;
|
---|
100 | }
|
---|
101 | |
---|
102 |
|
---|
103 | /* Hash table of all global variable definitions. */
|
---|
104 |
|
---|
105 | static unsigned long
|
---|
106 | variable_hash_1 (const void *keyv)
|
---|
107 | {
|
---|
108 | struct variable const *key = (struct variable const *) keyv;
|
---|
109 | return_STRING_N_HASH_1 (key->name, key->length);
|
---|
110 | }
|
---|
111 |
|
---|
112 | static unsigned long
|
---|
113 | variable_hash_2 (const void *keyv)
|
---|
114 | {
|
---|
115 | struct variable const *key = (struct variable const *) keyv;
|
---|
116 | return_STRING_N_HASH_2 (key->name, key->length);
|
---|
117 | }
|
---|
118 |
|
---|
119 | static int
|
---|
120 | variable_hash_cmp (const void *xv, const void *yv)
|
---|
121 | {
|
---|
122 | struct variable const *x = (struct variable const *) xv;
|
---|
123 | struct variable const *y = (struct variable const *) yv;
|
---|
124 | int result = x->length - y->length;
|
---|
125 | if (result)
|
---|
126 | return result;
|
---|
127 | return_STRING_N_COMPARE (x->name, y->name, x->length);
|
---|
128 | }
|
---|
129 |
|
---|
130 | #ifndef VARIABLE_BUCKETS
|
---|
131 | #define VARIABLE_BUCKETS 523
|
---|
132 | #endif
|
---|
133 | #ifndef PERFILE_VARIABLE_BUCKETS
|
---|
134 | #define PERFILE_VARIABLE_BUCKETS 23
|
---|
135 | #endif
|
---|
136 | #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
|
---|
137 | #define SMALL_SCOPE_VARIABLE_BUCKETS 13
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | static struct variable_set global_variable_set;
|
---|
141 | static struct variable_set_list global_setlist
|
---|
142 | = { 0, &global_variable_set };
|
---|
143 | struct variable_set_list *current_variable_set_list = &global_setlist;
|
---|
144 | |
---|
145 |
|
---|
146 | /* Implement variables. */
|
---|
147 |
|
---|
148 | void
|
---|
149 | init_hash_global_variable_set (void)
|
---|
150 | {
|
---|
151 | hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
|
---|
152 | variable_hash_1, variable_hash_2, variable_hash_cmp);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /* Define variable named NAME with value VALUE in SET. VALUE is copied.
|
---|
156 | LENGTH is the length of NAME, which does not need to be null-terminated.
|
---|
157 | ORIGIN specifies the origin of the variable (makefile, command line
|
---|
158 | or environment).
|
---|
159 | If RECURSIVE is nonzero a flag is set in the variable saying
|
---|
160 | that it should be recursively re-expanded. */
|
---|
161 |
|
---|
162 | struct variable *
|
---|
163 | define_variable_in_set (const char *name, unsigned int length,
|
---|
164 | char *value, enum variable_origin origin,
|
---|
165 | int recursive, struct variable_set *set,
|
---|
166 | const struct floc *flocp)
|
---|
167 | {
|
---|
168 | struct variable *v;
|
---|
169 | struct variable **var_slot;
|
---|
170 | struct variable var_key;
|
---|
171 |
|
---|
172 | if (set == NULL)
|
---|
173 | set = &global_variable_set;
|
---|
174 |
|
---|
175 | var_key.name = (char *) name;
|
---|
176 | var_key.length = length;
|
---|
177 | var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
|
---|
178 |
|
---|
179 | if (env_overrides && origin == o_env)
|
---|
180 | origin = o_env_override;
|
---|
181 |
|
---|
182 | v = *var_slot;
|
---|
183 | if (! HASH_VACANT (v))
|
---|
184 | {
|
---|
185 | if (env_overrides && v->origin == o_env)
|
---|
186 | /* V came from in the environment. Since it was defined
|
---|
187 | before the switches were parsed, it wasn't affected by -e. */
|
---|
188 | v->origin = o_env_override;
|
---|
189 |
|
---|
190 | /* A variable of this name is already defined.
|
---|
191 | If the old definition is from a stronger source
|
---|
192 | than this one, don't redefine it. */
|
---|
193 | if ((int) origin >= (int) v->origin)
|
---|
194 | {
|
---|
195 | if (v->value != 0)
|
---|
196 | free (v->value);
|
---|
197 | v->value = xstrdup (value);
|
---|
198 | if (flocp != 0)
|
---|
199 | v->fileinfo = *flocp;
|
---|
200 | else
|
---|
201 | v->fileinfo.filenm = 0;
|
---|
202 | v->origin = origin;
|
---|
203 | v->recursive = recursive;
|
---|
204 | }
|
---|
205 | return v;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /* Create a new variable definition and add it to the hash table. */
|
---|
209 |
|
---|
210 | v = (struct variable *) xmalloc (sizeof (struct variable));
|
---|
211 | v->name = savestring (name, length);
|
---|
212 | v->length = length;
|
---|
213 | hash_insert_at (&set->table, v, var_slot);
|
---|
214 | v->value = xstrdup (value);
|
---|
215 | if (flocp != 0)
|
---|
216 | v->fileinfo = *flocp;
|
---|
217 | else
|
---|
218 | v->fileinfo.filenm = 0;
|
---|
219 | v->origin = origin;
|
---|
220 | v->recursive = recursive;
|
---|
221 | v->special = 0;
|
---|
222 | v->expanding = 0;
|
---|
223 | v->exp_count = 0;
|
---|
224 | v->per_target = 0;
|
---|
225 | v->append = 0;
|
---|
226 | v->export = v_default;
|
---|
227 |
|
---|
228 | v->exportable = 1;
|
---|
229 | if (*name != '_' && (*name < 'A' || *name > 'Z')
|
---|
230 | && (*name < 'a' || *name > 'z'))
|
---|
231 | v->exportable = 0;
|
---|
232 | else
|
---|
233 | {
|
---|
234 | for (++name; *name != '\0'; ++name)
|
---|
235 | if (*name != '_' && (*name < 'a' || *name > 'z')
|
---|
236 | && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
|
---|
237 | break;
|
---|
238 |
|
---|
239 | if (*name != '\0')
|
---|
240 | v->exportable = 0;
|
---|
241 | }
|
---|
242 |
|
---|
243 | return v;
|
---|
244 | }
|
---|
245 | |
---|
246 |
|
---|
247 | /* If the variable passed in is "special", handle its special nature.
|
---|
248 | Currently there are two such variables, both used for introspection:
|
---|
249 | .VARIABLES expands to a list of all the variables defined in this instance
|
---|
250 | of make.
|
---|
251 | .TARGETS expands to a list of all the targets defined in this
|
---|
252 | instance of make.
|
---|
253 | Returns the variable reference passed in. */
|
---|
254 |
|
---|
255 | #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
|
---|
256 |
|
---|
257 | static struct variable *
|
---|
258 | handle_special_var (struct variable *var)
|
---|
259 | {
|
---|
260 | static unsigned long last_var_count = 0;
|
---|
261 |
|
---|
262 |
|
---|
263 | /* This one actually turns out to be very hard, due to the way the parser
|
---|
264 | records targets. The way it works is that target information is collected
|
---|
265 | internally until make knows the target is completely specified. It unitl
|
---|
266 | it sees that some new construct (a new target or variable) is defined that
|
---|
267 | it knows the previous one is done. In short, this means that if you do
|
---|
268 | this:
|
---|
269 |
|
---|
270 | all:
|
---|
271 |
|
---|
272 | TARGS := $(.TARGETS)
|
---|
273 |
|
---|
274 | then $(TARGS) won't contain "all", because it's not until after the
|
---|
275 | variable is created that the previous target is completed.
|
---|
276 |
|
---|
277 | Changing this would be a major pain. I think a less complex way to do it
|
---|
278 | would be to pre-define the target files as soon as the first line is
|
---|
279 | parsed, then come back and do the rest of the definition as now. That
|
---|
280 | would allow $(.TARGETS) to be correct without a major change to the way
|
---|
281 | the parser works.
|
---|
282 |
|
---|
283 | if (streq (var->name, ".TARGETS"))
|
---|
284 | var->value = build_target_list (var->value);
|
---|
285 | else
|
---|
286 | */
|
---|
287 |
|
---|
288 | if (streq (var->name, ".VARIABLES")
|
---|
289 | && global_variable_set.table.ht_fill != last_var_count)
|
---|
290 | {
|
---|
291 | unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
|
---|
292 | unsigned long len;
|
---|
293 | char *p;
|
---|
294 | struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
|
---|
295 | struct variable **end = &vp[global_variable_set.table.ht_size];
|
---|
296 |
|
---|
297 | /* Make sure we have at least MAX bytes in the allocated buffer. */
|
---|
298 | var->value = xrealloc (var->value, max);
|
---|
299 |
|
---|
300 | /* Walk through the hash of variables, constructing a list of names. */
|
---|
301 | p = var->value;
|
---|
302 | len = 0;
|
---|
303 | for (; vp < end; ++vp)
|
---|
304 | if (!HASH_VACANT (*vp))
|
---|
305 | {
|
---|
306 | struct variable *v = *vp;
|
---|
307 | int l = v->length;
|
---|
308 |
|
---|
309 | len += l + 1;
|
---|
310 | if (len > max)
|
---|
311 | {
|
---|
312 | unsigned long off = p - var->value;
|
---|
313 |
|
---|
314 | max += EXPANSION_INCREMENT (l + 1);
|
---|
315 | var->value = xrealloc (var->value, max);
|
---|
316 | p = &var->value[off];
|
---|
317 | }
|
---|
318 |
|
---|
319 | bcopy (v->name, p, l);
|
---|
320 | p += l;
|
---|
321 | *(p++) = ' ';
|
---|
322 | }
|
---|
323 | *(p-1) = '\0';
|
---|
324 |
|
---|
325 | /* Remember how many variables are in our current count. Since we never
|
---|
326 | remove variables from the list, this is a reliable way to know whether
|
---|
327 | the list is up to date or needs to be recomputed. */
|
---|
328 |
|
---|
329 | last_var_count = global_variable_set.table.ht_fill;
|
---|
330 | }
|
---|
331 |
|
---|
332 | return var;
|
---|
333 | }
|
---|
334 |
|
---|
335 | |
---|
336 |
|
---|
337 | /* Lookup a variable whose name is a string starting at NAME
|
---|
338 | and with LENGTH chars. NAME need not be null-terminated.
|
---|
339 | Returns address of the `struct variable' containing all info
|
---|
340 | on the variable, or nil if no such variable is defined. */
|
---|
341 |
|
---|
342 | struct variable *
|
---|
343 | lookup_variable (const char *name, unsigned int length)
|
---|
344 | {
|
---|
345 | const struct variable_set_list *setlist;
|
---|
346 | struct variable var_key;
|
---|
347 |
|
---|
348 | var_key.name = (char *) name;
|
---|
349 | var_key.length = length;
|
---|
350 |
|
---|
351 | for (setlist = current_variable_set_list;
|
---|
352 | setlist != 0; setlist = setlist->next)
|
---|
353 | {
|
---|
354 | const struct variable_set *set = setlist->set;
|
---|
355 | struct variable *v;
|
---|
356 |
|
---|
357 | v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
|
---|
358 | if (v)
|
---|
359 | return v->special ? handle_special_var (v) : v;
|
---|
360 | }
|
---|
361 |
|
---|
362 | #ifdef VMS
|
---|
363 | /* since we don't read envp[] on startup, try to get the
|
---|
364 | variable via getenv() here. */
|
---|
365 | {
|
---|
366 | char *vname = alloca (length + 1);
|
---|
367 | char *value;
|
---|
368 | strncpy (vname, name, length);
|
---|
369 | vname[length] = 0;
|
---|
370 | value = getenv (vname);
|
---|
371 | if (value != 0)
|
---|
372 | {
|
---|
373 | char *sptr;
|
---|
374 | int scnt;
|
---|
375 |
|
---|
376 | sptr = value;
|
---|
377 | scnt = 0;
|
---|
378 |
|
---|
379 | while ((sptr = strchr (sptr, '$')))
|
---|
380 | {
|
---|
381 | scnt++;
|
---|
382 | sptr++;
|
---|
383 | }
|
---|
384 |
|
---|
385 | if (scnt > 0)
|
---|
386 | {
|
---|
387 | char *nvalue;
|
---|
388 | char *nptr;
|
---|
389 |
|
---|
390 | nvalue = alloca (strlen (value) + scnt + 1);
|
---|
391 | sptr = value;
|
---|
392 | nptr = nvalue;
|
---|
393 |
|
---|
394 | while (*sptr)
|
---|
395 | {
|
---|
396 | if (*sptr == '$')
|
---|
397 | {
|
---|
398 | *nptr++ = '$';
|
---|
399 | *nptr++ = '$';
|
---|
400 | }
|
---|
401 | else
|
---|
402 | {
|
---|
403 | *nptr++ = *sptr;
|
---|
404 | }
|
---|
405 | sptr++;
|
---|
406 | }
|
---|
407 |
|
---|
408 | *nptr = '\0';
|
---|
409 | return define_variable (vname, length, nvalue, o_env, 1);
|
---|
410 |
|
---|
411 | }
|
---|
412 |
|
---|
413 | return define_variable (vname, length, value, o_env, 1);
|
---|
414 | }
|
---|
415 | }
|
---|
416 | #endif /* VMS */
|
---|
417 |
|
---|
418 | return 0;
|
---|
419 | }
|
---|
420 | |
---|
421 |
|
---|
422 | /* Lookup a variable whose name is a string starting at NAME
|
---|
423 | and with LENGTH chars in set SET. NAME need not be null-terminated.
|
---|
424 | Returns address of the `struct variable' containing all info
|
---|
425 | on the variable, or nil if no such variable is defined. */
|
---|
426 |
|
---|
427 | struct variable *
|
---|
428 | lookup_variable_in_set (const char *name, unsigned int length,
|
---|
429 | const struct variable_set *set)
|
---|
430 | {
|
---|
431 | struct variable var_key;
|
---|
432 |
|
---|
433 | var_key.name = (char *) name;
|
---|
434 | var_key.length = length;
|
---|
435 |
|
---|
436 | return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
|
---|
437 | }
|
---|
438 | |
---|
439 |
|
---|
440 | /* Initialize FILE's variable set list. If FILE already has a variable set
|
---|
441 | list, the topmost variable set is left intact, but the the rest of the
|
---|
442 | chain is replaced with FILE->parent's setlist. If FILE is a double-colon
|
---|
443 | rule, then we will use the "root" double-colon target's variable set as the
|
---|
444 | parent of FILE's variable set.
|
---|
445 |
|
---|
446 | If we're READing a makefile, don't do the pattern variable search now,
|
---|
447 | since the pattern variable might not have been defined yet. */
|
---|
448 |
|
---|
449 | void
|
---|
450 | initialize_file_variables (struct file *file, int reading)
|
---|
451 | {
|
---|
452 | register struct variable_set_list *l = file->variables;
|
---|
453 |
|
---|
454 | if (l == 0)
|
---|
455 | {
|
---|
456 | l = (struct variable_set_list *)
|
---|
457 | xmalloc (sizeof (struct variable_set_list));
|
---|
458 | l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
|
---|
459 | hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
|
---|
460 | variable_hash_1, variable_hash_2, variable_hash_cmp);
|
---|
461 | file->variables = l;
|
---|
462 | }
|
---|
463 |
|
---|
464 | /* If this is a double-colon, then our "parent" is the "root" target for
|
---|
465 | this double-colon rule. Since that rule has the same name, parent,
|
---|
466 | etc. we can just use its variables as the "next" for ours. */
|
---|
467 |
|
---|
468 | if (file->double_colon && file->double_colon != file)
|
---|
469 | {
|
---|
470 | initialize_file_variables (file->double_colon, reading);
|
---|
471 | l->next = file->double_colon->variables;
|
---|
472 | return;
|
---|
473 | }
|
---|
474 |
|
---|
475 | if (file->parent == 0)
|
---|
476 | l->next = &global_setlist;
|
---|
477 | else
|
---|
478 | {
|
---|
479 | initialize_file_variables (file->parent, reading);
|
---|
480 | l->next = file->parent->variables;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /* If we're not reading makefiles and we haven't looked yet, see if
|
---|
484 | we can find pattern variables for this target. */
|
---|
485 |
|
---|
486 | if (!reading && !file->pat_searched)
|
---|
487 | {
|
---|
488 | struct pattern_var *p;
|
---|
489 |
|
---|
490 | p = lookup_pattern_var (0, file->name);
|
---|
491 | if (p != 0)
|
---|
492 | {
|
---|
493 | struct variable_set_list *global = current_variable_set_list;
|
---|
494 |
|
---|
495 | /* We found at least one. Set up a new variable set to accumulate
|
---|
496 | all the pattern variables that match this target. */
|
---|
497 |
|
---|
498 | file->pat_variables = create_new_variable_set ();
|
---|
499 | current_variable_set_list = file->pat_variables;
|
---|
500 |
|
---|
501 | do
|
---|
502 | {
|
---|
503 | /* We found one, so insert it into the set. */
|
---|
504 |
|
---|
505 | struct variable *v;
|
---|
506 |
|
---|
507 | if (p->variable.flavor == f_simple)
|
---|
508 | {
|
---|
509 | v = define_variable_loc (
|
---|
510 | p->variable.name, strlen (p->variable.name),
|
---|
511 | p->variable.value, p->variable.origin,
|
---|
512 | 0, &p->variable.fileinfo);
|
---|
513 |
|
---|
514 | v->flavor = f_simple;
|
---|
515 | }
|
---|
516 | else
|
---|
517 | {
|
---|
518 | v = do_variable_definition (
|
---|
519 | &p->variable.fileinfo, p->variable.name,
|
---|
520 | p->variable.value, p->variable.origin,
|
---|
521 | p->variable.flavor, 1);
|
---|
522 | }
|
---|
523 |
|
---|
524 | /* Also mark it as a per-target and copy export status. */
|
---|
525 | v->per_target = p->variable.per_target;
|
---|
526 | v->export = p->variable.export;
|
---|
527 | }
|
---|
528 | while ((p = lookup_pattern_var (p, file->name)) != 0);
|
---|
529 |
|
---|
530 | current_variable_set_list = global;
|
---|
531 | }
|
---|
532 | file->pat_searched = 1;
|
---|
533 | }
|
---|
534 |
|
---|
535 | /* If we have a pattern variable match, set it up. */
|
---|
536 |
|
---|
537 | if (file->pat_variables != 0)
|
---|
538 | {
|
---|
539 | file->pat_variables->next = l->next;
|
---|
540 | l->next = file->pat_variables;
|
---|
541 | }
|
---|
542 | }
|
---|
543 | |
---|
544 |
|
---|
545 | /* Pop the top set off the current variable set list,
|
---|
546 | and free all its storage. */
|
---|
547 |
|
---|
548 | static void
|
---|
549 | free_variable_name_and_value (const void *item)
|
---|
550 | {
|
---|
551 | struct variable *v = (struct variable *) item;
|
---|
552 | free (v->name);
|
---|
553 | free (v->value);
|
---|
554 | }
|
---|
555 |
|
---|
556 | void
|
---|
557 | pop_variable_scope (void)
|
---|
558 | {
|
---|
559 | struct variable_set_list *setlist = current_variable_set_list;
|
---|
560 | struct variable_set *set = setlist->set;
|
---|
561 |
|
---|
562 | current_variable_set_list = setlist->next;
|
---|
563 | free ((char *) setlist);
|
---|
564 |
|
---|
565 | hash_map (&set->table, free_variable_name_and_value);
|
---|
566 | hash_free (&set->table, 1);
|
---|
567 |
|
---|
568 | free ((char *) set);
|
---|
569 | }
|
---|
570 |
|
---|
571 | struct variable_set_list *
|
---|
572 | create_new_variable_set (void)
|
---|
573 | {
|
---|
574 | register struct variable_set_list *setlist;
|
---|
575 | register struct variable_set *set;
|
---|
576 |
|
---|
577 | set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
|
---|
578 | hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
|
---|
579 | variable_hash_1, variable_hash_2, variable_hash_cmp);
|
---|
580 |
|
---|
581 | setlist = (struct variable_set_list *)
|
---|
582 | xmalloc (sizeof (struct variable_set_list));
|
---|
583 | setlist->set = set;
|
---|
584 | setlist->next = current_variable_set_list;
|
---|
585 |
|
---|
586 | return setlist;
|
---|
587 | }
|
---|
588 |
|
---|
589 | /* Create a new variable set and push it on the current setlist. */
|
---|
590 |
|
---|
591 | struct variable_set_list *
|
---|
592 | push_new_variable_scope (void)
|
---|
593 | {
|
---|
594 | return (current_variable_set_list = create_new_variable_set());
|
---|
595 | }
|
---|
596 | |
---|
597 |
|
---|
598 | /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
|
---|
599 |
|
---|
600 | static void
|
---|
601 | merge_variable_sets (struct variable_set *to_set,
|
---|
602 | struct variable_set *from_set)
|
---|
603 | {
|
---|
604 | struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
|
---|
605 | struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
|
---|
606 |
|
---|
607 | for ( ; from_var_slot < from_var_end; from_var_slot++)
|
---|
608 | if (! HASH_VACANT (*from_var_slot))
|
---|
609 | {
|
---|
610 | struct variable *from_var = *from_var_slot;
|
---|
611 | struct variable **to_var_slot
|
---|
612 | = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
|
---|
613 | if (HASH_VACANT (*to_var_slot))
|
---|
614 | hash_insert_at (&to_set->table, from_var, to_var_slot);
|
---|
615 | else
|
---|
616 | {
|
---|
617 | /* GKM FIXME: delete in from_set->table */
|
---|
618 | free (from_var->value);
|
---|
619 | free (from_var);
|
---|
620 | }
|
---|
621 | }
|
---|
622 | }
|
---|
623 |
|
---|
624 | /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
|
---|
625 |
|
---|
626 | void
|
---|
627 | merge_variable_set_lists (struct variable_set_list **setlist0,
|
---|
628 | struct variable_set_list *setlist1)
|
---|
629 | {
|
---|
630 | register struct variable_set_list *list0 = *setlist0;
|
---|
631 | struct variable_set_list *last0 = 0;
|
---|
632 |
|
---|
633 | while (setlist1 != 0 && list0 != 0)
|
---|
634 | {
|
---|
635 | struct variable_set_list *next = setlist1;
|
---|
636 | setlist1 = setlist1->next;
|
---|
637 |
|
---|
638 | merge_variable_sets (list0->set, next->set);
|
---|
639 |
|
---|
640 | last0 = list0;
|
---|
641 | list0 = list0->next;
|
---|
642 | }
|
---|
643 |
|
---|
644 | if (setlist1 != 0)
|
---|
645 | {
|
---|
646 | if (last0 == 0)
|
---|
647 | *setlist0 = setlist1;
|
---|
648 | else
|
---|
649 | last0->next = setlist1;
|
---|
650 | }
|
---|
651 | }
|
---|
652 | |
---|
653 |
|
---|
654 | /* Define the automatic variables, and record the addresses
|
---|
655 | of their structures so we can change their values quickly. */
|
---|
656 |
|
---|
657 | void
|
---|
658 | define_automatic_variables (void)
|
---|
659 | {
|
---|
660 | #if defined(WINDOWS32) || defined(__EMX__)
|
---|
661 | extern char* default_shell;
|
---|
662 | #else
|
---|
663 | extern char default_shell[];
|
---|
664 | #endif
|
---|
665 | register struct variable *v;
|
---|
666 | char buf[200];
|
---|
667 |
|
---|
668 | sprintf (buf, "%u", makelevel);
|
---|
669 | (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
|
---|
670 |
|
---|
671 | sprintf (buf, "%s%s%s",
|
---|
672 | version_string,
|
---|
673 | (remote_description == 0 || remote_description[0] == '\0')
|
---|
674 | ? "" : "-",
|
---|
675 | (remote_description == 0 || remote_description[0] == '\0')
|
---|
676 | ? "" : remote_description);
|
---|
677 | (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
|
---|
678 |
|
---|
679 | #ifdef __MSDOS__
|
---|
680 | /* Allow to specify a special shell just for Make,
|
---|
681 | and use $COMSPEC as the default $SHELL when appropriate. */
|
---|
682 | {
|
---|
683 | static char shell_str[] = "SHELL";
|
---|
684 | const int shlen = sizeof (shell_str) - 1;
|
---|
685 | struct variable *mshp = lookup_variable ("MAKESHELL", 9);
|
---|
686 | struct variable *comp = lookup_variable ("COMSPEC", 7);
|
---|
687 |
|
---|
688 | /* Make $MAKESHELL override $SHELL even if -e is in effect. */
|
---|
689 | if (mshp)
|
---|
690 | (void) define_variable (shell_str, shlen,
|
---|
691 | mshp->value, o_env_override, 0);
|
---|
692 | else if (comp)
|
---|
693 | {
|
---|
694 | /* $COMSPEC shouldn't override $SHELL. */
|
---|
695 | struct variable *shp = lookup_variable (shell_str, shlen);
|
---|
696 |
|
---|
697 | if (!shp)
|
---|
698 | (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
|
---|
699 | }
|
---|
700 | }
|
---|
701 | #elif defined(__EMX__)
|
---|
702 | {
|
---|
703 | static char shell_str[] = "SHELL";
|
---|
704 | const int shlen = sizeof (shell_str) - 1;
|
---|
705 | struct variable *shell = lookup_variable (shell_str, shlen);
|
---|
706 | struct variable *replace = lookup_variable ("MAKESHELL", 9);
|
---|
707 |
|
---|
708 | /* if $MAKESHELL is defined in the environment assume o_env_override */
|
---|
709 | if (replace && *replace->value && replace->origin == o_env)
|
---|
710 | replace->origin = o_env_override;
|
---|
711 |
|
---|
712 | /* if $MAKESHELL is not defined use $SHELL but only if the variable
|
---|
713 | did not come from the environment */
|
---|
714 | if (!replace || !*replace->value)
|
---|
715 | if (shell && *shell->value && (shell->origin == o_env
|
---|
716 | || shell->origin == o_env_override))
|
---|
717 | {
|
---|
718 | /* overwrite whatever we got from the environment */
|
---|
719 | free(shell->value);
|
---|
720 | shell->value = xstrdup (default_shell);
|
---|
721 | shell->origin = o_default;
|
---|
722 | }
|
---|
723 |
|
---|
724 | /* Some people do not like cmd to be used as the default
|
---|
725 | if $SHELL is not defined in the Makefile.
|
---|
726 | With -DNO_CMD_DEFAULT you can turn off this behaviour */
|
---|
727 | # ifndef NO_CMD_DEFAULT
|
---|
728 | /* otherwise use $COMSPEC */
|
---|
729 | if (!replace || !*replace->value)
|
---|
730 | replace = lookup_variable ("COMSPEC", 7);
|
---|
731 |
|
---|
732 | /* otherwise use $OS2_SHELL */
|
---|
733 | if (!replace || !*replace->value)
|
---|
734 | replace = lookup_variable ("OS2_SHELL", 9);
|
---|
735 | # else
|
---|
736 | # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
|
---|
737 | # endif
|
---|
738 |
|
---|
739 | if (replace && *replace->value)
|
---|
740 | /* overwrite $SHELL */
|
---|
741 | (void) define_variable (shell_str, shlen, replace->value,
|
---|
742 | replace->origin, 0);
|
---|
743 | else
|
---|
744 | /* provide a definition if there is none */
|
---|
745 | (void) define_variable (shell_str, shlen, default_shell,
|
---|
746 | o_default, 0);
|
---|
747 | }
|
---|
748 |
|
---|
749 | #endif
|
---|
750 |
|
---|
751 | /* This won't override any definition, but it will provide one if there
|
---|
752 | isn't one there. */
|
---|
753 | v = define_variable ("SHELL", 5, default_shell, o_default, 0);
|
---|
754 |
|
---|
755 | /* On MSDOS we do use SHELL from environment, since it isn't a standard
|
---|
756 | environment variable on MSDOS, so whoever sets it, does that on purpose.
|
---|
757 | On OS/2 we do not use SHELL from environment but we have already handled
|
---|
758 | that problem above. */
|
---|
759 | #if !defined(__MSDOS__) && !defined(__EMX__)
|
---|
760 | /* Don't let SHELL come from the environment. */
|
---|
761 | if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
|
---|
762 | {
|
---|
763 | free (v->value);
|
---|
764 | v->origin = o_file;
|
---|
765 | v->value = xstrdup (default_shell);
|
---|
766 | }
|
---|
767 | #endif
|
---|
768 |
|
---|
769 | /* Make sure MAKEFILES gets exported if it is set. */
|
---|
770 | v = define_variable ("MAKEFILES", 9, "", o_default, 0);
|
---|
771 | v->export = v_ifset;
|
---|
772 |
|
---|
773 | /* Define the magic D and F variables in terms of
|
---|
774 | the automatic variables they are variations of. */
|
---|
775 |
|
---|
776 | #ifdef VMS
|
---|
777 | define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
|
---|
778 | define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
|
---|
779 | define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
|
---|
780 | define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
|
---|
781 | define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
|
---|
782 | define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
|
---|
783 | define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
|
---|
784 | #else
|
---|
785 | define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
|
---|
786 | define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
|
---|
787 | define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
|
---|
788 | define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
|
---|
789 | define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
|
---|
790 | define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
|
---|
791 | define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
|
---|
792 | #endif
|
---|
793 | define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
|
---|
794 | define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
|
---|
795 | define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
|
---|
796 | define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
|
---|
797 | define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
|
---|
798 | define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
|
---|
799 | define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
|
---|
800 | }
|
---|
801 | |
---|
802 |
|
---|
803 | int export_all_variables;
|
---|
804 |
|
---|
805 | /* Create a new environment for FILE's commands.
|
---|
806 | If FILE is nil, this is for the `shell' function.
|
---|
807 | The child's MAKELEVEL variable is incremented. */
|
---|
808 |
|
---|
809 | char **
|
---|
810 | target_environment (struct file *file)
|
---|
811 | {
|
---|
812 | struct variable_set_list *set_list;
|
---|
813 | register struct variable_set_list *s;
|
---|
814 | struct hash_table table;
|
---|
815 | struct variable **v_slot;
|
---|
816 | struct variable **v_end;
|
---|
817 | struct variable makelevel_key;
|
---|
818 | char **result_0;
|
---|
819 | char **result;
|
---|
820 |
|
---|
821 | if (file == 0)
|
---|
822 | set_list = current_variable_set_list;
|
---|
823 | else
|
---|
824 | set_list = file->variables;
|
---|
825 |
|
---|
826 | hash_init (&table, VARIABLE_BUCKETS,
|
---|
827 | variable_hash_1, variable_hash_2, variable_hash_cmp);
|
---|
828 |
|
---|
829 | /* Run through all the variable sets in the list,
|
---|
830 | accumulating variables in TABLE. */
|
---|
831 | for (s = set_list; s != 0; s = s->next)
|
---|
832 | {
|
---|
833 | struct variable_set *set = s->set;
|
---|
834 | v_slot = (struct variable **) set->table.ht_vec;
|
---|
835 | v_end = v_slot + set->table.ht_size;
|
---|
836 | for ( ; v_slot < v_end; v_slot++)
|
---|
837 | if (! HASH_VACANT (*v_slot))
|
---|
838 | {
|
---|
839 | struct variable **new_slot;
|
---|
840 | struct variable *v = *v_slot;
|
---|
841 |
|
---|
842 | /* If this is a per-target variable and it hasn't been touched
|
---|
843 | already then look up the global version and take its export
|
---|
844 | value. */
|
---|
845 | if (v->per_target && v->export == v_default)
|
---|
846 | {
|
---|
847 | struct variable *gv;
|
---|
848 |
|
---|
849 | gv = lookup_variable_in_set (v->name, strlen(v->name),
|
---|
850 | &global_variable_set);
|
---|
851 | if (gv)
|
---|
852 | v->export = gv->export;
|
---|
853 | }
|
---|
854 |
|
---|
855 | switch (v->export)
|
---|
856 | {
|
---|
857 | case v_default:
|
---|
858 | if (v->origin == o_default || v->origin == o_automatic)
|
---|
859 | /* Only export default variables by explicit request. */
|
---|
860 | continue;
|
---|
861 |
|
---|
862 | /* The variable doesn't have a name that can be exported. */
|
---|
863 | if (! v->exportable)
|
---|
864 | continue;
|
---|
865 |
|
---|
866 | if (! export_all_variables
|
---|
867 | && v->origin != o_command
|
---|
868 | && v->origin != o_env && v->origin != o_env_override)
|
---|
869 | continue;
|
---|
870 | break;
|
---|
871 |
|
---|
872 | case v_export:
|
---|
873 | break;
|
---|
874 |
|
---|
875 | case v_noexport:
|
---|
876 | /* If this is the SHELL variable and it's not exported, then
|
---|
877 | add the value from our original environment. */
|
---|
878 | if (streq (v->name, "SHELL"))
|
---|
879 | {
|
---|
880 | extern struct variable shell_var;
|
---|
881 | v = &shell_var;
|
---|
882 | break;
|
---|
883 | }
|
---|
884 | continue;
|
---|
885 |
|
---|
886 | case v_ifset:
|
---|
887 | if (v->origin == o_default)
|
---|
888 | continue;
|
---|
889 | break;
|
---|
890 | }
|
---|
891 |
|
---|
892 | new_slot = (struct variable **) hash_find_slot (&table, v);
|
---|
893 | if (HASH_VACANT (*new_slot))
|
---|
894 | hash_insert_at (&table, v, new_slot);
|
---|
895 | }
|
---|
896 | }
|
---|
897 |
|
---|
898 | makelevel_key.name = MAKELEVEL_NAME;
|
---|
899 | makelevel_key.length = MAKELEVEL_LENGTH;
|
---|
900 | hash_delete (&table, &makelevel_key);
|
---|
901 |
|
---|
902 | result = result_0 = (char **) xmalloc ((table.ht_fill + 2) * sizeof (char *));
|
---|
903 |
|
---|
904 | v_slot = (struct variable **) table.ht_vec;
|
---|
905 | v_end = v_slot + table.ht_size;
|
---|
906 | for ( ; v_slot < v_end; v_slot++)
|
---|
907 | if (! HASH_VACANT (*v_slot))
|
---|
908 | {
|
---|
909 | struct variable *v = *v_slot;
|
---|
910 |
|
---|
911 | /* If V is recursively expanded and didn't come from the environment,
|
---|
912 | expand its value. If it came from the environment, it should
|
---|
913 | go back into the environment unchanged. */
|
---|
914 | if (v->recursive
|
---|
915 | && v->origin != o_env && v->origin != o_env_override)
|
---|
916 | {
|
---|
917 | char *value = recursively_expand_for_file (v, file);
|
---|
918 | #ifdef WINDOWS32
|
---|
919 | if (strcmp(v->name, "Path") == 0 ||
|
---|
920 | strcmp(v->name, "PATH") == 0)
|
---|
921 | convert_Path_to_windows32(value, ';');
|
---|
922 | #endif
|
---|
923 | *result++ = concat (v->name, "=", value);
|
---|
924 | free (value);
|
---|
925 | }
|
---|
926 | else
|
---|
927 | {
|
---|
928 | #ifdef WINDOWS32
|
---|
929 | if (strcmp(v->name, "Path") == 0 ||
|
---|
930 | strcmp(v->name, "PATH") == 0)
|
---|
931 | convert_Path_to_windows32(v->value, ';');
|
---|
932 | #endif
|
---|
933 | *result++ = concat (v->name, "=", v->value);
|
---|
934 | }
|
---|
935 | }
|
---|
936 |
|
---|
937 | *result = (char *) xmalloc (100);
|
---|
938 | (void) sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
|
---|
939 | *++result = 0;
|
---|
940 |
|
---|
941 | hash_free (&table, 0);
|
---|
942 |
|
---|
943 | return result_0;
|
---|
944 | }
|
---|
945 | |
---|
946 |
|
---|
947 | /* Given a variable, a value, and a flavor, define the variable.
|
---|
948 | See the try_variable_definition() function for details on the parameters. */
|
---|
949 |
|
---|
950 | struct variable *
|
---|
951 | do_variable_definition (const struct floc *flocp, const char *varname,
|
---|
952 | char *value, enum variable_origin origin,
|
---|
953 | enum variable_flavor flavor, int target_var)
|
---|
954 | {
|
---|
955 | char *p, *alloc_value = NULL;
|
---|
956 | struct variable *v;
|
---|
957 | int append = 0;
|
---|
958 | int conditional = 0;
|
---|
959 |
|
---|
960 | /* Calculate the variable's new value in VALUE. */
|
---|
961 |
|
---|
962 | switch (flavor)
|
---|
963 | {
|
---|
964 | default:
|
---|
965 | case f_bogus:
|
---|
966 | /* Should not be possible. */
|
---|
967 | abort ();
|
---|
968 | case f_simple:
|
---|
969 | /* A simple variable definition "var := value". Expand the value.
|
---|
970 | We have to allocate memory since otherwise it'll clobber the
|
---|
971 | variable buffer, and we may still need that if we're looking at a
|
---|
972 | target-specific variable. */
|
---|
973 | p = alloc_value = allocated_variable_expand (value);
|
---|
974 | break;
|
---|
975 | case f_conditional:
|
---|
976 | /* A conditional variable definition "var ?= value".
|
---|
977 | The value is set IFF the variable is not defined yet. */
|
---|
978 | v = lookup_variable (varname, strlen (varname));
|
---|
979 | if (v)
|
---|
980 | return v;
|
---|
981 |
|
---|
982 | conditional = 1;
|
---|
983 | flavor = f_recursive;
|
---|
984 | /* FALLTHROUGH */
|
---|
985 | case f_recursive:
|
---|
986 | /* A recursive variable definition "var = value".
|
---|
987 | The value is used verbatim. */
|
---|
988 | p = value;
|
---|
989 | break;
|
---|
990 | case f_append:
|
---|
991 | {
|
---|
992 | /* If we have += but we're in a target variable context, we want to
|
---|
993 | append only with other variables in the context of this target. */
|
---|
994 | if (target_var)
|
---|
995 | {
|
---|
996 | append = 1;
|
---|
997 | v = lookup_variable_in_set (varname, strlen (varname),
|
---|
998 | current_variable_set_list->set);
|
---|
999 |
|
---|
1000 | /* Don't append from the global set if a previous non-appending
|
---|
1001 | target-specific variable definition exists. */
|
---|
1002 | if (v && !v->append)
|
---|
1003 | append = 0;
|
---|
1004 | }
|
---|
1005 | else
|
---|
1006 | v = lookup_variable (varname, strlen (varname));
|
---|
1007 |
|
---|
1008 | if (v == 0)
|
---|
1009 | {
|
---|
1010 | /* There was no old value.
|
---|
1011 | This becomes a normal recursive definition. */
|
---|
1012 | p = value;
|
---|
1013 | flavor = f_recursive;
|
---|
1014 | }
|
---|
1015 | else
|
---|
1016 | {
|
---|
1017 | /* Paste the old and new values together in VALUE. */
|
---|
1018 |
|
---|
1019 | unsigned int oldlen, vallen;
|
---|
1020 | char *val;
|
---|
1021 |
|
---|
1022 | val = value;
|
---|
1023 | if (v->recursive)
|
---|
1024 | /* The previous definition of the variable was recursive.
|
---|
1025 | The new value is the unexpanded old and new values. */
|
---|
1026 | flavor = f_recursive;
|
---|
1027 | else
|
---|
1028 | /* The previous definition of the variable was simple.
|
---|
1029 | The new value comes from the old value, which was expanded
|
---|
1030 | when it was set; and from the expanded new value. Allocate
|
---|
1031 | memory for the expansion as we may still need the rest of the
|
---|
1032 | buffer if we're looking at a target-specific variable. */
|
---|
1033 | val = alloc_value = allocated_variable_expand (val);
|
---|
1034 |
|
---|
1035 | oldlen = strlen (v->value);
|
---|
1036 | vallen = strlen (val);
|
---|
1037 | p = (char *) alloca (oldlen + 1 + vallen + 1);
|
---|
1038 | bcopy (v->value, p, oldlen);
|
---|
1039 | p[oldlen] = ' ';
|
---|
1040 | bcopy (val, &p[oldlen + 1], vallen + 1);
|
---|
1041 | }
|
---|
1042 | }
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 | #ifdef __MSDOS__
|
---|
1046 | /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
|
---|
1047 | non-Unix systems don't conform to this default configuration (in
|
---|
1048 | fact, most of them don't even have `/bin'). On the other hand,
|
---|
1049 | $SHELL in the environment, if set, points to the real pathname of
|
---|
1050 | the shell.
|
---|
1051 | Therefore, we generally won't let lines like "SHELL=/bin/sh" from
|
---|
1052 | the Makefile override $SHELL from the environment. But first, we
|
---|
1053 | look for the basename of the shell in the directory where SHELL=
|
---|
1054 | points, and along the $PATH; if it is found in any of these places,
|
---|
1055 | we define $SHELL to be the actual pathname of the shell. Thus, if
|
---|
1056 | you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
|
---|
1057 | your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
|
---|
1058 | defining SHELL to be "d:/unix/bash.exe". */
|
---|
1059 | if ((origin == o_file || origin == o_override)
|
---|
1060 | && strcmp (varname, "SHELL") == 0)
|
---|
1061 | {
|
---|
1062 | PATH_VAR (shellpath);
|
---|
1063 | extern char * __dosexec_find_on_path (const char *, char *[], char *);
|
---|
1064 |
|
---|
1065 | /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
|
---|
1066 | if (__dosexec_find_on_path (p, (char **)0, shellpath))
|
---|
1067 | {
|
---|
1068 | char *p;
|
---|
1069 |
|
---|
1070 | for (p = shellpath; *p; p++)
|
---|
1071 | {
|
---|
1072 | if (*p == '\\')
|
---|
1073 | *p = '/';
|
---|
1074 | }
|
---|
1075 | v = define_variable_loc (varname, strlen (varname),
|
---|
1076 | shellpath, origin, flavor == f_recursive,
|
---|
1077 | flocp);
|
---|
1078 | }
|
---|
1079 | else
|
---|
1080 | {
|
---|
1081 | char *shellbase, *bslash;
|
---|
1082 | struct variable *pathv = lookup_variable ("PATH", 4);
|
---|
1083 | char *path_string;
|
---|
1084 | char *fake_env[2];
|
---|
1085 | size_t pathlen = 0;
|
---|
1086 |
|
---|
1087 | shellbase = strrchr (p, '/');
|
---|
1088 | bslash = strrchr (p, '\\');
|
---|
1089 | if (!shellbase || bslash > shellbase)
|
---|
1090 | shellbase = bslash;
|
---|
1091 | if (!shellbase && p[1] == ':')
|
---|
1092 | shellbase = p + 1;
|
---|
1093 | if (shellbase)
|
---|
1094 | shellbase++;
|
---|
1095 | else
|
---|
1096 | shellbase = p;
|
---|
1097 |
|
---|
1098 | /* Search for the basename of the shell (with standard
|
---|
1099 | executable extensions) along the $PATH. */
|
---|
1100 | if (pathv)
|
---|
1101 | pathlen = strlen (pathv->value);
|
---|
1102 | path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
|
---|
1103 | /* On MSDOS, current directory is considered as part of $PATH. */
|
---|
1104 | sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
|
---|
1105 | fake_env[0] = path_string;
|
---|
1106 | fake_env[1] = (char *)0;
|
---|
1107 | if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
|
---|
1108 | {
|
---|
1109 | char *p;
|
---|
1110 |
|
---|
1111 | for (p = shellpath; *p; p++)
|
---|
1112 | {
|
---|
1113 | if (*p == '\\')
|
---|
1114 | *p = '/';
|
---|
1115 | }
|
---|
1116 | v = define_variable_loc (varname, strlen (varname),
|
---|
1117 | shellpath, origin,
|
---|
1118 | flavor == f_recursive, flocp);
|
---|
1119 | }
|
---|
1120 | else
|
---|
1121 | v = lookup_variable (varname, strlen (varname));
|
---|
1122 |
|
---|
1123 | free (path_string);
|
---|
1124 | }
|
---|
1125 | }
|
---|
1126 | else
|
---|
1127 | #endif /* __MSDOS__ */
|
---|
1128 | #ifdef WINDOWS32
|
---|
1129 | if ((origin == o_file || origin == o_override) && streq (varname, "SHELL"))
|
---|
1130 | {
|
---|
1131 | extern char *default_shell;
|
---|
1132 |
|
---|
1133 | /* Call shell locator function. If it returns TRUE, then
|
---|
1134 | set no_default_sh_exe to indicate sh was found and
|
---|
1135 | set new value for SHELL variable. */
|
---|
1136 |
|
---|
1137 | if (find_and_set_default_shell (p))
|
---|
1138 | {
|
---|
1139 | v = define_variable_in_set (varname, strlen (varname), default_shell,
|
---|
1140 | origin, flavor == f_recursive,
|
---|
1141 | (target_var
|
---|
1142 | ? current_variable_set_list->set
|
---|
1143 | : NULL),
|
---|
1144 | flocp);
|
---|
1145 | no_default_sh_exe = 0;
|
---|
1146 | }
|
---|
1147 | else
|
---|
1148 | v = lookup_variable (varname, strlen (varname));
|
---|
1149 | }
|
---|
1150 | else
|
---|
1151 | #endif
|
---|
1152 |
|
---|
1153 | /* If we are defining variables inside an $(eval ...), we might have a
|
---|
1154 | different variable context pushed, not the global context (maybe we're
|
---|
1155 | inside a $(call ...) or something. Since this function is only ever
|
---|
1156 | invoked in places where we want to define globally visible variables,
|
---|
1157 | make sure we define this variable in the global set. */
|
---|
1158 |
|
---|
1159 | v = define_variable_in_set (varname, strlen (varname), p,
|
---|
1160 | origin, flavor == f_recursive,
|
---|
1161 | (target_var
|
---|
1162 | ? current_variable_set_list->set : NULL),
|
---|
1163 | flocp);
|
---|
1164 | v->append = append;
|
---|
1165 | v->conditional = conditional;
|
---|
1166 |
|
---|
1167 | if (alloc_value)
|
---|
1168 | free (alloc_value);
|
---|
1169 |
|
---|
1170 | return v;
|
---|
1171 | }
|
---|
1172 | |
---|
1173 |
|
---|
1174 | /* Try to interpret LINE (a null-terminated string) as a variable definition.
|
---|
1175 |
|
---|
1176 | ORIGIN may be o_file, o_override, o_env, o_env_override,
|
---|
1177 | or o_command specifying that the variable definition comes
|
---|
1178 | from a makefile, an override directive, the environment with
|
---|
1179 | or without the -e switch, or the command line.
|
---|
1180 |
|
---|
1181 | See the comments for parse_variable_definition().
|
---|
1182 |
|
---|
1183 | If LINE was recognized as a variable definition, a pointer to its `struct
|
---|
1184 | variable' is returned. If LINE is not a variable definition, NULL is
|
---|
1185 | returned. */
|
---|
1186 |
|
---|
1187 | struct variable *
|
---|
1188 | parse_variable_definition (struct variable *v, char *line)
|
---|
1189 | {
|
---|
1190 | register int c;
|
---|
1191 | register char *p = line;
|
---|
1192 | register char *beg;
|
---|
1193 | register char *end;
|
---|
1194 | enum variable_flavor flavor = f_bogus;
|
---|
1195 | char *name;
|
---|
1196 |
|
---|
1197 | while (1)
|
---|
1198 | {
|
---|
1199 | c = *p++;
|
---|
1200 | if (c == '\0' || c == '#')
|
---|
1201 | return 0;
|
---|
1202 | if (c == '=')
|
---|
1203 | {
|
---|
1204 | end = p - 1;
|
---|
1205 | flavor = f_recursive;
|
---|
1206 | break;
|
---|
1207 | }
|
---|
1208 | else if (c == ':')
|
---|
1209 | if (*p == '=')
|
---|
1210 | {
|
---|
1211 | end = p++ - 1;
|
---|
1212 | flavor = f_simple;
|
---|
1213 | break;
|
---|
1214 | }
|
---|
1215 | else
|
---|
1216 | /* A colon other than := is a rule line, not a variable defn. */
|
---|
1217 | return 0;
|
---|
1218 | else if (c == '+' && *p == '=')
|
---|
1219 | {
|
---|
1220 | end = p++ - 1;
|
---|
1221 | flavor = f_append;
|
---|
1222 | break;
|
---|
1223 | }
|
---|
1224 | else if (c == '?' && *p == '=')
|
---|
1225 | {
|
---|
1226 | end = p++ - 1;
|
---|
1227 | flavor = f_conditional;
|
---|
1228 | break;
|
---|
1229 | }
|
---|
1230 | else if (c == '$')
|
---|
1231 | {
|
---|
1232 | /* This might begin a variable expansion reference. Make sure we
|
---|
1233 | don't misrecognize chars inside the reference as =, := or +=. */
|
---|
1234 | char closeparen;
|
---|
1235 | int count;
|
---|
1236 | c = *p++;
|
---|
1237 | if (c == '(')
|
---|
1238 | closeparen = ')';
|
---|
1239 | else if (c == '{')
|
---|
1240 | closeparen = '}';
|
---|
1241 | else
|
---|
1242 | continue; /* Nope. */
|
---|
1243 |
|
---|
1244 | /* P now points past the opening paren or brace.
|
---|
1245 | Count parens or braces until it is matched. */
|
---|
1246 | count = 0;
|
---|
1247 | for (; *p != '\0'; ++p)
|
---|
1248 | {
|
---|
1249 | if (*p == c)
|
---|
1250 | ++count;
|
---|
1251 | else if (*p == closeparen && --count < 0)
|
---|
1252 | {
|
---|
1253 | ++p;
|
---|
1254 | break;
|
---|
1255 | }
|
---|
1256 | }
|
---|
1257 | }
|
---|
1258 | }
|
---|
1259 | v->flavor = flavor;
|
---|
1260 |
|
---|
1261 | beg = next_token (line);
|
---|
1262 | while (end > beg && isblank ((unsigned char)end[-1]))
|
---|
1263 | --end;
|
---|
1264 | p = next_token (p);
|
---|
1265 | v->value = p;
|
---|
1266 |
|
---|
1267 | /* Expand the name, so "$(foo)bar = baz" works. */
|
---|
1268 | name = (char *) alloca (end - beg + 1);
|
---|
1269 | bcopy (beg, name, end - beg);
|
---|
1270 | name[end - beg] = '\0';
|
---|
1271 | v->name = allocated_variable_expand (name);
|
---|
1272 |
|
---|
1273 | if (v->name[0] == '\0')
|
---|
1274 | fatal (&v->fileinfo, _("empty variable name"));
|
---|
1275 |
|
---|
1276 | return v;
|
---|
1277 | }
|
---|
1278 | |
---|
1279 |
|
---|
1280 | /* Try to interpret LINE (a null-terminated string) as a variable definition.
|
---|
1281 |
|
---|
1282 | ORIGIN may be o_file, o_override, o_env, o_env_override,
|
---|
1283 | or o_command specifying that the variable definition comes
|
---|
1284 | from a makefile, an override directive, the environment with
|
---|
1285 | or without the -e switch, or the command line.
|
---|
1286 |
|
---|
1287 | See the comments for parse_variable_definition().
|
---|
1288 |
|
---|
1289 | If LINE was recognized as a variable definition, a pointer to its `struct
|
---|
1290 | variable' is returned. If LINE is not a variable definition, NULL is
|
---|
1291 | returned. */
|
---|
1292 |
|
---|
1293 | struct variable *
|
---|
1294 | try_variable_definition (const struct floc *flocp, char *line,
|
---|
1295 | enum variable_origin origin, int target_var)
|
---|
1296 | {
|
---|
1297 | struct variable v;
|
---|
1298 | struct variable *vp;
|
---|
1299 |
|
---|
1300 | if (flocp != 0)
|
---|
1301 | v.fileinfo = *flocp;
|
---|
1302 | else
|
---|
1303 | v.fileinfo.filenm = 0;
|
---|
1304 |
|
---|
1305 | if (!parse_variable_definition (&v, line))
|
---|
1306 | return 0;
|
---|
1307 |
|
---|
1308 | vp = do_variable_definition (flocp, v.name, v.value,
|
---|
1309 | origin, v.flavor, target_var);
|
---|
1310 |
|
---|
1311 | free (v.name);
|
---|
1312 |
|
---|
1313 | return vp;
|
---|
1314 | }
|
---|
1315 | |
---|
1316 |
|
---|
1317 | /* Print information for variable V, prefixing it with PREFIX. */
|
---|
1318 |
|
---|
1319 | static void
|
---|
1320 | print_variable (const void *item, void *arg)
|
---|
1321 | {
|
---|
1322 | const struct variable *v = (struct variable *) item;
|
---|
1323 | const char *prefix = (char *) arg;
|
---|
1324 | const char *origin;
|
---|
1325 |
|
---|
1326 | switch (v->origin)
|
---|
1327 | {
|
---|
1328 | case o_default:
|
---|
1329 | origin = _("default");
|
---|
1330 | break;
|
---|
1331 | case o_env:
|
---|
1332 | origin = _("environment");
|
---|
1333 | break;
|
---|
1334 | case o_file:
|
---|
1335 | origin = _("makefile");
|
---|
1336 | break;
|
---|
1337 | case o_env_override:
|
---|
1338 | origin = _("environment under -e");
|
---|
1339 | break;
|
---|
1340 | case o_command:
|
---|
1341 | origin = _("command line");
|
---|
1342 | break;
|
---|
1343 | case o_override:
|
---|
1344 | origin = _("`override' directive");
|
---|
1345 | break;
|
---|
1346 | case o_automatic:
|
---|
1347 | origin = _("automatic");
|
---|
1348 | break;
|
---|
1349 | case o_invalid:
|
---|
1350 | default:
|
---|
1351 | abort ();
|
---|
1352 | }
|
---|
1353 | fputs ("# ", stdout);
|
---|
1354 | fputs (origin, stdout);
|
---|
1355 | if (v->fileinfo.filenm)
|
---|
1356 | printf (_(" (from `%s', line %lu)"),
|
---|
1357 | v->fileinfo.filenm, v->fileinfo.lineno);
|
---|
1358 | putchar ('\n');
|
---|
1359 | fputs (prefix, stdout);
|
---|
1360 |
|
---|
1361 | /* Is this a `define'? */
|
---|
1362 | if (v->recursive && strchr (v->value, '\n') != 0)
|
---|
1363 | printf ("define %s\n%s\nendef\n", v->name, v->value);
|
---|
1364 | else
|
---|
1365 | {
|
---|
1366 | register char *p;
|
---|
1367 |
|
---|
1368 | printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
|
---|
1369 |
|
---|
1370 | /* Check if the value is just whitespace. */
|
---|
1371 | p = next_token (v->value);
|
---|
1372 | if (p != v->value && *p == '\0')
|
---|
1373 | /* All whitespace. */
|
---|
1374 | printf ("$(subst ,,%s)", v->value);
|
---|
1375 | else if (v->recursive)
|
---|
1376 | fputs (v->value, stdout);
|
---|
1377 | else
|
---|
1378 | /* Double up dollar signs. */
|
---|
1379 | for (p = v->value; *p != '\0'; ++p)
|
---|
1380 | {
|
---|
1381 | if (*p == '$')
|
---|
1382 | putchar ('$');
|
---|
1383 | putchar (*p);
|
---|
1384 | }
|
---|
1385 | putchar ('\n');
|
---|
1386 | }
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 |
|
---|
1390 | /* Print all the variables in SET. PREFIX is printed before
|
---|
1391 | the actual variable definitions (everything else is comments). */
|
---|
1392 |
|
---|
1393 | void
|
---|
1394 | print_variable_set (struct variable_set *set, char *prefix)
|
---|
1395 | {
|
---|
1396 | hash_map_arg (&set->table, print_variable, prefix);
|
---|
1397 |
|
---|
1398 | fputs (_("# variable set hash-table stats:\n"), stdout);
|
---|
1399 | fputs ("# ", stdout);
|
---|
1400 | hash_print_stats (&set->table, stdout);
|
---|
1401 | putc ('\n', stdout);
|
---|
1402 | }
|
---|
1403 |
|
---|
1404 | /* Print the data base of variables. */
|
---|
1405 |
|
---|
1406 | void
|
---|
1407 | print_variable_data_base (void)
|
---|
1408 | {
|
---|
1409 | puts (_("\n# Variables\n"));
|
---|
1410 |
|
---|
1411 | print_variable_set (&global_variable_set, "");
|
---|
1412 |
|
---|
1413 | puts (_("\n# Pattern-specific Variable Values"));
|
---|
1414 |
|
---|
1415 | {
|
---|
1416 | struct pattern_var *p;
|
---|
1417 | int rules = 0;
|
---|
1418 |
|
---|
1419 | for (p = pattern_vars; p != 0; p = p->next)
|
---|
1420 | {
|
---|
1421 | ++rules;
|
---|
1422 | printf ("\n%s :\n", p->target);
|
---|
1423 | print_variable (&p->variable, "# ");
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | if (rules == 0)
|
---|
1427 | puts (_("\n# No pattern-specific variable values."));
|
---|
1428 | else
|
---|
1429 | printf (_("\n# %u pattern-specific variable values"), rules);
|
---|
1430 | }
|
---|
1431 | }
|
---|
1432 |
|
---|
1433 |
|
---|
1434 | /* Print all the local variables of FILE. */
|
---|
1435 |
|
---|
1436 | void
|
---|
1437 | print_file_variables (struct file *file)
|
---|
1438 | {
|
---|
1439 | if (file->variables != 0)
|
---|
1440 | print_variable_set (file->variables->set, "# ");
|
---|
1441 | }
|
---|
1442 |
|
---|
1443 | #ifdef WINDOWS32
|
---|
1444 | void
|
---|
1445 | sync_Path_environment (void)
|
---|
1446 | {
|
---|
1447 | char *path = allocated_variable_expand ("$(PATH)");
|
---|
1448 | static char *environ_path = NULL;
|
---|
1449 |
|
---|
1450 | if (!path)
|
---|
1451 | return;
|
---|
1452 |
|
---|
1453 | /*
|
---|
1454 | * If done this before, don't leak memory unnecessarily.
|
---|
1455 | * Free the previous entry before allocating new one.
|
---|
1456 | */
|
---|
1457 | if (environ_path)
|
---|
1458 | free (environ_path);
|
---|
1459 |
|
---|
1460 | /*
|
---|
1461 | * Create something WINDOWS32 world can grok
|
---|
1462 | */
|
---|
1463 | convert_Path_to_windows32 (path, ';');
|
---|
1464 | environ_path = concat ("PATH", "=", path);
|
---|
1465 | putenv (environ_path);
|
---|
1466 | free (path);
|
---|
1467 | }
|
---|
1468 | #endif
|
---|