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