VirtualBox

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

Last change on this file since 1840 was 1840, checked in by bird, 16 years ago

better? predictions can do bad stuff...

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

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