VirtualBox

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

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

kmk: variable hash hacking. (yet again)

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