VirtualBox

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

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

kmk: variable+strcache2 hacking.

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