1 /* Internals of variables for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2, or (at your option) any later version.
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 GNU Make; see the file COPYING. If not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
30 #include "pathstuff.h"
37 /* Chain of all pattern-specific variables. */
39 static struct pattern_var
*pattern_vars
;
41 /* Pointer to last struct in the chain, so we can add onto the end. */
43 static struct pattern_var
*last_pattern_var
;
45 /* Create a new pattern-specific variable struct. */
48 create_pattern_var (const char *target
, const char *suffix
)
50 register struct pattern_var
*p
= xmalloc (sizeof (struct pattern_var
));
52 if (last_pattern_var
!= 0)
53 last_pattern_var
->next
= p
;
60 p
->len
= strlen (target
);
61 p
->suffix
= suffix
+ 1;
66 /* Look up a target in the pattern-specific variable list. */
68 static struct pattern_var
*
69 lookup_pattern_var (struct pattern_var
*start
, const char *target
)
71 struct pattern_var
*p
;
72 unsigned int targlen
= strlen(target
);
74 for (p
= start
? start
->next
: pattern_vars
; p
!= 0; p
= p
->next
)
80 /* It can't possibly match. */
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;
88 /* Compare the text in the pattern before the stem, if any. */
89 if (stem
> target
&& !strneq (p
->target
, target
, stem
- target
))
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
97 if (*p
->suffix
== stem
[stemlen
]
98 && (*p
->suffix
== '\0' || streq (&p
->suffix
[1], &stem
[stemlen
+1])))
105 /* Hash table of all global variable definitions. */
107 #if defined(VARIABLE_HASH) || defined(CONFIG_WITH_OPTIMIZATION_HACKS)
109 # define inline _inline
110 typedef signed int int32_t;
111 typedef signed short int int16_t;
113 static inline unsigned long variable_hash_2i(register const char *var
, register int length
)
115 # define UPDATE_HASH(ch) hash = (ch) + (hash << 6) + (hash << 16) - hash
116 # ifndef CONFIG_WITH_OPTIMIZATION_HACKS
118 register const unsigned char *uvar
= (const unsigned char *)var
;
119 register unsigned long hash
= 0;
121 UPDATE_HASH(*uvar
++);
124 return_STRING_N_HASH_2 (var
, length
);
126 # else /* CONFIG_WITH_OPTIMIZATION_HACKS */
127 register unsigned long hash
= 0;
128 register const unsigned char *uvar
= (const unsigned char *)var
;
129 register const unsigned char *uvar_end
= uvar
+ length
;
133 case 32: //UPDATE_HASH(uvar_end[-16]);
134 case 31: UPDATE_HASH(uvar_end
[-15]);
135 case 30: //UPDATE_HASH(uvar_end[-14]);
136 case 29: UPDATE_HASH(uvar_end
[-13]);
137 case 28: //UPDATE_HASH(uvar_end[-12]);
138 case 27: UPDATE_HASH(uvar_end
[-11]);
139 case 26: //UPDATE_HASH(uvar_end[-10]);
140 case 25: UPDATE_HASH(uvar_end
[-9]);
141 case 24: //UPDATE_HASH(uvar[15]);
142 case 23: UPDATE_HASH(uvar
[14]);
143 case 22: //UPDATE_HASH(uvar[13]);
144 case 21: UPDATE_HASH(uvar
[12]);
145 case 20: //UPDATE_HASH(uvar[11]);
146 case 19: UPDATE_HASH(uvar
[10]);
147 case 18: //UPDATE_HASH(uvar[9]);
148 case 17: UPDATE_HASH(uvar
[8]);
149 case 16: //UPDATE_HASH(uvar_end[-8]);
150 case 15: UPDATE_HASH(uvar_end
[-7]);
151 case 14: //UPDATE_HASH(uvar_end[-6]);
152 case 13: UPDATE_HASH(uvar_end
[-5]);
153 case 12: //UPDATE_HASH(uvar_end[-4]);
154 case 11: UPDATE_HASH(uvar_end
[-3]);
155 case 10: //UPDATE_HASH(uvar_end[-2]);
156 case 9: UPDATE_HASH(uvar_end
[-1]);
157 case 8: //UPDATE_HASH(uvar[7]);
158 case 7: UPDATE_HASH(uvar
[6]);
159 case 6: //UPDATE_HASH(uvar[5]);
160 case 5: UPDATE_HASH(uvar
[4]);
161 case 4: //UPDATE_HASH(uvar[3]);
162 case 3: UPDATE_HASH(uvar
[2]);
163 case 2: //UPDATE_HASH(uvar[1]);
164 case 1: UPDATE_HASH(uvar
[0]);
168 # endif /* CONFIG_WITH_OPTIMIZATION_HACKS*/
172 static inline unsigned long variable_hash_1i(register const char *var
, register int length
)
174 # define UPDATE_HASH(ch) hash = ((hash << 5) + hash) + (ch)
175 # ifndef CONFIG_WITH_OPTIMIZATION_HACKS
177 register const unsigned char *uvar
= (const unsigned char *)var
;
178 register unsigned long hash
= 5381;
180 UPDATE_HASH(*uvar
++);
183 return_STRING_N_HASH_1 (var
, length
);
185 # else /* CONFIG_WITH_OPTIMIZATION_HACKS */
186 register const unsigned char *uvar
= (const unsigned char *)var
;
187 register const unsigned char *uvar_end
= (const unsigned char *)var
+ length
;
188 register unsigned long hash
= ((5381 << 5) + 5381) + *uvar
;
192 #if 0 /* seems to be a waste of time. */
193 case 97: UPDATE_HASH(uvar_end
[-77]);
194 case 96: //UPDATE_HASH(uvar_end[-76]);
195 case 95: //UPDATE_HASH(uvar_end[-75]);
196 case 94: //UPDATE_HASH(uvar_end[-74]);
197 case 93: UPDATE_HASH(uvar_end
[-73]);
198 case 92: //UPDATE_HASH(uvar_end[-72]);
199 case 91: //UPDATE_HASH(uvar_end[-71]);
200 case 90: //UPDATE_HASH(uvar_end[-70]);
201 case 89: UPDATE_HASH(uvar_end
[-69]);
202 case 88: //UPDATE_HASH(uvar_end[-68]);
203 case 87: //UPDATE_HASH(uvar_end[-67]);
204 case 86: //UPDATE_HASH(uvar_end[-66]);
205 case 85: UPDATE_HASH(uvar_end
[-65]);
206 case 84: //UPDATE_HASH(uvar_end[-64]);
207 case 83: //UPDATE_HASH(uvar_end[-63]);
208 case 82: //UPDATE_HASH(uvar_end[-62]);
209 case 81: UPDATE_HASH(uvar_end
[-61]);
210 case 80: //UPDATE_HASH(uvar_end[-60]);
211 case 79: //UPDATE_HASH(uvar_end[-59]);
212 case 78: //UPDATE_HASH(uvar_end[-58]);
213 case 77: UPDATE_HASH(uvar_end
[-57]);
214 case 76: //UPDATE_HASH(uvar_end[-56]);
215 case 75: //UPDATE_HASH(uvar_end[-55]);
216 case 74: //UPDATE_HASH(uvar_end[-54]);
217 case 73: UPDATE_HASH(uvar_end
[-53]);
218 case 72: //UPDATE_HASH(uvar_end[-52]);
219 case 71: //UPDATE_HASH(uvar_end[-51]);
220 case 70: //UPDATE_HASH(uvar_end[-50]);
221 case 69: UPDATE_HASH(uvar_end
[-49]);
222 case 68: //UPDATE_HASH(uvar_end[-48]);
223 case 67: //UPDATE_HASH(uvar_end[-47]);
224 case 66: //UPDATE_HASH(uvar_end[-46]);
225 case 65: UPDATE_HASH(uvar_end
[-49]);
226 case 64: //UPDATE_HASH(uvar_end[-48]);
227 case 63: //UPDATE_HASH(uvar_end[-47]);
228 case 62: //UPDATE_HASH(uvar_end[-46]);
229 case 61: UPDATE_HASH(uvar_end
[-45]);
230 case 60: //UPDATE_HASH(uvar_end[-44]);
231 case 59: //UPDATE_HASH(uvar_end[-43]);
232 case 58: //UPDATE_HASH(uvar_end[-42]);
233 case 57: UPDATE_HASH(uvar_end
[-41]);
234 case 56: //UPDATE_HASH(uvar_end[-40]);
235 case 55: //UPDATE_HASH(uvar_end[-39]);
236 case 54: //UPDATE_HASH(uvar_end[-38]);
237 case 53: UPDATE_HASH(uvar_end
[-37]);
238 case 52: //UPDATE_HASH(uvar_end[-36]);
239 case 51: UPDATE_HASH(uvar_end
[-35]);
240 case 50: //UPDATE_HASH(uvar_end[-34]);
241 case 49: UPDATE_HASH(uvar_end
[-33]);
243 case 48: //UPDATE_HASH(uvar_end[-32]);
244 case 47: UPDATE_HASH(uvar_end
[-31]);
245 case 46: //UPDATE_HASH(uvar_end[-30]);
246 case 45: UPDATE_HASH(uvar_end
[-29]);
247 case 44: //UPDATE_HASH(uvar_end[-28]);
248 case 43: UPDATE_HASH(uvar_end
[-27]);
249 case 42: //UPDATE_HASH(uvar_end[-26]);
250 case 41: UPDATE_HASH(uvar_end
[-25]);
251 case 40: //UPDATE_HASH(uvar_end[-24]);
252 case 39: UPDATE_HASH(uvar_end
[-23]);
253 case 38: //UPDATE_HASH(uvar_end[-22]);
254 case 37: UPDATE_HASH(uvar_end
[-21]);
255 case 36: //UPDATE_HASH(uvar_end[-20]);
256 case 35: UPDATE_HASH(uvar_end
[-19]);
257 case 34: //UPDATE_HASH(uvar_end[-18]);
258 case 33: UPDATE_HASH(uvar_end
[-17]);
260 case 32: UPDATE_HASH(uvar_end
[-16]);
261 case 31: UPDATE_HASH(uvar_end
[-15]);
262 case 30: UPDATE_HASH(uvar_end
[-14]);
263 case 29: UPDATE_HASH(uvar_end
[-13]);
264 case 28: UPDATE_HASH(uvar
[15]);
265 case 27: UPDATE_HASH(uvar
[14]);
266 case 26: UPDATE_HASH(uvar
[13]);
267 case 25: UPDATE_HASH(uvar
[12]);
269 case 24: UPDATE_HASH(uvar_end
[-12]);
270 case 23: UPDATE_HASH(uvar_end
[-11]);
271 case 22: UPDATE_HASH(uvar_end
[-10]);
272 case 21: UPDATE_HASH(uvar_end
[-9]);
273 case 20: UPDATE_HASH(uvar
[7]);
274 case 19: UPDATE_HASH(uvar
[6]);
275 case 18: UPDATE_HASH(uvar
[5]);
276 case 17: UPDATE_HASH(uvar
[4]);
278 case 16: UPDATE_HASH(uvar_end
[-8]);
279 case 15: UPDATE_HASH(uvar_end
[-7]);
280 case 14: UPDATE_HASH(uvar_end
[-6]);
281 case 13: UPDATE_HASH(uvar_end
[-5]);
282 case 12: UPDATE_HASH(uvar
[11]);
283 case 11: UPDATE_HASH(uvar
[10]);
284 case 10: UPDATE_HASH(uvar
[9]);
285 case 9: UPDATE_HASH(uvar
[8]);
287 case 8: UPDATE_HASH(uvar_end
[-4]);
288 case 7: UPDATE_HASH(uvar_end
[-3]);
289 case 6: UPDATE_HASH(uvar_end
[-2]);
290 case 5: UPDATE_HASH(uvar_end
[-1]);
291 case 4: UPDATE_HASH(uvar
[3]);
292 case 3: UPDATE_HASH(uvar
[2]);
293 case 2: UPDATE_HASH(uvar
[1]);
295 case 0: return 5381; /* shouldn't happen */
297 # endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
300 #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
303 variable_hash_1 (const void *keyv
)
305 struct variable
const *key
= (struct variable
const *) keyv
;
306 #ifdef VARIABLE_HASH /* bird */
307 # ifdef VARIABLE_HASH_STRICT
308 if (key
->hash1
!= variable_hash_1i (key
->name
, key
->length
))
310 if (key
->hash2
&& key
->hash2
!= variable_hash_2i (key
->name
, key
->length
))
315 # ifdef CONFIG_WITH_OPTIMIZATION_HACKS
316 return variable_hash_1i (key
->name
, key
->length
);
318 return_STRING_N_HASH_1 (key
->name
, key
->length
);
324 variable_hash_2 (const void *keyv
)
326 #ifdef VARIABLE_HASH /* bird */
327 struct variable
*key
= (struct variable
*) keyv
;
329 key
->hash2
= variable_hash_2i (key
->name
, key
->length
);
332 struct variable
const *key
= (struct variable
const *) keyv
;
333 # ifdef CONFIG_WITH_OPTIMIZATION_HACKS
334 return variable_hash_2i (key
->name
, key
->length
);
336 return_STRING_N_HASH_2 (key
->name
, key
->length
);
342 variable_hash_cmp (const void *xv
, const void *yv
)
344 struct variable
const *x
= (struct variable
const *) xv
;
345 struct variable
const *y
= (struct variable
const *) yv
;
346 int result
= x
->length
- y
->length
;
350 #ifdef VARIABLE_HASH_STRICT /* bird */
351 if (x
->hash1
!= variable_hash_1i (x
->name
, x
->length
))
353 if (x
->hash2
&& x
->hash2
!= variable_hash_2i (x
->name
, x
->length
))
355 if (y
->hash1
!= variable_hash_1i (y
->name
, y
->length
))
357 if (y
->hash2
&& y
->hash2
!= variable_hash_2i (y
->name
, y
->length
))
361 result
= x
->hash1
- y
->hash1
;
365 #ifdef CONFIG_WITH_OPTIMIZATION_HACKS /* bird: speed */
367 const char *xs
= x
->name
;
368 const char *ys
= y
->name
;
372 result
= *(int32_t*)(xs
+ 4) - *(int32_t*)(ys
+ 4);
375 return *(int32_t*)xs
- *(int32_t*)ys
;
377 result
= xs
[6] - ys
[6];
381 result
= *(int32_t*)xs
- *(int32_t*)ys
;
384 return *(int16_t*)(xs
+ 4) - *(int16_t*)(ys
+ 4);
386 result
= xs
[4] - ys
[4];
390 return *(int32_t*)xs
- *(int32_t*)ys
;
392 result
= xs
[2] - ys
[2];
396 return *(int16_t*)xs
- *(int16_t*)ys
;
403 #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
407 ((struct variable
*)x
)->hash2
= variable_hash_2i (x
->name
, x
->length
);
409 ((struct variable
*)y
)->hash2
= variable_hash_2i (y
->name
, y
->length
);
410 result
= x
->hash2
- y
->hash2
;
414 #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
415 return memcmp (x
->name
, y
->name
, x
->length
);
417 return_STRING_N_COMPARE (x
->name
, y
->name
, x
->length
);
421 #ifndef VARIABLE_BUCKETS
422 # ifdef KMK /* Move to Makefile.kmk? */
423 # define VARIABLE_BUCKETS 16384
425 #define VARIABLE_BUCKETS 523
428 #ifndef PERFILE_VARIABLE_BUCKETS
429 # ifdef KMK /* Move to Makefile.kmk? */
430 # define PERFILE_VARIABLE_BUCKETS 64
432 #define PERFILE_VARIABLE_BUCKETS 23
435 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
436 # ifdef KMK /* Move to Makefile.kmk? */
437 # define SMALL_SCOPE_VARIABLE_BUCKETS 32
439 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
443 static struct variable_set global_variable_set
;
444 static struct variable_set_list global_setlist
445 = { 0, &global_variable_set
};
446 struct variable_set_list
*current_variable_set_list
= &global_setlist
;
448 /* Implement variables. */
451 init_hash_global_variable_set (void)
453 hash_init (&global_variable_set
.table
, VARIABLE_BUCKETS
,
454 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
457 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
458 LENGTH is the length of NAME, which does not need to be null-terminated.
459 ORIGIN specifies the origin of the variable (makefile, command line
461 If RECURSIVE is nonzero a flag is set in the variable saying
462 that it should be recursively re-expanded. */
464 #ifdef CONFIG_WITH_VALUE_LENGTH
466 define_variable_in_set (const char *name
, unsigned int length
,
467 const char *value
, unsigned int value_length
, int duplicate_value
,
468 enum variable_origin origin
, int recursive
,
469 struct variable_set
*set
, const struct floc
*flocp
)
472 define_variable_in_set (const char *name
, unsigned int length
,
473 const char *value
, enum variable_origin origin
,
474 int recursive
, struct variable_set
*set
,
475 const struct floc
*flocp
)
479 struct variable
**var_slot
;
480 struct variable var_key
;
483 set
= &global_variable_set
;
485 var_key
.name
= (char *) name
;
486 var_key
.length
= length
;
487 #ifdef VARIABLE_HASH /* bird */
488 var_key
.hash1
= variable_hash_1i (name
, length
);
491 var_slot
= (struct variable
**) hash_find_slot (&set
->table
, &var_key
);
493 if (env_overrides
&& origin
== o_env
)
494 origin
= o_env_override
;
497 if (! HASH_VACANT (v
))
499 if (env_overrides
&& v
->origin
== o_env
)
500 /* V came from in the environment. Since it was defined
501 before the switches were parsed, it wasn't affected by -e. */
502 v
->origin
= o_env_override
;
504 /* A variable of this name is already defined.
505 If the old definition is from a stronger source
506 than this one, don't redefine it. */
507 if ((int) origin
>= (int) v
->origin
)
509 #ifdef CONFIG_WITH_VALUE_LENGTH
510 if (value_length
== ~0U)
511 value_length
= strlen (value
);
513 assert (value_length
== strlen (value
));
514 if (!duplicate_value
)
518 v
->value
= (char *)value
;
519 v
->value_alloc_len
= value_length
+ 1;
523 if ((unsigned int)v
->value_alloc_len
<= value_length
)
526 v
->value_alloc_len
= (value_length
+ 0x40) & ~0x3f;
527 v
->value
= xmalloc (v
->value_alloc_len
);
529 memcpy (v
->value
, value
, value_length
+ 1);
531 v
->value_length
= value_length
;
535 v
->value
= xstrdup (value
);
538 v
->fileinfo
= *flocp
;
540 v
->fileinfo
.filenm
= 0;
542 v
->recursive
= recursive
;
547 /* Create a new variable definition and add it to the hash table. */
549 v
= xmalloc (sizeof (struct variable
));
550 v
->name
= savestring (name
, length
);
552 #ifdef VARIABLE_HASH /* bird */
553 v
->hash1
= variable_hash_1i (name
, length
);
556 hash_insert_at (&set
->table
, v
, var_slot
);
557 #ifdef CONFIG_WITH_VALUE_LENGTH
558 if (value_length
== ~0U)
559 value_length
= strlen (value
);
561 assert (value_length
== strlen (value
));
562 v
->value_length
= value_length
;
563 if (!duplicate_value
)
565 v
->value_alloc_len
= value_length
+ 1;
566 v
->value
= (char *)value
;
570 v
->value_alloc_len
= (value_length
+ 32) & ~31;
571 v
->value
= xmalloc (v
->value_alloc_len
);
572 memcpy (v
->value
, value
, value_length
+ 1);
575 v
->value
= xstrdup (value
);
578 v
->fileinfo
= *flocp
;
580 v
->fileinfo
.filenm
= 0;
582 v
->recursive
= recursive
;
588 v
->export
= v_default
;
591 if (*name
!= '_' && (*name
< 'A' || *name
> 'Z')
592 && (*name
< 'a' || *name
> 'z'))
596 for (++name
; *name
!= '\0'; ++name
)
597 if (*name
!= '_' && (*name
< 'a' || *name
> 'z')
598 && (*name
< 'A' || *name
> 'Z') && !ISDIGIT(*name
))
608 /* If the variable passed in is "special", handle its special nature.
609 Currently there are two such variables, both used for introspection:
610 .VARIABLES expands to a list of all the variables defined in this instance
612 .TARGETS expands to a list of all the targets defined in this
614 Returns the variable reference passed in. */
616 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
618 static struct variable
*
619 handle_special_var (struct variable
*var
)
621 static unsigned long last_var_count
= 0;
624 /* This one actually turns out to be very hard, due to the way the parser
625 records targets. The way it works is that target information is collected
626 internally until make knows the target is completely specified. It unitl
627 it sees that some new construct (a new target or variable) is defined that
628 it knows the previous one is done. In short, this means that if you do
635 then $(TARGS) won't contain "all", because it's not until after the
636 variable is created that the previous target is completed.
638 Changing this would be a major pain. I think a less complex way to do it
639 would be to pre-define the target files as soon as the first line is
640 parsed, then come back and do the rest of the definition as now. That
641 would allow $(.TARGETS) to be correct without a major change to the way
644 if (streq (var->name, ".TARGETS"))
645 var->value = build_target_list (var->value);
649 if (streq (var
->name
, ".VARIABLES")
650 && global_variable_set
.table
.ht_fill
!= last_var_count
)
652 unsigned long max
= EXPANSION_INCREMENT (strlen (var
->value
));
655 struct variable
**vp
= (struct variable
**) global_variable_set
.table
.ht_vec
;
656 struct variable
**end
= &vp
[global_variable_set
.table
.ht_size
];
658 /* Make sure we have at least MAX bytes in the allocated buffer. */
659 var
->value
= xrealloc (var
->value
, max
);
661 /* Walk through the hash of variables, constructing a list of names. */
664 for (; vp
< end
; ++vp
)
665 if (!HASH_VACANT (*vp
))
667 struct variable
*v
= *vp
;
673 unsigned long off
= p
- var
->value
;
675 max
+= EXPANSION_INCREMENT (l
+ 1);
676 var
->value
= xrealloc (var
->value
, max
);
677 p
= &var
->value
[off
];
680 memcpy (p
, v
->name
, l
);
686 /* Remember how many variables are in our current count. Since we never
687 remove variables from the list, this is a reliable way to know whether
688 the list is up to date or needs to be recomputed. */
690 last_var_count
= global_variable_set
.table
.ht_fill
;
697 /* Lookup a variable whose name is a string starting at NAME
698 and with LENGTH chars. NAME need not be null-terminated.
699 Returns address of the `struct variable' containing all info
700 on the variable, or nil if no such variable is defined. */
703 lookup_variable (const char *name
, unsigned int length
)
705 const struct variable_set_list
*setlist
;
706 struct variable var_key
;
708 var_key
.name
= (char *) name
;
709 var_key
.length
= length
;
710 #ifdef VARIABLE_HASH /* bird */
711 var_key
.hash1
= variable_hash_1i (name
, length
);
715 for (setlist
= current_variable_set_list
;
716 setlist
!= 0; setlist
= setlist
->next
)
718 #ifdef VARIABLE_HASH /* bird: speed */
719 struct hash_table
*ht
= &setlist
->set
->table
;
720 unsigned int hash_1
= var_key
.hash1
;
726 hash_1
&= (ht
->ht_size
- 1);
727 v
= (struct variable
*)ht
->ht_vec
[hash_1
];
731 if ((void *)v
!= hash_deleted_item
)
733 if (variable_hash_cmp(&var_key
, v
) == 0)
735 # ifdef VARIABLE_HASH_STRICT /* bird */
736 struct variable
*v2
= (struct variable
*) hash_find_item ((struct hash_table
*) &setlist
->set
->table
, &var_key
);
739 return v
->special
? handle_special_var (v
) : v
;
744 var_key
.hash2
= variable_hash_2i(name
, length
);
745 hash_1
+= (var_key
.hash2
| 1);
748 #else /* !VARIABLE_HASH */
749 const struct variable_set
*set
= setlist
->set
;
752 v
= (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
754 return v
->special
? handle_special_var (v
) : v
;
755 #endif /* !VARIABLE_HASH */
759 /* since we don't read envp[] on startup, try to get the
760 variable via getenv() here. */
762 char *vname
= alloca (length
+ 1);
764 strncpy (vname
, name
, length
);
766 value
= getenv (vname
);
775 while ((sptr
= strchr (sptr
, '$')))
786 nvalue
= alloca (strlen (value
) + scnt
+ 1);
805 return define_variable (vname
, length
, nvalue
, o_env
, 1);
809 return define_variable (vname
, length
, value
, o_env
, 1);
817 /* Lookup a variable whose name is a string starting at NAME
818 and with LENGTH chars in set SET. NAME need not be null-terminated.
819 Returns address of the `struct variable' containing all info
820 on the variable, or nil if no such variable is defined. */
823 lookup_variable_in_set (const char *name
, unsigned int length
,
824 const struct variable_set
*set
)
826 struct variable var_key
;
828 var_key
.name
= (char *) name
;
829 var_key
.length
= length
;
830 #ifdef VARIABLE_HASH /* bird */
831 var_key
.hash1
= variable_hash_1i (name
, length
);
835 return (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
838 /* Initialize FILE's variable set list. If FILE already has a variable set
839 list, the topmost variable set is left intact, but the the rest of the
840 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
841 rule, then we will use the "root" double-colon target's variable set as the
842 parent of FILE's variable set.
844 If we're READING a makefile, don't do the pattern variable search now,
845 since the pattern variable might not have been defined yet. */
848 initialize_file_variables (struct file
*file
, int reading
)
850 struct variable_set_list
*l
= file
->variables
;
854 l
= (struct variable_set_list
*)
855 xmalloc (sizeof (struct variable_set_list
));
856 l
->set
= xmalloc (sizeof (struct variable_set
));
857 hash_init (&l
->set
->table
, PERFILE_VARIABLE_BUCKETS
,
858 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
862 /* If this is a double-colon, then our "parent" is the "root" target for
863 this double-colon rule. Since that rule has the same name, parent,
864 etc. we can just use its variables as the "next" for ours. */
866 if (file
->double_colon
&& file
->double_colon
!= file
)
868 initialize_file_variables (file
->double_colon
, reading
);
869 l
->next
= file
->double_colon
->variables
;
873 if (file
->parent
== 0)
874 l
->next
= &global_setlist
;
877 initialize_file_variables (file
->parent
, reading
);
878 l
->next
= file
->parent
->variables
;
881 /* If we're not reading makefiles and we haven't looked yet, see if
882 we can find pattern variables for this target. */
884 if (!reading
&& !file
->pat_searched
)
886 struct pattern_var
*p
;
888 p
= lookup_pattern_var (0, file
->name
);
891 struct variable_set_list
*global
= current_variable_set_list
;
893 /* We found at least one. Set up a new variable set to accumulate
894 all the pattern variables that match this target. */
896 file
->pat_variables
= create_new_variable_set ();
897 current_variable_set_list
= file
->pat_variables
;
901 /* We found one, so insert it into the set. */
905 if (p
->variable
.flavor
== f_simple
)
907 v
= define_variable_loc (
908 p
->variable
.name
, strlen (p
->variable
.name
),
909 p
->variable
.value
, p
->variable
.origin
,
910 0, &p
->variable
.fileinfo
);
912 v
->flavor
= f_simple
;
916 v
= do_variable_definition (
917 &p
->variable
.fileinfo
, p
->variable
.name
,
918 p
->variable
.value
, p
->variable
.origin
,
919 p
->variable
.flavor
, 1);
922 /* Also mark it as a per-target and copy export status. */
923 v
->per_target
= p
->variable
.per_target
;
924 v
->export
= p
->variable
.export
;
926 while ((p
= lookup_pattern_var (p
, file
->name
)) != 0);
928 current_variable_set_list
= global
;
930 file
->pat_searched
= 1;
933 /* If we have a pattern variable match, set it up. */
935 if (file
->pat_variables
!= 0)
937 file
->pat_variables
->next
= l
->next
;
938 l
->next
= file
->pat_variables
;
942 /* Pop the top set off the current variable set list,
943 and free all its storage. */
945 struct variable_set_list
*
946 create_new_variable_set (void)
948 register struct variable_set_list
*setlist
;
949 register struct variable_set
*set
;
951 set
= xmalloc (sizeof (struct variable_set
));
952 hash_init (&set
->table
, SMALL_SCOPE_VARIABLE_BUCKETS
,
953 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
955 setlist
= (struct variable_set_list
*)
956 xmalloc (sizeof (struct variable_set_list
));
958 setlist
->next
= current_variable_set_list
;
964 free_variable_name_and_value (const void *item
)
966 struct variable
*v
= (struct variable
*) item
;
972 free_variable_set (struct variable_set_list
*list
)
974 hash_map (&list
->set
->table
, free_variable_name_and_value
);
975 hash_free (&list
->set
->table
, 1);
980 /* Create a new variable set and push it on the current setlist.
981 If we're pushing a global scope (that is, the current scope is the global
982 scope) then we need to "push" it the other way: file variable sets point
983 directly to the global_setlist so we need to replace that with the new one.
986 struct variable_set_list
*
987 push_new_variable_scope (void)
989 current_variable_set_list
= create_new_variable_set();
990 if (current_variable_set_list
->next
== &global_setlist
)
992 /* It was the global, so instead of new -> &global we want to replace
993 &global with the new one and have &global -> new, with current still
994 pointing to &global */
995 struct variable_set
*set
= current_variable_set_list
->set
;
996 current_variable_set_list
->set
= global_setlist
.set
;
997 global_setlist
.set
= set
;
998 current_variable_set_list
->next
= global_setlist
.next
;
999 global_setlist
.next
= current_variable_set_list
;
1000 current_variable_set_list
= &global_setlist
;
1002 return (current_variable_set_list
);
1006 pop_variable_scope (void)
1008 struct variable_set_list
*setlist
;
1009 struct variable_set
*set
;
1011 /* Can't call this if there's no scope to pop! */
1012 assert(current_variable_set_list
->next
!= NULL
);
1014 if (current_variable_set_list
!= &global_setlist
)
1016 /* We're not pointing to the global setlist, so pop this one. */
1017 setlist
= current_variable_set_list
;
1019 current_variable_set_list
= setlist
->next
;
1023 /* This set is the one in the global_setlist, but there is another global
1024 set beyond that. We want to copy that set to global_setlist, then
1025 delete what used to be in global_setlist. */
1026 setlist
= global_setlist
.next
;
1027 set
= global_setlist
.set
;
1028 global_setlist
.set
= setlist
->set
;
1029 global_setlist
.next
= setlist
->next
;
1032 /* Free the one we no longer need. */
1034 hash_map (&set
->table
, free_variable_name_and_value
);
1035 hash_free (&set
->table
, 1);
1039 /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
1042 merge_variable_sets (struct variable_set
*to_set
,
1043 struct variable_set
*from_set
)
1045 struct variable
**from_var_slot
= (struct variable
**) from_set
->table
.ht_vec
;
1046 struct variable
**from_var_end
= from_var_slot
+ from_set
->table
.ht_size
;
1048 for ( ; from_var_slot
< from_var_end
; from_var_slot
++)
1049 if (! HASH_VACANT (*from_var_slot
))
1051 struct variable
*from_var
= *from_var_slot
;
1052 struct variable
**to_var_slot
1053 = (struct variable
**) hash_find_slot (&to_set
->table
, *from_var_slot
);
1054 if (HASH_VACANT (*to_var_slot
))
1055 hash_insert_at (&to_set
->table
, from_var
, to_var_slot
);
1058 /* GKM FIXME: delete in from_set->table */
1059 free (from_var
->value
);
1065 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
1068 merge_variable_set_lists (struct variable_set_list
**setlist0
,
1069 struct variable_set_list
*setlist1
)
1071 struct variable_set_list
*to
= *setlist0
;
1072 struct variable_set_list
*last0
= 0;
1074 /* If there's nothing to merge, stop now. */
1078 /* This loop relies on the fact that all setlists terminate with the global
1079 setlist (before NULL). If that's not true, arguably we SHOULD die. */
1081 while (setlist1
!= &global_setlist
&& to
!= &global_setlist
)
1083 struct variable_set_list
*from
= setlist1
;
1084 setlist1
= setlist1
->next
;
1086 merge_variable_sets (to
->set
, from
->set
);
1092 if (setlist1
!= &global_setlist
)
1095 *setlist0
= setlist1
;
1097 last0
->next
= setlist1
;
1101 /* Define the automatic variables, and record the addresses
1102 of their structures so we can change their values quickly. */
1105 define_automatic_variables (void)
1107 #if defined(WINDOWS32) || defined(__EMX__)
1108 extern char* default_shell
;
1110 extern char default_shell
[];
1112 register struct variable
*v
;
1118 struct variable
*envvar1
;
1119 struct variable
*envvar2
;
1122 sprintf (buf
, "%u", makelevel
);
1123 (void) define_variable (MAKELEVEL_NAME
, MAKELEVEL_LENGTH
, buf
, o_env
, 0);
1125 sprintf (buf
, "%s%s%s",
1127 (remote_description
== 0 || remote_description
[0] == '\0')
1129 (remote_description
== 0 || remote_description
[0] == '\0')
1130 ? "" : remote_description
);
1132 (void) define_variable ("MAKE_VERSION", 12, buf
, o_default
, 0);
1135 /* Define KMK_VERSION to indicate kMk. */
1136 (void) define_variable ("KMK_VERSION", 11, buf
, o_default
, 0);
1138 /* Define KBUILD_VERSION* */
1139 sprintf (buf
, "%d", KBUILD_VERSION_MAJOR
);
1140 define_variable ("KBUILD_VERSION_MAJOR", sizeof ("KBUILD_VERSION_MAJOR") - 1,
1142 sprintf (buf
, "%d", KBUILD_VERSION_MINOR
);
1143 define_variable ("KBUILD_VERSION_MINOR", sizeof("KBUILD_VERSION_MINOR") - 1,
1145 sprintf (buf
, "%d", KBUILD_VERSION_PATCH
);
1146 define_variable ("KBUILD_VERSION_PATCH", sizeof ("KBUILD_VERSION_PATCH") - 1,
1148 sprintf (buf
, "%d", KBUILD_SVN_REV
);
1149 define_variable ("KBUILD_KMK_REVISION", sizeof ("KBUILD_KMK_REVISION") - 1,
1152 sprintf (buf
, "%d.%d.%d-r%d", KBUILD_VERSION_MAJOR
, KBUILD_VERSION_MINOR
,
1153 KBUILD_VERSION_PATCH
, KBUILD_SVN_REV
);
1154 define_variable ("KBUILD_VERSION", sizeof ("KBUILD_VERSION") - 1,
1157 /* The host defaults. The BUILD_* stuff will be replaced by KBUILD_* soon. */
1158 envvar1
= lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST"));
1159 envvar2
= lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM"));
1160 val
= envvar1
? envvar1
->value
: envvar2
? envvar2
->value
: KBUILD_HOST
;
1161 if (envvar1
&& envvar2
&& strcmp (envvar1
->value
, envvar2
->value
))
1162 error (NULL
, _("KBUILD_HOST and BUILD_PLATFORM differs, using KBUILD_HOST=%s."), val
);
1164 define_variable ("KBUILD_HOST", sizeof ("KBUILD_HOST") - 1,
1167 define_variable ("BUILD_PLATFORM", sizeof ("BUILD_PLATFORM") - 1,
1170 envvar1
= lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_ARCH"));
1171 envvar2
= lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_ARCH"));
1172 val
= envvar1
? envvar1
->value
: envvar2
? envvar2
->value
: KBUILD_HOST_ARCH
;
1173 if (envvar1
&& envvar2
&& strcmp (envvar1
->value
, envvar2
->value
))
1174 error (NULL
, _("KBUILD_HOST_ARCH and BUILD_PLATFORM_ARCH differs, using KBUILD_HOST_ARCH=%s."), val
);
1176 define_variable ("KBUILD_HOST_ARCH", sizeof ("KBUILD_HOST_ARCH") - 1,
1179 define_variable ("BUILD_PLATFORM_ARCH", sizeof ("BUILD_PLATFORM_ARCH") - 1,
1182 envvar1
= lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_CPU"));
1183 envvar2
= lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_CPU"));
1184 val
= envvar1
? envvar1
->value
: envvar2
? envvar2
->value
: KBUILD_HOST_CPU
;
1185 if (envvar1
&& envvar2
&& strcmp (envvar1
->value
, envvar2
->value
))
1186 error (NULL
, _("KBUILD_HOST_CPU and BUILD_PLATFORM_CPU differs, using KBUILD_HOST_CPU=%s."), val
);
1188 define_variable ("KBUILD_HOST_CPU", sizeof ("KBUILD_HOST_CPU") - 1,
1191 define_variable ("BUILD_PLATFORM_CPU", sizeof ("BUILD_PLATFORM_CPU") - 1,
1194 /* The kBuild locations. */
1195 define_variable ("KBUILD_PATH", sizeof ("KBUILD_PATH") - 1,
1196 get_kbuild_path (), o_default
, 0);
1197 define_variable ("KBUILD_BIN_PATH", sizeof ("KBUILD_BIN_PATH") - 1,
1198 get_kbuild_bin_path (), o_default
, 0);
1200 define_variable ("PATH_KBUILD", sizeof ("PATH_KBUILD") - 1,
1201 get_kbuild_path (), o_default
, 0);
1202 define_variable ("PATH_KBUILD_BIN", sizeof ("PATH_KBUILD_BIN") - 1,
1203 get_kbuild_bin_path (), o_default
, 0);
1205 /* Define KMK_FEATURES to indicate various working KMK features. */
1206 # if defined (CONFIG_WITH_RSORT) \
1207 && defined (CONFIG_WITH_ABSPATHEX) \
1208 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1209 && defined (CONFIG_WITH_DEFINED) \
1210 && defined (CONFIG_WITH_VALUE_LENGTH) && defined (CONFIG_WITH_COMPARE) \
1211 && defined (CONFIG_WITH_STACK) \
1212 && defined (CONFIG_WITH_MATH) \
1213 && defined (CONFIG_WITH_XARGS) \
1214 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1215 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1216 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1217 && defined (CONFIG_WITH_DATE) \
1218 && defined (CONFIG_WITH_FILE_SIZE) \
1219 && defined (CONFIG_WITH_WHICH) \
1220 && defined (CONFIG_WITH_EVALPLUS) \
1221 && defined (CONFIG_WITH_MAKE_STATS) \
1222 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1223 && defined (KMK_HELPERS)
1224 (void) define_variable ("KMK_FEATURES", 12,
1225 "append-dash-n abspath"
1230 " comp-vars comp-cmds comp-cmds-ex"
1234 " explicit-multitarget"
1235 " prepend-assignment"
1240 " evalctx evalval evalvalctx evalcall evalcall2"
1243 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one "
1245 # else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1246 # error "All features should be enabled by default!"
1247 strcpy (buf
, "append-dash-n abspath");
1248 # if defined (CONFIG_WITH_RSORT)
1249 strcat (buf
, " rsort");
1251 # if defined (CONFIG_WITH_ABSPATHEX)
1252 strcat (buf
, " abspathex");
1254 # if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1255 strcat (buf
, " toupper tolower");
1257 # if defined (CONFIG_WITH_DEFINED)
1258 strcat (buf
, " defined");
1260 # if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1261 strcat (buf
, " comp-vars comp-cmds comp-cmds-ex");
1263 # if defined (CONFIG_WITH_STACK)
1264 strcat (buf
, " stack");
1266 # if defined (CONFIG_WITH_MATH)
1267 strcat (buf
, " math-int");
1269 # if defined (CONFIG_WITH_XARGS)
1270 strcat (buf
, " xargs");
1272 # if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1273 strcat (buf
, " explicit-multitarget");
1275 # if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1276 strcat (buf
, " prepend-assignment");
1278 # if defined (CONFIG_WITH_SET_CONDITIONALS)
1279 strcat (buf
, " set-conditionals");
1281 # if defined (CONFIG_WITH_DATE)
1282 strcat (buf
, " date");
1284 # if defined (CONFIG_WITH_FILE_SIZE)
1285 strcat (buf
, " file-size");
1287 # if defined (CONFIG_WITH_WHICH)
1288 strcat (buf
, " which");
1290 # if defined (CONFIG_WITH_EVALPLUS)
1291 strcat (buf
, " evalctx evalval evalvalctx evalcall evalcall2");
1293 # if defined (CONFIG_WITH_MAKE_STATS)
1294 strcat (buf
, " make-stats");
1296 # if defined (CONFIG_WITH_COMMANDS_FUNC)
1297 strcat (buf
, " commands");
1299 # if defined (KMK_HELPERS)
1300 strcat (buf
, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one");
1302 (void) define_variable ("KMK_FEATURES", 12, buf
, o_default
, 0);
1307 #ifdef CONFIG_WITH_KMK_BUILTIN
1308 /* The supported kMk Builtin commands. */
1309 (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);
1313 /* Allow to specify a special shell just for Make,
1314 and use $COMSPEC as the default $SHELL when appropriate. */
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);
1321 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
1323 (void) define_variable (shell_str
, shlen
,
1324 mshp
->value
, o_env_override
, 0);
1327 /* $COMSPEC shouldn't override $SHELL. */
1328 struct variable
*shp
= lookup_variable (shell_str
, shlen
);
1331 (void) define_variable (shell_str
, shlen
, comp
->value
, o_env
, 0);
1334 #elif defined(__EMX__)
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);
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
;
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
))
1351 /* overwrite whatever we got from the environment */
1353 shell
->value
= xstrdup (default_shell
);
1354 shell
->origin
= o_default
;
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);
1365 /* otherwise use $OS2_SHELL */
1366 if (!replace
|| !*replace
->value
)
1367 replace
= lookup_variable ("OS2_SHELL", 9);
1369 # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1372 if (replace
&& *replace
->value
)
1373 /* overwrite $SHELL */
1374 (void) define_variable (shell_str
, shlen
, replace
->value
,
1375 replace
->origin
, 0);
1377 /* provide a definition if there is none */
1378 (void) define_variable (shell_str
, shlen
, default_shell
,
1384 /* This won't override any definition, but it will provide one if there
1386 v
= define_variable ("SHELL", 5, default_shell
, o_default
, 0);
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
)
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;
1406 /* Make sure MAKEFILES gets exported if it is set. */
1407 v
= define_variable ("MAKEFILES", 9, "", o_default
, 0);
1408 v
->export
= v_ifset
;
1410 /* Define the magic D and F variables in terms of
1411 the automatic variables they are variations of. */
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);
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);
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);
1439 int export_all_variables
;
1441 /* Create a new environment for FILE's commands.
1442 If FILE is nil, this is for the `shell' function.
1443 The child's MAKELEVEL variable is incremented. */
1446 target_environment (struct file
*file
)
1448 struct variable_set_list
*set_list
;
1449 register struct variable_set_list
*s
;
1450 struct hash_table table
;
1451 struct variable
**v_slot
;
1452 struct variable
**v_end
;
1453 struct variable makelevel_key
;
1458 set_list
= current_variable_set_list
;
1460 set_list
= file
->variables
;
1462 hash_init (&table
, VARIABLE_BUCKETS
,
1463 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
1465 /* Run through all the variable sets in the list,
1466 accumulating variables in TABLE. */
1467 for (s
= set_list
; s
!= 0; s
= s
->next
)
1469 struct variable_set
*set
= s
->set
;
1470 v_slot
= (struct variable
**) set
->table
.ht_vec
;
1471 v_end
= v_slot
+ set
->table
.ht_size
;
1472 for ( ; v_slot
< v_end
; v_slot
++)
1473 if (! HASH_VACANT (*v_slot
))
1475 struct variable
**new_slot
;
1476 struct variable
*v
= *v_slot
;
1478 /* If this is a per-target variable and it hasn't been touched
1479 already then look up the global version and take its export
1481 if (v
->per_target
&& v
->export
== v_default
)
1483 struct variable
*gv
;
1485 gv
= lookup_variable_in_set (v
->name
, strlen(v
->name
),
1486 &global_variable_set
);
1488 v
->export
= gv
->export
;
1494 if (v
->origin
== o_default
|| v
->origin
== o_automatic
)
1495 /* Only export default variables by explicit request. */
1498 /* The variable doesn't have a name that can be exported. */
1499 if (! v
->exportable
)
1502 if (! export_all_variables
1503 && v
->origin
!= o_command
1504 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
1512 /* If this is the SHELL variable and it's not exported, then
1513 add the value from our original environment. */
1514 if (streq (v
->name
, "SHELL"))
1516 extern struct variable shell_var
;
1523 if (v
->origin
== o_default
)
1528 new_slot
= (struct variable
**) hash_find_slot (&table
, v
);
1529 if (HASH_VACANT (*new_slot
))
1530 hash_insert_at (&table
, v
, new_slot
);
1534 makelevel_key
.name
= MAKELEVEL_NAME
;
1535 makelevel_key
.length
= MAKELEVEL_LENGTH
;
1536 #ifdef VARIABLE_HASH /* bird */
1537 makelevel_key
.hash1
= variable_hash_1i (MAKELEVEL_NAME
, MAKELEVEL_LENGTH
);
1538 makelevel_key
.hash2
= 0;
1540 hash_delete (&table
, &makelevel_key
);
1542 result
= result_0
= xmalloc ((table
.ht_fill
+ 2) * sizeof (char *));
1544 v_slot
= (struct variable
**) table
.ht_vec
;
1545 v_end
= v_slot
+ table
.ht_size
;
1546 for ( ; v_slot
< v_end
; v_slot
++)
1547 if (! HASH_VACANT (*v_slot
))
1549 struct variable
*v
= *v_slot
;
1551 /* If V is recursively expanded and didn't come from the environment,
1552 expand its value. If it came from the environment, it should
1553 go back into the environment unchanged. */
1555 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
1557 char *value
= recursively_expand_for_file (v
, file
);
1559 if (strcmp(v
->name
, "Path") == 0 ||
1560 strcmp(v
->name
, "PATH") == 0)
1561 convert_Path_to_windows32(value
, ';');
1563 *result
++ = xstrdup (concat (v
->name
, "=", value
));
1569 if (strcmp(v
->name
, "Path") == 0 ||
1570 strcmp(v
->name
, "PATH") == 0)
1571 convert_Path_to_windows32(v
->value
, ';');
1573 *result
++ = xstrdup (concat (v
->name
, "=", v
->value
));
1577 *result
= xmalloc (100);
1578 sprintf (*result
, "%s=%u", MAKELEVEL_NAME
, makelevel
+ 1);
1581 hash_free (&table
, 0);
1586 #ifdef CONFIG_WITH_VALUE_LENGTH
1587 /* Worker function for do_variable_definition_append() and
1588 append_expanded_string_to_variable().
1589 The APPEND argument indicates whether it's an append or prepend operation. */
1590 void append_string_to_variable (struct variable
*v
, const char *value
, unsigned int value_len
, int append
)
1592 /* The previous definition of the variable was recursive.
1593 The new value is the unexpanded old and new values. */
1594 unsigned int new_value_len
= value_len
+ (v
->value_length
!= 0 ? 1 + v
->value_length
: 0);
1595 int done_1st_prepend_copy
= 0;
1597 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1601 /* adjust the size. */
1602 if ((unsigned)v
->value_alloc_len
<= new_value_len
+ 1)
1604 v
->value_alloc_len
*= 2;
1605 if (v
->value_alloc_len
< new_value_len
+ 1)
1606 v
->value_alloc_len
= (new_value_len
+ 1 + value_len
+ 0x7f) + ~0x7fU
;
1607 if (append
|| !v
->value_length
)
1608 v
->value
= xrealloc (v
->value
, v
->value_alloc_len
);
1611 /* avoid the extra memcpy the xrealloc may have to do */
1612 char *new_buf
= xmalloc (v
->value_alloc_len
);
1613 memcpy (&new_buf
[value_len
+ 1], v
->value
, v
->value_length
+ 1);
1614 done_1st_prepend_copy
= 1;
1620 /* insert the new bits */
1621 if (v
->value_length
!= 0)
1625 v
->value
[v
->value_length
] = ' ';
1626 memcpy (&v
->value
[v
->value_length
+ 1], value
, value_len
+ 1);
1630 if (!done_1st_prepend_copy
)
1631 memmove (&v
->value
[value_len
+ 1], v
->value
, v
->value_length
+ 1);
1632 v
->value
[value_len
] = ' ';
1633 memcpy (v
->value
, value
, value_len
);
1637 memcpy (v
->value
, value
, value_len
+ 1);
1638 v
->value_length
= new_value_len
;
1641 static struct variable
*
1642 do_variable_definition_append (const struct floc
*flocp
, struct variable
*v
, const char *value
,
1643 enum variable_origin origin
, int append
)
1645 if (env_overrides
&& origin
== o_env
)
1646 origin
= o_env_override
;
1648 if (env_overrides
&& v
->origin
== o_env
)
1649 /* V came from in the environment. Since it was defined
1650 before the switches were parsed, it wasn't affected by -e. */
1651 v
->origin
= o_env_override
;
1653 /* A variable of this name is already defined.
1654 If the old definition is from a stronger source
1655 than this one, don't redefine it. */
1656 if ((int) origin
< (int) v
->origin
)
1662 v
->fileinfo
= *flocp
;
1664 /* The juicy bits, append the specified value to the variable
1665 This is a heavily exercised code path in kBuild. */
1667 append_string_to_variable (v
, value
, strlen (value
), append
);
1669 /* The previous definition of the variable was simple.
1670 The new value comes from the old value, which was expanded
1671 when it was set; and from the expanded new value. */
1672 append_expanded_string_to_variable (v
, value
, append
);
1674 /* update the variable */
1677 #endif /* CONFIG_WITH_VALUE_LENGTH */
1679 /* Given a variable, a value, and a flavor, define the variable.
1680 See the try_variable_definition() function for details on the parameters. */
1683 do_variable_definition (const struct floc
*flocp
, const char *varname
,
1684 const char *value
, enum variable_origin origin
,
1685 enum variable_flavor flavor
, int target_var
)
1688 char *alloc_value
= NULL
;
1691 int conditional
= 0;
1692 const size_t varname_len
= strlen (varname
); /* bird */
1693 #ifdef CONFIG_WITH_VALUE_LENGTH
1694 unsigned int value_len
= ~0U;
1697 /* Calculate the variable's new value in VALUE. */
1703 /* Should not be possible. */
1706 /* A simple variable definition "var := value". Expand the value.
1707 We have to allocate memory since otherwise it'll clobber the
1708 variable buffer, and we may still need that if we're looking at a
1709 target-specific variable. */
1710 p
= alloc_value
= allocated_variable_expand (value
);
1713 /* A conditional variable definition "var ?= value".
1714 The value is set IFF the variable is not defined yet. */
1715 v
= lookup_variable (varname
, varname_len
);
1720 flavor
= f_recursive
;
1723 /* A recursive variable definition "var = value".
1724 The value is used verbatim. */
1727 #ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1731 const enum variable_flavor org_flavor
= flavor
;
1737 #ifdef CONFIG_WITH_LOCAL_VARIABLES
1738 /* If we have += but we're in a target or local variable context,
1739 we want to append only with other variables in the context of
1741 if (target_var
|| origin
== o_local
)
1743 /* If we have += but we're in a target variable context, we want to
1744 append only with other variables in the context of this target. */
1749 v
= lookup_variable_in_set (varname
, varname_len
,
1750 current_variable_set_list
->set
);
1752 /* Don't append from the global set if a previous non-appending
1753 target-specific variable definition exists. */
1754 if (v
&& !v
->append
)
1758 v
= lookup_variable (varname
, varname_len
);
1762 /* There was no old value.
1763 This becomes a normal recursive definition. */
1765 flavor
= f_recursive
;
1769 #ifdef CONFIG_WITH_VALUE_LENGTH
1771 # ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1772 return do_variable_definition_append (flocp
, v
, value
, origin
, org_flavor
== f_append
);
1774 return do_variable_definition_append (flocp
, v
, value
, origin
, 1);
1776 #else /* !CONFIG_WITH_VALUE_LENGTH */
1778 /* Paste the old and new values together in VALUE. */
1780 unsigned int oldlen
, vallen
;
1786 /* The previous definition of the variable was recursive.
1787 The new value is the unexpanded old and new values. */
1788 flavor
= f_recursive
;
1790 /* The previous definition of the variable was simple.
1791 The new value comes from the old value, which was expanded
1792 when it was set; and from the expanded new value. Allocate
1793 memory for the expansion as we may still need the rest of the
1794 buffer if we're looking at a target-specific variable. */
1795 val
= alloc_value
= allocated_variable_expand (val
);
1797 oldlen
= strlen (v
->value
);
1798 vallen
= strlen (val
);
1799 tp
= alloca (oldlen
+ 1 + vallen
+ 1);
1800 # ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1801 if (org_flavor
== f_prepend
)
1803 memcpy (tp
, val
, vallen
);
1805 memcpy (&tp
[oldlen
+ 1], v
->value
, oldlen
+ 1);
1808 # endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
1810 memcpy (tp
, v
->value
, oldlen
);
1812 memcpy (&tp
[oldlen
+ 1], val
, vallen
+ 1);
1815 #endif /* !CONFIG_WITH_VALUE_LENGTH */
1821 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1822 non-Unix systems don't conform to this default configuration (in
1823 fact, most of them don't even have `/bin'). On the other hand,
1824 $SHELL in the environment, if set, points to the real pathname of
1826 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1827 the Makefile override $SHELL from the environment. But first, we
1828 look for the basename of the shell in the directory where SHELL=
1829 points, and along the $PATH; if it is found in any of these places,
1830 we define $SHELL to be the actual pathname of the shell. Thus, if
1831 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1832 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1833 defining SHELL to be "d:/unix/bash.exe". */
1834 if ((origin
== o_file
|| origin
== o_override
)
1835 && strcmp (varname
, "SHELL") == 0)
1837 PATH_VAR (shellpath
);
1838 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1840 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1841 if (__dosexec_find_on_path (p
, NULL
, shellpath
))
1845 for (tp
= shellpath
; *tp
; tp
++)
1849 v
= define_variable_loc (varname
, varname_len
,
1850 shellpath
, origin
, flavor
== f_recursive
,
1855 const char *shellbase
, *bslash
;
1856 struct variable
*pathv
= lookup_variable ("PATH", 4);
1861 shellbase
= strrchr (p
, '/');
1862 bslash
= strrchr (p
, '\\');
1863 if (!shellbase
|| bslash
> shellbase
)
1865 if (!shellbase
&& p
[1] == ':')
1872 /* Search for the basename of the shell (with standard
1873 executable extensions) along the $PATH. */
1875 pathlen
= strlen (pathv
->value
);
1876 path_string
= xmalloc (5 + pathlen
+ 2 + 1);
1877 /* On MSDOS, current directory is considered as part of $PATH. */
1878 sprintf (path_string
, "PATH=.;%s", pathv
? pathv
->value
: "");
1879 fake_env
[0] = path_string
;
1881 if (__dosexec_find_on_path (shellbase
, fake_env
, shellpath
))
1885 for (tp
= shellpath
; *tp
; tp
++)
1889 v
= define_variable_loc (varname
, varname_len
,
1891 flavor
== f_recursive
, flocp
);
1894 v
= lookup_variable (varname
, varname_len
);
1900 #endif /* __MSDOS__ */
1902 if ( varname_len
== sizeof("SHELL") - 1 /* bird */
1903 && (origin
== o_file
|| origin
== o_override
|| origin
== o_command
)
1904 && streq (varname
, "SHELL"))
1906 extern char *default_shell
;
1908 /* Call shell locator function. If it returns TRUE, then
1909 set no_default_sh_exe to indicate sh was found and
1910 set new value for SHELL variable. */
1912 if (find_and_set_default_shell (p
))
1914 v
= define_variable_in_set (varname
, varname_len
, default_shell
,
1915 # ifdef CONFIG_WITH_VALUE_LENGTH
1916 ~0U, 1 /* duplicate_value */,
1918 origin
, flavor
== f_recursive
,
1920 ? current_variable_set_list
->set
1923 no_default_sh_exe
= 0;
1926 v
= lookup_variable (varname
, varname_len
);
1931 /* If we are defining variables inside an $(eval ...), we might have a
1932 different variable context pushed, not the global context (maybe we're
1933 inside a $(call ...) or something. Since this function is only ever
1934 invoked in places where we want to define globally visible variables,
1935 make sure we define this variable in the global set. */
1937 v
= define_variable_in_set (varname
, varname_len
, p
,
1938 #ifdef CONFIG_WITH_VALUE_LENGTH
1939 value_len
, !alloc_value
,
1941 origin
, flavor
== f_recursive
,
1942 #ifdef CONFIG_WITH_LOCAL_VARIABLES
1943 (target_var
|| origin
== o_local
1947 ? current_variable_set_list
->set
: NULL
),
1950 v
->conditional
= conditional
;
1952 #ifndef CONFIG_WITH_VALUE_LENGTH
1960 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1962 ORIGIN may be o_file, o_override, o_env, o_env_override,
1963 or o_command specifying that the variable definition comes
1964 from a makefile, an override directive, the environment with
1965 or without the -e switch, or the command line.
1967 See the comments for parse_variable_definition().
1969 If LINE was recognized as a variable definition, a pointer to its `struct
1970 variable' is returned. If LINE is not a variable definition, NULL is
1974 parse_variable_definition (struct variable
*v
, char *line
)
1977 register char *p
= line
;
1980 enum variable_flavor flavor
= f_bogus
;
1986 if (c
== '\0' || c
== '#')
1991 flavor
= f_recursive
;
2002 /* A colon other than := is a rule line, not a variable defn. */
2004 else if (c
== '+' && *p
== '=')
2010 #ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2011 else if (c
== '<' && *p
== '=')
2018 else if (c
== '?' && *p
== '=')
2021 flavor
= f_conditional
;
2026 /* This might begin a variable expansion reference. Make sure we
2027 don't misrecognize chars inside the reference as =, := or +=. */
2036 continue; /* Nope. */
2038 /* P now points past the opening paren or brace.
2039 Count parens or braces until it is matched. */
2041 for (; *p
!= '\0'; ++p
)
2045 else if (*p
== closeparen
&& --count
< 0)
2055 beg
= next_token (line
);
2056 while (end
> beg
&& isblank ((unsigned char)end
[-1]))
2060 #ifdef CONFIG_WITH_VALUE_LENGTH
2061 v
->value_length
= v
->value_alloc_len
= -1;
2064 /* Expand the name, so "$(foo)bar = baz" works. */
2065 name
= alloca (end
- beg
+ 1);
2066 memcpy (name
, beg
, end
- beg
);
2067 name
[end
- beg
] = '\0';
2068 v
->name
= allocated_variable_expand (name
);
2070 if (v
->name
[0] == '\0')
2071 fatal (&v
->fileinfo
, _("empty variable name"));
2076 /* Try to interpret LINE (a null-terminated string) as a variable definition.
2078 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
2079 or o_command specifying that the variable definition comes
2080 from a makefile, an override directive, the environment with
2081 or without the -e switch, or the command line.
2083 See the comments for parse_variable_definition().
2085 If LINE was recognized as a variable definition, a pointer to its `struct
2086 variable' is returned. If LINE is not a variable definition, NULL is
2090 try_variable_definition (const struct floc
*flocp
, char *line
,
2091 enum variable_origin origin
, int target_var
)
2094 struct variable
*vp
;
2097 v
.fileinfo
= *flocp
;
2099 v
.fileinfo
.filenm
= 0;
2101 if (!parse_variable_definition (&v
, line
))
2104 vp
= do_variable_definition (flocp
, v
.name
, v
.value
,
2105 origin
, v
.flavor
, target_var
);
2112 /* Print information for variable V, prefixing it with PREFIX. */
2115 print_variable (const void *item
, void *arg
)
2117 const struct variable
*v
= item
;
2118 const char *prefix
= arg
;
2124 origin
= _("default");
2127 origin
= _("environment");
2130 origin
= _("makefile");
2132 case o_env_override
:
2133 origin
= _("environment under -e");
2136 origin
= _("command line");
2139 origin
= _("`override' directive");
2142 origin
= _("automatic");
2144 #ifdef CONFIG_WITH_LOCAL_VARIABLES
2146 origin
= _("`local' directive");
2153 fputs ("# ", stdout
);
2154 fputs (origin
, stdout
);
2155 if (v
->fileinfo
.filenm
)
2156 printf (_(" (from `%s', line %lu)"),
2157 v
->fileinfo
.filenm
, v
->fileinfo
.lineno
);
2159 fputs (prefix
, stdout
);
2161 /* Is this a `define'? */
2162 if (v
->recursive
&& strchr (v
->value
, '\n') != 0)
2163 printf ("define %s\n%s\nendef\n", v
->name
, v
->value
);
2168 printf ("%s %s= ", v
->name
, v
->recursive
? v
->append
? "+" : "" : ":");
2170 /* Check if the value is just whitespace. */
2171 p
= next_token (v
->value
);
2172 if (p
!= v
->value
&& *p
== '\0')
2173 /* All whitespace. */
2174 printf ("$(subst ,,%s)", v
->value
);
2175 else if (v
->recursive
)
2176 fputs (v
->value
, stdout
);
2178 /* Double up dollar signs. */
2179 for (p
= v
->value
; *p
!= '\0'; ++p
)
2190 /* Print all the variables in SET. PREFIX is printed before
2191 the actual variable definitions (everything else is comments). */
2194 print_variable_set (struct variable_set
*set
, char *prefix
)
2196 hash_map_arg (&set
->table
, print_variable
, prefix
);
2198 fputs (_("# variable set hash-table stats:\n"), stdout
);
2199 fputs ("# ", stdout
);
2200 hash_print_stats (&set
->table
, stdout
);
2201 putc ('\n', stdout
);
2204 /* Print the data base of variables. */
2207 print_variable_data_base (void)
2209 puts (_("\n# Variables\n"));
2211 print_variable_set (&global_variable_set
, "");
2213 puts (_("\n# Pattern-specific Variable Values"));
2216 struct pattern_var
*p
;
2219 for (p
= pattern_vars
; p
!= 0; p
= p
->next
)
2222 printf ("\n%s :\n", p
->target
);
2223 print_variable (&p
->variable
, "# ");
2227 puts (_("\n# No pattern-specific variable values."));
2229 printf (_("\n# %u pattern-specific variable values"), rules
);
2234 /* Print all the local variables of FILE. */
2237 print_file_variables (const struct file
*file
)
2239 if (file
->variables
!= 0)
2240 print_variable_set (file
->variables
->set
, "# ");
2245 sync_Path_environment (void)
2247 char *path
= allocated_variable_expand ("$(PATH)");
2248 static char *environ_path
= NULL
;
2254 * If done this before, don't leak memory unnecessarily.
2255 * Free the previous entry before allocating new one.
2258 free (environ_path
);
2261 * Create something WINDOWS32 world can grok
2263 convert_Path_to_windows32 (path
, ';');
2264 environ_path
= xstrdup (concat ("PATH", "=", path
));
2265 putenv (environ_path
);