VirtualBox

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

Last change on this file since 1527 was 1503, checked in by bird, 17 years ago

Environment / globals cleanup. The new policy is to use KBUILD_ and KMK_ prefixes for things that is put into or/and picked from the environment. This will take a good while to complete.

Renamed (old still valid for some versions):
PATH_KBUILD -> KBUILD_PATH
PATH_KBUILD_BIN -> KBUILD_BIN_PATH
BUILD_PLATFORM -> KBUILD_HOST
BUILD_PLATFORM_ARCH -> KBUILD_HOST_ARCH
BUILD_PLATFORM_CPU -> KBUILD_HOST_CPU

Renamed - old not checked:
MAKELEVEL -> KMK_LEVEL
KMKFLAGS -> KMK_FLAGS
MAKEFLAGS -> KMK_FLAGS
MAKEOVERRIDES -> KMK_OVERRIDES

Removed:
MAKE_VERSION
MFLAGS

  • Property svn:eol-style set to native
File size: 69.6 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? */
424# define VARIABLE_BUCKETS 16384
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 64
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 32
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_length, int duplicate_value,
470 enum variable_origin origin, int recursive,
471 struct variable_set *set, const struct floc *flocp)
472#else
473struct variable *
474define_variable_in_set (const char *name, unsigned int length,
475 const char *value, enum variable_origin origin,
476 int recursive, struct variable_set *set,
477 const struct floc *flocp)
478#endif
479{
480 struct variable *v;
481 struct variable **var_slot;
482 struct variable var_key;
483
484 if (set == NULL)
485 set = &global_variable_set;
486
487 var_key.name = (char *) name;
488 var_key.length = length;
489#ifdef VARIABLE_HASH /* bird */
490 var_key.hash1 = variable_hash_1i (name, length);
491 var_key.hash2 = 0;
492#endif
493 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
494
495 if (env_overrides && origin == o_env)
496 origin = o_env_override;
497
498 v = *var_slot;
499 if (! HASH_VACANT (v))
500 {
501 if (env_overrides && v->origin == o_env)
502 /* V came from in the environment. Since it was defined
503 before the switches were parsed, it wasn't affected by -e. */
504 v->origin = o_env_override;
505
506 /* A variable of this name is already defined.
507 If the old definition is from a stronger source
508 than this one, don't redefine it. */
509 if ((int) origin >= (int) v->origin)
510 {
511#ifdef CONFIG_WITH_VALUE_LENGTH
512 if (value_length == ~0U)
513 value_length = strlen (value);
514 else
515 assert (value_length == strlen (value));
516 if (!duplicate_value)
517 {
518 if (v->value != 0)
519 free (v->value);
520 v->value = (char *)value;
521 v->value_alloc_len = value_length + 1;
522 }
523 else
524 {
525 if ((unsigned int)v->value_alloc_len <= value_length)
526 {
527 free (v->value);
528 v->value_alloc_len = (value_length + 0x40) & ~0x3f;
529 v->value = xmalloc (v->value_alloc_len);
530 }
531 memcpy (v->value, value, value_length + 1);
532 }
533 v->value_length = value_length;
534#else
535 if (v->value != 0)
536 free (v->value);
537 v->value = xstrdup (value);
538#endif
539 if (flocp != 0)
540 v->fileinfo = *flocp;
541 else
542 v->fileinfo.filenm = 0;
543 v->origin = origin;
544 v->recursive = recursive;
545 }
546 return v;
547 }
548
549 /* Create a new variable definition and add it to the hash table. */
550
551 v = xmalloc (sizeof (struct variable));
552 v->name = savestring (name, length);
553 v->length = length;
554#ifdef VARIABLE_HASH /* bird */
555 v->hash1 = variable_hash_1i (name, length);
556 v->hash2 = 0;
557#endif
558 hash_insert_at (&set->table, v, var_slot);
559#ifdef CONFIG_WITH_VALUE_LENGTH
560 if (value_length == ~0U)
561 value_length = strlen (value);
562 else
563 assert (value_length == strlen (value));
564 v->value_length = value_length;
565 if (!duplicate_value)
566 {
567 v->value_alloc_len = value_length + 1;
568 v->value = (char *)value;
569 }
570 else
571 {
572 v->value_alloc_len = (value_length + 32) & ~31;
573 v->value = xmalloc (v->value_alloc_len);
574 memcpy (v->value, value, value_length + 1);
575 }
576#else
577 v->value = xstrdup (value);
578#endif
579 if (flocp != 0)
580 v->fileinfo = *flocp;
581 else
582 v->fileinfo.filenm = 0;
583 v->origin = origin;
584 v->recursive = recursive;
585 v->special = 0;
586 v->expanding = 0;
587 v->exp_count = 0;
588 v->per_target = 0;
589 v->append = 0;
590 v->export = v_default;
591
592 v->exportable = 1;
593 if (*name != '_' && (*name < 'A' || *name > 'Z')
594 && (*name < 'a' || *name > 'z'))
595 v->exportable = 0;
596 else
597 {
598 for (++name; *name != '\0'; ++name)
599 if (*name != '_' && (*name < 'a' || *name > 'z')
600 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
601 break;
602
603 if (*name != '\0')
604 v->exportable = 0;
605 }
606
607 return v;
608}
609
610
611/* If the variable passed in is "special", handle its special nature.
612 Currently there are two such variables, both used for introspection:
613 .VARIABLES expands to a list of all the variables defined in this instance
614 of make.
615 .TARGETS expands to a list of all the targets defined in this
616 instance of make.
617 Returns the variable reference passed in. */
618
619#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
620
621static struct variable *
622handle_special_var (struct variable *var)
623{
624 static unsigned long last_var_count = 0;
625
626
627 /* This one actually turns out to be very hard, due to the way the parser
628 records targets. The way it works is that target information is collected
629 internally until make knows the target is completely specified. It unitl
630 it sees that some new construct (a new target or variable) is defined that
631 it knows the previous one is done. In short, this means that if you do
632 this:
633
634 all:
635
636 TARGS := $(.TARGETS)
637
638 then $(TARGS) won't contain "all", because it's not until after the
639 variable is created that the previous target is completed.
640
641 Changing this would be a major pain. I think a less complex way to do it
642 would be to pre-define the target files as soon as the first line is
643 parsed, then come back and do the rest of the definition as now. That
644 would allow $(.TARGETS) to be correct without a major change to the way
645 the parser works.
646
647 if (streq (var->name, ".TARGETS"))
648 var->value = build_target_list (var->value);
649 else
650 */
651
652 if (streq (var->name, ".VARIABLES")
653 && global_variable_set.table.ht_fill != last_var_count)
654 {
655 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
656 unsigned long len;
657 char *p;
658 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
659 struct variable **end = &vp[global_variable_set.table.ht_size];
660
661 /* Make sure we have at least MAX bytes in the allocated buffer. */
662 var->value = xrealloc (var->value, max);
663
664 /* Walk through the hash of variables, constructing a list of names. */
665 p = var->value;
666 len = 0;
667 for (; vp < end; ++vp)
668 if (!HASH_VACANT (*vp))
669 {
670 struct variable *v = *vp;
671 int l = v->length;
672
673 len += l + 1;
674 if (len > max)
675 {
676 unsigned long off = p - var->value;
677
678 max += EXPANSION_INCREMENT (l + 1);
679 var->value = xrealloc (var->value, max);
680 p = &var->value[off];
681 }
682
683 memcpy (p, v->name, l);
684 p += l;
685 *(p++) = ' ';
686 }
687 *(p-1) = '\0';
688
689 /* Remember how many variables are in our current count. Since we never
690 remove variables from the list, this is a reliable way to know whether
691 the list is up to date or needs to be recomputed. */
692
693 last_var_count = global_variable_set.table.ht_fill;
694 }
695
696 return var;
697}
698
699
700
701/* Lookup a variable whose name is a string starting at NAME
702 and with LENGTH chars. NAME need not be null-terminated.
703 Returns address of the `struct variable' containing all info
704 on the variable, or nil if no such variable is defined. */
705
706struct variable *
707lookup_variable (const char *name, unsigned int length)
708{
709 const struct variable_set_list *setlist;
710 struct variable var_key;
711
712 var_key.name = (char *) name;
713 var_key.length = length;
714#ifdef VARIABLE_HASH /* bird */
715 var_key.hash1 = variable_hash_1i (name, length);
716 var_key.hash2 = 0;
717#endif
718
719 for (setlist = current_variable_set_list;
720 setlist != 0; setlist = setlist->next)
721 {
722#ifdef VARIABLE_HASH /* bird: speed */
723 struct hash_table *ht = &setlist->set->table;
724 unsigned int hash_1 = var_key.hash1;
725 struct variable *v;
726
727 ht->ht_lookups++;
728 for (;;)
729 {
730 hash_1 &= (ht->ht_size - 1);
731 v = (struct variable *)ht->ht_vec[hash_1];
732
733 if (v == 0)
734 break;
735 if ((void *)v != hash_deleted_item)
736 {
737 if (variable_hash_cmp(&var_key, v) == 0)
738 {
739# ifdef VARIABLE_HASH_STRICT /* bird */
740 struct variable *v2 = (struct variable *) hash_find_item ((struct hash_table *) &setlist->set->table, &var_key);
741 assert(v2 == v);
742# endif
743 return v->special ? handle_special_var (v) : v;
744 }
745 ht->ht_collisions++;
746 }
747 if (!var_key.hash2)
748 var_key.hash2 = variable_hash_2i(name, length);
749 hash_1 += (var_key.hash2 | 1);
750 }
751
752#else /* !VARIABLE_HASH */
753 const struct variable_set *set = setlist->set;
754 struct variable *v;
755
756 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
757 if (v)
758 return v->special ? handle_special_var (v) : v;
759#endif /* !VARIABLE_HASH */
760 }
761
762#ifdef VMS
763 /* since we don't read envp[] on startup, try to get the
764 variable via getenv() here. */
765 {
766 char *vname = alloca (length + 1);
767 char *value;
768 strncpy (vname, name, length);
769 vname[length] = 0;
770 value = getenv (vname);
771 if (value != 0)
772 {
773 char *sptr;
774 int scnt;
775
776 sptr = value;
777 scnt = 0;
778
779 while ((sptr = strchr (sptr, '$')))
780 {
781 scnt++;
782 sptr++;
783 }
784
785 if (scnt > 0)
786 {
787 char *nvalue;
788 char *nptr;
789
790 nvalue = alloca (strlen (value) + scnt + 1);
791 sptr = value;
792 nptr = nvalue;
793
794 while (*sptr)
795 {
796 if (*sptr == '$')
797 {
798 *nptr++ = '$';
799 *nptr++ = '$';
800 }
801 else
802 {
803 *nptr++ = *sptr;
804 }
805 sptr++;
806 }
807
808 *nptr = '\0';
809 return define_variable (vname, length, nvalue, o_env, 1);
810
811 }
812
813 return define_variable (vname, length, value, o_env, 1);
814 }
815 }
816#endif /* VMS */
817
818 return 0;
819}
820
821
822/* Lookup a variable whose name is a string starting at NAME
823 and with LENGTH chars in set SET. NAME need not be null-terminated.
824 Returns address of the `struct variable' containing all info
825 on the variable, or nil if no such variable is defined. */
826
827struct variable *
828lookup_variable_in_set (const char *name, unsigned int length,
829 const struct variable_set *set)
830{
831 struct variable var_key;
832
833 var_key.name = (char *) name;
834 var_key.length = length;
835#ifdef VARIABLE_HASH /* bird */
836 var_key.hash1 = variable_hash_1i (name, length);
837 var_key.hash2 = 0;
838#endif
839
840 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
841}
842
843
844/* Initialize FILE's variable set list. If FILE already has a variable set
845 list, the topmost variable set is left intact, but the the rest of the
846 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
847 rule, then we will use the "root" double-colon target's variable set as the
848 parent of FILE's variable set.
849
850 If we're READING a makefile, don't do the pattern variable search now,
851 since the pattern variable might not have been defined yet. */
852
853void
854initialize_file_variables (struct file *file, int reading)
855{
856 struct variable_set_list *l = file->variables;
857
858 if (l == 0)
859 {
860 l = (struct variable_set_list *)
861 xmalloc (sizeof (struct variable_set_list));
862 l->set = xmalloc (sizeof (struct variable_set));
863 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
864 variable_hash_1, variable_hash_2, variable_hash_cmp);
865 file->variables = l;
866 }
867
868 /* If this is a double-colon, then our "parent" is the "root" target for
869 this double-colon rule. Since that rule has the same name, parent,
870 etc. we can just use its variables as the "next" for ours. */
871
872 if (file->double_colon && file->double_colon != file)
873 {
874 initialize_file_variables (file->double_colon, reading);
875 l->next = file->double_colon->variables;
876 return;
877 }
878
879 if (file->parent == 0)
880 l->next = &global_setlist;
881 else
882 {
883 initialize_file_variables (file->parent, reading);
884 l->next = file->parent->variables;
885 }
886
887 /* If we're not reading makefiles and we haven't looked yet, see if
888 we can find pattern variables for this target. */
889
890 if (!reading && !file->pat_searched)
891 {
892 struct pattern_var *p;
893
894 p = lookup_pattern_var (0, file->name);
895 if (p != 0)
896 {
897 struct variable_set_list *global = current_variable_set_list;
898
899 /* We found at least one. Set up a new variable set to accumulate
900 all the pattern variables that match this target. */
901
902 file->pat_variables = create_new_variable_set ();
903 current_variable_set_list = file->pat_variables;
904
905 do
906 {
907 /* We found one, so insert it into the set. */
908
909 struct variable *v;
910
911 if (p->variable.flavor == f_simple)
912 {
913 v = define_variable_loc (
914 p->variable.name, strlen (p->variable.name),
915 p->variable.value, p->variable.origin,
916 0, &p->variable.fileinfo);
917
918 v->flavor = f_simple;
919 }
920 else
921 {
922 v = do_variable_definition (
923 &p->variable.fileinfo, p->variable.name,
924 p->variable.value, p->variable.origin,
925 p->variable.flavor, 1);
926 }
927
928 /* Also mark it as a per-target and copy export status. */
929 v->per_target = p->variable.per_target;
930 v->export = p->variable.export;
931 }
932 while ((p = lookup_pattern_var (p, file->name)) != 0);
933
934 current_variable_set_list = global;
935 }
936 file->pat_searched = 1;
937 }
938
939 /* If we have a pattern variable match, set it up. */
940
941 if (file->pat_variables != 0)
942 {
943 file->pat_variables->next = l->next;
944 l->next = file->pat_variables;
945 }
946}
947
948
949/* Pop the top set off the current variable set list,
950 and free all its storage. */
951
952struct variable_set_list *
953create_new_variable_set (void)
954{
955 register struct variable_set_list *setlist;
956 register struct variable_set *set;
957
958 set = xmalloc (sizeof (struct variable_set));
959 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
960 variable_hash_1, variable_hash_2, variable_hash_cmp);
961
962 setlist = (struct variable_set_list *)
963 xmalloc (sizeof (struct variable_set_list));
964 setlist->set = set;
965 setlist->next = current_variable_set_list;
966
967 return setlist;
968}
969
970static void
971free_variable_name_and_value (const void *item)
972{
973 struct variable *v = (struct variable *) item;
974 free (v->name);
975 free (v->value);
976}
977
978void
979free_variable_set (struct variable_set_list *list)
980{
981 hash_map (&list->set->table, free_variable_name_and_value);
982 hash_free (&list->set->table, 1);
983 free (list->set);
984 free (list);
985}
986
987/* Create a new variable set and push it on the current setlist.
988 If we're pushing a global scope (that is, the current scope is the global
989 scope) then we need to "push" it the other way: file variable sets point
990 directly to the global_setlist so we need to replace that with the new one.
991 */
992
993struct variable_set_list *
994push_new_variable_scope (void)
995{
996 current_variable_set_list = create_new_variable_set();
997 if (current_variable_set_list->next == &global_setlist)
998 {
999 /* It was the global, so instead of new -> &global we want to replace
1000 &global with the new one and have &global -> new, with current still
1001 pointing to &global */
1002 struct variable_set *set = current_variable_set_list->set;
1003 current_variable_set_list->set = global_setlist.set;
1004 global_setlist.set = set;
1005 current_variable_set_list->next = global_setlist.next;
1006 global_setlist.next = current_variable_set_list;
1007 current_variable_set_list = &global_setlist;
1008 }
1009 return (current_variable_set_list);
1010}
1011
1012void
1013pop_variable_scope (void)
1014{
1015 struct variable_set_list *setlist;
1016 struct variable_set *set;
1017
1018 /* Can't call this if there's no scope to pop! */
1019 assert(current_variable_set_list->next != NULL);
1020
1021 if (current_variable_set_list != &global_setlist)
1022 {
1023 /* We're not pointing to the global setlist, so pop this one. */
1024 setlist = current_variable_set_list;
1025 set = setlist->set;
1026 current_variable_set_list = setlist->next;
1027 }
1028 else
1029 {
1030 /* This set is the one in the global_setlist, but there is another global
1031 set beyond that. We want to copy that set to global_setlist, then
1032 delete what used to be in global_setlist. */
1033 setlist = global_setlist.next;
1034 set = global_setlist.set;
1035 global_setlist.set = setlist->set;
1036 global_setlist.next = setlist->next;
1037 }
1038
1039 /* Free the one we no longer need. */
1040 free (setlist);
1041 hash_map (&set->table, free_variable_name_and_value);
1042 hash_free (&set->table, 1);
1043 free (set);
1044}
1045
1046
1047/* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
1048
1049static void
1050merge_variable_sets (struct variable_set *to_set,
1051 struct variable_set *from_set)
1052{
1053 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
1054 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
1055
1056 for ( ; from_var_slot < from_var_end; from_var_slot++)
1057 if (! HASH_VACANT (*from_var_slot))
1058 {
1059 struct variable *from_var = *from_var_slot;
1060 struct variable **to_var_slot
1061 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
1062 if (HASH_VACANT (*to_var_slot))
1063 hash_insert_at (&to_set->table, from_var, to_var_slot);
1064 else
1065 {
1066 /* GKM FIXME: delete in from_set->table */
1067 free (from_var->value);
1068 free (from_var);
1069 }
1070 }
1071}
1072
1073/* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
1074
1075void
1076merge_variable_set_lists (struct variable_set_list **setlist0,
1077 struct variable_set_list *setlist1)
1078{
1079 struct variable_set_list *to = *setlist0;
1080 struct variable_set_list *last0 = 0;
1081
1082 /* If there's nothing to merge, stop now. */
1083 if (!setlist1)
1084 return;
1085
1086 /* This loop relies on the fact that all setlists terminate with the global
1087 setlist (before NULL). If that's not true, arguably we SHOULD die. */
1088 if (to)
1089 while (setlist1 != &global_setlist && to != &global_setlist)
1090 {
1091 struct variable_set_list *from = setlist1;
1092 setlist1 = setlist1->next;
1093
1094 merge_variable_sets (to->set, from->set);
1095
1096 last0 = to;
1097 to = to->next;
1098 }
1099
1100 if (setlist1 != &global_setlist)
1101 {
1102 if (last0 == 0)
1103 *setlist0 = setlist1;
1104 else
1105 last0->next = setlist1;
1106 }
1107}
1108
1109
1110/* Define the automatic variables, and record the addresses
1111 of their structures so we can change their values quickly. */
1112
1113void
1114define_automatic_variables (void)
1115{
1116#if defined(WINDOWS32) || defined(__EMX__)
1117 extern char* default_shell;
1118#else
1119 extern char default_shell[];
1120#endif
1121 register struct variable *v;
1122#ifndef KMK
1123 char buf[200];
1124#else
1125 char buf[1024];
1126 const char *val;
1127 struct variable *envvar1;
1128 struct variable *envvar2;
1129#endif
1130
1131 sprintf (buf, "%u", makelevel);
1132 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
1133
1134 sprintf (buf, "%s%s%s",
1135 version_string,
1136 (remote_description == 0 || remote_description[0] == '\0')
1137 ? "" : "-",
1138 (remote_description == 0 || remote_description[0] == '\0')
1139 ? "" : remote_description);
1140#ifndef KMK
1141 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
1142#else /* KMK */
1143
1144 /* Define KMK_VERSION to indicate kMk. */
1145 (void) define_variable ("KMK_VERSION", 11, buf, o_default, 0);
1146
1147 /* Define KBUILD_VERSION* */
1148 sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
1149 define_variable ("KBUILD_VERSION_MAJOR", sizeof ("KBUILD_VERSION_MAJOR") - 1,
1150 buf, o_default, 0);
1151 sprintf (buf, "%d", KBUILD_VERSION_MINOR);
1152 define_variable ("KBUILD_VERSION_MINOR", sizeof("KBUILD_VERSION_MINOR") - 1,
1153 buf, o_default, 0);
1154 sprintf (buf, "%d", KBUILD_VERSION_PATCH);
1155 define_variable ("KBUILD_VERSION_PATCH", sizeof ("KBUILD_VERSION_PATCH") - 1,
1156 buf, o_default, 0);
1157
1158 sprintf (buf, "%d.%d.%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH);
1159 define_variable ("KBUILD_VERSION", sizeof ("KBUILD_VERSION") - 1,
1160 buf, o_default, 0);
1161
1162 /* The host defaults. The BUILD_* stuff will be replaced by KBUILD_* soon. */
1163 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST"));
1164 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM"));
1165 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST;
1166 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1167 error (NULL, _("KBUILD_HOST and BUILD_PLATFORM differs, using KBUILD_HOST=%s."), val);
1168 if (!envvar1)
1169 define_variable ("KBUILD_HOST", sizeof ("KBUILD_HOST") - 1,
1170 val, o_default, 0);
1171 if (!envvar2)
1172 define_variable ("BUILD_PLATFORM", sizeof ("BUILD_PLATFORM") - 1,
1173 val, o_default, 0);
1174
1175 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_ARCH"));
1176 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_ARCH"));
1177 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_ARCH;
1178 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1179 error (NULL, _("KBUILD_HOST_ARCH and BUILD_PLATFORM_ARCH differs, using KBUILD_HOST_ARCH=%s."), val);
1180 if (!envvar1)
1181 define_variable ("KBUILD_HOST_ARCH", sizeof ("KBUILD_HOST_ARCH") - 1,
1182 val, o_default, 0);
1183 if (!envvar2)
1184 define_variable ("BUILD_PLATFORM_ARCH", sizeof ("BUILD_PLATFORM_ARCH") - 1,
1185 val, o_default, 0);
1186
1187 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_CPU"));
1188 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_CPU"));
1189 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_CPU;
1190 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1191 error (NULL, _("KBUILD_HOST_CPU and BUILD_PLATFORM_CPU differs, using KBUILD_HOST_CPU=%s."), val);
1192 if (!envvar1)
1193 define_variable ("KBUILD_HOST_CPU", sizeof ("KBUILD_HOST_CPU") - 1,
1194 val, o_default, 0);
1195 if (!envvar2)
1196 define_variable ("BUILD_PLATFORM_CPU", sizeof ("BUILD_PLATFORM_CPU") - 1,
1197 val, o_default, 0);
1198
1199 /* The kBuild locations. */
1200 define_variable ("KBUILD_PATH", sizeof ("KBUILD_PATH") - 1,
1201 get_kbuild_path (), o_default, 0);
1202 define_variable ("KBUILD_BIN_PATH", sizeof ("KBUILD_BIN_PATH") - 1,
1203 get_kbuild_bin_path (), o_default, 0);
1204
1205 define_variable ("PATH_KBUILD", sizeof ("PATH_KBUILD") - 1,
1206 get_kbuild_path (), o_default, 0);
1207 define_variable ("PATH_KBUILD_BIN", sizeof ("PATH_KBUILD_BIN") - 1,
1208 get_kbuild_bin_path (), o_default, 0);
1209
1210 /* Define KMK_FEATURES to indicate various working KMK features. */
1211# if defined (CONFIG_WITH_RSORT) \
1212 && defined (CONFIG_WITH_ABSPATHEX) \
1213 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1214 && defined (CONFIG_WITH_VALUE_LENGTH) && defined (CONFIG_WITH_COMPARE) \
1215 && defined (CONFIG_WITH_STACK) \
1216 && defined (CONFIG_WITH_MATH) \
1217 && defined (CONFIG_WITH_XARGS) \
1218 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1219 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1220 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1221 && defined (CONFIG_WITH_DATE) \
1222 && defined (CONFIG_WITH_FILE_SIZE) \
1223 && defined (CONFIG_WITH_WHICH) \
1224 && defined (CONFIG_WITH_EVALPLUS) \
1225 && defined (CONFIG_WITH_MAKE_STATS) \
1226 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1227 && defined (KMK_HELPERS)
1228 (void) define_variable ("KMK_FEATURES", 12,
1229 "append-dash-n abspath"
1230 " rsort"
1231 " abspathex"
1232 " toupper tolower"
1233 " comp-vars comp-cmds comp-cmds-ex"
1234 " stack"
1235 " math-int"
1236 " xargs"
1237 " explicit-multitarget"
1238 " prepend-assignment"
1239 " set-conditionals"
1240 " date"
1241 " file-size"
1242 " which"
1243 " evalctx evalval evalvalctx evalcall evalcall2"
1244 " make-stats"
1245 " commands"
1246 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one "
1247 , o_default, 0);
1248# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1249# error "All features should be enabled by default!"
1250 strcpy (buf, "append-dash-n abspath");
1251# if defined (CONFIG_WITH_RSORT)
1252 strcat (buf, " rsort");
1253# endif
1254# if defined (CONFIG_WITH_ABSPATHEX)
1255 strcat (buf, " abspathex");
1256# endif
1257# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1258 strcat (buf, " toupper tolower");
1259# endif
1260# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1261 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1262# endif
1263# if defined (CONFIG_WITH_STACK)
1264 strcat (buf, " stack");
1265# endif
1266# if defined (CONFIG_WITH_MATH)
1267 strcat (buf, " math-int");
1268# endif
1269# if defined (CONFIG_WITH_XARGS)
1270 strcat (buf, " xargs");
1271# endif
1272# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1273 strcat (buf, " explicit-multitarget");
1274# endif
1275# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1276 strcat (buf, " prepend-assignment");
1277# endif
1278# if defined (CONFIG_WITH_SET_CONDITIONALS)
1279 strcat (buf, " set-conditionals");
1280# endif
1281# if defined (CONFIG_WITH_DATE)
1282 strcat (buf, " date");
1283# endif
1284# if defined (CONFIG_WITH_FILE_SIZE)
1285 strcat (buf, " file-size");
1286# endif
1287# if defined (CONFIG_WITH_WHICH)
1288 strcat (buf, " which");
1289# endif
1290# if defined (CONFIG_WITH_EVALPLUS)
1291 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2");
1292# endif
1293# if defined (CONFIG_WITH_MAKE_STATS)
1294 strcat (buf, " make-stats");
1295# endif
1296# if defined (CONFIG_WITH_COMMANDS_FUNC)
1297 strcat (buf, " commands");
1298# endif
1299# if defined (KMK_HELPERS)
1300 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one");
1301# endif
1302 (void) define_variable ("KMK_FEATURES", 12, buf, o_default, 0);
1303# endif
1304
1305#endif /* KMK */
1306
1307#ifdef CONFIG_WITH_KMK_BUILTIN
1308 /* The supported kMk Builtin commands. */
1309 (void) define_variable ("KMK_BUILTIN", 11, "append cat cp cmp echo install kDepIDB ln md5sum mkdir mv printf rm rmdir test", o_default, 0);
1310#endif
1311
1312#ifdef __MSDOS__
1313 /* Allow to specify a special shell just for Make,
1314 and use $COMSPEC as the default $SHELL when appropriate. */
1315 {
1316 static char shell_str[] = "SHELL";
1317 const int shlen = sizeof (shell_str) - 1;
1318 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1319 struct variable *comp = lookup_variable ("COMSPEC", 7);
1320
1321 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
1322 if (mshp)
1323 (void) define_variable (shell_str, shlen,
1324 mshp->value, o_env_override, 0);
1325 else if (comp)
1326 {
1327 /* $COMSPEC shouldn't override $SHELL. */
1328 struct variable *shp = lookup_variable (shell_str, shlen);
1329
1330 if (!shp)
1331 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1332 }
1333 }
1334#elif defined(__EMX__)
1335 {
1336 static char shell_str[] = "SHELL";
1337 const int shlen = sizeof (shell_str) - 1;
1338 struct variable *shell = lookup_variable (shell_str, shlen);
1339 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1340
1341 /* if $MAKESHELL is defined in the environment assume o_env_override */
1342 if (replace && *replace->value && replace->origin == o_env)
1343 replace->origin = o_env_override;
1344
1345 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1346 did not come from the environment */
1347 if (!replace || !*replace->value)
1348 if (shell && *shell->value && (shell->origin == o_env
1349 || shell->origin == o_env_override))
1350 {
1351 /* overwrite whatever we got from the environment */
1352 free(shell->value);
1353 shell->value = xstrdup (default_shell);
1354 shell->origin = o_default;
1355 }
1356
1357 /* Some people do not like cmd to be used as the default
1358 if $SHELL is not defined in the Makefile.
1359 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1360# ifndef NO_CMD_DEFAULT
1361 /* otherwise use $COMSPEC */
1362 if (!replace || !*replace->value)
1363 replace = lookup_variable ("COMSPEC", 7);
1364
1365 /* otherwise use $OS2_SHELL */
1366 if (!replace || !*replace->value)
1367 replace = lookup_variable ("OS2_SHELL", 9);
1368# else
1369# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1370# endif
1371
1372 if (replace && *replace->value)
1373 /* overwrite $SHELL */
1374 (void) define_variable (shell_str, shlen, replace->value,
1375 replace->origin, 0);
1376 else
1377 /* provide a definition if there is none */
1378 (void) define_variable (shell_str, shlen, default_shell,
1379 o_default, 0);
1380 }
1381
1382#endif
1383
1384 /* This won't override any definition, but it will provide one if there
1385 isn't one there. */
1386 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
1387
1388 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1389 environment variable on MSDOS, so whoever sets it, does that on purpose.
1390 On OS/2 we do not use SHELL from environment but we have already handled
1391 that problem above. */
1392#if !defined(__MSDOS__) && !defined(__EMX__)
1393 /* Don't let SHELL come from the environment. */
1394 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1395 {
1396 free (v->value);
1397 v->origin = o_file;
1398 v->value = xstrdup (default_shell);
1399#ifdef CONFIG_WITH_VALUE_LENGTH
1400 v->value_length = strlen (v->value);
1401 v->value_alloc_len = v->value_length + 1;
1402#endif
1403 }
1404#endif
1405
1406 /* Make sure MAKEFILES gets exported if it is set. */
1407 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
1408 v->export = v_ifset;
1409
1410 /* Define the magic D and F variables in terms of
1411 the automatic variables they are variations of. */
1412
1413#ifdef VMS
1414 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
1415 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
1416 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
1417 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
1418 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
1419 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
1420 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
1421#else
1422 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
1423 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
1424 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
1425 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
1426 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
1427 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1428 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1429#endif
1430 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
1431 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
1432 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
1433 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
1434 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
1435 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
1436 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
1437}
1438
1439
1440int export_all_variables;
1441
1442/* Create a new environment for FILE's commands.
1443 If FILE is nil, this is for the `shell' function.
1444 The child's MAKELEVEL variable is incremented. */
1445
1446char **
1447target_environment (struct file *file)
1448{
1449 struct variable_set_list *set_list;
1450 register struct variable_set_list *s;
1451 struct hash_table table;
1452 struct variable **v_slot;
1453 struct variable **v_end;
1454 struct variable makelevel_key;
1455 char **result_0;
1456 char **result;
1457
1458 if (file == 0)
1459 set_list = current_variable_set_list;
1460 else
1461 set_list = file->variables;
1462
1463 hash_init (&table, VARIABLE_BUCKETS,
1464 variable_hash_1, variable_hash_2, variable_hash_cmp);
1465
1466 /* Run through all the variable sets in the list,
1467 accumulating variables in TABLE. */
1468 for (s = set_list; s != 0; s = s->next)
1469 {
1470 struct variable_set *set = s->set;
1471 v_slot = (struct variable **) set->table.ht_vec;
1472 v_end = v_slot + set->table.ht_size;
1473 for ( ; v_slot < v_end; v_slot++)
1474 if (! HASH_VACANT (*v_slot))
1475 {
1476 struct variable **new_slot;
1477 struct variable *v = *v_slot;
1478
1479 /* If this is a per-target variable and it hasn't been touched
1480 already then look up the global version and take its export
1481 value. */
1482 if (v->per_target && v->export == v_default)
1483 {
1484 struct variable *gv;
1485
1486 gv = lookup_variable_in_set (v->name, strlen(v->name),
1487 &global_variable_set);
1488 if (gv)
1489 v->export = gv->export;
1490 }
1491
1492 switch (v->export)
1493 {
1494 case v_default:
1495 if (v->origin == o_default || v->origin == o_automatic)
1496 /* Only export default variables by explicit request. */
1497 continue;
1498
1499 /* The variable doesn't have a name that can be exported. */
1500 if (! v->exportable)
1501 continue;
1502
1503 if (! export_all_variables
1504 && v->origin != o_command
1505 && v->origin != o_env && v->origin != o_env_override)
1506 continue;
1507 break;
1508
1509 case v_export:
1510 break;
1511
1512 case v_noexport:
1513 /* If this is the SHELL variable and it's not exported, then
1514 add the value from our original environment. */
1515 if (streq (v->name, "SHELL"))
1516 {
1517 extern struct variable shell_var;
1518 v = &shell_var;
1519 break;
1520 }
1521 continue;
1522
1523 case v_ifset:
1524 if (v->origin == o_default)
1525 continue;
1526 break;
1527 }
1528
1529 new_slot = (struct variable **) hash_find_slot (&table, v);
1530 if (HASH_VACANT (*new_slot))
1531 hash_insert_at (&table, v, new_slot);
1532 }
1533 }
1534
1535 makelevel_key.name = MAKELEVEL_NAME;
1536 makelevel_key.length = MAKELEVEL_LENGTH;
1537#ifdef VARIABLE_HASH /* bird */
1538 makelevel_key.hash1 = variable_hash_1i (MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1539 makelevel_key.hash2 = 0;
1540#endif
1541 hash_delete (&table, &makelevel_key);
1542
1543 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1544
1545 v_slot = (struct variable **) table.ht_vec;
1546 v_end = v_slot + table.ht_size;
1547 for ( ; v_slot < v_end; v_slot++)
1548 if (! HASH_VACANT (*v_slot))
1549 {
1550 struct variable *v = *v_slot;
1551
1552 /* If V is recursively expanded and didn't come from the environment,
1553 expand its value. If it came from the environment, it should
1554 go back into the environment unchanged. */
1555 if (v->recursive
1556 && v->origin != o_env && v->origin != o_env_override)
1557 {
1558 char *value = recursively_expand_for_file (v, file);
1559#ifdef WINDOWS32
1560 if (strcmp(v->name, "Path") == 0 ||
1561 strcmp(v->name, "PATH") == 0)
1562 convert_Path_to_windows32(value, ';');
1563#endif
1564 *result++ = xstrdup (concat (v->name, "=", value));
1565 free (value);
1566 }
1567 else
1568 {
1569#ifdef WINDOWS32
1570 if (strcmp(v->name, "Path") == 0 ||
1571 strcmp(v->name, "PATH") == 0)
1572 convert_Path_to_windows32(v->value, ';');
1573#endif
1574 *result++ = xstrdup (concat (v->name, "=", v->value));
1575 }
1576 }
1577
1578 *result = xmalloc (100);
1579 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1580 *++result = 0;
1581
1582 hash_free (&table, 0);
1583
1584 return result_0;
1585}
1586
1587
1588#ifdef CONFIG_WITH_VALUE_LENGTH
1589static struct variable *
1590do_variable_definition_append (const struct floc *flocp, struct variable *v, const char *value,
1591 enum variable_origin origin, int append)
1592{
1593 if (env_overrides && origin == o_env)
1594 origin = o_env_override;
1595
1596 if (env_overrides && v->origin == o_env)
1597 /* V came from in the environment. Since it was defined
1598 before the switches were parsed, it wasn't affected by -e. */
1599 v->origin = o_env_override;
1600
1601 /* A variable of this name is already defined.
1602 If the old definition is from a stronger source
1603 than this one, don't redefine it. */
1604 if ((int) origin < (int) v->origin)
1605 return v;
1606 v->origin = origin;
1607
1608 /* location */
1609 if (flocp != 0)
1610 v->fileinfo = *flocp;
1611
1612 /* The juicy bits, append the specified value to the variable
1613 This is a heavily exercised code path in kBuild. */
1614 if (v->recursive)
1615 {
1616 /* The previous definition of the variable was recursive.
1617 The new value is the unexpanded old and new values. */
1618 unsigned int value_len = strlen (value);
1619 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
1620 int done_1st_prepend_copy = 0;
1621
1622 /* adjust the size. */
1623 if ((unsigned)v->value_alloc_len <= new_value_len + 1)
1624 {
1625 v->value_alloc_len *= 2;
1626 if (v->value_alloc_len < new_value_len + 1)
1627 v->value_alloc_len = (new_value_len + 1 + value_len + 0x7f) + ~0x7fU;
1628 if (append || !v->value_length)
1629 v->value = xrealloc (v->value, v->value_alloc_len);
1630 else
1631 {
1632 /* avoid the extra memcpy the xrealloc may have to do */
1633 char *new_buf = xmalloc (v->value_alloc_len);
1634 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
1635 done_1st_prepend_copy = 1;
1636 free (v->value);
1637 v->value = new_buf;
1638 }
1639 }
1640
1641 /* insert the new bits */
1642 if (v->value_length != 0)
1643 {
1644 if (append)
1645 {
1646 v->value[v->value_length] = ' ';
1647 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
1648 }
1649 else
1650 {
1651 if (!done_1st_prepend_copy)
1652 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
1653 v->value[value_len] = ' ';
1654 memcpy (v->value, value, value_len);
1655 }
1656 }
1657 else
1658 memcpy (v->value, value, value_len + 1);
1659 v->value_length = new_value_len;
1660 }
1661 else
1662 {
1663 /* The previous definition of the variable was simple.
1664 The new value comes from the old value, which was expanded
1665 when it was set; and from the expanded new value. */
1666 append_expanded_string_to_variable(v, value);
1667 }
1668
1669 /* update the variable */
1670 return v;
1671}
1672#endif /* CONFIG_WITH_VALUE_LENGTH */
1673
1674
1675/* Given a variable, a value, and a flavor, define the variable.
1676 See the try_variable_definition() function for details on the parameters. */
1677
1678struct variable *
1679do_variable_definition (const struct floc *flocp, const char *varname,
1680 const char *value, enum variable_origin origin,
1681 enum variable_flavor flavor, int target_var)
1682{
1683 const char *p;
1684 char *alloc_value = NULL;
1685 struct variable *v;
1686 int append = 0;
1687 int conditional = 0;
1688 const size_t varname_len = strlen (varname); /* bird */
1689#ifdef CONFIG_WITH_VALUE_LENGTH
1690 unsigned int value_len = ~0U;
1691#endif
1692
1693 /* Calculate the variable's new value in VALUE. */
1694
1695 switch (flavor)
1696 {
1697 default:
1698 case f_bogus:
1699 /* Should not be possible. */
1700 abort ();
1701 case f_simple:
1702 /* A simple variable definition "var := value". Expand the value.
1703 We have to allocate memory since otherwise it'll clobber the
1704 variable buffer, and we may still need that if we're looking at a
1705 target-specific variable. */
1706 p = alloc_value = allocated_variable_expand (value);
1707 break;
1708 case f_conditional:
1709 /* A conditional variable definition "var ?= value".
1710 The value is set IFF the variable is not defined yet. */
1711 v = lookup_variable (varname, varname_len);
1712 if (v)
1713 return v;
1714
1715 conditional = 1;
1716 flavor = f_recursive;
1717 /* FALLTHROUGH */
1718 case f_recursive:
1719 /* A recursive variable definition "var = value".
1720 The value is used verbatim. */
1721 p = value;
1722 break;
1723#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1724 case f_append:
1725 case f_prepend:
1726 {
1727 const enum variable_flavor org_flavor = flavor;
1728#else
1729 case f_append:
1730 {
1731#endif
1732
1733#ifdef CONFIG_WITH_LOCAL_VARIABLES
1734 /* If we have += but we're in a target or local variable context,
1735 we want to append only with other variables in the context of
1736 this target. */
1737 if (target_var || origin == o_local)
1738#else
1739 /* If we have += but we're in a target variable context, we want to
1740 append only with other variables in the context of this target. */
1741 if (target_var)
1742#endif
1743 {
1744 append = 1;
1745 v = lookup_variable_in_set (varname, varname_len,
1746 current_variable_set_list->set);
1747
1748 /* Don't append from the global set if a previous non-appending
1749 target-specific variable definition exists. */
1750 if (v && !v->append)
1751 append = 0;
1752 }
1753 else
1754 v = lookup_variable (varname, varname_len);
1755
1756 if (v == 0)
1757 {
1758 /* There was no old value.
1759 This becomes a normal recursive definition. */
1760 p = value;
1761 flavor = f_recursive;
1762 }
1763 else
1764 {
1765#ifdef CONFIG_WITH_VALUE_LENGTH
1766 v->append = append;
1767# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1768 return do_variable_definition_append (flocp, v, value, origin, org_flavor == f_append);
1769# else
1770 return do_variable_definition_append (flocp, v, value, origin, 1);
1771# endif
1772#else /* !CONFIG_WITH_VALUE_LENGTH */
1773
1774 /* Paste the old and new values together in VALUE. */
1775
1776 unsigned int oldlen, vallen;
1777 const char *val;
1778 char *tp;
1779
1780 val = value;
1781 if (v->recursive)
1782 /* The previous definition of the variable was recursive.
1783 The new value is the unexpanded old and new values. */
1784 flavor = f_recursive;
1785 else
1786 /* The previous definition of the variable was simple.
1787 The new value comes from the old value, which was expanded
1788 when it was set; and from the expanded new value. Allocate
1789 memory for the expansion as we may still need the rest of the
1790 buffer if we're looking at a target-specific variable. */
1791 val = alloc_value = allocated_variable_expand (val);
1792
1793 oldlen = strlen (v->value);
1794 vallen = strlen (val);
1795 tp = alloca (oldlen + 1 + vallen + 1);
1796# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1797 if (org_flavor == f_prepend)
1798 {
1799 memcpy (tp, val, vallen);
1800 tp[oldlen] = ' ';
1801 memcpy (&tp[oldlen + 1], v->value, oldlen + 1);
1802 }
1803 else
1804# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
1805 {
1806 memcpy (tp, v->value, oldlen);
1807 tp[oldlen] = ' ';
1808 memcpy (&tp[oldlen + 1], val, vallen + 1);
1809 }
1810 p = tp;
1811#endif /* !CONFIG_WITH_VALUE_LENGTH */
1812 }
1813 }
1814 }
1815
1816#ifdef __MSDOS__
1817 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1818 non-Unix systems don't conform to this default configuration (in
1819 fact, most of them don't even have `/bin'). On the other hand,
1820 $SHELL in the environment, if set, points to the real pathname of
1821 the shell.
1822 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1823 the Makefile override $SHELL from the environment. But first, we
1824 look for the basename of the shell in the directory where SHELL=
1825 points, and along the $PATH; if it is found in any of these places,
1826 we define $SHELL to be the actual pathname of the shell. Thus, if
1827 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1828 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1829 defining SHELL to be "d:/unix/bash.exe". */
1830 if ((origin == o_file || origin == o_override)
1831 && strcmp (varname, "SHELL") == 0)
1832 {
1833 PATH_VAR (shellpath);
1834 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1835
1836 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1837 if (__dosexec_find_on_path (p, NULL, shellpath))
1838 {
1839 char *tp;
1840
1841 for (tp = shellpath; *tp; tp++)
1842 if (*tp == '\\')
1843 *tp = '/';
1844
1845 v = define_variable_loc (varname, varname_len,
1846 shellpath, origin, flavor == f_recursive,
1847 flocp);
1848 }
1849 else
1850 {
1851 const char *shellbase, *bslash;
1852 struct variable *pathv = lookup_variable ("PATH", 4);
1853 char *path_string;
1854 char *fake_env[2];
1855 size_t pathlen = 0;
1856
1857 shellbase = strrchr (p, '/');
1858 bslash = strrchr (p, '\\');
1859 if (!shellbase || bslash > shellbase)
1860 shellbase = bslash;
1861 if (!shellbase && p[1] == ':')
1862 shellbase = p + 1;
1863 if (shellbase)
1864 shellbase++;
1865 else
1866 shellbase = p;
1867
1868 /* Search for the basename of the shell (with standard
1869 executable extensions) along the $PATH. */
1870 if (pathv)
1871 pathlen = strlen (pathv->value);
1872 path_string = xmalloc (5 + pathlen + 2 + 1);
1873 /* On MSDOS, current directory is considered as part of $PATH. */
1874 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1875 fake_env[0] = path_string;
1876 fake_env[1] = 0;
1877 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1878 {
1879 char *tp;
1880
1881 for (tp = shellpath; *tp; tp++)
1882 if (*tp == '\\')
1883 *tp = '/';
1884
1885 v = define_variable_loc (varname, varname_len,
1886 shellpath, origin,
1887 flavor == f_recursive, flocp);
1888 }
1889 else
1890 v = lookup_variable (varname, varname_len);
1891
1892 free (path_string);
1893 }
1894 }
1895 else
1896#endif /* __MSDOS__ */
1897#ifdef WINDOWS32
1898 if ( varname_len == sizeof("SHELL") - 1 /* bird */
1899 && (origin == o_file || origin == o_override || origin == o_command)
1900 && streq (varname, "SHELL"))
1901 {
1902 extern char *default_shell;
1903
1904 /* Call shell locator function. If it returns TRUE, then
1905 set no_default_sh_exe to indicate sh was found and
1906 set new value for SHELL variable. */
1907
1908 if (find_and_set_default_shell (p))
1909 {
1910 v = define_variable_in_set (varname, varname_len, default_shell,
1911# ifdef CONFIG_WITH_VALUE_LENGTH
1912 ~0U, 1 /* duplicate_value */,
1913# endif
1914 origin, flavor == f_recursive,
1915 (target_var
1916 ? current_variable_set_list->set
1917 : NULL),
1918 flocp);
1919 no_default_sh_exe = 0;
1920 }
1921 else
1922 v = lookup_variable (varname, varname_len);
1923 }
1924 else
1925#endif
1926
1927 /* If we are defining variables inside an $(eval ...), we might have a
1928 different variable context pushed, not the global context (maybe we're
1929 inside a $(call ...) or something. Since this function is only ever
1930 invoked in places where we want to define globally visible variables,
1931 make sure we define this variable in the global set. */
1932
1933 v = define_variable_in_set (varname, varname_len, p,
1934#ifdef CONFIG_WITH_VALUE_LENGTH
1935 value_len, !alloc_value,
1936#endif
1937 origin, flavor == f_recursive,
1938#ifdef CONFIG_WITH_LOCAL_VARIABLES
1939 (target_var || origin == o_local
1940#else
1941 (target_var
1942#endif
1943 ? current_variable_set_list->set : NULL),
1944 flocp);
1945 v->append = append;
1946 v->conditional = conditional;
1947
1948#ifndef CONFIG_WITH_VALUE_LENGTH
1949 if (alloc_value)
1950 free (alloc_value);
1951#endif
1952
1953 return v;
1954}
1955
1956
1957/* Try to interpret LINE (a null-terminated string) as a variable definition.
1958
1959 ORIGIN may be o_file, o_override, o_env, o_env_override,
1960 or o_command specifying that the variable definition comes
1961 from a makefile, an override directive, the environment with
1962 or without the -e switch, or the command line.
1963
1964 See the comments for parse_variable_definition().
1965
1966 If LINE was recognized as a variable definition, a pointer to its `struct
1967 variable' is returned. If LINE is not a variable definition, NULL is
1968 returned. */
1969
1970struct variable *
1971parse_variable_definition (struct variable *v, char *line)
1972{
1973 register int c;
1974 register char *p = line;
1975 register char *beg;
1976 register char *end;
1977 enum variable_flavor flavor = f_bogus;
1978 char *name;
1979
1980 while (1)
1981 {
1982 c = *p++;
1983 if (c == '\0' || c == '#')
1984 return 0;
1985 if (c == '=')
1986 {
1987 end = p - 1;
1988 flavor = f_recursive;
1989 break;
1990 }
1991 else if (c == ':')
1992 if (*p == '=')
1993 {
1994 end = p++ - 1;
1995 flavor = f_simple;
1996 break;
1997 }
1998 else
1999 /* A colon other than := is a rule line, not a variable defn. */
2000 return 0;
2001 else if (c == '+' && *p == '=')
2002 {
2003 end = p++ - 1;
2004 flavor = f_append;
2005 break;
2006 }
2007#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2008 else if (c == '<' && *p == '=')
2009 {
2010 end = p++ - 1;
2011 flavor = f_prepend;
2012 break;
2013 }
2014#endif
2015 else if (c == '?' && *p == '=')
2016 {
2017 end = p++ - 1;
2018 flavor = f_conditional;
2019 break;
2020 }
2021 else if (c == '$')
2022 {
2023 /* This might begin a variable expansion reference. Make sure we
2024 don't misrecognize chars inside the reference as =, := or +=. */
2025 char closeparen;
2026 int count;
2027 c = *p++;
2028 if (c == '(')
2029 closeparen = ')';
2030 else if (c == '{')
2031 closeparen = '}';
2032 else
2033 continue; /* Nope. */
2034
2035 /* P now points past the opening paren or brace.
2036 Count parens or braces until it is matched. */
2037 count = 0;
2038 for (; *p != '\0'; ++p)
2039 {
2040 if (*p == c)
2041 ++count;
2042 else if (*p == closeparen && --count < 0)
2043 {
2044 ++p;
2045 break;
2046 }
2047 }
2048 }
2049 }
2050 v->flavor = flavor;
2051
2052 beg = next_token (line);
2053 while (end > beg && isblank ((unsigned char)end[-1]))
2054 --end;
2055 p = next_token (p);
2056 v->value = p;
2057#ifdef CONFIG_WITH_VALUE_LENGTH
2058 v->value_length = v->value_alloc_len = -1;
2059#endif
2060
2061 /* Expand the name, so "$(foo)bar = baz" works. */
2062 name = alloca (end - beg + 1);
2063 memcpy (name, beg, end - beg);
2064 name[end - beg] = '\0';
2065 v->name = allocated_variable_expand (name);
2066
2067 if (v->name[0] == '\0')
2068 fatal (&v->fileinfo, _("empty variable name"));
2069
2070 return v;
2071}
2072
2073
2074/* Try to interpret LINE (a null-terminated string) as a variable definition.
2075
2076 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
2077 or o_command specifying that the variable definition comes
2078 from a makefile, an override directive, the environment with
2079 or without the -e switch, or the command line.
2080
2081 See the comments for parse_variable_definition().
2082
2083 If LINE was recognized as a variable definition, a pointer to its `struct
2084 variable' is returned. If LINE is not a variable definition, NULL is
2085 returned. */
2086
2087struct variable *
2088try_variable_definition (const struct floc *flocp, char *line,
2089 enum variable_origin origin, int target_var)
2090{
2091 struct variable v;
2092 struct variable *vp;
2093
2094 if (flocp != 0)
2095 v.fileinfo = *flocp;
2096 else
2097 v.fileinfo.filenm = 0;
2098
2099 if (!parse_variable_definition (&v, line))
2100 return 0;
2101
2102 vp = do_variable_definition (flocp, v.name, v.value,
2103 origin, v.flavor, target_var);
2104
2105 free (v.name);
2106
2107 return vp;
2108}
2109
2110
2111/* Print information for variable V, prefixing it with PREFIX. */
2112
2113static void
2114print_variable (const void *item, void *arg)
2115{
2116 const struct variable *v = item;
2117 const char *prefix = arg;
2118 const char *origin;
2119
2120 switch (v->origin)
2121 {
2122 case o_default:
2123 origin = _("default");
2124 break;
2125 case o_env:
2126 origin = _("environment");
2127 break;
2128 case o_file:
2129 origin = _("makefile");
2130 break;
2131 case o_env_override:
2132 origin = _("environment under -e");
2133 break;
2134 case o_command:
2135 origin = _("command line");
2136 break;
2137 case o_override:
2138 origin = _("`override' directive");
2139 break;
2140 case o_automatic:
2141 origin = _("automatic");
2142 break;
2143#ifdef CONFIG_WITH_LOCAL_VARIABLES
2144 case o_local:
2145 origin = _("`local' directive");
2146 break;
2147#endif
2148 case o_invalid:
2149 default:
2150 abort ();
2151 }
2152 fputs ("# ", stdout);
2153 fputs (origin, stdout);
2154 if (v->fileinfo.filenm)
2155 printf (_(" (from `%s', line %lu)"),
2156 v->fileinfo.filenm, v->fileinfo.lineno);
2157 putchar ('\n');
2158 fputs (prefix, stdout);
2159
2160 /* Is this a `define'? */
2161 if (v->recursive && strchr (v->value, '\n') != 0)
2162 printf ("define %s\n%s\nendef\n", v->name, v->value);
2163 else
2164 {
2165 register char *p;
2166
2167 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
2168
2169 /* Check if the value is just whitespace. */
2170 p = next_token (v->value);
2171 if (p != v->value && *p == '\0')
2172 /* All whitespace. */
2173 printf ("$(subst ,,%s)", v->value);
2174 else if (v->recursive)
2175 fputs (v->value, stdout);
2176 else
2177 /* Double up dollar signs. */
2178 for (p = v->value; *p != '\0'; ++p)
2179 {
2180 if (*p == '$')
2181 putchar ('$');
2182 putchar (*p);
2183 }
2184 putchar ('\n');
2185 }
2186}
2187
2188
2189/* Print all the variables in SET. PREFIX is printed before
2190 the actual variable definitions (everything else is comments). */
2191
2192void
2193print_variable_set (struct variable_set *set, char *prefix)
2194{
2195 hash_map_arg (&set->table, print_variable, prefix);
2196
2197 fputs (_("# variable set hash-table stats:\n"), stdout);
2198 fputs ("# ", stdout);
2199 hash_print_stats (&set->table, stdout);
2200 putc ('\n', stdout);
2201}
2202
2203/* Print the data base of variables. */
2204
2205void
2206print_variable_data_base (void)
2207{
2208 puts (_("\n# Variables\n"));
2209
2210 print_variable_set (&global_variable_set, "");
2211
2212 puts (_("\n# Pattern-specific Variable Values"));
2213
2214 {
2215 struct pattern_var *p;
2216 int rules = 0;
2217
2218 for (p = pattern_vars; p != 0; p = p->next)
2219 {
2220 ++rules;
2221 printf ("\n%s :\n", p->target);
2222 print_variable (&p->variable, "# ");
2223 }
2224
2225 if (rules == 0)
2226 puts (_("\n# No pattern-specific variable values."));
2227 else
2228 printf (_("\n# %u pattern-specific variable values"), rules);
2229 }
2230}
2231
2232
2233/* Print all the local variables of FILE. */
2234
2235void
2236print_file_variables (const struct file *file)
2237{
2238 if (file->variables != 0)
2239 print_variable_set (file->variables->set, "# ");
2240}
2241
2242#ifdef WINDOWS32
2243void
2244sync_Path_environment (void)
2245{
2246 char *path = allocated_variable_expand ("$(PATH)");
2247 static char *environ_path = NULL;
2248
2249 if (!path)
2250 return;
2251
2252 /*
2253 * If done this before, don't leak memory unnecessarily.
2254 * Free the previous entry before allocating new one.
2255 */
2256 if (environ_path)
2257 free (environ_path);
2258
2259 /*
2260 * Create something WINDOWS32 world can grok
2261 */
2262 convert_Path_to_windows32 (path, ';');
2263 environ_path = xstrdup (concat ("PATH", "=", path));
2264 putenv (environ_path);
2265 free (path);
2266}
2267#endif
Note: See TracBrowser for help on using the repository browser.

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