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

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

kmk: use alloc caches for variables, variable sets and varaible set lists.

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