source: trunk/src/kmk/variable.c@ 1863

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

kmk: Allocation caches for nameseq, dep and idep. next: variable.

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