VirtualBox

source: kBuild/trunk/src/kmk/variable.c@ 1822

Last change on this file since 1822 was 1811, checked in by bird, 17 years ago

kmk: More string length optimizations.

  • Property svn:eol-style set to native
File size: 72.9 KB
Line 
1/* Internals of variables for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
18
19#include "make.h"
20
21#include <assert.h>
22
23#include "dep.h"
24#include "filedef.h"
25#include "job.h"
26#include "commands.h"
27#include "variable.h"
28#include "rule.h"
29#ifdef WINDOWS32
30#include "pathstuff.h"
31#endif
32#include "hash.h"
33#ifdef KMK
34# include "kbuild.h"
35#endif
36
37/* Chain of all pattern-specific variables. */
38
39static struct pattern_var *pattern_vars;
40
41/* Pointer to last struct in the chain, so we can add onto the end. */
42
43static struct pattern_var *last_pattern_var;
44
45/* Create a new pattern-specific variable struct. */
46
47struct pattern_var *
48create_pattern_var (const char *target, const char *suffix)
49{
50 register struct pattern_var *p = xmalloc (sizeof (struct pattern_var));
51
52 if (last_pattern_var != 0)
53 last_pattern_var->next = p;
54 else
55 pattern_vars = p;
56 last_pattern_var = p;
57 p->next = 0;
58
59 p->target = target;
60 p->len = strlen (target);
61 p->suffix = suffix + 1;
62
63 return p;
64}
65
66/* Look up a target in the pattern-specific variable list. */
67
68static struct pattern_var *
69lookup_pattern_var (struct pattern_var *start, const char *target)
70{
71 struct pattern_var *p;
72 unsigned int targlen = strlen(target);
73
74 for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
75 {
76 const char *stem;
77 unsigned int stemlen;
78
79 if (p->len > targlen)
80 /* It can't possibly match. */
81 continue;
82
83 /* From the lengths of the filename and the pattern parts,
84 find the stem: the part of the filename that matches the %. */
85 stem = target + (p->suffix - p->target - 1);
86 stemlen = targlen - p->len + 1;
87
88 /* Compare the text in the pattern before the stem, if any. */
89 if (stem > target && !strneq (p->target, target, stem - target))
90 continue;
91
92 /* Compare the text in the pattern after the stem, if any.
93 We could test simply using streq, but this way we compare the
94 first two characters immediately. This saves time in the very
95 common case where the first character matches because it is a
96 period. */
97 if (*p->suffix == stem[stemlen]
98 && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
99 break;
100 }
101
102 return p;
103}
104
105
106/* Hash table of all global variable definitions. */
107
108#if defined(VARIABLE_HASH) || defined(CONFIG_WITH_OPTIMIZATION_HACKS)
109# ifdef _MSC_VER
110# define inline _inline
111typedef signed int int32_t;
112typedef signed short int int16_t;
113# endif
114static inline unsigned long variable_hash_2i(register const char *var, register int length)
115{
116# define UPDATE_HASH(ch) hash = (ch) + (hash << 6) + (hash << 16) - hash
117# ifndef CONFIG_WITH_OPTIMIZATION_HACKS
118# if 1
119 register const unsigned char *uvar = (const unsigned char *)var;
120 register unsigned long hash = 0;
121 while (length-- > 0)
122 UPDATE_HASH(*uvar++);
123 return hash;
124# else
125 return_STRING_N_HASH_2 (var, length);
126# endif
127# else /* CONFIG_WITH_OPTIMIZATION_HACKS */
128 register unsigned long hash = 0;
129 register const unsigned char *uvar = (const unsigned char *)var;
130 register const unsigned char *uvar_end = uvar + length;
131 switch (length)
132 {
133 default:
134 case 32: //UPDATE_HASH(uvar_end[-16]);
135 case 31: UPDATE_HASH(uvar_end[-15]);
136 case 30: //UPDATE_HASH(uvar_end[-14]);
137 case 29: UPDATE_HASH(uvar_end[-13]);
138 case 28: //UPDATE_HASH(uvar_end[-12]);
139 case 27: UPDATE_HASH(uvar_end[-11]);
140 case 26: //UPDATE_HASH(uvar_end[-10]);
141 case 25: UPDATE_HASH(uvar_end[-9]);
142 case 24: //UPDATE_HASH(uvar[15]);
143 case 23: UPDATE_HASH(uvar[14]);
144 case 22: //UPDATE_HASH(uvar[13]);
145 case 21: UPDATE_HASH(uvar[12]);
146 case 20: //UPDATE_HASH(uvar[11]);
147 case 19: UPDATE_HASH(uvar[10]);
148 case 18: //UPDATE_HASH(uvar[9]);
149 case 17: UPDATE_HASH(uvar[8]);
150 case 16: //UPDATE_HASH(uvar_end[-8]);
151 case 15: UPDATE_HASH(uvar_end[-7]);
152 case 14: //UPDATE_HASH(uvar_end[-6]);
153 case 13: UPDATE_HASH(uvar_end[-5]);
154 case 12: //UPDATE_HASH(uvar_end[-4]);
155 case 11: UPDATE_HASH(uvar_end[-3]);
156 case 10: //UPDATE_HASH(uvar_end[-2]);
157 case 9: UPDATE_HASH(uvar_end[-1]);
158 case 8: //UPDATE_HASH(uvar[7]);
159 case 7: UPDATE_HASH(uvar[6]);
160 case 6: //UPDATE_HASH(uvar[5]);
161 case 5: UPDATE_HASH(uvar[4]);
162 case 4: //UPDATE_HASH(uvar[3]);
163 case 3: UPDATE_HASH(uvar[2]);
164 case 2: //UPDATE_HASH(uvar[1]);
165 case 1: UPDATE_HASH(uvar[0]);
166 case 0:
167 return hash;
168 }
169# endif /* CONFIG_WITH_OPTIMIZATION_HACKS*/
170# undef UPDATE_HASH
171}
172
173static inline unsigned long variable_hash_1i(register const char *var, register int length)
174{
175# define UPDATE_HASH(ch) hash = ((hash << 5) + hash) + (ch)
176# ifndef CONFIG_WITH_OPTIMIZATION_HACKS
177# if 1
178 register const unsigned char *uvar = (const unsigned char *)var;
179 register unsigned long hash = 5381;
180 while (length-- > 0)
181 UPDATE_HASH(*uvar++);
182 return hash;
183# else
184 return_STRING_N_HASH_1 (var, length);
185# endif
186# else /* CONFIG_WITH_OPTIMIZATION_HACKS */
187 register const unsigned char *uvar = (const unsigned char *)var;
188 register const unsigned char *uvar_end = (const unsigned char *)var + length;
189 register unsigned long hash = ((5381 << 5) + 5381) + *uvar;
190 switch (length)
191 {
192 default:
193#if 0 /* seems to be a waste of time. */
194 case 97: UPDATE_HASH(uvar_end[-77]);
195 case 96: //UPDATE_HASH(uvar_end[-76]);
196 case 95: //UPDATE_HASH(uvar_end[-75]);
197 case 94: //UPDATE_HASH(uvar_end[-74]);
198 case 93: UPDATE_HASH(uvar_end[-73]);
199 case 92: //UPDATE_HASH(uvar_end[-72]);
200 case 91: //UPDATE_HASH(uvar_end[-71]);
201 case 90: //UPDATE_HASH(uvar_end[-70]);
202 case 89: UPDATE_HASH(uvar_end[-69]);
203 case 88: //UPDATE_HASH(uvar_end[-68]);
204 case 87: //UPDATE_HASH(uvar_end[-67]);
205 case 86: //UPDATE_HASH(uvar_end[-66]);
206 case 85: UPDATE_HASH(uvar_end[-65]);
207 case 84: //UPDATE_HASH(uvar_end[-64]);
208 case 83: //UPDATE_HASH(uvar_end[-63]);
209 case 82: //UPDATE_HASH(uvar_end[-62]);
210 case 81: UPDATE_HASH(uvar_end[-61]);
211 case 80: //UPDATE_HASH(uvar_end[-60]);
212 case 79: //UPDATE_HASH(uvar_end[-59]);
213 case 78: //UPDATE_HASH(uvar_end[-58]);
214 case 77: UPDATE_HASH(uvar_end[-57]);
215 case 76: //UPDATE_HASH(uvar_end[-56]);
216 case 75: //UPDATE_HASH(uvar_end[-55]);
217 case 74: //UPDATE_HASH(uvar_end[-54]);
218 case 73: UPDATE_HASH(uvar_end[-53]);
219 case 72: //UPDATE_HASH(uvar_end[-52]);
220 case 71: //UPDATE_HASH(uvar_end[-51]);
221 case 70: //UPDATE_HASH(uvar_end[-50]);
222 case 69: UPDATE_HASH(uvar_end[-49]);
223 case 68: //UPDATE_HASH(uvar_end[-48]);
224 case 67: //UPDATE_HASH(uvar_end[-47]);
225 case 66: //UPDATE_HASH(uvar_end[-46]);
226 case 65: UPDATE_HASH(uvar_end[-49]);
227 case 64: //UPDATE_HASH(uvar_end[-48]);
228 case 63: //UPDATE_HASH(uvar_end[-47]);
229 case 62: //UPDATE_HASH(uvar_end[-46]);
230 case 61: UPDATE_HASH(uvar_end[-45]);
231 case 60: //UPDATE_HASH(uvar_end[-44]);
232 case 59: //UPDATE_HASH(uvar_end[-43]);
233 case 58: //UPDATE_HASH(uvar_end[-42]);
234 case 57: UPDATE_HASH(uvar_end[-41]);
235 case 56: //UPDATE_HASH(uvar_end[-40]);
236 case 55: //UPDATE_HASH(uvar_end[-39]);
237 case 54: //UPDATE_HASH(uvar_end[-38]);
238 case 53: UPDATE_HASH(uvar_end[-37]);
239 case 52: //UPDATE_HASH(uvar_end[-36]);
240 case 51: UPDATE_HASH(uvar_end[-35]);
241 case 50: //UPDATE_HASH(uvar_end[-34]);
242 case 49: UPDATE_HASH(uvar_end[-33]);
243#endif
244 case 48: //UPDATE_HASH(uvar_end[-32]);
245 case 47: UPDATE_HASH(uvar_end[-31]);
246 case 46: //UPDATE_HASH(uvar_end[-30]);
247 case 45: UPDATE_HASH(uvar_end[-29]);
248 case 44: //UPDATE_HASH(uvar_end[-28]);
249 case 43: UPDATE_HASH(uvar_end[-27]);
250 case 42: //UPDATE_HASH(uvar_end[-26]);
251 case 41: UPDATE_HASH(uvar_end[-25]);
252 case 40: //UPDATE_HASH(uvar_end[-24]);
253 case 39: UPDATE_HASH(uvar_end[-23]);
254 case 38: //UPDATE_HASH(uvar_end[-22]);
255 case 37: UPDATE_HASH(uvar_end[-21]);
256 case 36: //UPDATE_HASH(uvar_end[-20]);
257 case 35: UPDATE_HASH(uvar_end[-19]);
258 case 34: //UPDATE_HASH(uvar_end[-18]);
259 case 33: UPDATE_HASH(uvar_end[-17]);
260
261 case 32: UPDATE_HASH(uvar_end[-16]);
262 case 31: UPDATE_HASH(uvar_end[-15]);
263 case 30: UPDATE_HASH(uvar_end[-14]);
264 case 29: UPDATE_HASH(uvar_end[-13]);
265 case 28: UPDATE_HASH(uvar[15]);
266 case 27: UPDATE_HASH(uvar[14]);
267 case 26: UPDATE_HASH(uvar[13]);
268 case 25: UPDATE_HASH(uvar[12]);
269
270 case 24: UPDATE_HASH(uvar_end[-12]);
271 case 23: UPDATE_HASH(uvar_end[-11]);
272 case 22: UPDATE_HASH(uvar_end[-10]);
273 case 21: UPDATE_HASH(uvar_end[-9]);
274 case 20: UPDATE_HASH(uvar[7]);
275 case 19: UPDATE_HASH(uvar[6]);
276 case 18: UPDATE_HASH(uvar[5]);
277 case 17: UPDATE_HASH(uvar[4]);
278
279 case 16: UPDATE_HASH(uvar_end[-8]);
280 case 15: UPDATE_HASH(uvar_end[-7]);
281 case 14: UPDATE_HASH(uvar_end[-6]);
282 case 13: UPDATE_HASH(uvar_end[-5]);
283 case 12: UPDATE_HASH(uvar[11]);
284 case 11: UPDATE_HASH(uvar[10]);
285 case 10: UPDATE_HASH(uvar[9]);
286 case 9: UPDATE_HASH(uvar[8]);
287
288 case 8: UPDATE_HASH(uvar_end[-4]);
289 case 7: UPDATE_HASH(uvar_end[-3]);
290 case 6: UPDATE_HASH(uvar_end[-2]);
291 case 5: UPDATE_HASH(uvar_end[-1]);
292 case 4: UPDATE_HASH(uvar[3]);
293 case 3: UPDATE_HASH(uvar[2]);
294 case 2: UPDATE_HASH(uvar[1]);
295 case 1: return hash;
296 case 0: return 5381; /* shouldn't happen */
297 }
298# endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
299# undef UPDATE_HASH
300}
301#endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
302
303static unsigned long
304variable_hash_1 (const void *keyv)
305{
306 struct variable const *key = (struct variable const *) keyv;
307#ifdef VARIABLE_HASH /* bird */
308# ifdef VARIABLE_HASH_STRICT
309 if (key->hash1 != variable_hash_1i (key->name, key->length))
310 __asm__("int3");
311 if (key->hash2 && key->hash2 != variable_hash_2i (key->name, key->length))
312 __asm__("int3");
313# endif
314 return key->hash1;
315#else
316# ifdef CONFIG_WITH_OPTIMIZATION_HACKS
317 return variable_hash_1i (key->name, key->length);
318# else
319 return_STRING_N_HASH_1 (key->name, key->length);
320# endif
321#endif
322}
323
324static unsigned long
325variable_hash_2 (const void *keyv)
326{
327#ifdef VARIABLE_HASH /* bird */
328 struct variable *key = (struct variable *) keyv;
329 if (!key->hash2)
330 key->hash2 = variable_hash_2i (key->name, key->length);
331 return key->hash2;
332#else
333 struct variable const *key = (struct variable const *) keyv;
334# ifdef CONFIG_WITH_OPTIMIZATION_HACKS
335 return variable_hash_2i (key->name, key->length);
336# else
337 return_STRING_N_HASH_2 (key->name, key->length);
338# endif
339#endif
340}
341
342static int
343variable_hash_cmp (const void *xv, const void *yv)
344{
345 struct variable const *x = (struct variable const *) xv;
346 struct variable const *y = (struct variable const *) yv;
347 int result = x->length - y->length;
348 if (result)
349 return result;
350#ifdef VARIABLE_HASH
351#ifdef VARIABLE_HASH_STRICT /* bird */
352 if (x->hash1 != variable_hash_1i (x->name, x->length))
353 __asm__("int3");
354 if (x->hash2 && x->hash2 != variable_hash_2i (x->name, x->length))
355 __asm__("int3");
356 if (y->hash1 != variable_hash_1i (y->name, y->length))
357 __asm__("int3");
358 if (y->hash2 && y->hash2 != variable_hash_2i (y->name, y->length))
359 __asm__("int3");
360#endif
361 /* hash 1 */
362 result = x->hash1 - y->hash1;
363 if (result)
364 return result;
365#endif
366#ifdef CONFIG_WITH_OPTIMIZATION_HACKS /* bird: speed */
367 {
368 const char *xs = x->name;
369 const char *ys = y->name;
370 switch (x->length)
371 {
372 case 8:
373 result = *(int32_t*)(xs + 4) - *(int32_t*)(ys + 4);
374 if (result)
375 return result;
376 return *(int32_t*)xs - *(int32_t*)ys;
377 case 7:
378 result = xs[6] - ys[6];
379 if (result)
380 return result;
381 case 6:
382 result = *(int32_t*)xs - *(int32_t*)ys;
383 if (result)
384 return result;
385 return *(int16_t*)(xs + 4) - *(int16_t*)(ys + 4);
386 case 5:
387 result = xs[4] - ys[4];
388 if (result)
389 return result;
390 case 4:
391 return *(int32_t*)xs - *(int32_t*)ys;
392 case 3:
393 result = xs[2] - ys[2];
394 if (result)
395 return result;
396 case 2:
397 return *(int16_t*)xs - *(int16_t*)ys;
398 case 1:
399 return *xs - *ys;
400 case 0:
401 return 0;
402 }
403 }
404#endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
405#ifdef VARIABLE_HASH
406 /* hash 2 */
407 if (!x->hash2)
408 ((struct variable *)x)->hash2 = variable_hash_2i (x->name, x->length);
409 if (!y->hash2)
410 ((struct variable *)y)->hash2 = variable_hash_2i (y->name, y->length);
411 result = x->hash2 - y->hash2;
412 if (result)
413 return result;
414#endif
415#ifdef CONFIG_WITH_OPTIMIZATION_HACKS
416 return memcmp (x->name, y->name, x->length);
417#else
418 return_STRING_N_COMPARE (x->name, y->name, x->length);
419#endif
420}
421
422#ifndef VARIABLE_BUCKETS
423# ifdef KMK /* Move to Makefile.kmk? (insanely high, but wtf, it gets the collitions down) */
424# define VARIABLE_BUCKETS 65535
425# else /*!KMK*/
426#define VARIABLE_BUCKETS 523
427# endif /*!KMK*/
428#endif
429#ifndef PERFILE_VARIABLE_BUCKETS
430# ifdef KMK /* Move to Makefile.kmk? */
431# define PERFILE_VARIABLE_BUCKETS 127
432# else
433#define PERFILE_VARIABLE_BUCKETS 23
434# endif
435#endif
436#ifndef SMALL_SCOPE_VARIABLE_BUCKETS
437# ifdef KMK /* Move to Makefile.kmk? */
438# define SMALL_SCOPE_VARIABLE_BUCKETS 63
439# else
440#define SMALL_SCOPE_VARIABLE_BUCKETS 13
441# endif
442#endif
443
444static struct variable_set global_variable_set;
445static struct variable_set_list global_setlist
446 = { 0, &global_variable_set };
447struct variable_set_list *current_variable_set_list = &global_setlist;
448
449
450/* Implement variables. */
451
452void
453init_hash_global_variable_set (void)
454{
455 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
456 variable_hash_1, variable_hash_2, variable_hash_cmp);
457}
458
459/* Define variable named NAME with value VALUE in SET. VALUE is copied.
460 LENGTH is the length of NAME, which does not need to be null-terminated.
461 ORIGIN specifies the origin of the variable (makefile, command line
462 or environment).
463 If RECURSIVE is nonzero a flag is set in the variable saying
464 that it should be recursively re-expanded. */
465
466#ifdef CONFIG_WITH_VALUE_LENGTH
467struct variable *
468define_variable_in_set (const char *name, unsigned int length,
469 const char *value, unsigned int value_len,
470 int duplicate_value, enum variable_origin origin,
471 int recursive, struct variable_set *set,
472 const struct floc *flocp)
473#else
474struct variable *
475define_variable_in_set (const char *name, unsigned int length,
476 const char *value, enum variable_origin origin,
477 int recursive, struct variable_set *set,
478 const struct floc *flocp)
479#endif
480{
481 struct variable *v;
482 struct variable **var_slot;
483 struct variable var_key;
484
485 if (set == NULL)
486 set = &global_variable_set;
487
488 var_key.name = (char *) name;
489 var_key.length = length;
490#ifdef VARIABLE_HASH /* bird */
491 var_key.hash1 = variable_hash_1i (name, length);
492 var_key.hash2 = 0;
493#endif
494 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
495
496 if (env_overrides && origin == o_env)
497 origin = o_env_override;
498
499 v = *var_slot;
500 if (! HASH_VACANT (v))
501 {
502 if (env_overrides && v->origin == o_env)
503 /* V came from in the environment. Since it was defined
504 before the switches were parsed, it wasn't affected by -e. */
505 v->origin = o_env_override;
506
507 /* A variable of this name is already defined.
508 If the old definition is from a stronger source
509 than this one, don't redefine it. */
510 if ((int) origin >= (int) v->origin)
511 {
512#ifdef CONFIG_WITH_VALUE_LENGTH
513 if (value_len == ~0U)
514 value_len = strlen (value);
515 else
516 assert (value_len == strlen (value));
517 if (!duplicate_value)
518 {
519 if (v->value != 0)
520 free (v->value);
521 v->value = (char *)value;
522 v->value_alloc_len = value_len + 1;
523 }
524 else
525 {
526 if ((unsigned int)v->value_alloc_len <= value_len)
527 {
528 free (v->value);
529 v->value_alloc_len = (value_len + 0x40) & ~0x3f;
530 v->value = xmalloc (v->value_alloc_len);
531 }
532 memcpy (v->value, value, value_len + 1);
533 }
534 v->value_length = value_len;
535#else
536 if (v->value != 0)
537 free (v->value);
538 v->value = xstrdup (value);
539#endif
540 if (flocp != 0)
541 v->fileinfo = *flocp;
542 else
543 v->fileinfo.filenm = 0;
544 v->origin = origin;
545 v->recursive = recursive;
546 }
547 return v;
548 }
549
550 /* Create a new variable definition and add it to the hash table. */
551
552 v = xmalloc (sizeof (struct variable));
553 v->name = savestring (name, length);
554 v->length = length;
555#ifdef VARIABLE_HASH /* bird */
556 v->hash1 = variable_hash_1i (name, length);
557 v->hash2 = 0;
558#endif
559 hash_insert_at (&set->table, v, var_slot);
560#ifdef CONFIG_WITH_VALUE_LENGTH
561 if (value_len == ~0U)
562 value_len = strlen (value);
563 else
564 assert (value_len == strlen (value));
565 v->value_length = value_len;
566 if (!duplicate_value)
567 {
568 v->value_alloc_len = value_len + 1;
569 v->value = (char *)value;
570 }
571 else
572 {
573 v->value_alloc_len = (value_len + 32) & ~31;
574 v->value = xmalloc (v->value_alloc_len);
575 memcpy (v->value, value, value_len + 1);
576 }
577#else
578 v->value = xstrdup (value);
579#endif
580 if (flocp != 0)
581 v->fileinfo = *flocp;
582 else
583 v->fileinfo.filenm = 0;
584 v->origin = origin;
585 v->recursive = recursive;
586 v->special = 0;
587 v->expanding = 0;
588 v->exp_count = 0;
589 v->per_target = 0;
590 v->append = 0;
591 v->export = v_default;
592
593 v->exportable = 1;
594 if (*name != '_' && (*name < 'A' || *name > 'Z')
595 && (*name < 'a' || *name > 'z'))
596 v->exportable = 0;
597 else
598 {
599 for (++name; *name != '\0'; ++name)
600 if (*name != '_' && (*name < 'a' || *name > 'z')
601 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
602 break;
603
604 if (*name != '\0')
605 v->exportable = 0;
606 }
607
608 return v;
609}
610
611
612/* If the variable passed in is "special", handle its special nature.
613 Currently there are two such variables, both used for introspection:
614 .VARIABLES expands to a list of all the variables defined in this instance
615 of make.
616 .TARGETS expands to a list of all the targets defined in this
617 instance of make.
618 Returns the variable reference passed in. */
619
620#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
621
622static struct variable *
623handle_special_var (struct variable *var)
624{
625 static unsigned long last_var_count = 0;
626
627
628 /* This one actually turns out to be very hard, due to the way the parser
629 records targets. The way it works is that target information is collected
630 internally until make knows the target is completely specified. It unitl
631 it sees that some new construct (a new target or variable) is defined that
632 it knows the previous one is done. In short, this means that if you do
633 this:
634
635 all:
636
637 TARGS := $(.TARGETS)
638
639 then $(TARGS) won't contain "all", because it's not until after the
640 variable is created that the previous target is completed.
641
642 Changing this would be a major pain. I think a less complex way to do it
643 would be to pre-define the target files as soon as the first line is
644 parsed, then come back and do the rest of the definition as now. That
645 would allow $(.TARGETS) to be correct without a major change to the way
646 the parser works.
647
648 if (streq (var->name, ".TARGETS"))
649 var->value = build_target_list (var->value);
650 else
651 */
652
653 if (streq (var->name, ".VARIABLES")
654 && global_variable_set.table.ht_fill != last_var_count)
655 {
656 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
657 unsigned long len;
658 char *p;
659 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
660 struct variable **end = &vp[global_variable_set.table.ht_size];
661
662 /* Make sure we have at least MAX bytes in the allocated buffer. */
663 var->value = xrealloc (var->value, max);
664
665 /* Walk through the hash of variables, constructing a list of names. */
666 p = var->value;
667 len = 0;
668 for (; vp < end; ++vp)
669 if (!HASH_VACANT (*vp))
670 {
671 struct variable *v = *vp;
672 int l = v->length;
673
674 len += l + 1;
675 if (len > max)
676 {
677 unsigned long off = p - var->value;
678
679 max += EXPANSION_INCREMENT (l + 1);
680 var->value = xrealloc (var->value, max);
681 p = &var->value[off];
682 }
683
684 memcpy (p, v->name, l);
685 p += l;
686 *(p++) = ' ';
687 }
688 *(p-1) = '\0';
689
690 /* Remember how many variables are in our current count. Since we never
691 remove variables from the list, this is a reliable way to know whether
692 the list is up to date or needs to be recomputed. */
693
694 last_var_count = global_variable_set.table.ht_fill;
695 }
696
697 return var;
698}
699
700
701
702/* Lookup a variable whose name is a string starting at NAME
703 and with LENGTH chars. NAME need not be null-terminated.
704 Returns address of the `struct variable' containing all info
705 on the variable, or nil if no such variable is defined. */
706
707struct variable *
708lookup_variable (const char *name, unsigned int length)
709{
710 const struct variable_set_list *setlist;
711 struct variable var_key;
712
713 var_key.name = (char *) name;
714 var_key.length = length;
715#ifdef VARIABLE_HASH /* bird */
716 var_key.hash1 = variable_hash_1i (name, length);
717 var_key.hash2 = 0;
718#endif
719
720 for (setlist = current_variable_set_list;
721 setlist != 0; setlist = setlist->next)
722 {
723#ifdef VARIABLE_HASH /* bird: speed */
724 struct hash_table *ht = &setlist->set->table;
725 unsigned int hash_1 = var_key.hash1;
726 struct variable *v;
727
728 ht->ht_lookups++;
729 for (;;)
730 {
731 hash_1 &= (ht->ht_size - 1);
732 v = (struct variable *)ht->ht_vec[hash_1];
733
734 if (v == 0)
735 break;
736 if ((void *)v != hash_deleted_item)
737 {
738 if (variable_hash_cmp(&var_key, v) == 0)
739 {
740# ifdef VARIABLE_HASH_STRICT /* bird */
741 struct variable *v2 = (struct variable *) hash_find_item ((struct hash_table *) &setlist->set->table, &var_key);
742 assert(v2 == v);
743# endif
744 return v->special ? handle_special_var (v) : v;
745 }
746 ht->ht_collisions++;
747 }
748 if (!var_key.hash2)
749 var_key.hash2 = variable_hash_2i(name, length);
750 hash_1 += (var_key.hash2 | 1);
751 }
752
753#else /* !VARIABLE_HASH */
754 const struct variable_set *set = setlist->set;
755 struct variable *v;
756
757 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
758 if (v)
759 return v->special ? handle_special_var (v) : v;
760#endif /* !VARIABLE_HASH */
761 }
762
763#ifdef VMS
764 /* since we don't read envp[] on startup, try to get the
765 variable via getenv() here. */
766 {
767 char *vname = alloca (length + 1);
768 char *value;
769 strncpy (vname, name, length);
770 vname[length] = 0;
771 value = getenv (vname);
772 if (value != 0)
773 {
774 char *sptr;
775 int scnt;
776
777 sptr = value;
778 scnt = 0;
779
780 while ((sptr = strchr (sptr, '$')))
781 {
782 scnt++;
783 sptr++;
784 }
785
786 if (scnt > 0)
787 {
788 char *nvalue;
789 char *nptr;
790
791 nvalue = alloca (strlen (value) + scnt + 1);
792 sptr = value;
793 nptr = nvalue;
794
795 while (*sptr)
796 {
797 if (*sptr == '$')
798 {
799 *nptr++ = '$';
800 *nptr++ = '$';
801 }
802 else
803 {
804 *nptr++ = *sptr;
805 }
806 sptr++;
807 }
808
809 *nptr = '\0';
810 return define_variable (vname, length, nvalue, o_env, 1);
811
812 }
813
814 return define_variable (vname, length, value, o_env, 1);
815 }
816 }
817#endif /* VMS */
818
819 return 0;
820}
821
822
823/* Lookup a variable whose name is a string starting at NAME
824 and with LENGTH chars in set SET. NAME need not be null-terminated.
825 Returns address of the `struct variable' containing all info
826 on the variable, or nil if no such variable is defined. */
827
828struct variable *
829lookup_variable_in_set (const char *name, unsigned int length,
830 const struct variable_set *set)
831{
832 struct variable var_key;
833
834 var_key.name = (char *) name;
835 var_key.length = length;
836#ifdef VARIABLE_HASH /* bird */
837 var_key.hash1 = variable_hash_1i (name, length);
838 var_key.hash2 = 0;
839#endif
840
841 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
842}
843
844
845/* Initialize FILE's variable set list. If FILE already has a variable set
846 list, the topmost variable set is left intact, but the the rest of the
847 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
848 rule, then we will use the "root" double-colon target's variable set as the
849 parent of FILE's variable set.
850
851 If we're READING a makefile, don't do the pattern variable search now,
852 since the pattern variable might not have been defined yet. */
853
854void
855initialize_file_variables (struct file *file, int reading)
856{
857 struct variable_set_list *l = file->variables;
858
859 if (l == 0)
860 {
861 l = (struct variable_set_list *)
862 xmalloc (sizeof (struct variable_set_list));
863 l->set = xmalloc (sizeof (struct variable_set));
864 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
865 variable_hash_1, variable_hash_2, variable_hash_cmp);
866 file->variables = l;
867 }
868
869 /* If this is a double-colon, then our "parent" is the "root" target for
870 this double-colon rule. Since that rule has the same name, parent,
871 etc. we can just use its variables as the "next" for ours. */
872
873 if (file->double_colon && file->double_colon != file)
874 {
875 initialize_file_variables (file->double_colon, reading);
876 l->next = file->double_colon->variables;
877 return;
878 }
879
880 if (file->parent == 0)
881 l->next = &global_setlist;
882 else
883 {
884 initialize_file_variables (file->parent, reading);
885 l->next = file->parent->variables;
886 }
887
888 /* If we're not reading makefiles and we haven't looked yet, see if
889 we can find pattern variables for this target. */
890
891 if (!reading && !file->pat_searched)
892 {
893 struct pattern_var *p;
894
895 p = lookup_pattern_var (0, file->name);
896 if (p != 0)
897 {
898 struct variable_set_list *global = current_variable_set_list;
899
900 /* We found at least one. Set up a new variable set to accumulate
901 all the pattern variables that match this target. */
902
903 file->pat_variables = create_new_variable_set ();
904 current_variable_set_list = file->pat_variables;
905
906 do
907 {
908 /* We found one, so insert it into the set. */
909
910 struct variable *v;
911
912 if (p->variable.flavor == f_simple)
913 {
914 v = define_variable_loc (
915 p->variable.name, strlen (p->variable.name),
916 p->variable.value, p->variable.origin,
917 0, &p->variable.fileinfo);
918
919 v->flavor = f_simple;
920 }
921 else
922 {
923#ifndef CONFIG_WITH_VALUE_LENGTH
924 v = do_variable_definition (
925 &p->variable.fileinfo, p->variable.name,
926 p->variable.value, p->variable.origin,
927 p->variable.flavor, 1);
928#else
929 v = do_variable_definition_2 (
930 &p->variable.fileinfo, p->variable.name,
931 p->variable.value, p->variable.value_length, 0, 0,
932 p->variable.origin, p->variable.flavor, 1);
933#endif
934 }
935
936 /* Also mark it as a per-target and copy export status. */
937 v->per_target = p->variable.per_target;
938 v->export = p->variable.export;
939 }
940 while ((p = lookup_pattern_var (p, file->name)) != 0);
941
942 current_variable_set_list = global;
943 }
944 file->pat_searched = 1;
945 }
946
947 /* If we have a pattern variable match, set it up. */
948
949 if (file->pat_variables != 0)
950 {
951 file->pat_variables->next = l->next;
952 l->next = file->pat_variables;
953 }
954}
955
956
957/* Pop the top set off the current variable set list,
958 and free all its storage. */
959
960struct variable_set_list *
961create_new_variable_set (void)
962{
963 register struct variable_set_list *setlist;
964 register struct variable_set *set;
965
966 set = xmalloc (sizeof (struct variable_set));
967 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
968 variable_hash_1, variable_hash_2, variable_hash_cmp);
969
970 setlist = (struct variable_set_list *)
971 xmalloc (sizeof (struct variable_set_list));
972 setlist->set = set;
973 setlist->next = current_variable_set_list;
974
975 return setlist;
976}
977
978static void
979free_variable_name_and_value (const void *item)
980{
981 struct variable *v = (struct variable *) item;
982 free (v->name);
983 free (v->value);
984}
985
986void
987free_variable_set (struct variable_set_list *list)
988{
989 hash_map (&list->set->table, free_variable_name_and_value);
990 hash_free (&list->set->table, 1);
991 free (list->set);
992 free (list);
993}
994
995/* Create a new variable set and push it on the current setlist.
996 If we're pushing a global scope (that is, the current scope is the global
997 scope) then we need to "push" it the other way: file variable sets point
998 directly to the global_setlist so we need to replace that with the new one.
999 */
1000
1001struct variable_set_list *
1002push_new_variable_scope (void)
1003{
1004 current_variable_set_list = create_new_variable_set();
1005 if (current_variable_set_list->next == &global_setlist)
1006 {
1007 /* It was the global, so instead of new -> &global we want to replace
1008 &global with the new one and have &global -> new, with current still
1009 pointing to &global */
1010 struct variable_set *set = current_variable_set_list->set;
1011 current_variable_set_list->set = global_setlist.set;
1012 global_setlist.set = set;
1013 current_variable_set_list->next = global_setlist.next;
1014 global_setlist.next = current_variable_set_list;
1015 current_variable_set_list = &global_setlist;
1016 }
1017 return (current_variable_set_list);
1018}
1019
1020void
1021pop_variable_scope (void)
1022{
1023 struct variable_set_list *setlist;
1024 struct variable_set *set;
1025
1026 /* Can't call this if there's no scope to pop! */
1027 assert(current_variable_set_list->next != NULL);
1028
1029 if (current_variable_set_list != &global_setlist)
1030 {
1031 /* We're not pointing to the global setlist, so pop this one. */
1032 setlist = current_variable_set_list;
1033 set = setlist->set;
1034 current_variable_set_list = setlist->next;
1035 }
1036 else
1037 {
1038 /* This set is the one in the global_setlist, but there is another global
1039 set beyond that. We want to copy that set to global_setlist, then
1040 delete what used to be in global_setlist. */
1041 setlist = global_setlist.next;
1042 set = global_setlist.set;
1043 global_setlist.set = setlist->set;
1044 global_setlist.next = setlist->next;
1045 }
1046
1047 /* Free the one we no longer need. */
1048 free (setlist);
1049 hash_map (&set->table, free_variable_name_and_value);
1050 hash_free (&set->table, 1);
1051 free (set);
1052}
1053
1054
1055/* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
1056
1057static void
1058merge_variable_sets (struct variable_set *to_set,
1059 struct variable_set *from_set)
1060{
1061 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
1062 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
1063
1064 for ( ; from_var_slot < from_var_end; from_var_slot++)
1065 if (! HASH_VACANT (*from_var_slot))
1066 {
1067 struct variable *from_var = *from_var_slot;
1068 struct variable **to_var_slot
1069 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
1070 if (HASH_VACANT (*to_var_slot))
1071 hash_insert_at (&to_set->table, from_var, to_var_slot);
1072 else
1073 {
1074 /* GKM FIXME: delete in from_set->table */
1075 free (from_var->value);
1076 free (from_var);
1077 }
1078 }
1079}
1080
1081/* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
1082
1083void
1084merge_variable_set_lists (struct variable_set_list **setlist0,
1085 struct variable_set_list *setlist1)
1086{
1087 struct variable_set_list *to = *setlist0;
1088 struct variable_set_list *last0 = 0;
1089
1090 /* If there's nothing to merge, stop now. */
1091 if (!setlist1)
1092 return;
1093
1094 /* This loop relies on the fact that all setlists terminate with the global
1095 setlist (before NULL). If that's not true, arguably we SHOULD die. */
1096 if (to)
1097 while (setlist1 != &global_setlist && to != &global_setlist)
1098 {
1099 struct variable_set_list *from = setlist1;
1100 setlist1 = setlist1->next;
1101
1102 merge_variable_sets (to->set, from->set);
1103
1104 last0 = to;
1105 to = to->next;
1106 }
1107
1108 if (setlist1 != &global_setlist)
1109 {
1110 if (last0 == 0)
1111 *setlist0 = setlist1;
1112 else
1113 last0->next = setlist1;
1114 }
1115}
1116
1117
1118/* Define the automatic variables, and record the addresses
1119 of their structures so we can change their values quickly. */
1120
1121void
1122define_automatic_variables (void)
1123{
1124#if defined(WINDOWS32) || defined(__EMX__)
1125 extern char* default_shell;
1126#else
1127 extern char default_shell[];
1128#endif
1129 register struct variable *v;
1130#ifndef KMK
1131 char buf[200];
1132#else
1133 char buf[1024];
1134 const char *val;
1135 struct variable *envvar1;
1136 struct variable *envvar2;
1137#endif
1138
1139 sprintf (buf, "%u", makelevel);
1140 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
1141
1142 sprintf (buf, "%s%s%s",
1143 version_string,
1144 (remote_description == 0 || remote_description[0] == '\0')
1145 ? "" : "-",
1146 (remote_description == 0 || remote_description[0] == '\0')
1147 ? "" : remote_description);
1148#ifndef KMK
1149 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
1150#else /* KMK */
1151
1152 /* Define KMK_VERSION to indicate kMk. */
1153 (void) define_variable ("KMK_VERSION", 11, buf, o_default, 0);
1154
1155 /* Define KBUILD_VERSION* */
1156 sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
1157 define_variable ("KBUILD_VERSION_MAJOR", sizeof ("KBUILD_VERSION_MAJOR") - 1,
1158 buf, o_default, 0);
1159 sprintf (buf, "%d", KBUILD_VERSION_MINOR);
1160 define_variable ("KBUILD_VERSION_MINOR", sizeof("KBUILD_VERSION_MINOR") - 1,
1161 buf, o_default, 0);
1162 sprintf (buf, "%d", KBUILD_VERSION_PATCH);
1163 define_variable ("KBUILD_VERSION_PATCH", sizeof ("KBUILD_VERSION_PATCH") - 1,
1164 buf, o_default, 0);
1165 sprintf (buf, "%d", KBUILD_SVN_REV);
1166 define_variable ("KBUILD_KMK_REVISION", sizeof ("KBUILD_KMK_REVISION") - 1,
1167 buf, o_default, 0);
1168
1169 sprintf (buf, "%d.%d.%d-r%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR,
1170 KBUILD_VERSION_PATCH, KBUILD_SVN_REV);
1171 define_variable ("KBUILD_VERSION", sizeof ("KBUILD_VERSION") - 1,
1172 buf, o_default, 0);
1173
1174 /* The host defaults. The BUILD_* stuff will be replaced by KBUILD_* soon. */
1175 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST"));
1176 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM"));
1177 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST;
1178 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1179 error (NULL, _("KBUILD_HOST and BUILD_PLATFORM differs, using KBUILD_HOST=%s."), val);
1180 if (!envvar1)
1181 define_variable ("KBUILD_HOST", sizeof ("KBUILD_HOST") - 1,
1182 val, o_default, 0);
1183 if (!envvar2)
1184 define_variable ("BUILD_PLATFORM", sizeof ("BUILD_PLATFORM") - 1,
1185 val, o_default, 0);
1186
1187 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_ARCH"));
1188 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_ARCH"));
1189 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_ARCH;
1190 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1191 error (NULL, _("KBUILD_HOST_ARCH and BUILD_PLATFORM_ARCH differs, using KBUILD_HOST_ARCH=%s."), val);
1192 if (!envvar1)
1193 define_variable ("KBUILD_HOST_ARCH", sizeof ("KBUILD_HOST_ARCH") - 1,
1194 val, o_default, 0);
1195 if (!envvar2)
1196 define_variable ("BUILD_PLATFORM_ARCH", sizeof ("BUILD_PLATFORM_ARCH") - 1,
1197 val, o_default, 0);
1198
1199 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_CPU"));
1200 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_CPU"));
1201 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_CPU;
1202 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1203 error (NULL, _("KBUILD_HOST_CPU and BUILD_PLATFORM_CPU differs, using KBUILD_HOST_CPU=%s."), val);
1204 if (!envvar1)
1205 define_variable ("KBUILD_HOST_CPU", sizeof ("KBUILD_HOST_CPU") - 1,
1206 val, o_default, 0);
1207 if (!envvar2)
1208 define_variable ("BUILD_PLATFORM_CPU", sizeof ("BUILD_PLATFORM_CPU") - 1,
1209 val, o_default, 0);
1210
1211 /* The kBuild locations. */
1212 define_variable ("KBUILD_PATH", sizeof ("KBUILD_PATH") - 1,
1213 get_kbuild_path (), o_default, 0);
1214 define_variable ("KBUILD_BIN_PATH", sizeof ("KBUILD_BIN_PATH") - 1,
1215 get_kbuild_bin_path (), o_default, 0);
1216
1217 define_variable ("PATH_KBUILD", sizeof ("PATH_KBUILD") - 1,
1218 get_kbuild_path (), o_default, 0);
1219 define_variable ("PATH_KBUILD_BIN", sizeof ("PATH_KBUILD_BIN") - 1,
1220 get_kbuild_bin_path (), o_default, 0);
1221
1222 /* Define KMK_FEATURES to indicate various working KMK features. */
1223# if defined (CONFIG_WITH_RSORT) \
1224 && defined (CONFIG_WITH_ABSPATHEX) \
1225 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1226 && defined (CONFIG_WITH_DEFINED) \
1227 && defined (CONFIG_WITH_VALUE_LENGTH) && defined (CONFIG_WITH_COMPARE) \
1228 && defined (CONFIG_WITH_STACK) \
1229 && defined (CONFIG_WITH_MATH) \
1230 && defined (CONFIG_WITH_XARGS) \
1231 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1232 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1233 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1234 && defined (CONFIG_WITH_DATE) \
1235 && defined (CONFIG_WITH_FILE_SIZE) \
1236 && defined (CONFIG_WITH_WHICH) \
1237 && defined (CONFIG_WITH_EVALPLUS) \
1238 && defined (CONFIG_WITH_MAKE_STATS) \
1239 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1240 && defined (KMK_HELPERS)
1241 (void) define_variable ("KMK_FEATURES", 12,
1242 "append-dash-n abspath includedep-queue"
1243 " rsort"
1244 " abspathex"
1245 " toupper tolower"
1246 " defined"
1247 " comp-vars comp-cmds comp-cmds-ex"
1248 " stack"
1249 " math-int"
1250 " xargs"
1251 " explicit-multitarget"
1252 " prepend-assignment"
1253 " set-conditionals"
1254 " date"
1255 " file-size"
1256 " expr if-expr"
1257 " which"
1258 " evalctx evalval evalvalctx evalcall evalcall2"
1259 " make-stats"
1260 " commands"
1261 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one "
1262 , o_default, 0);
1263# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1264# error "All features should be enabled by default!"
1265 strcpy (buf, "append-dash-n abspath includedep-queue");
1266# if defined (CONFIG_WITH_RSORT)
1267 strcat (buf, " rsort");
1268# endif
1269# if defined (CONFIG_WITH_ABSPATHEX)
1270 strcat (buf, " abspathex");
1271# endif
1272# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1273 strcat (buf, " toupper tolower");
1274# endif
1275# if defined (CONFIG_WITH_DEFINED)
1276 strcat (buf, " defined");
1277# endif
1278# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1279 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1280# endif
1281# if defined (CONFIG_WITH_STACK)
1282 strcat (buf, " stack");
1283# endif
1284# if defined (CONFIG_WITH_MATH)
1285 strcat (buf, " math-int");
1286# endif
1287# if defined (CONFIG_WITH_XARGS)
1288 strcat (buf, " xargs");
1289# endif
1290# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1291 strcat (buf, " explicit-multitarget");
1292# endif
1293# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1294 strcat (buf, " prepend-assignment");
1295# endif
1296# if defined (CONFIG_WITH_SET_CONDITIONALS)
1297 strcat (buf, " set-conditionals");
1298# endif
1299# if defined (CONFIG_WITH_DATE)
1300 strcat (buf, " date");
1301# endif
1302# if defined (CONFIG_WITH_FILE_SIZE)
1303 strcat (buf, " file-size");
1304# endif
1305# if defined (CONFIG_WITH_IF_CONDITIONALS)
1306 strcat (buf, " expr if-expr");
1307# endif
1308# if defined (CONFIG_WITH_WHICH)
1309 strcat (buf, " which");
1310# endif
1311# if defined (CONFIG_WITH_EVALPLUS)
1312 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2");
1313# endif
1314# if defined (CONFIG_WITH_MAKE_STATS)
1315 strcat (buf, " make-stats");
1316# endif
1317# if defined (CONFIG_WITH_COMMANDS_FUNC)
1318 strcat (buf, " commands");
1319# endif
1320# if defined (KMK_HELPERS)
1321 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one");
1322# endif
1323 (void) define_variable ("KMK_FEATURES", 12, buf, o_default, 0);
1324# endif
1325
1326#endif /* KMK */
1327
1328#ifdef CONFIG_WITH_KMK_BUILTIN
1329 /* The supported kMk Builtin commands. */
1330 (void) define_variable ("KMK_BUILTIN", 11, "append cat chmod cp cmp echo expr install kDepIDB ln md5sum mkdir mv printf rm rmdir test", o_default, 0);
1331#endif
1332
1333#ifdef __MSDOS__
1334 /* Allow to specify a special shell just for Make,
1335 and use $COMSPEC as the default $SHELL when appropriate. */
1336 {
1337 static char shell_str[] = "SHELL";
1338 const int shlen = sizeof (shell_str) - 1;
1339 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1340 struct variable *comp = lookup_variable ("COMSPEC", 7);
1341
1342 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
1343 if (mshp)
1344 (void) define_variable (shell_str, shlen,
1345 mshp->value, o_env_override, 0);
1346 else if (comp)
1347 {
1348 /* $COMSPEC shouldn't override $SHELL. */
1349 struct variable *shp = lookup_variable (shell_str, shlen);
1350
1351 if (!shp)
1352 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1353 }
1354 }
1355#elif defined(__EMX__)
1356 {
1357 static char shell_str[] = "SHELL";
1358 const int shlen = sizeof (shell_str) - 1;
1359 struct variable *shell = lookup_variable (shell_str, shlen);
1360 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1361
1362 /* if $MAKESHELL is defined in the environment assume o_env_override */
1363 if (replace && *replace->value && replace->origin == o_env)
1364 replace->origin = o_env_override;
1365
1366 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1367 did not come from the environment */
1368 if (!replace || !*replace->value)
1369 if (shell && *shell->value && (shell->origin == o_env
1370 || shell->origin == o_env_override))
1371 {
1372 /* overwrite whatever we got from the environment */
1373 free(shell->value);
1374 shell->value = xstrdup (default_shell);
1375 shell->origin = o_default;
1376 }
1377
1378 /* Some people do not like cmd to be used as the default
1379 if $SHELL is not defined in the Makefile.
1380 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1381# ifndef NO_CMD_DEFAULT
1382 /* otherwise use $COMSPEC */
1383 if (!replace || !*replace->value)
1384 replace = lookup_variable ("COMSPEC", 7);
1385
1386 /* otherwise use $OS2_SHELL */
1387 if (!replace || !*replace->value)
1388 replace = lookup_variable ("OS2_SHELL", 9);
1389# else
1390# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1391# endif
1392
1393 if (replace && *replace->value)
1394 /* overwrite $SHELL */
1395 (void) define_variable (shell_str, shlen, replace->value,
1396 replace->origin, 0);
1397 else
1398 /* provide a definition if there is none */
1399 (void) define_variable (shell_str, shlen, default_shell,
1400 o_default, 0);
1401 }
1402
1403#endif
1404
1405 /* This won't override any definition, but it will provide one if there
1406 isn't one there. */
1407 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
1408
1409 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1410 environment variable on MSDOS, so whoever sets it, does that on purpose.
1411 On OS/2 we do not use SHELL from environment but we have already handled
1412 that problem above. */
1413#if !defined(__MSDOS__) && !defined(__EMX__)
1414 /* Don't let SHELL come from the environment. */
1415 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1416 {
1417 free (v->value);
1418 v->origin = o_file;
1419 v->value = xstrdup (default_shell);
1420#ifdef CONFIG_WITH_VALUE_LENGTH
1421 v->value_length = strlen (v->value);
1422 v->value_alloc_len = v->value_length + 1;
1423#endif
1424 }
1425#endif
1426
1427 /* Make sure MAKEFILES gets exported if it is set. */
1428 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
1429 v->export = v_ifset;
1430
1431 /* Define the magic D and F variables in terms of
1432 the automatic variables they are variations of. */
1433
1434#ifdef VMS
1435 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
1436 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
1437 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
1438 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
1439 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
1440 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
1441 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
1442#else
1443 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
1444 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
1445 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
1446 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
1447 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
1448 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1449 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1450#endif
1451 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
1452 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
1453 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
1454 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
1455 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
1456 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
1457 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
1458}
1459
1460
1461int export_all_variables;
1462
1463/* Create a new environment for FILE's commands.
1464 If FILE is nil, this is for the `shell' function.
1465 The child's MAKELEVEL variable is incremented. */
1466
1467char **
1468target_environment (struct file *file)
1469{
1470 struct variable_set_list *set_list;
1471 register struct variable_set_list *s;
1472 struct hash_table table;
1473 struct variable **v_slot;
1474 struct variable **v_end;
1475 struct variable makelevel_key;
1476 char **result_0;
1477 char **result;
1478
1479 if (file == 0)
1480 set_list = current_variable_set_list;
1481 else
1482 set_list = file->variables;
1483
1484 hash_init (&table, VARIABLE_BUCKETS,
1485 variable_hash_1, variable_hash_2, variable_hash_cmp);
1486
1487 /* Run through all the variable sets in the list,
1488 accumulating variables in TABLE. */
1489 for (s = set_list; s != 0; s = s->next)
1490 {
1491 struct variable_set *set = s->set;
1492 v_slot = (struct variable **) set->table.ht_vec;
1493 v_end = v_slot + set->table.ht_size;
1494 for ( ; v_slot < v_end; v_slot++)
1495 if (! HASH_VACANT (*v_slot))
1496 {
1497 struct variable **new_slot;
1498 struct variable *v = *v_slot;
1499
1500 /* If this is a per-target variable and it hasn't been touched
1501 already then look up the global version and take its export
1502 value. */
1503 if (v->per_target && v->export == v_default)
1504 {
1505 struct variable *gv;
1506
1507 gv = lookup_variable_in_set (v->name, strlen(v->name),
1508 &global_variable_set);
1509 if (gv)
1510 v->export = gv->export;
1511 }
1512
1513 switch (v->export)
1514 {
1515 case v_default:
1516 if (v->origin == o_default || v->origin == o_automatic)
1517 /* Only export default variables by explicit request. */
1518 continue;
1519
1520 /* The variable doesn't have a name that can be exported. */
1521 if (! v->exportable)
1522 continue;
1523
1524 if (! export_all_variables
1525 && v->origin != o_command
1526 && v->origin != o_env && v->origin != o_env_override)
1527 continue;
1528 break;
1529
1530 case v_export:
1531 break;
1532
1533 case v_noexport:
1534 /* If this is the SHELL variable and it's not exported, then
1535 add the value from our original environment. */
1536 if (streq (v->name, "SHELL"))
1537 {
1538 extern struct variable shell_var;
1539 v = &shell_var;
1540 break;
1541 }
1542 continue;
1543
1544 case v_ifset:
1545 if (v->origin == o_default)
1546 continue;
1547 break;
1548 }
1549
1550 new_slot = (struct variable **) hash_find_slot (&table, v);
1551 if (HASH_VACANT (*new_slot))
1552 hash_insert_at (&table, v, new_slot);
1553 }
1554 }
1555
1556 makelevel_key.name = MAKELEVEL_NAME;
1557 makelevel_key.length = MAKELEVEL_LENGTH;
1558#ifdef VARIABLE_HASH /* bird */
1559 makelevel_key.hash1 = variable_hash_1i (MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1560 makelevel_key.hash2 = 0;
1561#endif
1562 hash_delete (&table, &makelevel_key);
1563
1564 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1565
1566 v_slot = (struct variable **) table.ht_vec;
1567 v_end = v_slot + table.ht_size;
1568 for ( ; v_slot < v_end; v_slot++)
1569 if (! HASH_VACANT (*v_slot))
1570 {
1571 struct variable *v = *v_slot;
1572
1573 /* If V is recursively expanded and didn't come from the environment,
1574 expand its value. If it came from the environment, it should
1575 go back into the environment unchanged. */
1576 if (v->recursive
1577 && v->origin != o_env && v->origin != o_env_override)
1578 {
1579 char *value = recursively_expand_for_file (v, file);
1580#ifdef WINDOWS32
1581 if (strcmp(v->name, "Path") == 0 ||
1582 strcmp(v->name, "PATH") == 0)
1583 convert_Path_to_windows32(value, ';');
1584#endif
1585 *result++ = xstrdup (concat (v->name, "=", value));
1586 free (value);
1587 }
1588 else
1589 {
1590#ifdef WINDOWS32
1591 if (strcmp(v->name, "Path") == 0 ||
1592 strcmp(v->name, "PATH") == 0)
1593 convert_Path_to_windows32(v->value, ';');
1594#endif
1595 *result++ = xstrdup (concat (v->name, "=", v->value));
1596 }
1597 }
1598
1599 *result = xmalloc (100);
1600 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1601 *++result = 0;
1602
1603 hash_free (&table, 0);
1604
1605 return result_0;
1606}
1607
1608
1609#ifdef CONFIG_WITH_VALUE_LENGTH
1610/* Worker function for do_variable_definition_append() and
1611 append_expanded_string_to_variable().
1612 The APPEND argument indicates whether it's an append or prepend operation. */
1613void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
1614{
1615 /* The previous definition of the variable was recursive.
1616 The new value is the unexpanded old and new values. */
1617 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
1618 int done_1st_prepend_copy = 0;
1619
1620 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1621 if (!value_len)
1622 return;
1623
1624 /* adjust the size. */
1625 if ((unsigned)v->value_alloc_len <= new_value_len + 1)
1626 {
1627 v->value_alloc_len *= 2;
1628 if ((unsigned)v->value_alloc_len < new_value_len + 1)
1629 v->value_alloc_len = (new_value_len + 1 + value_len + 0x7f) + ~0x7fU;
1630 if (append || !v->value_length)
1631 v->value = xrealloc (v->value, v->value_alloc_len);
1632 else
1633 {
1634 /* avoid the extra memcpy the xrealloc may have to do */
1635 char *new_buf = xmalloc (v->value_alloc_len);
1636 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
1637 done_1st_prepend_copy = 1;
1638 free (v->value);
1639 v->value = new_buf;
1640 }
1641 }
1642
1643 /* insert the new bits */
1644 if (v->value_length != 0)
1645 {
1646 if (append)
1647 {
1648 v->value[v->value_length] = ' ';
1649 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
1650 }
1651 else
1652 {
1653 if (!done_1st_prepend_copy)
1654 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
1655 v->value[value_len] = ' ';
1656 memcpy (v->value, value, value_len);
1657 }
1658 }
1659 else
1660 memcpy (v->value, value, value_len + 1);
1661 v->value_length = new_value_len;
1662}
1663
1664static struct variable *
1665do_variable_definition_append (const struct floc *flocp, struct variable *v,
1666 const char *value, unsigned int value_len,
1667 int simple_value, enum variable_origin origin,
1668 int append)
1669{
1670 if (env_overrides && origin == o_env)
1671 origin = o_env_override;
1672
1673 if (env_overrides && v->origin == o_env)
1674 /* V came from in the environment. Since it was defined
1675 before the switches were parsed, it wasn't affected by -e. */
1676 v->origin = o_env_override;
1677
1678 /* A variable of this name is already defined.
1679 If the old definition is from a stronger source
1680 than this one, don't redefine it. */
1681 if ((int) origin < (int) v->origin)
1682 return v;
1683 v->origin = origin;
1684
1685 /* location */
1686 if (flocp != 0)
1687 v->fileinfo = *flocp;
1688
1689 /* The juicy bits, append the specified value to the variable
1690 This is a heavily exercised code path in kBuild. */
1691 if (value_len == ~0U)
1692 value_len = strlen (value);
1693 if (v->recursive || simple_value)
1694 append_string_to_variable (v, value, value_len, append);
1695 else
1696 /* The previous definition of the variable was simple.
1697 The new value comes from the old value, which was expanded
1698 when it was set; and from the expanded new value. */
1699 append_expanded_string_to_variable (v, value, value_len, append);
1700
1701 /* update the variable */
1702 return v;
1703}
1704#endif /* CONFIG_WITH_VALUE_LENGTH */
1705
1706
1707/* Given a variable, a value, and a flavor, define the variable.
1708 See the try_variable_definition() function for details on the parameters. */
1709
1710struct variable *
1711#ifndef CONFIG_WITH_VALUE_LENGTH
1712do_variable_definition (const struct floc *flocp, const char *varname,
1713 const char *value, enum variable_origin origin,
1714 enum variable_flavor flavor, int target_var)
1715#else /* CONFIG_WITH_VALUE_LENGTH */
1716do_variable_definition_2 (const struct floc *flocp,
1717 const char *varname, const char *value,
1718 unsigned int value_len, int simple_value,
1719 char *free_value,
1720 enum variable_origin origin,
1721 enum variable_flavor flavor,
1722 int target_var)
1723#endif /* CONFIG_WITH_VALUE_LENGTH */
1724{
1725 const char *p;
1726 char *alloc_value = NULL;
1727 struct variable *v;
1728 int append = 0;
1729 int conditional = 0;
1730 const size_t varname_len = strlen (varname); /* bird */
1731#ifdef CONFIG_WITH_VALUE_LENGTH
1732 assert (value_len == ~0U || value_len == strlen (value));
1733#endif
1734
1735 /* Calculate the variable's new value in VALUE. */
1736
1737 switch (flavor)
1738 {
1739 default:
1740 case f_bogus:
1741 /* Should not be possible. */
1742 abort ();
1743 case f_simple:
1744 /* A simple variable definition "var := value". Expand the value.
1745 We have to allocate memory since otherwise it'll clobber the
1746 variable buffer, and we may still need that if we're looking at a
1747 target-specific variable. */
1748#ifndef CONFIG_WITH_VALUE_LENGTH
1749 p = alloc_value = allocated_variable_expand (value);
1750#else /* CONFIG_WITH_VALUE_LENGTH */
1751 if (!simple_value)
1752 p = alloc_value = allocated_variable_expand_2 (value, value_len, &value_len);
1753 else
1754 {
1755 if (value_len == ~0U)
1756 value_len = strlen (value);
1757 if (!free_value)
1758 p = alloc_value = savestring (value, value_len);
1759 else
1760 {
1761 assert (value == free_value);
1762 p = alloc_value = free_value;
1763 free_value = 0;
1764 }
1765 }
1766#endif /* CONFIG_WITH_VALUE_LENGTH */
1767 break;
1768 case f_conditional:
1769 /* A conditional variable definition "var ?= value".
1770 The value is set IFF the variable is not defined yet. */
1771 v = lookup_variable (varname, varname_len);
1772 if (v)
1773 return v;
1774
1775 conditional = 1;
1776 flavor = f_recursive;
1777 /* FALLTHROUGH */
1778 case f_recursive:
1779 /* A recursive variable definition "var = value".
1780 The value is used verbatim. */
1781 p = value;
1782 break;
1783#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1784 case f_append:
1785 case f_prepend:
1786 {
1787 const enum variable_flavor org_flavor = flavor;
1788#else
1789 case f_append:
1790 {
1791#endif
1792
1793#ifdef CONFIG_WITH_LOCAL_VARIABLES
1794 /* If we have += but we're in a target or local variable context,
1795 we want to append only with other variables in the context of
1796 this target. */
1797 if (target_var || origin == o_local)
1798#else
1799 /* If we have += but we're in a target variable context, we want to
1800 append only with other variables in the context of this target. */
1801 if (target_var)
1802#endif
1803 {
1804 append = 1;
1805 v = lookup_variable_in_set (varname, varname_len,
1806 current_variable_set_list->set);
1807
1808 /* Don't append from the global set if a previous non-appending
1809 target-specific variable definition exists. */
1810 if (v && !v->append)
1811 append = 0;
1812 }
1813 else
1814 v = lookup_variable (varname, varname_len);
1815
1816 if (v == 0)
1817 {
1818 /* There was no old value.
1819 This becomes a normal recursive definition. */
1820 p = value;
1821 flavor = f_recursive;
1822 }
1823 else
1824 {
1825#ifdef CONFIG_WITH_VALUE_LENGTH
1826 v->append = append;
1827 v = do_variable_definition_append (flocp, v, value, value_len,
1828 simple_value, origin,
1829# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1830 org_flavor == f_append);
1831# else
1832 1);
1833# endif
1834 if (free_value)
1835 free (free_value);
1836 return v;
1837#else /* !CONFIG_WITH_VALUE_LENGTH */
1838
1839 /* Paste the old and new values together in VALUE. */
1840
1841 unsigned int oldlen, vallen;
1842 const char *val;
1843 char *tp;
1844
1845 val = value;
1846 if (v->recursive)
1847 /* The previous definition of the variable was recursive.
1848 The new value is the unexpanded old and new values. */
1849 flavor = f_recursive;
1850 else
1851 /* The previous definition of the variable was simple.
1852 The new value comes from the old value, which was expanded
1853 when it was set; and from the expanded new value. Allocate
1854 memory for the expansion as we may still need the rest of the
1855 buffer if we're looking at a target-specific variable. */
1856 val = alloc_value = allocated_variable_expand (val);
1857
1858 oldlen = strlen (v->value);
1859 vallen = strlen (val);
1860 tp = alloca (oldlen + 1 + vallen + 1);
1861# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1862 if (org_flavor == f_prepend)
1863 {
1864 memcpy (tp, val, vallen);
1865 tp[oldlen] = ' ';
1866 memcpy (&tp[oldlen + 1], v->value, oldlen + 1);
1867 }
1868 else
1869# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
1870 {
1871 memcpy (tp, v->value, oldlen);
1872 tp[oldlen] = ' ';
1873 memcpy (&tp[oldlen + 1], val, vallen + 1);
1874 }
1875 p = tp;
1876#endif /* !CONFIG_WITH_VALUE_LENGTH */
1877 }
1878 }
1879 }
1880
1881#ifdef __MSDOS__
1882 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1883 non-Unix systems don't conform to this default configuration (in
1884 fact, most of them don't even have `/bin'). On the other hand,
1885 $SHELL in the environment, if set, points to the real pathname of
1886 the shell.
1887 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1888 the Makefile override $SHELL from the environment. But first, we
1889 look for the basename of the shell in the directory where SHELL=
1890 points, and along the $PATH; if it is found in any of these places,
1891 we define $SHELL to be the actual pathname of the shell. Thus, if
1892 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1893 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1894 defining SHELL to be "d:/unix/bash.exe". */
1895 if ((origin == o_file || origin == o_override)
1896 && strcmp (varname, "SHELL") == 0)
1897 {
1898 PATH_VAR (shellpath);
1899 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1900
1901 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1902 if (__dosexec_find_on_path (p, NULL, shellpath))
1903 {
1904 char *tp;
1905
1906 for (tp = shellpath; *tp; tp++)
1907 if (*tp == '\\')
1908 *tp = '/';
1909
1910 v = define_variable_loc (varname, varname_len,
1911 shellpath, origin, flavor == f_recursive,
1912 flocp);
1913 }
1914 else
1915 {
1916 const char *shellbase, *bslash;
1917 struct variable *pathv = lookup_variable ("PATH", 4);
1918 char *path_string;
1919 char *fake_env[2];
1920 size_t pathlen = 0;
1921
1922 shellbase = strrchr (p, '/');
1923 bslash = strrchr (p, '\\');
1924 if (!shellbase || bslash > shellbase)
1925 shellbase = bslash;
1926 if (!shellbase && p[1] == ':')
1927 shellbase = p + 1;
1928 if (shellbase)
1929 shellbase++;
1930 else
1931 shellbase = p;
1932
1933 /* Search for the basename of the shell (with standard
1934 executable extensions) along the $PATH. */
1935 if (pathv)
1936 pathlen = strlen (pathv->value);
1937 path_string = xmalloc (5 + pathlen + 2 + 1);
1938 /* On MSDOS, current directory is considered as part of $PATH. */
1939 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1940 fake_env[0] = path_string;
1941 fake_env[1] = 0;
1942 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1943 {
1944 char *tp;
1945
1946 for (tp = shellpath; *tp; tp++)
1947 if (*tp == '\\')
1948 *tp = '/';
1949
1950 v = define_variable_loc (varname, varname_len,
1951 shellpath, origin,
1952 flavor == f_recursive, flocp);
1953 }
1954 else
1955 v = lookup_variable (varname, varname_len);
1956
1957 free (path_string);
1958 }
1959 }
1960 else
1961#endif /* __MSDOS__ */
1962#ifdef WINDOWS32
1963 if ( varname_len == sizeof("SHELL") - 1 /* bird */
1964 && (origin == o_file || origin == o_override || origin == o_command)
1965 && streq (varname, "SHELL"))
1966 {
1967 extern char *default_shell;
1968
1969 /* Call shell locator function. If it returns TRUE, then
1970 set no_default_sh_exe to indicate sh was found and
1971 set new value for SHELL variable. */
1972
1973 if (find_and_set_default_shell (p))
1974 {
1975 v = define_variable_in_set (varname, varname_len, default_shell,
1976# ifdef CONFIG_WITH_VALUE_LENGTH
1977 ~0U, 1 /* duplicate_value */,
1978# endif
1979 origin, flavor == f_recursive,
1980 (target_var
1981 ? current_variable_set_list->set
1982 : NULL),
1983 flocp);
1984 no_default_sh_exe = 0;
1985 }
1986 else
1987 v = lookup_variable (varname, varname_len);
1988 }
1989 else
1990#endif
1991
1992 /* If we are defining variables inside an $(eval ...), we might have a
1993 different variable context pushed, not the global context (maybe we're
1994 inside a $(call ...) or something. Since this function is only ever
1995 invoked in places where we want to define globally visible variables,
1996 make sure we define this variable in the global set. */
1997
1998 v = define_variable_in_set (varname, varname_len, p,
1999#ifdef CONFIG_WITH_VALUE_LENGTH
2000 value_len, !alloc_value,
2001#endif
2002 origin, flavor == f_recursive,
2003#ifdef CONFIG_WITH_LOCAL_VARIABLES
2004 (target_var || origin == o_local
2005#else
2006 (target_var
2007#endif
2008 ? current_variable_set_list->set : NULL),
2009 flocp);
2010 v->append = append;
2011 v->conditional = conditional;
2012
2013#ifndef CONFIG_WITH_VALUE_LENGTH
2014 if (alloc_value)
2015 free (alloc_value);
2016#else
2017 if (free_value)
2018 free (free_value);
2019#endif
2020
2021 return v;
2022}
2023
2024
2025/* Try to interpret LINE (a null-terminated string) as a variable definition.
2026
2027 ORIGIN may be o_file, o_override, o_env, o_env_override,
2028 or o_command specifying that the variable definition comes
2029 from a makefile, an override directive, the environment with
2030 or without the -e switch, or the command line.
2031
2032 See the comments for parse_variable_definition().
2033
2034 If LINE was recognized as a variable definition, a pointer to its `struct
2035 variable' is returned. If LINE is not a variable definition, NULL is
2036 returned. */
2037
2038struct variable *
2039#ifndef CONFIG_WITH_VALUE_LENGTH
2040parse_variable_definition (struct variable *v, char *line)
2041#else
2042parse_variable_definition (struct variable *v, char *line, char *eos)
2043#endif
2044{
2045 register int c;
2046 register char *p = line;
2047 register char *beg;
2048 register char *end;
2049 enum variable_flavor flavor = f_bogus;
2050#ifndef CONFIG_WITH_VALUE_LENGTH
2051 char *name;
2052#endif
2053
2054 while (1)
2055 {
2056 c = *p++;
2057 if (c == '\0' || c == '#')
2058 return 0;
2059 if (c == '=')
2060 {
2061 end = p - 1;
2062 flavor = f_recursive;
2063 break;
2064 }
2065 else if (c == ':')
2066 if (*p == '=')
2067 {
2068 end = p++ - 1;
2069 flavor = f_simple;
2070 break;
2071 }
2072 else
2073 /* A colon other than := is a rule line, not a variable defn. */
2074 return 0;
2075 else if (c == '+' && *p == '=')
2076 {
2077 end = p++ - 1;
2078 flavor = f_append;
2079 break;
2080 }
2081#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2082 else if (c == '<' && *p == '=')
2083 {
2084 end = p++ - 1;
2085 flavor = f_prepend;
2086 break;
2087 }
2088#endif
2089 else if (c == '?' && *p == '=')
2090 {
2091 end = p++ - 1;
2092 flavor = f_conditional;
2093 break;
2094 }
2095 else if (c == '$')
2096 {
2097 /* This might begin a variable expansion reference. Make sure we
2098 don't misrecognize chars inside the reference as =, := or +=. */
2099 char closeparen;
2100 int count;
2101 c = *p++;
2102 if (c == '(')
2103 closeparen = ')';
2104 else if (c == '{')
2105 closeparen = '}';
2106 else
2107 continue; /* Nope. */
2108
2109 /* P now points past the opening paren or brace.
2110 Count parens or braces until it is matched. */
2111 count = 0;
2112 for (; *p != '\0'; ++p)
2113 {
2114 if (*p == c)
2115 ++count;
2116 else if (*p == closeparen && --count < 0)
2117 {
2118 ++p;
2119 break;
2120 }
2121 }
2122 }
2123 }
2124 v->flavor = flavor;
2125
2126 beg = next_token (line);
2127 while (end > beg && isblank ((unsigned char)end[-1]))
2128 --end;
2129 p = next_token (p);
2130 v->value = p;
2131#ifdef CONFIG_WITH_VALUE_LENGTH
2132 v->value_alloc_len = -1;
2133 v->value_length = eos != NULL ? eos - p : -1;
2134 assert (eos == NULL || strchr (p, '\0') == eos);
2135#endif
2136
2137 /* Expand the name, so "$(foo)bar = baz" works. */
2138#ifndef CONFIG_WITH_VALUE_LENGTH
2139 name = alloca (end - beg + 1);
2140 memcpy (name, beg, end - beg);
2141 name[end - beg] = '\0';
2142 v->name = allocated_variable_expand (name);
2143#else /* CONFIG_WITH_VALUE_LENGTH */
2144 v->name = allocated_variable_expand_2 (beg, end - beg, NULL);
2145#endif /* CONFIG_WITH_VALUE_LENGTH */
2146
2147 if (v->name[0] == '\0')
2148 fatal (&v->fileinfo, _("empty variable name"));
2149
2150 return v;
2151}
2152
2153
2154/* Try to interpret LINE (a null-terminated string) as a variable definition.
2155
2156 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
2157 or o_command specifying that the variable definition comes
2158 from a makefile, an override directive, the environment with
2159 or without the -e switch, or the command line.
2160
2161 See the comments for parse_variable_definition().
2162
2163 If LINE was recognized as a variable definition, a pointer to its `struct
2164 variable' is returned. If LINE is not a variable definition, NULL is
2165 returned. */
2166
2167struct variable *
2168#ifndef CONFIG_WITH_VALUE_LENGTH
2169try_variable_definition (const struct floc *flocp, char *line,
2170 enum variable_origin origin, int target_var)
2171#else
2172try_variable_definition (const struct floc *flocp, char *line, char *eos,
2173 enum variable_origin origin, int target_var)
2174#endif
2175{
2176 struct variable v;
2177 struct variable *vp;
2178
2179 if (flocp != 0)
2180 v.fileinfo = *flocp;
2181 else
2182 v.fileinfo.filenm = 0;
2183
2184#ifndef CONFIG_WITH_VALUE_LENGTH
2185 if (!parse_variable_definition (&v, line))
2186 return 0;
2187
2188 vp = do_variable_definition (flocp, v.name, v.value,
2189 origin, v.flavor, target_var);
2190#else
2191 if (!parse_variable_definition (&v, line, eos))
2192 return 0;
2193
2194 vp = do_variable_definition_2 (flocp, v.name, v.value,
2195 v.value_length != -1 ? v.value_length : ~0U,
2196 0, NULL, origin, v.flavor, target_var);
2197#endif
2198
2199 free (v.name);
2200
2201 return vp;
2202}
2203
2204
2205/* Print information for variable V, prefixing it with PREFIX. */
2206
2207static void
2208print_variable (const void *item, void *arg)
2209{
2210 const struct variable *v = item;
2211 const char *prefix = arg;
2212 const char *origin;
2213
2214 switch (v->origin)
2215 {
2216 case o_default:
2217 origin = _("default");
2218 break;
2219 case o_env:
2220 origin = _("environment");
2221 break;
2222 case o_file:
2223 origin = _("makefile");
2224 break;
2225 case o_env_override:
2226 origin = _("environment under -e");
2227 break;
2228 case o_command:
2229 origin = _("command line");
2230 break;
2231 case o_override:
2232 origin = _("`override' directive");
2233 break;
2234 case o_automatic:
2235 origin = _("automatic");
2236 break;
2237#ifdef CONFIG_WITH_LOCAL_VARIABLES
2238 case o_local:
2239 origin = _("`local' directive");
2240 break;
2241#endif
2242 case o_invalid:
2243 default:
2244 abort ();
2245 }
2246 fputs ("# ", stdout);
2247 fputs (origin, stdout);
2248 if (v->fileinfo.filenm)
2249 printf (_(" (from `%s', line %lu)"),
2250 v->fileinfo.filenm, v->fileinfo.lineno);
2251 putchar ('\n');
2252 fputs (prefix, stdout);
2253
2254 /* Is this a `define'? */
2255 if (v->recursive && strchr (v->value, '\n') != 0)
2256 printf ("define %s\n%s\nendef\n", v->name, v->value);
2257 else
2258 {
2259 register char *p;
2260
2261 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
2262
2263 /* Check if the value is just whitespace. */
2264 p = next_token (v->value);
2265 if (p != v->value && *p == '\0')
2266 /* All whitespace. */
2267 printf ("$(subst ,,%s)", v->value);
2268 else if (v->recursive)
2269 fputs (v->value, stdout);
2270 else
2271 /* Double up dollar signs. */
2272 for (p = v->value; *p != '\0'; ++p)
2273 {
2274 if (*p == '$')
2275 putchar ('$');
2276 putchar (*p);
2277 }
2278 putchar ('\n');
2279 }
2280}
2281
2282
2283/* Print all the variables in SET. PREFIX is printed before
2284 the actual variable definitions (everything else is comments). */
2285
2286void
2287print_variable_set (struct variable_set *set, char *prefix)
2288{
2289 hash_map_arg (&set->table, print_variable, prefix);
2290
2291 fputs (_("# variable set hash-table stats:\n"), stdout);
2292 fputs ("# ", stdout);
2293 hash_print_stats (&set->table, stdout);
2294 putc ('\n', stdout);
2295}
2296
2297/* Print the data base of variables. */
2298
2299void
2300print_variable_data_base (void)
2301{
2302 puts (_("\n# Variables\n"));
2303
2304 print_variable_set (&global_variable_set, "");
2305
2306 puts (_("\n# Pattern-specific Variable Values"));
2307
2308 {
2309 struct pattern_var *p;
2310 int rules = 0;
2311
2312 for (p = pattern_vars; p != 0; p = p->next)
2313 {
2314 ++rules;
2315 printf ("\n%s :\n", p->target);
2316 print_variable (&p->variable, "# ");
2317 }
2318
2319 if (rules == 0)
2320 puts (_("\n# No pattern-specific variable values."));
2321 else
2322 printf (_("\n# %u pattern-specific variable values"), rules);
2323 }
2324}
2325
2326
2327/* Print all the local variables of FILE. */
2328
2329void
2330print_file_variables (const struct file *file)
2331{
2332 if (file->variables != 0)
2333 print_variable_set (file->variables->set, "# ");
2334}
2335
2336#ifdef WINDOWS32
2337void
2338sync_Path_environment (void)
2339{
2340 char *path = allocated_variable_expand ("$(PATH)");
2341 static char *environ_path = NULL;
2342
2343 if (!path)
2344 return;
2345
2346 /*
2347 * If done this before, don't leak memory unnecessarily.
2348 * Free the previous entry before allocating new one.
2349 */
2350 if (environ_path)
2351 free (environ_path);
2352
2353 /*
2354 * Create something WINDOWS32 world can grok
2355 */
2356 convert_Path_to_windows32 (path, ';');
2357 environ_path = xstrdup (concat ("PATH", "=", path));
2358 putenv (environ_path);
2359 free (path);
2360}
2361#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette