VirtualBox

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

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

kmk: cache variable name strings.

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