VirtualBox

source: kBuild/trunk/src/kmk/kbuild.c@ 2771

Last change on this file since 2771 was 2771, checked in by bird, 10 years ago

Optimizations, tuning and bug fixes for the 'compiled' string expansion code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 96.6 KB
Line 
1/* $Id: kbuild.c 2771 2015-02-01 20:48:36Z bird $ */
2/** @file
3 * kBuild specific make functionality.
4 */
5
6/*
7 * Copyright (c) 2006-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
8 *
9 * This file is part of kBuild.
10 *
11 * kBuild is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * kBuild is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild. If not, see <http://www.gnu.org/licenses/>
23 *
24 */
25
26/* No GNU coding style here! */
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "make.h"
32#include "filedef.h"
33#include "variable.h"
34#include "dep.h"
35#include "debug.h"
36#ifdef WINDOWS32
37# include "pathstuff.h"
38# include <Windows.h>
39#endif
40#if defined(__APPLE__)
41# include <mach-o/dyld.h>
42#endif
43#if defined(__FreeBSD__)
44# include <dlfcn.h>
45# include <sys/link_elf.h>
46#endif
47
48#include "kbuild.h"
49
50#include <assert.h>
51
52
53/*******************************************************************************
54* Defined Constants And Macros *
55*******************************************************************************/
56/** Helper for passing a string constant to kbuild_get_variable_n. */
57#define ST(strconst) strconst, sizeof(strconst) - 1
58
59#if 1
60# define my_memcpy(dst, src, len) \
61 do { \
62 if (len > 8) \
63 memcpy(dst, src, len); \
64 else \
65 switch (len) \
66 { \
67 case 8: dst[7] = src[7]; \
68 case 7: dst[6] = src[6]; \
69 case 6: dst[5] = src[5]; \
70 case 5: dst[4] = src[4]; \
71 case 4: dst[3] = src[3]; \
72 case 3: dst[2] = src[2]; \
73 case 2: dst[1] = src[1]; \
74 case 1: dst[0] = src[0]; \
75 case 0: break; \
76 } \
77 } while (0)
78#elif defined(__GNUC__)
79# define my_memcpy __builtin_memcpy
80#elif defined(_MSC_VER)
81# pragma instrinic(memcpy)
82# define my_memcpy memcpy
83#endif
84
85
86/*******************************************************************************
87* Global Variables *
88*******************************************************************************/
89/** The argv[0] passed to main. */
90static const char *g_pszExeName;
91/** The initial working directory. */
92static char *g_pszInitialCwd;
93
94
95/**
96 * Initialize kBuild stuff.
97 *
98 * @param argc Number of arguments to main().
99 * @param argv The main() argument vector.
100 */
101void init_kbuild(int argc, char **argv)
102{
103 int rc;
104 PATH_VAR(szTmp);
105
106 /*
107 * Get the initial cwd for use in my_abspath.
108 */
109#ifdef WINDOWS32
110 if (getcwd_fs(szTmp, GET_PATH_MAX) != 0)
111#else
112 if (getcwd(szTmp, GET_PATH_MAX) != 0)
113#endif
114 g_pszInitialCwd = xstrdup(szTmp);
115 else
116 fatal(NILF, _("getcwd failed"));
117
118 /*
119 * Determin the executable name.
120 */
121 rc = -1;
122#if defined(__APPLE__)
123 {
124 const char *pszImageName = _dyld_get_image_name(0);
125 if (pszImageName)
126 {
127 size_t cchImageName = strlen(pszImageName);
128 if (cchImageName < GET_PATH_MAX)
129 {
130 memcpy(szTmp, pszImageName, cchImageName + 1);
131 rc = 0;
132 }
133 }
134 }
135
136#elif defined(__FreeBSD__)
137 rc = readlink("/proc/curproc/file", szTmp, GET_PATH_MAX - 1);
138 if (rc < 0 || rc == GET_PATH_MAX - 1)
139 {
140 rc = -1;
141# if 0 /* doesn't work because l_name isn't always absolute, it's just argv0 from exec or something. */
142 /* /proc is optional, try rtdl. */
143 void *hExe = dlopen(NULL, 0);
144 rc = -1;
145 if (hExe)
146 {
147 struct link_map const *pLinkMap = 0;
148 if (dlinfo(hExe, RTLD_DI_LINKMAP, &pLinkMap) == 0)
149 {
150 const char *pszImageName = pLinkMap->l_name;
151 size_t cchImageName = strlen(pszImageName);
152 if (cchImageName < GET_PATH_MAX)
153 {
154 memcpy(szTmp, pszImageName, cchImageName + 1);
155 rc = 0;
156 }
157 }
158
159 }
160# endif
161 }
162 else
163 szTmp[rc] = '\0';
164
165#elif defined(__gnu_linux__) || defined(__linux__)
166 rc = readlink("/proc/self/exe", szTmp, GET_PATH_MAX - 1);
167 if (rc < 0 || rc == GET_PATH_MAX - 1)
168 rc = -1;
169 else
170 szTmp[rc] = '\0';
171
172#elif defined(__OS2__)
173 _execname(szTmp, GET_PATH_MAX);
174 rc = 0;
175
176#elif defined(__sun__)
177 {
178 char szTmp2[64];
179 snprintf(szTmp2, sizeof(szTmp2), "/proc/%ld/path/a.out", (long)getpid());
180 rc = readlink(szTmp2, szTmp, GET_PATH_MAX - 1);
181 if (rc < 0 || rc == GET_PATH_MAX - 1)
182 rc = -1;
183 else
184 szTmp[rc] = '\0';
185 }
186
187#elif defined(WINDOWS32)
188 if (GetModuleFileName(GetModuleHandle(NULL), szTmp, GET_PATH_MAX))
189 rc = 0;
190
191#endif
192
193#if !defined(__OS2__) && !defined(WINDOWS32)
194 /* fallback, try use the path to locate the binary. */
195 if ( rc < 0
196 && access(argv[0], X_OK))
197 {
198 size_t cchArgv0 = strlen(argv[0]);
199 const char *pszPath = getenv("PATH");
200 char *pszCopy = xstrdup(pszPath ? pszPath : ".");
201 char *psz = pszCopy;
202 while (*psz)
203 {
204 size_t cch;
205 char *pszEnd = strchr(psz, PATH_SEPARATOR_CHAR);
206 if (!pszEnd)
207 pszEnd = strchr(psz, '\0');
208 cch = pszEnd - psz;
209 if (cch + cchArgv0 + 2 <= GET_PATH_MAX)
210 {
211 memcpy(szTmp, psz, cch);
212 szTmp[cch] = '/';
213 memcpy(&szTmp[cch + 1], argv[0], cchArgv0 + 1);
214 if (!access(szTmp, X_OK))
215 {
216 rc = 0;
217 break;
218 }
219 }
220
221 /* next */
222 psz = pszEnd;
223 while (*psz == PATH_SEPARATOR_CHAR)
224 psz++;
225 }
226 free(pszCopy);
227 }
228#endif
229
230 if (rc < 0)
231 g_pszExeName = argv[0];
232 else
233 g_pszExeName = xstrdup(szTmp);
234
235 (void)argc;
236}
237
238
239/**
240 * Wrapper that ensures correct starting_directory.
241 */
242static char *my_abspath(const char *pszIn, char *pszOut)
243{
244 char *pszSaved, *pszRet;
245
246 pszSaved = starting_directory;
247 starting_directory = g_pszInitialCwd;
248 pszRet = abspath(pszIn, pszOut);
249 starting_directory = pszSaved;
250
251 return pszRet;
252}
253
254
255/**
256 * Determin the KBUILD_PATH value.
257 *
258 * @returns Pointer to static a buffer containing the value (consider it read-only).
259 */
260const char *get_kbuild_path(void)
261{
262 static const char *s_pszPath = NULL;
263 if (!s_pszPath)
264 {
265 PATH_VAR(szTmpPath);
266 const char *pszEnvVar = getenv("KBUILD_PATH");
267 if ( !pszEnvVar
268 || !my_abspath(pszEnvVar, szTmpPath))
269 {
270 pszEnvVar = getenv("PATH_KBUILD");
271 if ( !pszEnvVar
272 || !my_abspath(pszEnvVar, szTmpPath))
273 {
274#ifdef KBUILD_PATH
275 return s_pszPath = KBUILD_PATH;
276#else
277 /* $(abspath $(KBUILD_BIN_PATH)/../..)*/
278 size_t cch = strlen(get_kbuild_bin_path());
279 char *pszTmp2 = alloca(cch + sizeof("/../.."));
280 strcat(strcpy(pszTmp2, get_kbuild_bin_path()), "/../..");
281 if (!my_abspath(pszTmp2, szTmpPath))
282 fatal(NILF, _("failed to determin KBUILD_PATH"));
283#endif
284 }
285 }
286 s_pszPath = xstrdup(szTmpPath);
287 }
288 return s_pszPath;
289}
290
291
292/**
293 * Determin the KBUILD_BIN_PATH value.
294 *
295 * @returns Pointer to static a buffer containing the value (consider it read-only).
296 */
297const char *get_kbuild_bin_path(void)
298{
299 static const char *s_pszPath = NULL;
300 if (!s_pszPath)
301 {
302 PATH_VAR(szTmpPath);
303
304 const char *pszEnvVar = getenv("KBUILD_BIN_PATH");
305 if ( !pszEnvVar
306 || !my_abspath(pszEnvVar, szTmpPath))
307 {
308 pszEnvVar = getenv("PATH_KBUILD_BIN");
309 if ( !pszEnvVar
310 || !my_abspath(pszEnvVar, szTmpPath))
311 {
312#ifdef KBUILD_PATH
313 return s_pszPath = KBUILD_BIN_PATH;
314#else
315 /* $(abspath $(dir $(ARGV0)).) */
316 size_t cch = strlen(g_pszExeName);
317 char *pszTmp2 = alloca(cch + sizeof("."));
318 char *pszSep = pszTmp2 + cch - 1;
319 memcpy(pszTmp2, g_pszExeName, cch);
320# ifdef HAVE_DOS_PATHS
321 while (pszSep >= pszTmp2 && *pszSep != '/' && *pszSep != '\\' && *pszSep != ':')
322# else
323 while (pszSep >= pszTmp2 && *pszSep != '/')
324# endif
325 pszSep--;
326 if (pszSep >= pszTmp2)
327 strcpy(pszSep + 1, ".");
328 else
329 strcpy(pszTmp2, ".");
330
331 if (!my_abspath(pszTmp2, szTmpPath))
332 fatal(NILF, _("failed to determin KBUILD_BIN_PATH (pszTmp2=%s szTmpPath=%s)"), pszTmp2, szTmpPath);
333#endif /* !KBUILD_PATH */
334 }
335 }
336 s_pszPath = xstrdup(szTmpPath);
337 }
338 return s_pszPath;
339}
340
341
342/**
343 * Determin the location of default kBuild shell.
344 *
345 * @returns Pointer to static a buffer containing the location (consider it read-only).
346 */
347const char *get_default_kbuild_shell(void)
348{
349 static char *s_pszDefaultShell = NULL;
350 if (!s_pszDefaultShell)
351 {
352#if defined(__OS2__) || defined(_WIN32) || defined(WINDOWS32)
353 static const char s_szShellName[] = "/kmk_ash.exe";
354#else
355 static const char s_szShellName[] = "/kmk_ash";
356#endif
357 const char *pszBin = get_kbuild_bin_path();
358 size_t cchBin = strlen(pszBin);
359 s_pszDefaultShell = xmalloc(cchBin + sizeof(s_szShellName));
360 memcpy(s_pszDefaultShell, pszBin, cchBin);
361 memcpy(&s_pszDefaultShell[cchBin], s_szShellName, sizeof(s_szShellName));
362 }
363 return s_pszDefaultShell;
364}
365
366#ifdef KMK_HELPERS
367
368/**
369 * Applies the specified default path to any relative paths in *ppsz.
370 *
371 * @param pDefPath The default path.
372 * @param ppsz Pointer to the string pointer. If we expand anything, *ppsz
373 * will be replaced and the caller is responsible for calling free() on it.
374 * @param pcch IN: *pcch contains the current string length.
375 * OUT: *pcch contains the new string length.
376 * @param pcchAlloc *pcchAlloc contains the length allocated for the string. Can be NULL.
377 * @param fCanFree Whether *ppsz should be freed when we replace it.
378 */
379static void
380kbuild_apply_defpath(struct variable *pDefPath, char **ppsz, unsigned int *pcch, unsigned int *pcchAlloc, int fCanFree)
381{
382 const char *pszIterator;
383 const char *pszInCur;
384 unsigned int cchInCur;
385 unsigned int cRelativePaths;
386
387 /*
388 * The first pass, count the relative paths.
389 */
390 cRelativePaths = 0;
391 pszIterator = *ppsz;
392 while ((pszInCur = find_next_token(&pszIterator, &cchInCur)))
393 {
394 /* is relative? */
395#ifdef HAVE_DOS_PATHS
396 if (pszInCur[0] != '/' && pszInCur[0] != '\\' && (cchInCur < 2 || pszInCur[1] != ':'))
397#else
398 if (pszInCur[0] != '/')
399#endif
400 cRelativePaths++;
401 }
402
403 /*
404 * The second pass construct the new string.
405 */
406 if (cRelativePaths)
407 {
408 const size_t cchOut = *pcch + cRelativePaths * (pDefPath->value_length + 1) + 1;
409 char *pszOut = xmalloc(cchOut);
410 char *pszOutCur = pszOut;
411 const char *pszInNextCopy = *ppsz;
412
413 cRelativePaths = 0;
414 pszIterator = *ppsz;
415 while ((pszInCur = find_next_token(&pszIterator, &cchInCur)))
416 {
417 /* is relative? */
418#ifdef HAVE_DOS_PATHS
419 if (pszInCur[0] != '/' && pszInCur[0] != '\\' && (cchInCur < 2 || pszInCur[1] != ':'))
420#else
421 if (pszInCur[0] != '/')
422#endif
423 {
424 PATH_VAR(szAbsPathIn);
425 PATH_VAR(szAbsPathOut);
426
427 if (pDefPath->value_length + cchInCur + 1 >= GET_PATH_MAX)
428 continue;
429
430 /* Create the abspath input. */
431 memcpy(szAbsPathIn, pDefPath->value, pDefPath->value_length);
432 szAbsPathIn[pDefPath->value_length] = '/';
433 memcpy(&szAbsPathIn[pDefPath->value_length + 1], pszInCur, cchInCur);
434 szAbsPathIn[pDefPath->value_length + 1 + cchInCur] = '\0';
435
436 if (abspath(szAbsPathIn, szAbsPathOut) != NULL)
437 {
438 const size_t cchAbsPathOut = strlen(szAbsPathOut);
439 assert(cchAbsPathOut <= pDefPath->value_length + 1 + cchInCur);
440
441 /* copy leading input */
442 if (pszInCur != pszInNextCopy)
443 {
444 const size_t cchCopy = pszInCur - pszInNextCopy;
445 memcpy(pszOutCur, pszInNextCopy, cchCopy);
446 pszOutCur += cchCopy;
447 }
448 pszInNextCopy = pszInCur + cchInCur;
449
450 /* copy out the abspath. */
451 memcpy(pszOutCur, szAbsPathOut, cchAbsPathOut);
452 pszOutCur += cchAbsPathOut;
453 }
454 }
455 }
456 /* the final copy (includes the nil). */
457 cchInCur = *ppsz + *pcch - pszInNextCopy;
458 memcpy(pszOutCur, pszInNextCopy, cchInCur);
459 pszOutCur += cchInCur;
460 *pszOutCur = '\0';
461 assert((size_t)(pszOutCur - pszOut) < cchOut);
462
463 /* set return values */
464 if (fCanFree)
465 free(*ppsz);
466 *ppsz = pszOut;
467 *pcch = pszOutCur - pszOut;
468 if (pcchAlloc)
469 *pcchAlloc = cchOut;
470 }
471}
472
473/**
474 * Gets a variable that must exist.
475 * Will cause a fatal failure if the variable doesn't exist.
476 *
477 * @returns Pointer to the variable.
478 * @param pszName The variable name.
479 * @param cchName The name length.
480 */
481MY_INLINE struct variable *
482kbuild_get_variable_n(const char *pszName, size_t cchName)
483{
484 struct variable *pVar = lookup_variable(pszName, cchName);
485 if (!pVar)
486 fatal(NILF, _("variable `%.*s' isn't defined!"), (int)cchName, pszName);
487 if (pVar->recursive)
488 fatal(NILF, _("variable `%.*s' is defined as `recursive' instead of `simple'!"), (int)cchName, pszName);
489
490 MY_ASSERT_MSG(strlen(pVar->value) == pVar->value_length,
491 ("%u != %u %.*s\n", pVar->value_length, (unsigned int)strlen(pVar->value), (int)cchName, pVar->name));
492 return pVar;
493}
494
495
496/**
497 * Gets a variable that must exist and can be recursive.
498 * Will cause a fatal failure if the variable doesn't exist.
499 *
500 * @returns Pointer to the variable.
501 * @param pszName The variable name.
502 */
503static struct variable *
504kbuild_get_recursive_variable(const char *pszName)
505{
506 struct variable *pVar = lookup_variable(pszName, strlen(pszName));
507 if (!pVar)
508 fatal(NILF, _("variable `%s' isn't defined!"), pszName);
509
510 MY_ASSERT_MSG(strlen(pVar->value) == pVar->value_length,
511 ("%u != %u %s\n", pVar->value_length, (unsigned int)strlen(pVar->value), pVar->name));
512 return pVar;
513}
514
515
516/**
517 * Gets a variable that doesn't have to exit, but if it does can be recursive.
518 *
519 * @returns Pointer to the variable.
520 * NULL if not found.
521 * @param pszName The variable name. Doesn't need to be terminated.
522 * @param cchName The name length.
523 */
524static struct variable *
525kbuild_query_recursive_variable_n(const char *pszName, size_t cchName)
526{
527 struct variable *pVar = lookup_variable(pszName, cchName);
528 MY_ASSERT_MSG(!pVar || strlen(pVar->value) == pVar->value_length,
529 ("%u != %u %.*s\n", pVar->value_length, (unsigned int)strlen(pVar->value), (int)cchName, pVar->name));
530 return pVar;
531}
532
533
534/**
535 * Gets a variable that doesn't have to exit, but if it does can be recursive.
536 *
537 * @returns Pointer to the variable.
538 * NULL if not found.
539 * @param pszName The variable name.
540 */
541static struct variable *
542kbuild_query_recursive_variable(const char *pszName)
543{
544 return kbuild_query_recursive_variable_n(pszName, strlen(pszName));
545}
546
547
548/**
549 * Converts the specified variable into a 'simple' one.
550 * @returns pVar.
551 * @param pVar The variable.
552 */
553static struct variable *
554kbuild_simplify_variable(struct variable *pVar)
555{
556 if (memchr(pVar->value, '$', pVar->value_length))
557 {
558 unsigned int value_len;
559 char *pszExpanded = allocated_variable_expand_2(pVar->value, pVar->value_length, &value_len);
560#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
561 if (pVar->rdonly_val)
562 pVar->rdonly_val = 0;
563 else
564#endif
565 free(pVar->value);
566 assert(pVar->origin != o_automatic);
567 pVar->value = pszExpanded;
568 pVar->value_length = value_len;
569 pVar->value_alloc_len = value_len + 1;
570 }
571 pVar->recursive = 0;
572 VARIABLE_CHANGED(pVar);
573 return pVar;
574}
575
576
577/**
578 * Looks up a variable.
579 * The value_length field is valid upon successful return.
580 *
581 * @returns Pointer to the variable. NULL if not found.
582 * @param pszName The variable name.
583 * @param cchName The name length.
584 */
585MY_INLINE struct variable *
586kbuild_lookup_variable_n(const char *pszName, size_t cchName)
587{
588 struct variable *pVar = lookup_variable(pszName, cchName);
589 if (pVar)
590 {
591 MY_ASSERT_MSG(strlen(pVar->value) == pVar->value_length,
592 ("%u != %u %.*s\n", pVar->value_length, (unsigned int)strlen(pVar->value), (int)cchName, pVar->name));
593
594 /* Make sure the variable is simple, convert it if necessary. */
595 if (pVar->recursive)
596 kbuild_simplify_variable(pVar);
597 }
598 return pVar;
599}
600
601
602/**
603 * Looks up a variable.
604 * The value_length field is valid upon successful return.
605 *
606 * @returns Pointer to the variable. NULL if not found.
607 * @param pszName The variable name.
608 */
609MY_INLINE struct variable *
610kbuild_lookup_variable(const char *pszName)
611{
612 return kbuild_lookup_variable_n(pszName, strlen(pszName));
613}
614
615
616/**
617 * Looks up a variable and applies default a path to all relative paths.
618 * The value_length field is valid upon successful return.
619 *
620 * @returns Pointer to the variable. NULL if not found.
621 * @param pDefPath The default path.
622 * @param pszName The variable name.
623 * @param cchName The name length.
624 */
625MY_INLINE struct variable *
626kbuild_lookup_variable_defpath_n(struct variable *pDefPath, const char *pszName, size_t cchName)
627{
628 struct variable *pVar = kbuild_lookup_variable_n(pszName, cchName);
629 if (pVar && pDefPath)
630 {
631 assert(pVar->origin != o_automatic);
632#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
633 assert(!pVar->rdonly_val);
634#endif
635 kbuild_apply_defpath(pDefPath, &pVar->value, &pVar->value_length, &pVar->value_alloc_len, 1);
636 }
637 return pVar;
638}
639
640
641/**
642 * Looks up a variable and applies default a path to all relative paths.
643 * The value_length field is valid upon successful return.
644 *
645 * @returns Pointer to the variable. NULL if not found.
646 * @param pDefPath The default path.
647 * @param pszName The variable name.
648 */
649MY_INLINE struct variable *
650kbuild_lookup_variable_defpath(struct variable *pDefPath, const char *pszName)
651{
652 struct variable *pVar = kbuild_lookup_variable(pszName);
653 if (pVar && pDefPath)
654 {
655 assert(pVar->origin != o_automatic);
656#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
657 assert(!pVar->rdonly_val);
658#endif
659 kbuild_apply_defpath(pDefPath, &pVar->value, &pVar->value_length, &pVar->value_alloc_len, 1);
660 }
661 return pVar;
662}
663
664
665/**
666 * Gets the first defined property variable.
667 */
668static struct variable *
669kbuild_first_prop(struct variable *pTarget, struct variable *pSource,
670 struct variable *pTool, struct variable *pType,
671 struct variable *pBldTrg, struct variable *pBldTrgArch,
672 const char *pszPropF1, char cchPropF1,
673 const char *pszPropF2, char cchPropF2,
674 const char *pszVarName)
675{
676 struct variable *pVar;
677 size_t cchBuf;
678 char *pszBuf;
679 char *psz, *psz1, *psz2, *psz3, *psz4, *pszEnd;
680
681 /* calc and allocate a too big name buffer. */
682 cchBuf = cchPropF2 + 1
683 + cchPropF1 + 1
684 + pTarget->value_length + 1
685 + pSource->value_length + 1
686 + (pTool ? pTool->value_length + 1 : 0)
687 + pType->value_length + 1
688 + pBldTrg->value_length + 1
689 + pBldTrgArch->value_length + 1;
690 pszBuf = xmalloc(cchBuf);
691
692#define ADD_VAR(pVar) do { my_memcpy(psz, (pVar)->value, (pVar)->value_length); psz += (pVar)->value_length; } while (0)
693#define ADD_STR(pszStr, cchStr) do { my_memcpy(psz, (pszStr), (cchStr)); psz += (cchStr); } while (0)
694#define ADD_CSTR(pszStr) do { my_memcpy(psz, pszStr, sizeof(pszStr) - 1); psz += sizeof(pszStr) - 1; } while (0)
695#define ADD_CH(ch) do { *psz++ = (ch); } while (0)
696
697 /*
698 * $(target)_$(source)_$(type)$(propf2).$(bld_trg).$(bld_trg_arch)
699 */
700 psz = pszBuf;
701 ADD_VAR(pTarget);
702 ADD_CH('_');
703 ADD_VAR(pSource);
704 ADD_CH('_');
705 psz2 = psz;
706 ADD_VAR(pType);
707 ADD_STR(pszPropF2, cchPropF2);
708 psz3 = psz;
709 ADD_CH('.');
710 ADD_VAR(pBldTrg);
711 psz4 = psz;
712 ADD_CH('.');
713 ADD_VAR(pBldTrgArch);
714 pVar = kbuild_lookup_variable_n(pszBuf, psz - pszBuf);
715
716 /* $(target)_$(source)_$(type)$(propf2).$(bld_trg) */
717 if (!pVar)
718 pVar = kbuild_lookup_variable_n(pszBuf, psz4 - pszBuf);
719
720 /* $(target)_$(source)_$(type)$(propf2) */
721 if (!pVar)
722 pVar = kbuild_lookup_variable_n(pszBuf, psz3 - pszBuf);
723
724 /*
725 * $(target)_$(source)_$(propf2).$(bld_trg).$(bld_trg_arch)
726 */
727 if (!pVar)
728 {
729 psz = psz2;
730 ADD_STR(pszPropF2, cchPropF2);
731 psz3 = psz;
732 ADD_CH('.');
733 ADD_VAR(pBldTrg);
734 psz4 = psz;
735 ADD_CH('.');
736 ADD_VAR(pBldTrgArch);
737 pVar = kbuild_lookup_variable_n(pszBuf, psz - pszBuf);
738
739 /* $(target)_$(source)_$(propf2).$(bld_trg) */
740 if (!pVar)
741 pVar = kbuild_lookup_variable_n(pszBuf, psz4 - pszBuf);
742
743 /* $(target)_$(source)_$(propf2) */
744 if (!pVar)
745 pVar = kbuild_lookup_variable_n(pszBuf, psz3 - pszBuf);
746 }
747
748
749 /*
750 * $(source)_$(type)$(propf2).$(bld_trg).$(bld_trg_arch)
751 */
752 if (!pVar)
753 {
754 psz = pszBuf;
755 ADD_VAR(pSource);
756 ADD_CH('_');
757 psz2 = psz;
758 ADD_VAR(pType);
759 ADD_STR(pszPropF2, cchPropF2);
760 psz3 = psz;
761 ADD_CH('.');
762 ADD_VAR(pBldTrg);
763 psz4 = psz;
764 ADD_CH('.');
765 ADD_VAR(pBldTrgArch);
766 pVar = kbuild_lookup_variable_n(pszBuf, psz - pszBuf);
767
768 /* $(source)_$(type)$(propf2).$(bld_trg) */
769 if (!pVar)
770 pVar = kbuild_lookup_variable_n(pszBuf, psz4 - pszBuf);
771
772 /* $(source)_$(type)$(propf2) */
773 if (!pVar)
774 pVar = kbuild_lookup_variable_n(pszBuf, psz3 - pszBuf);
775
776 /*
777 * $(source)_$(propf2).$(bld_trg).$(bld_trg_arch)
778 */
779 if (!pVar)
780 {
781 psz = psz2;
782 ADD_STR(pszPropF2, cchPropF2);
783 psz3 = psz;
784 ADD_CH('.');
785 ADD_VAR(pBldTrg);
786 psz4 = psz;
787 ADD_CH('.');
788 ADD_VAR(pBldTrgArch);
789 pVar = kbuild_lookup_variable_n(pszBuf, psz - pszBuf);
790
791 /* $(source)_$(propf2).$(bld_trg) */
792 if (!pVar)
793 pVar = kbuild_lookup_variable_n(pszBuf, psz4 - pszBuf);
794
795 /* $(source)_$(propf2) */
796 if (!pVar)
797 pVar = kbuild_lookup_variable_n(pszBuf, psz3 - pszBuf);
798 }
799 }
800
801 /*
802 * $(target)_$(type)$(propf2).$(bld_trg).$(bld_trg_arch)
803 */
804 if (!pVar)
805 {
806 psz = pszBuf;
807 ADD_VAR(pTarget);
808 ADD_CH('_');
809 psz2 = psz;
810 ADD_VAR(pType);
811 ADD_STR(pszPropF2, cchPropF2);
812 psz3 = psz;
813 ADD_CH('.');
814 ADD_VAR(pBldTrg);
815 psz4 = psz;
816 ADD_CH('.');
817 ADD_VAR(pBldTrgArch);
818 pVar = kbuild_lookup_variable_n(pszBuf, psz - pszBuf);
819
820 /* $(target)_$(type)$(propf2).$(bld_trg) */
821 if (!pVar)
822 pVar = kbuild_lookup_variable_n(pszBuf, psz4 - pszBuf);
823
824 /* $(target)_$(type)$(propf2) */
825 if (!pVar)
826 pVar = kbuild_lookup_variable_n(pszBuf, psz3 - pszBuf);
827
828 /* $(target)_$(propf2).$(bld_trg).$(bld_trg_arch) */
829 if (!pVar)
830 {
831 psz = psz2;
832 ADD_STR(pszPropF2, cchPropF2);
833 psz3 = psz;
834 ADD_CH('.');
835 ADD_VAR(pBldTrg);
836 psz4 = psz;
837 ADD_CH('.');
838 ADD_VAR(pBldTrgArch);
839 pVar = kbuild_lookup_variable_n(pszBuf, psz - pszBuf);
840 }
841
842 /* $(target)_$(propf2).$(bld_trg) */
843 if (!pVar)
844 pVar = kbuild_lookup_variable_n(pszBuf, psz4 - pszBuf);
845
846 /* $(target)_$(propf2) */
847 if (!pVar)
848 pVar = kbuild_lookup_variable_n(pszBuf, psz3 - pszBuf);
849 }
850
851 /*
852 * TOOL_$(tool)_$(type)$(propf2).$(bld_trg).$(bld_trg_arch)
853 */
854 if (!pVar && pTool)
855 {
856 psz = pszBuf;
857 ADD_CSTR("TOOL_");
858 ADD_VAR(pTool);
859 ADD_CH('_');
860 psz2 = psz;
861 ADD_VAR(pType);
862 ADD_STR(pszPropF2, cchPropF2);
863 psz3 = psz;
864 ADD_CH('.');
865 ADD_VAR(pBldTrg);
866 psz4 = psz;
867 ADD_CH('.');
868 ADD_VAR(pBldTrgArch);
869 pVar = kbuild_lookup_variable_n(pszBuf, psz - pszBuf);
870
871 /* TOOL_$(tool)_$(type)$(propf2).$(bld_trg) */
872 if (!pVar)
873 pVar = kbuild_lookup_variable_n(pszBuf, psz4 - pszBuf);
874
875 /* TOOL_$(tool)_$(type)$(propf2) */
876 if (!pVar)
877 pVar = kbuild_lookup_variable_n(pszBuf, psz3 - pszBuf);
878
879 /* TOOL_$(tool)_$(propf2).$(bld_trg).$(bld_trg_arch) */
880 if (!pVar)
881 {
882 psz = psz2;
883 ADD_STR(pszPropF2, cchPropF2);
884 psz3 = psz;
885 ADD_CH('.');
886 ADD_VAR(pBldTrg);
887 psz4 = psz;
888 ADD_CH('.');
889 ADD_VAR(pBldTrgArch);
890 pVar = kbuild_lookup_variable_n(pszBuf, psz - pszBuf);
891
892 /* TOOL_$(tool)_$(propf2).$(bld_trg) */
893 if (!pVar)
894 pVar = kbuild_lookup_variable_n(pszBuf, psz4 - pszBuf);
895
896 /* TOOL_$(tool)_$(propf2) */
897 if (!pVar)
898 pVar = kbuild_lookup_variable_n(pszBuf, psz3 - pszBuf);
899 }
900 }
901
902 /*
903 * $(type)$(propf1).$(bld_trg).$(bld_trg_arch)
904 */
905 if (!pVar)
906 {
907 psz = pszBuf;
908 ADD_VAR(pType);
909 ADD_STR(pszPropF1, cchPropF1);
910 psz3 = psz;
911 ADD_CH('.');
912 ADD_VAR(pBldTrg);
913 psz4 = psz;
914 ADD_CH('.');
915 ADD_VAR(pBldTrgArch);
916 pVar = kbuild_lookup_variable_n(pszBuf, psz - pszBuf);
917
918 /* $(type)$(propf1).$(bld_trg) */
919 if (!pVar)
920 pVar = kbuild_lookup_variable_n(pszBuf, psz4 - pszBuf);
921
922 /* $(type)$(propf1) */
923 if (!pVar)
924 pVar = kbuild_lookup_variable_n(pszBuf, psz3 - pszBuf);
925
926 /*
927 * $(propf1).$(bld_trg).$(bld_trg_arch)
928 */
929 if (!pVar)
930 {
931 psz1 = pszBuf + pType->value_length;
932 pVar = kbuild_lookup_variable_n(psz1, psz - psz1);
933
934 /* $(propf1).$(bld_trg) */
935 if (!pVar)
936 pVar = kbuild_lookup_variable_n(psz1, psz4 - psz1);
937
938 /* $(propf1) */
939 if (!pVar)
940 pVar = kbuild_lookup_variable_n(pszPropF1, cchPropF1);
941 }
942 }
943 free(pszBuf);
944#undef ADD_VAR
945#undef ADD_STR
946#undef ADD_CSTR
947#undef ADD_CH
948
949 if (pVar)
950 {
951 /* strip it */
952 psz = pVar->value;
953 pszEnd = psz + pVar->value_length;
954 while (isblank((unsigned char)*psz))
955 psz++;
956 while (pszEnd > psz && isblank((unsigned char)pszEnd[-1]))
957 pszEnd--;
958 if (pszEnd > psz)
959 {
960 char chSaved = *pszEnd;
961 *pszEnd = '\0';
962 pVar = define_variable_vl(pszVarName, strlen(pszVarName), psz, pszEnd - psz,
963 1 /* duplicate */, o_local, 0 /* !recursive */);
964 *pszEnd = chSaved;
965 if (pVar)
966 return pVar;
967 }
968 }
969 return NULL;
970}
971
972
973/*
974_SOURCE_TOOL = $(strip $(firstword \
975 $($(target)_$(source)_$(type)TOOL.$(bld_trg).$(bld_trg_arch)) \
976 $($(target)_$(source)_$(type)TOOL.$(bld_trg)) \
977 $($(target)_$(source)_$(type)TOOL) \
978 $($(target)_$(source)_TOOL.$(bld_trg).$(bld_trg_arch)) \
979 $($(target)_$(source)_TOOL.$(bld_trg)) \
980 $($(target)_$(source)_TOOL) \
981 $($(source)_$(type)TOOL.$(bld_trg).$(bld_trg_arch)) \
982 $($(source)_$(type)TOOL.$(bld_trg)) \
983 $($(source)_$(type)TOOL) \
984 $($(source)_TOOL.$(bld_trg).$(bld_trg_arch)) \
985 $($(source)_TOOL.$(bld_trg)) \
986 $($(source)_TOOL) \
987 $($(target)_$(type)TOOL.$(bld_trg).$(bld_trg_arch)) \
988 $($(target)_$(type)TOOL.$(bld_trg)) \
989 $($(target)_$(type)TOOL) \
990 $($(target)_TOOL.$(bld_trg).$(bld_trg_arch)) \
991 $($(target)_TOOL.$(bld_trg)) \
992 $($(target)_TOOL) \
993 $($(type)TOOL.$(bld_trg).$(bld_trg_arch)) \
994 $($(type)TOOL.$(bld_trg)) \
995 $($(type)TOOL) \
996 $(TOOL.$(bld_trg).$(bld_trg_arch)) \
997 $(TOOL.$(bld_trg)) \
998 $(TOOL) ))
999*/
1000static struct variable *
1001kbuild_get_source_tool(struct variable *pTarget, struct variable *pSource, struct variable *pType,
1002 struct variable *pBldTrg, struct variable *pBldTrgArch, const char *pszVarName)
1003{
1004 struct variable *pVar = kbuild_first_prop(pTarget, pSource, NULL, pType, pBldTrg, pBldTrgArch,
1005 "TOOL", sizeof("TOOL") - 1,
1006 "TOOL", sizeof("TOOL") - 1,
1007 pszVarName);
1008 if (!pVar)
1009 fatal(NILF, _("no tool for source `%s' in target `%s'!"), pSource->value, pTarget->value);
1010 return pVar;
1011}
1012
1013
1014/* Implements _SOURCE_TOOL. */
1015char *
1016func_kbuild_source_tool(char *o, char **argv, const char *pszFuncName)
1017{
1018 struct variable *pVar = kbuild_get_source_tool(kbuild_get_variable_n(ST("target")),
1019 kbuild_get_variable_n(ST("source")),
1020 kbuild_get_variable_n(ST("type")),
1021 kbuild_get_variable_n(ST("bld_trg")),
1022 kbuild_get_variable_n(ST("bld_trg_arch")),
1023 argv[0]);
1024 if (pVar)
1025 o = variable_buffer_output(o, pVar->value, pVar->value_length);
1026 (void)pszFuncName;
1027 return o;
1028
1029}
1030
1031
1032/* This has been extended a bit, it's now identical to _SOURCE_TOOL.
1033$(firstword \
1034 $($(target)_$(source)_OBJSUFF.$(bld_trg).$(bld_trg_arch))\
1035 $($(target)_$(source)_OBJSUFF.$(bld_trg))\
1036 $($(target)_$(source)_OBJSUFF)\
1037 $($(source)_OBJSUFF.$(bld_trg).$(bld_trg_arch))\
1038 $($(source)_OBJSUFF.$(bld_trg))\
1039 $($(source)_OBJSUFF)\
1040 $($(target)_OBJSUFF.$(bld_trg).$(bld_trg_arch))\
1041 $($(target)_OBJSUFF.$(bld_trg))\
1042 $($(target)_OBJSUFF)\
1043 $(TOOL_$(tool)_$(type)OBJSUFF.$(bld_trg).$(bld_trg_arch))\
1044 $(TOOL_$(tool)_$(type)OBJSUFF.$(bld_trg))\
1045 $(TOOL_$(tool)_$(type)OBJSUFF)\
1046 $(SUFF_OBJ))
1047*/
1048static struct variable *
1049kbuild_get_object_suffix(struct variable *pTarget, struct variable *pSource,
1050 struct variable *pTool, struct variable *pType,
1051 struct variable *pBldTrg, struct variable *pBldTrgArch, const char *pszVarName)
1052{
1053 struct variable *pVar = kbuild_first_prop(pTarget, pSource, pTool, pType, pBldTrg, pBldTrgArch,
1054 "SUFF_OBJ", sizeof("SUFF_OBJ") - 1,
1055 "OBJSUFF", sizeof("OBJSUFF") - 1,
1056 pszVarName);
1057 if (!pVar)
1058 fatal(NILF, _("no OBJSUFF attribute or SUFF_OBJ default for source `%s' in target `%s'!"), pSource->value, pTarget->value);
1059 return pVar;
1060}
1061
1062
1063/* */
1064char *
1065func_kbuild_object_suffix(char *o, char **argv, const char *pszFuncName)
1066{
1067 struct variable *pVar = kbuild_get_object_suffix(kbuild_get_variable_n(ST("target")),
1068 kbuild_get_variable_n(ST("source")),
1069 kbuild_get_variable_n(ST("tool")),
1070 kbuild_get_variable_n(ST("type")),
1071 kbuild_get_variable_n(ST("bld_trg")),
1072 kbuild_get_variable_n(ST("bld_trg_arch")),
1073 argv[0]);
1074 if (pVar)
1075 o = variable_buffer_output(o, pVar->value, pVar->value_length);
1076 (void)pszFuncName;
1077 return o;
1078
1079}
1080
1081
1082/*
1083## Figure out where to put object files.
1084# @param $1 source file
1085# @param $2 normalized main target
1086# @remark There are two major hacks here:
1087# 1. Source files in the output directory are translated into a gen/ subdir.
1088# 2. Catch anyone specifying $(PATH_SUB_CURRENT)/sourcefile.c.
1089_OBJECT_BASE = $(PATH_TARGET)/$(2)/$(call no-root-slash,$(call no-drive,$(basename \
1090 $(patsubst $(PATH_ROOT)/%,%,$(patsubst $(PATH_SUB_CURRENT)/%,%,$(patsubst $(PATH_TARGET)/$(2)/%,gen/%,$(1)))))))
1091*/
1092static struct variable *
1093kbuild_get_object_base(struct variable *pTarget, struct variable *pSource, const char *pszVarName)
1094{
1095 struct variable *pPathTarget = kbuild_get_variable_n(ST("PATH_TARGET"));
1096 struct variable *pPathRoot = kbuild_get_variable_n(ST("PATH_ROOT"));
1097 struct variable *pPathSubCur = kbuild_get_variable_n(ST("PATH_SUB_CURRENT"));
1098 const char *pszSrcPrefix = NULL;
1099 size_t cchSrcPrefix = 0;
1100 size_t cchSrc = 0;
1101 const char *pszSrcEnd;
1102 char *pszSrc;
1103 char *pszResult;
1104 char *psz;
1105 char *pszDot;
1106 size_t cch;
1107
1108 /*
1109 * Strip the source filename of any uncessary leading path and root specs.
1110 */
1111 /* */
1112 if ( pSource->value_length > pPathTarget->value_length
1113 && !strncmp(pSource->value, pPathTarget->value, pPathTarget->value_length))
1114 {
1115 pszSrc = pSource->value + pPathTarget->value_length;
1116 pszSrcPrefix = "gen/";
1117 cchSrcPrefix = sizeof("gen/") - 1;
1118 if ( *pszSrc == '/'
1119 && !strncmp(pszSrc + 1, pTarget->value, pTarget->value_length)
1120 && ( pszSrc[pTarget->value_length + 1] == '/'
1121 || pszSrc[pTarget->value_length + 1] == '\0'))
1122 pszSrc += 1 + pTarget->value_length;
1123 }
1124 else if ( pSource->value_length > pPathRoot->value_length
1125 && !strncmp(pSource->value, pPathRoot->value, pPathRoot->value_length))
1126 {
1127 pszSrc = pSource->value + pPathRoot->value_length;
1128 if ( *pszSrc == '/'
1129 && !strncmp(pszSrc + 1, pPathSubCur->value, pPathSubCur->value_length)
1130 && ( pszSrc[pPathSubCur->value_length + 1] == '/'
1131 || pszSrc[pPathSubCur->value_length + 1] == '\0'))
1132 pszSrc += 1 + pPathSubCur->value_length;
1133 }
1134 else
1135 pszSrc = pSource->value;
1136
1137 /* skip root specification */
1138#ifdef HAVE_DOS_PATHS
1139 if (isalpha(pszSrc[0]) && pszSrc[1] == ':')
1140 pszSrc += 2;
1141#endif
1142 while (*pszSrc == '/'
1143#ifdef HAVE_DOS_PATHS
1144 || *pszSrc == '\\'
1145#endif
1146 )
1147 pszSrc++;
1148
1149 /* drop the source extension. */
1150 pszSrcEnd = pSource->value + pSource->value_length;
1151 for (;;)
1152 {
1153 pszSrcEnd--;
1154 if ( pszSrcEnd <= pszSrc
1155 || *pszSrcEnd == '/'
1156#ifdef HAVE_DOS_PATHS
1157 || *pszSrcEnd == '\\'
1158 || *pszSrcEnd == ':'
1159#endif
1160 )
1161 {
1162 pszSrcEnd = pSource->value + pSource->value_length;
1163 break;
1164 }
1165 if (*pszSrcEnd == '.')
1166 break;
1167 }
1168
1169 /*
1170 * Assemble the string on the heap and define the objbase variable
1171 * which we then return.
1172 */
1173 cchSrc = pszSrcEnd - pszSrc;
1174 cch = pPathTarget->value_length
1175 + 1 /* slash */
1176 + pTarget->value_length
1177 + 1 /* slash */
1178 + cchSrcPrefix
1179 + cchSrc
1180 + 1;
1181 psz = pszResult = xmalloc(cch);
1182
1183 memcpy(psz, pPathTarget->value, pPathTarget->value_length); psz += pPathTarget->value_length;
1184 *psz++ = '/';
1185 memcpy(psz, pTarget->value, pTarget->value_length); psz += pTarget->value_length;
1186 *psz++ = '/';
1187 if (pszSrcPrefix)
1188 {
1189 memcpy(psz, pszSrcPrefix, cchSrcPrefix);
1190 psz += cchSrcPrefix;
1191 }
1192 pszDot = psz;
1193 memcpy(psz, pszSrc, cchSrc); psz += cchSrc;
1194 *psz = '\0';
1195
1196 /* convert '..' path elements in the source to 'dt'. */
1197 while ((pszDot = memchr(pszDot, '.', psz - pszDot)) != NULL)
1198 {
1199 if ( pszDot[1] == '.'
1200 && ( pszDot == psz
1201 || pszDot[-1] == '/'
1202#ifdef HAVE_DOS_PATHS
1203 || pszDot[-1] == '\\'
1204 || pszDot[-1] == ':'
1205#endif
1206 )
1207 && ( !pszDot[2]
1208 || pszDot[2] == '/'
1209#ifdef HAVE_DOS_PATHS
1210 || pszDot[2] == '\\'
1211 || pszDot[2] == ':'
1212#endif
1213 )
1214 )
1215 {
1216 *pszDot++ = 'd';
1217 *pszDot++ = 't';
1218 }
1219 else
1220 pszDot++;
1221 }
1222
1223 /*
1224 * Define the variable in the current set and return it.
1225 */
1226 return define_variable_vl(pszVarName, strlen(pszVarName), pszResult, cch - 1,
1227 0 /* use pszResult */, o_local, 0 /* !recursive */);
1228}
1229
1230
1231/* Implements _OBJECT_BASE. */
1232char *
1233func_kbuild_object_base(char *o, char **argv, const char *pszFuncName)
1234{
1235 struct variable *pVar = kbuild_get_object_base(kbuild_lookup_variable("target"),
1236 kbuild_lookup_variable("source"),
1237 argv[0]);
1238 if (pVar)
1239 o = variable_buffer_output(o, pVar->value, pVar->value_length);
1240 (void)pszFuncName;
1241 return o;
1242
1243}
1244
1245
1246struct kbuild_sdks
1247{
1248 char *apsz[4];
1249 struct variable *pa;
1250 unsigned c;
1251 unsigned iGlobal;
1252 unsigned cGlobal;
1253 unsigned iTarget;
1254 unsigned cTarget;
1255 unsigned iSource;
1256 unsigned cSource;
1257 unsigned iTargetSource;
1258 unsigned cTargetSource;
1259 unsigned int cchMax;
1260};
1261
1262
1263/* Fills in the SDK struct (remember to free it). */
1264static void
1265kbuild_get_sdks(struct kbuild_sdks *pSdks, struct variable *pTarget, struct variable *pSource,
1266 struct variable *pBldType, struct variable *pBldTrg, struct variable *pBldTrgArch)
1267{
1268 unsigned i;
1269 unsigned j;
1270 size_t cchTmp, cch;
1271 char *pszTmp;
1272 unsigned cchCur;
1273 char *pszCur;
1274 const char *pszIterator;
1275
1276 /** @todo rewrite this to avoid sprintf and allocated_varaible_expand_2. */
1277
1278 /* basic init. */
1279 pSdks->cchMax = 0;
1280 pSdks->pa = NULL;
1281 pSdks->c = 0;
1282 i = 0;
1283
1284 /* determin required tmp variable name space. */
1285 cchTmp = sizeof("$(__SDKS) $(__SDKS.) $(__SDKS.) $(__SDKS.) $(__SDKS..)")
1286 + (pTarget->value_length + pSource->value_length) * 5
1287 + pBldType->value_length
1288 + pBldTrg->value_length
1289 + pBldTrgArch->value_length
1290 + pBldTrg->value_length + pBldTrgArch->value_length;
1291 pszTmp = alloca(cchTmp);
1292
1293 /* the global sdks. */
1294 pSdks->iGlobal = i;
1295 pSdks->cGlobal = 0;
1296 cch = sprintf(pszTmp, "$(SDKS) $(SDKS.%s) $(SDKS.%s) $(SDKS.%s) $(SDKS.%s.%s)",
1297 pBldType->value,
1298 pBldTrg->value,
1299 pBldTrgArch->value,
1300 pBldTrg->value, pBldTrgArch->value);
1301 pszIterator = pSdks->apsz[0] = allocated_variable_expand_2(pszTmp, cch, NULL);
1302 while ((pszCur = find_next_token(&pszIterator, &cchCur)) != 0)
1303 pSdks->cGlobal++;
1304 i += pSdks->cGlobal;
1305
1306 /* the target sdks.*/
1307 pSdks->iTarget = i;
1308 pSdks->cTarget = 0;
1309 cch = sprintf(pszTmp, "$(%s_SDKS) $(%s_SDKS.%s) $(%s_SDKS.%s) $(%s_SDKS.%s) $(%s_SDKS.%s.%s)",
1310 pTarget->value,
1311 pTarget->value, pBldType->value,
1312 pTarget->value, pBldTrg->value,
1313 pTarget->value, pBldTrgArch->value,
1314 pTarget->value, pBldTrg->value, pBldTrgArch->value);
1315 pszIterator = pSdks->apsz[1] = allocated_variable_expand_2(pszTmp, cch, NULL);
1316 while ((pszCur = find_next_token(&pszIterator, &cchCur)) != 0)
1317 pSdks->cTarget++;
1318 i += pSdks->cTarget;
1319
1320 /* the source sdks.*/
1321 pSdks->iSource = i;
1322 pSdks->cSource = 0;
1323 cch = sprintf(pszTmp, "$(%s_SDKS) $(%s_SDKS.%s) $(%s_SDKS.%s) $(%s_SDKS.%s) $(%s_SDKS.%s.%s)",
1324 pSource->value,
1325 pSource->value, pBldType->value,
1326 pSource->value, pBldTrg->value,
1327 pSource->value, pBldTrgArch->value,
1328 pSource->value, pBldTrg->value, pBldTrgArch->value);
1329 pszIterator = pSdks->apsz[2] = allocated_variable_expand_2(pszTmp, cch, NULL);
1330 while ((pszCur = find_next_token(&pszIterator, &cchCur)) != 0)
1331 pSdks->cSource++;
1332 i += pSdks->cSource;
1333
1334 /* the target + source sdks. */
1335 pSdks->iTargetSource = i;
1336 pSdks->cTargetSource = 0;
1337 cch = sprintf(pszTmp, "$(%s_%s_SDKS) $(%s_%s_SDKS.%s) $(%s_%s_SDKS.%s) $(%s_%s_SDKS.%s) $(%s_%s_SDKS.%s.%s)",
1338 pTarget->value, pSource->value,
1339 pTarget->value, pSource->value, pBldType->value,
1340 pTarget->value, pSource->value, pBldTrg->value,
1341 pTarget->value, pSource->value, pBldTrgArch->value,
1342 pTarget->value, pSource->value, pBldTrg->value, pBldTrgArch->value);
1343 assert(cch < cchTmp); (void)cch;
1344 pszIterator = pSdks->apsz[3] = allocated_variable_expand_2(pszTmp, cch, NULL);
1345 while ((pszCur = find_next_token(&pszIterator, &cchCur)) != 0)
1346 pSdks->cTargetSource++;
1347 i += pSdks->cTargetSource;
1348
1349 pSdks->c = i;
1350 if (!i)
1351 return;
1352
1353 /*
1354 * Allocate the variable array and create the variables.
1355 */
1356 pSdks->pa = (struct variable *)xmalloc(sizeof(pSdks->pa[0]) * i);
1357 memset(pSdks->pa, 0, sizeof(pSdks->pa[0]) * i);
1358 for (i = j = 0; j < sizeof(pSdks->apsz) / sizeof(pSdks->apsz[0]); j++)
1359 {
1360 pszIterator = pSdks->apsz[j];
1361 while ((pszCur = find_next_token(&pszIterator, &cchCur)) != 0)
1362 {
1363 pSdks->pa[i].value = pszCur;
1364 pSdks->pa[i].value_length = cchCur;
1365 i++;
1366 }
1367 }
1368 assert(i == pSdks->c);
1369
1370 /* terminate them (find_next_token won't work if we terminate them in the previous loop). */
1371 while (i-- > 0)
1372 {
1373 pSdks->pa[i].value[pSdks->pa[i].value_length] = '\0';
1374
1375 /* calc the max variable length too. */
1376 if (pSdks->cchMax < (unsigned int)pSdks->pa[i].value_length)
1377 pSdks->cchMax = pSdks->pa[i].value_length;
1378 }
1379}
1380
1381
1382/* releases resources allocated in the kbuild_get_sdks. */
1383static void
1384kbuild_put_sdks(struct kbuild_sdks *pSdks)
1385{
1386 unsigned j;
1387 for (j = 0; j < sizeof(pSdks->apsz) / sizeof(pSdks->apsz[0]); j++)
1388 free(pSdks->apsz[j]);
1389 free(pSdks->pa);
1390}
1391
1392
1393/* this kind of stuff:
1394
1395defs := $(kb-src-exp defs)
1396 $(TOOL_$(tool)_DEFS)\
1397 $(TOOL_$(tool)_DEFS.$(bld_type))\
1398 $(TOOL_$(tool)_DEFS.$(bld_trg))\
1399 $(TOOL_$(tool)_DEFS.$(bld_trg_arch))\
1400 $(TOOL_$(tool)_DEFS.$(bld_trg).$(bld_trg_arch))\
1401 $(TOOL_$(tool)_DEFS.$(bld_trg_cpu))\
1402 $(TOOL_$(tool)_$(type)DEFS)\
1403 $(TOOL_$(tool)_$(type)DEFS.$(bld_type))\
1404 $(foreach sdk, $(SDKS.$(bld_trg)) \
1405 $(SDKS.$(bld_trg).$(bld_trg_arch)) \
1406 $(SDKS.$(bld_type)) \
1407 $(SDKS),\
1408 $(SDK_$(sdk)_DEFS)\
1409 $(SDK_$(sdk)_DEFS.$(bld_type))\
1410 $(SDK_$(sdk)_DEFS.$(bld_trg))\
1411 $(SDK_$(sdk)_DEFS.$(bld_trg_arch))\
1412 $(SDK_$(sdk)_DEFS.$(bld_trg).$(bld_trg_arch))\
1413 $(SDK_$(sdk)_DEFS.$(bld_trg_cpu))\
1414 $(SDK_$(sdk)_$(type)DEFS)\
1415 $(SDK_$(sdk)_$(type)DEFS.$(bld_type))\
1416 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg))\
1417 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_arch))\
1418 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1419 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_cpu)))\
1420 $(DEFS)\
1421 $(DEFS.$(bld_type))\
1422 $(DEFS.$(bld_trg))\
1423 $(DEFS.$(bld_trg_arch))\
1424 $(DEFS.$(bld_trg).$(bld_trg_arch))\
1425 $(DEFS.$(bld_trg_cpu))\
1426 $($(type)DEFS)\
1427 $($(type)DEFS.$(bld_type))\
1428 $($(type)DEFS.$(bld_trg))\
1429 $($(type)DEFS.$(bld_trg_arch))\
1430 $($(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1431 $($(type)DEFS.$(bld_trg_cpu))\
1432 $(foreach sdk, $($(target)_SDKS.$(bld_trg)) \
1433 $($(target)_SDKS.$(bld_trg).$(bld_trg_arch)) \
1434 $($(target)_SDKS.$(bld_type)) \
1435 $($(target)_SDKS),\
1436 $(SDK_$(sdk)_DEFS)\
1437 $(SDK_$(sdk)_DEFS.$(bld_type))\
1438 $(SDK_$(sdk)_DEFS.$(bld_trg))\
1439 $(SDK_$(sdk)_DEFS.$(bld_trg_arch))\
1440 $(SDK_$(sdk)_DEFS.$(bld_trg).$(bld_trg_arch))\
1441 $(SDK_$(sdk)_DEFS.$(bld_trg_cpu))\
1442 $(SDK_$(sdk)_$(type)DEFS)\
1443 $(SDK_$(sdk)_$(type)DEFS.$(bld_type))\
1444 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg))\
1445 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_arch))\
1446 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1447 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_cpu)))\
1448 $($(target)_DEFS)\
1449 $($(target)_DEFS.$(bld_type))\
1450 $($(target)_DEFS.$(bld_trg))\
1451 $($(target)_DEFS.$(bld_trg_arch))\
1452 $($(target)_DEFS.$(bld_trg).$(bld_trg_arch))\
1453 $($(target)_DEFS.$(bld_trg_cpu))\
1454 $($(target)_$(type)DEFS)\
1455 $($(target)_$(type)DEFS.$(bld_type))\
1456 $($(target)_$(type)DEFS.$(bld_trg))\
1457 $($(target)_$(type)DEFS.$(bld_trg_arch))\
1458 $($(target)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1459 $($(target)_$(type)DEFS.$(bld_trg_cpu))\
1460 $(foreach sdk, $($(source)_SDKS.$(bld_trg)) \
1461 $($(source)_SDKS.$(bld_trg).$(bld_trg_arch)) \
1462 $($(source)_SDKS.$(bld_type)) \
1463 $($(source)_SDKS),\
1464 $(SDK_$(sdk)_DEFS)\
1465 $(SDK_$(sdk)_DEFS.$(bld_type))\
1466 $(SDK_$(sdk)_DEFS.$(bld_trg))\
1467 $(SDK_$(sdk)_DEFS.$(bld_trg_arch))\
1468 $(SDK_$(sdk)_DEFS.$(bld_trg).$(bld_trg_arch))\
1469 $(SDK_$(sdk)_DEFS.$(bld_trg_cpu))\
1470 $(SDK_$(sdk)_$(type)DEFS)\
1471 $(SDK_$(sdk)_$(type)DEFS.$(bld_type))\
1472 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg))\
1473 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_arch))\
1474 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1475 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_cpu)))\
1476 $($(source)_DEFS)\
1477 $($(source)_DEFS.$(bld_type))\
1478 $($(source)_DEFS.$(bld_trg))\
1479 $($(source)_DEFS.$(bld_trg_arch))\
1480 $($(source)_DEFS.$(bld_trg).$(bld_trg_arch))\
1481 $($(source)_DEFS.$(bld_trg_cpu))\
1482 $($(source)_$(type)DEFS)\
1483 $($(source)_$(type)DEFS.$(bld_type))\
1484 $($(source)_$(type)DEFS.$(bld_trg))\
1485 $($(source)_$(type)DEFS.$(bld_trg_arch))\
1486 $($(source)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1487 $($(source)_$(type)DEFS.$(bld_trg_cpu))\
1488 $(foreach sdk, $($(target)_$(source)_SDKS.$(bld_trg)) \
1489 $($(target)_$(source)_SDKS.$(bld_trg).$(bld_trg_arch)) \
1490 $($(target)_$(source)_SDKS.$(bld_type)) \
1491 $($(target)_$(source)_SDKS),\
1492 $(SDK_$(sdk)_DEFS)\
1493 $(SDK_$(sdk)_DEFS.$(bld_type))\
1494 $(SDK_$(sdk)_DEFS.$(bld_trg))\
1495 $(SDK_$(sdk)_DEFS.$(bld_trg_arch))\
1496 $(SDK_$(sdk)_DEFS.$(bld_trg).$(bld_trg_arch))\
1497 $(SDK_$(sdk)_DEFS.$(bld_trg_cpu))\
1498 $(SDK_$(sdk)_$(type)DEFS)\
1499 $(SDK_$(sdk)_$(type)DEFS.$(bld_type))\
1500 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg))\
1501 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_arch))\
1502 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1503 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_cpu)))\
1504 $($(target)_$(source)_DEFS)\
1505 $($(target)_$(source)_DEFS.$(bld_type))\
1506 $($(target)_$(source)_DEFS.$(bld_trg))\
1507 $($(target)_$(source)_DEFS.$(bld_trg_arch))\
1508 $($(target)_$(source)_DEFS.$(bld_trg).$(bld_trg_arch))\
1509 $($(target)_$(source)_DEFS.$(bld_trg_cpu))\
1510 $($(target)_$(source)_$(type)DEFS)\
1511 $($(target)_$(source)_$(type)DEFS.$(bld_type))\
1512 $($(target)_$(source)_$(type)DEFS.$(bld_trg))\
1513 $($(target)_$(source)_$(type)DEFS.$(bld_trg_arch))\
1514 $($(target)_$(source)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1515 $($(target)_$(source)_$(type)DEFS.$(bld_trg_cpu))
1516*/
1517static struct variable *
1518kbuild_collect_source_prop(struct variable *pTarget, struct variable *pSource,
1519 struct variable *pTool, struct kbuild_sdks *pSdks,
1520 struct variable *pType, struct variable *pBldType,
1521 struct variable *pBldTrg, struct variable *pBldTrgArch, struct variable *pBldTrgCpu,
1522 struct variable *pDefPath,
1523 const char *pszProp, size_t cchProp,
1524 const char *pszVarName, size_t cchVarName,
1525 int iDirection)
1526{
1527 struct variable *pVar;
1528 unsigned iSdk, iSdkEnd;
1529 int cVars, iVar;
1530 size_t cchTotal, cchBuf;
1531 char *pszResult, *pszBuf, *psz, *psz2, *psz3;
1532 struct
1533 {
1534 struct variable *pVar;
1535 unsigned int cchExp;
1536 char *pszExp;
1537 } *paVars;
1538
1539 assert(iDirection == 1 || iDirection == -1);
1540
1541 /*
1542 * Calc and allocate a too big name buffer.
1543 */
1544 cchBuf = cchProp + 1
1545 + pTarget->value_length + 1
1546 + pSource->value_length + 1
1547 + pSdks->cchMax + 1
1548 + (pTool ? pTool->value_length + 1 : 0)
1549 + pType->value_length + 1
1550 + pBldTrg->value_length + 1
1551 + pBldTrgArch->value_length + 1
1552 + pBldTrgCpu->value_length + 1
1553 + pBldType->value_length + 1;
1554 pszBuf = xmalloc(cchBuf);
1555
1556 /*
1557 * Get the variables.
1558 *
1559 * The compiler will get a heart attack when it sees this code ... ;-)
1560 */
1561 cVars = 12 * (pSdks->c + 5);
1562 paVars = alloca(cVars * sizeof(paVars[0]));
1563
1564 iVar = 0;
1565 cchTotal = 0;
1566
1567#define ADD_VAR(pVar) do { my_memcpy(psz, (pVar)->value, (pVar)->value_length); psz += (pVar)->value_length; } while (0)
1568#define ADD_STR(pszStr, cchStr) do { my_memcpy(psz, (pszStr), (cchStr)); psz += (cchStr); } while (0)
1569#define ADD_CSTR(pszStr) do { my_memcpy(psz, pszStr, sizeof(pszStr) - 1); psz += sizeof(pszStr) - 1; } while (0)
1570#define ADD_CH(ch) do { *psz++ = (ch); } while (0)
1571#define DO_VAR_LOOKUP() \
1572 do { \
1573 pVar = kbuild_lookup_variable_n(pszBuf, psz - pszBuf); \
1574 if (pVar) \
1575 { \
1576 paVars[iVar].pVar = pVar; \
1577 if ( !pVar->recursive \
1578 || IS_VARIABLE_RECURSIVE_WITHOUT_DOLLAR(pVar)) \
1579 { \
1580 paVars[iVar].pszExp = pVar->value; \
1581 paVars[iVar].cchExp = pVar->value_length; \
1582 if (pDefPath && paVars[iVar].cchExp) \
1583 kbuild_apply_defpath(pDefPath, &paVars[iVar].pszExp, &paVars[iVar].cchExp, NULL, 0); \
1584 if (paVars[iVar].cchExp) \
1585 { \
1586 cchTotal += paVars[iVar].cchExp + 1; \
1587 iVar++; \
1588 } \
1589 } \
1590 else \
1591 { \
1592 paVars[iVar].pszExp = allocated_variable_expand_2(pVar->value, pVar->value_length, &paVars[iVar].cchExp); \
1593 if (pDefPath && paVars[iVar].cchExp) \
1594 kbuild_apply_defpath(pDefPath, &paVars[iVar].pszExp, &paVars[iVar].cchExp, NULL, 1); \
1595 if (paVars[iVar].cchExp) \
1596 { \
1597 cchTotal += paVars[iVar].cchExp + 1; \
1598 iVar++; \
1599 } \
1600 else \
1601 free(paVars[iVar].pszExp); \
1602 } \
1603 } \
1604 } while (0)
1605#define DO_SINGLE_PSZ3_VARIATION() \
1606 do { \
1607 DO_VAR_LOOKUP(); \
1608 \
1609 ADD_CH('.'); \
1610 psz3 = psz; \
1611 ADD_VAR(pBldType); \
1612 DO_VAR_LOOKUP(); \
1613 \
1614 psz = psz3; \
1615 ADD_VAR(pBldTrg); \
1616 DO_VAR_LOOKUP(); \
1617 \
1618 psz = psz3; \
1619 ADD_VAR(pBldTrgArch); \
1620 DO_VAR_LOOKUP(); \
1621 \
1622 psz = psz3; \
1623 ADD_VAR(pBldTrg); \
1624 ADD_CH('.'); \
1625 ADD_VAR(pBldTrgArch); \
1626 DO_VAR_LOOKUP(); \
1627 \
1628 psz = psz3; \
1629 ADD_VAR(pBldTrgCpu); \
1630 DO_VAR_LOOKUP(); \
1631 } while (0)
1632
1633#define DO_DOUBLE_PSZ2_VARIATION() \
1634 do { \
1635 psz2 = psz; \
1636 ADD_STR(pszProp, cchProp); \
1637 DO_SINGLE_PSZ3_VARIATION(); \
1638 \
1639 /* add prop before type */ \
1640 psz = psz2; \
1641 ADD_VAR(pType); \
1642 ADD_STR(pszProp, cchProp); \
1643 DO_SINGLE_PSZ3_VARIATION(); \
1644 } while (0)
1645
1646 /* the tool (lowest priority). */
1647 psz = pszBuf;
1648 ADD_CSTR("TOOL_");
1649 ADD_VAR(pTool);
1650 ADD_CH('_');
1651 DO_DOUBLE_PSZ2_VARIATION();
1652
1653
1654 /* the global sdks. */
1655 iSdkEnd = iDirection == 1 ? pSdks->iGlobal + pSdks->cGlobal : pSdks->iGlobal - 1;
1656 for (iSdk = iDirection == 1 ? pSdks->iGlobal : pSdks->iGlobal + pSdks->cGlobal - 1;
1657 iSdk != iSdkEnd;
1658 iSdk += iDirection)
1659 {
1660 struct variable *pSdk = &pSdks->pa[iSdk];
1661 psz = pszBuf;
1662 ADD_CSTR("SDK_");
1663 ADD_VAR(pSdk);
1664 ADD_CH('_');
1665 DO_DOUBLE_PSZ2_VARIATION();
1666 }
1667
1668 /* the globals. */
1669 psz = pszBuf;
1670 DO_DOUBLE_PSZ2_VARIATION();
1671
1672
1673 /* the target sdks. */
1674 iSdkEnd = iDirection == 1 ? pSdks->iTarget + pSdks->cTarget : pSdks->iTarget - 1;
1675 for (iSdk = iDirection == 1 ? pSdks->iTarget : pSdks->iTarget + pSdks->cTarget - 1;
1676 iSdk != iSdkEnd;
1677 iSdk += iDirection)
1678 {
1679 struct variable *pSdk = &pSdks->pa[iSdk];
1680 psz = pszBuf;
1681 ADD_CSTR("SDK_");
1682 ADD_VAR(pSdk);
1683 ADD_CH('_');
1684 DO_DOUBLE_PSZ2_VARIATION();
1685 }
1686
1687 /* the target. */
1688 psz = pszBuf;
1689 ADD_VAR(pTarget);
1690 ADD_CH('_');
1691 DO_DOUBLE_PSZ2_VARIATION();
1692
1693 /* the source sdks. */
1694 iSdkEnd = iDirection == 1 ? pSdks->iSource + pSdks->cSource : pSdks->iSource - 1;
1695 for (iSdk = iDirection == 1 ? pSdks->iSource : pSdks->iSource + pSdks->cSource - 1;
1696 iSdk != iSdkEnd;
1697 iSdk += iDirection)
1698 {
1699 struct variable *pSdk = &pSdks->pa[iSdk];
1700 psz = pszBuf;
1701 ADD_CSTR("SDK_");
1702 ADD_VAR(pSdk);
1703 ADD_CH('_');
1704 DO_DOUBLE_PSZ2_VARIATION();
1705 }
1706
1707 /* the source. */
1708 psz = pszBuf;
1709 ADD_VAR(pSource);
1710 ADD_CH('_');
1711 DO_DOUBLE_PSZ2_VARIATION();
1712
1713 /* the target + source sdks. */
1714 iSdkEnd = iDirection == 1 ? pSdks->iTargetSource + pSdks->cTargetSource : pSdks->iTargetSource - 1;
1715 for (iSdk = iDirection == 1 ? pSdks->iTargetSource : pSdks->iTargetSource + pSdks->cTargetSource - 1;
1716 iSdk != iSdkEnd;
1717 iSdk += iDirection)
1718 {
1719 struct variable *pSdk = &pSdks->pa[iSdk];
1720 psz = pszBuf;
1721 ADD_CSTR("SDK_");
1722 ADD_VAR(pSdk);
1723 ADD_CH('_');
1724 DO_DOUBLE_PSZ2_VARIATION();
1725 }
1726
1727 /* the target + source. */
1728 psz = pszBuf;
1729 ADD_VAR(pTarget);
1730 ADD_CH('_');
1731 ADD_VAR(pSource);
1732 ADD_CH('_');
1733 DO_DOUBLE_PSZ2_VARIATION();
1734
1735 free(pszBuf);
1736
1737 assert(iVar <= cVars);
1738 cVars = iVar;
1739
1740 /*
1741 * Construct the result value.
1742 */
1743 if (!cVars || !cchTotal)
1744 pVar = define_variable_vl(pszVarName, cchVarName, "", 0,
1745 1 /* duplicate value */ , o_local, 0 /* !recursive */);
1746 else
1747 {
1748 psz = pszResult = xmalloc(cchTotal + 1);
1749 if (iDirection == 1)
1750 {
1751 for (iVar = 0; iVar < cVars; iVar++)
1752 {
1753 my_memcpy(psz, paVars[iVar].pszExp, paVars[iVar].cchExp);
1754 psz += paVars[iVar].cchExp;
1755 *psz++ = ' ';
1756 if (paVars[iVar].pszExp != paVars[iVar].pVar->value)
1757 free(paVars[iVar].pszExp);
1758 }
1759 }
1760 else
1761 {
1762 iVar = cVars;
1763 while (iVar-- > 0)
1764 {
1765 my_memcpy(psz, paVars[iVar].pszExp, paVars[iVar].cchExp);
1766 psz += paVars[iVar].cchExp;
1767 *psz++ = ' ';
1768 if (paVars[iVar].pszExp != paVars[iVar].pVar->value)
1769 free(paVars[iVar].pszExp);
1770 }
1771
1772 }
1773 assert(psz != pszResult);
1774 assert(cchTotal == (size_t)(psz - pszResult));
1775 psz[-1] = '\0';
1776 cchTotal--;
1777
1778 pVar = define_variable_vl(pszVarName, cchVarName, pszResult, cchTotal,
1779 0 /* take pszResult */ , o_local, 0 /* !recursive */);
1780 }
1781
1782 return pVar;
1783
1784#undef ADD_VAR
1785#undef ADD_STR
1786#undef ADD_CSTR
1787#undef ADD_CH
1788#undef DO_VAR_LOOKUP
1789#undef DO_DOUBLE_PSZ2_VARIATION
1790#undef DO_SINGLE_PSZ3_VARIATION
1791}
1792
1793
1794/* get a source property. */
1795char *
1796func_kbuild_source_prop(char *o, char **argv, const char *pszFuncName)
1797{
1798 struct variable *pTarget = kbuild_get_variable_n(ST("target"));
1799 struct variable *pSource = kbuild_get_variable_n(ST("source"));
1800 struct variable *pDefPath = NULL;
1801 struct variable *pType = kbuild_get_variable_n(ST("type"));
1802 struct variable *pTool = kbuild_get_variable_n(ST("tool"));
1803 struct variable *pBldType = kbuild_get_variable_n(ST("bld_type"));
1804 struct variable *pBldTrg = kbuild_get_variable_n(ST("bld_trg"));
1805 struct variable *pBldTrgArch = kbuild_get_variable_n(ST("bld_trg_arch"));
1806 struct variable *pBldTrgCpu = kbuild_get_variable_n(ST("bld_trg_cpu"));
1807 struct variable *pVar;
1808 struct kbuild_sdks Sdks;
1809 int iDirection;
1810 if (!strcmp(argv[2], "left-to-right"))
1811 iDirection = 1;
1812 else if (!strcmp(argv[2], "right-to-left"))
1813 iDirection = -1;
1814 else
1815 fatal(NILF, _("incorrect direction argument `%s'!"), argv[2]);
1816 if (argv[3])
1817 {
1818 const char *psz = argv[3];
1819 while (isspace(*psz))
1820 psz++;
1821 if (*psz)
1822 pDefPath = kbuild_get_variable_n(ST("defpath"));
1823 }
1824
1825 kbuild_get_sdks(&Sdks, pTarget, pSource, pBldType, pBldTrg, pBldTrgArch);
1826
1827 pVar = kbuild_collect_source_prop(pTarget, pSource, pTool, &Sdks, pType,
1828 pBldType, pBldTrg, pBldTrgArch, pBldTrgCpu,
1829 pDefPath,
1830 argv[0], strlen(argv[0]),
1831 argv[1], strlen(argv[1]),
1832 iDirection);
1833 if (pVar)
1834 o = variable_buffer_output(o, pVar->value, pVar->value_length);
1835
1836 kbuild_put_sdks(&Sdks);
1837 (void)pszFuncName;
1838 return o;
1839}
1840
1841
1842/*
1843dep := $(obj)$(SUFF_DEP)
1844obj := $(outbase)$(objsuff)
1845dirdep := $(call DIRDEP,$(dir $(outbase)))
1846PATH_$(target)_$(source) := $(patsubst %/,%,$(dir $(outbase)))
1847*/
1848static struct variable *
1849kbuild_set_object_name_and_dep_and_dirdep_and_PATH_target_source(struct variable *pTarget, struct variable *pSource,
1850 struct variable *pOutBase, struct variable *pObjSuff,
1851 const char *pszVarName, struct variable **ppDep,
1852 struct variable **ppDirDep)
1853{
1854 struct variable *pDepSuff = kbuild_get_variable_n(ST("SUFF_DEP"));
1855 struct variable *pObj;
1856 size_t cch = pOutBase->value_length + pObjSuff->value_length + pDepSuff->value_length + 1;
1857 char *pszResult = alloca(cch);
1858 char *pszName, *psz;
1859
1860 /*
1861 * dep.
1862 */
1863 psz = pszResult;
1864 memcpy(psz, pOutBase->value, pOutBase->value_length); psz += pOutBase->value_length;
1865 memcpy(psz, pObjSuff->value, pObjSuff->value_length); psz += pObjSuff->value_length;
1866 memcpy(psz, pDepSuff->value, pDepSuff->value_length + 1);
1867 *ppDep = define_variable_vl("dep", 3, pszResult, cch - 1, 1 /*dup*/, o_local, 0 /* !recursive */);
1868
1869 /*
1870 * obj
1871 */
1872 *psz = '\0';
1873 pObj = define_variable_vl(pszVarName, strlen(pszVarName), pszResult, psz - pszResult,
1874 1/* dup */, o_local, 0 /* !recursive */);
1875
1876 /*
1877 * PATH_$(target)_$(source) - this is global!
1878 */
1879 /* calc variable name. */
1880 cch = sizeof("PATH_")-1 + pTarget->value_length + sizeof("_")-1 + pSource->value_length;
1881 psz = pszName = alloca(cch + 1);
1882 memcpy(psz, "PATH_", sizeof("PATH_") - 1); psz += sizeof("PATH_") - 1;
1883 memcpy(psz, pTarget->value, pTarget->value_length); psz += pTarget->value_length;
1884 *psz++ = '_';
1885 memcpy(psz, pSource->value, pSource->value_length + 1);
1886
1887 /* strip off the filename. */
1888 psz = pszResult + pOutBase->value_length;
1889 for (;;)
1890 {
1891 psz--;
1892 if (psz <= pszResult)
1893 fatal(NULL, "whut!?! no path? result=`%s'", pszResult);
1894#ifdef HAVE_DOS_PATHS
1895 if (*psz == ':')
1896 {
1897 psz++;
1898 break;
1899 }
1900#endif
1901 if ( *psz == '/'
1902#ifdef HAVE_DOS_PATHS
1903 || *psz == '\\'
1904#endif
1905 )
1906 {
1907 while ( psz - 1 > pszResult
1908 && psz[-1] == '/'
1909#ifdef HAVE_DOS_PATHS
1910 || psz[-1] == '\\'
1911#endif
1912 )
1913 psz--;
1914#ifdef HAVE_DOS_PATHS
1915 if (psz == pszResult + 2 && pszResult[1] == ':')
1916 psz++;
1917#endif
1918 break;
1919 }
1920 }
1921 *psz = '\0';
1922
1923 /* set global variable */
1924 define_variable_vl_global(pszName, cch, pszResult, psz - pszResult, 1/*dup*/, o_file, 0 /* !recursive */, NILF);
1925
1926 /*
1927 * dirdep
1928 */
1929 if ( psz[-1] != '/'
1930#ifdef HAVE_DOS_PATHS
1931 && psz[-1] != '\\'
1932 && psz[-1] != ':'
1933#endif
1934 )
1935 {
1936 *psz++ = '/';
1937 *psz = '\0';
1938 }
1939 *ppDirDep = define_variable_vl("dirdep", 6, pszResult, psz - pszResult, 1/*dup*/, o_local, 0 /* !recursive */);
1940
1941 return pObj;
1942}
1943
1944
1945/* setup the base variables for def_target_source_c_cpp_asm_new:
1946
1947X := $(kb-src-tool tool)
1948x := $(kb-obj-base outbase)
1949x := $(kb-obj-suff objsuff)
1950obj := $(outbase)$(objsuff)
1951PATH_$(target)_$(source) := $(patsubst %/,%,$(dir $(outbase)))
1952
1953x := $(kb-src-prop DEFS,defs,left-to-right)
1954x := $(kb-src-prop INCS,incs,right-to-left)
1955x := $(kb-src-prop FLAGS,flags,right-to-left)
1956
1957x := $(kb-src-prop DEPS,deps,left-to-right)
1958dirdep := $(call DIRDEP,$(dir $(outbase)))
1959dep := $(obj)$(SUFF_DEP)
1960*/
1961char *
1962func_kbuild_source_one(char *o, char **argv, const char *pszFuncName)
1963{
1964 static int s_fNoCompileCmdsDepsDefined = -1;
1965 struct variable *pTarget = kbuild_get_variable_n(ST("target"));
1966 struct variable *pSource = kbuild_get_variable_n(ST("source"));
1967 struct variable *pDefPath = kbuild_get_variable_n(ST("defpath"));
1968 struct variable *pType = kbuild_get_variable_n(ST("type"));
1969 struct variable *pBldType = kbuild_get_variable_n(ST("bld_type"));
1970 struct variable *pBldTrg = kbuild_get_variable_n(ST("bld_trg"));
1971 struct variable *pBldTrgArch= kbuild_get_variable_n(ST("bld_trg_arch"));
1972 struct variable *pBldTrgCpu = kbuild_get_variable_n(ST("bld_trg_cpu"));
1973 struct variable *pTool = kbuild_get_source_tool(pTarget, pSource, pType, pBldTrg, pBldTrgArch, "tool");
1974 struct variable *pOutBase = kbuild_get_object_base(pTarget, pSource, "outbase");
1975 struct variable *pObjSuff = kbuild_get_object_suffix(pTarget, pSource, pTool, pType, pBldTrg, pBldTrgArch, "objsuff");
1976 struct variable *pDefs, *pIncs, *pFlags, *pDeps, *pOrderDeps, *pDirDep, *pDep, *pVar, *pOutput, *pOutputMaybe;
1977 struct variable *pObj = kbuild_set_object_name_and_dep_and_dirdep_and_PATH_target_source(pTarget, pSource, pOutBase, pObjSuff, "obj", &pDep, &pDirDep);
1978 int fInstallOldVars = 0;
1979 char *pszDstVar, *pszDst, *pszSrcVar, *pszSrc, *pszVal, *psz;
1980 char *pszSavedVarBuf;
1981 unsigned cchSavedVarBuf;
1982 size_t cch;
1983 struct kbuild_sdks Sdks;
1984 int iVer;
1985
1986 /*
1987 * argv[0] is the function version. Prior to r1792 (early 0.1.5) this
1988 * was undefined and footer.kmk always passed an empty string.
1989 *
1990 * Version 2, as implemented in r1797, will make use of the async
1991 * includedep queue feature. This means the files will be read by one or
1992 * more background threads, leaving the eval'ing to be done later on by
1993 * the main thread (in snap_deps).
1994 */
1995 if (!argv[0][0])
1996 iVer = 0;
1997 else
1998 switch (argv[0][0] | (argv[0][1] << 8))
1999 {
2000 case '2': iVer = 2; break;
2001 case '3': iVer = 3; break;
2002 case '4': iVer = 4; break;
2003 case '5': iVer = 5; break;
2004 case '6': iVer = 6; break;
2005 case '7': iVer = 7; break;
2006 case '8': iVer = 8; break;
2007 case '9': iVer = 9; break;
2008 case '0': iVer = 0; break;
2009 case '1': iVer = 1; break;
2010 default:
2011 iVer = 0;
2012 psz = argv[0];
2013 while (isblank((unsigned char)*psz))
2014 psz++;
2015 if (*psz)
2016 iVer = atoi(psz);
2017 break;
2018 }
2019
2020 /*
2021 * Gather properties.
2022 */
2023 kbuild_get_sdks(&Sdks, pTarget, pSource, pBldType, pBldTrg, pBldTrgArch);
2024
2025 if (pDefPath && !pDefPath->value_length)
2026 pDefPath = NULL;
2027 pDefs = kbuild_collect_source_prop(pTarget, pSource, pTool, &Sdks, pType, pBldType, pBldTrg, pBldTrgArch, pBldTrgCpu, NULL,
2028 ST("DEFS"), ST("defs"), 1/* left-to-right */);
2029 pIncs = kbuild_collect_source_prop(pTarget, pSource, pTool, &Sdks, pType, pBldType, pBldTrg, pBldTrgArch, pBldTrgCpu, pDefPath,
2030 ST("INCS"), ST("incs"), -1/* right-to-left */);
2031 pFlags = kbuild_collect_source_prop(pTarget, pSource, pTool, &Sdks, pType, pBldType, pBldTrg, pBldTrgArch, pBldTrgCpu, NULL,
2032 ST("FLAGS"), ST("flags"), 1/* left-to-right */);
2033 pDeps = kbuild_collect_source_prop(pTarget, pSource, pTool, &Sdks, pType, pBldType, pBldTrg, pBldTrgArch, pBldTrgCpu, pDefPath,
2034 ST("DEPS"), ST("deps"), 1/* left-to-right */);
2035 pOrderDeps = kbuild_collect_source_prop(pTarget, pSource, pTool, &Sdks, pType, pBldType, pBldTrg, pBldTrgArch, pBldTrgCpu, pDefPath,
2036 ST("ORDERDEPS"), ST("orderdeps"), 1/* left-to-right */);
2037
2038 /*
2039 * If we've got a default path, we must expand the source now.
2040 * If we do this too early, "<source>_property = stuff" won't work becuase
2041 * our 'source' value isn't what the user expects.
2042 */
2043 if (pDefPath)
2044 {
2045 /** @todo assert(pSource->origin != o_automatic); We're changing 'source'
2046 * from the foreach loop! */
2047#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2048 assert(!pSource->rdonly_val);
2049#endif
2050 kbuild_apply_defpath(pDefPath, &pSource->value, &pSource->value_length, &pSource->value_alloc_len, 1 /* can free */);
2051 }
2052
2053 /*
2054 # dependencies
2055 ifndef NO_COMPILE_CMDS_DEPS
2056 _DEPFILES_INCLUDED += $(dep)
2057 $(if $(wildcard $(dep)),$(eval include $(dep)))
2058 endif
2059 */
2060 if (s_fNoCompileCmdsDepsDefined == -1)
2061 s_fNoCompileCmdsDepsDefined = kbuild_lookup_variable_n(ST("NO_COMPILE_CMDS_DEPS")) != NULL;
2062 if (!s_fNoCompileCmdsDepsDefined)
2063 {
2064 pVar = kbuild_query_recursive_variable_n("_DEPFILES_INCLUDED", sizeof("_DEPFILES_INCLUDED") - 1);
2065 if (pVar)
2066 {
2067 if (pVar->recursive)
2068 pVar = kbuild_simplify_variable(pVar);
2069 append_string_to_variable(pVar, pDep->value, pDep->value_length, 1 /* append */);
2070 }
2071 else
2072 define_variable_vl_global("_DEPFILES_INCLUDED", sizeof("_DEPFILES_INCLUDED") - 1,
2073 pDep->value, pDep->value_length,
2074 1 /* duplicate_value */,
2075 o_file,
2076 0 /* recursive */,
2077 NULL /* flocp */);
2078
2079 eval_include_dep(pDep->value, NILF, iVer >= 2 ? incdep_queue : incdep_read_it);
2080 }
2081
2082 /*
2083 # call the tool
2084 $(target)_$(source)_CMDS_ := $(TOOL_$(tool)_COMPILE_$(type)_CMDS)
2085 $(target)_$(source)_OUTPUT_ := $(TOOL_$(tool)_COMPILE_$(type)_OUTPUT)
2086 $(target)_$(source)_OUTPUT_MAYBE_ := $(TOOL_$(tool)_COMPILE_$(type)_OUTPUT_MAYBE)
2087 $(target)_$(source)_DEPEND_ := $(TOOL_$(tool)_COMPILE_$(type)_DEPEND) $(deps) $(source)
2088 $(target)_$(source)_DEPORD_ := $(TOOL_$(tool)_COMPILE_$(type)_DEPORD) $(dirdep)
2089 */
2090 /** @todo Make all these local variables, if someone needs the info later it
2091 * can be recalculated. (Ticket #80.) */
2092 cch = sizeof("TOOL_") + pTool->value_length + sizeof("_COMPILE_") + pType->value_length + sizeof("_OUTPUT_MAYBE");
2093 if (cch < pTarget->value_length + sizeof("$(_2_OBJS)"))
2094 cch = pTarget->value_length + sizeof("$(_2_OBJS)");
2095 psz = pszSrcVar = alloca(cch);
2096 memcpy(psz, "TOOL_", sizeof("TOOL_") - 1); psz += sizeof("TOOL_") - 1;
2097 memcpy(psz, pTool->value, pTool->value_length); psz += pTool->value_length;
2098 memcpy(psz, "_COMPILE_", sizeof("_COMPILE_") - 1); psz += sizeof("_COMPILE_") - 1;
2099 memcpy(psz, pType->value, pType->value_length); psz += pType->value_length;
2100 pszSrc = psz;
2101
2102 cch = pTarget->value_length + 1 + pSource->value_length + sizeof("_OUTPUT_MAYBE_");
2103 psz = pszDstVar = alloca(cch);
2104 memcpy(psz, pTarget->value, pTarget->value_length); psz += pTarget->value_length;
2105 *psz++ = '_';
2106 memcpy(psz, pSource->value, pSource->value_length); psz += pSource->value_length;
2107 pszDst = psz;
2108
2109 memcpy(pszSrc, "_CMDS", sizeof("_CMDS"));
2110 memcpy(pszDst, "_CMDS_", sizeof("_CMDS_"));
2111 pVar = kbuild_get_recursive_variable(pszSrcVar);
2112 do_variable_definition_2(NILF, pszDstVar, pVar->value, pVar->value_length,
2113 !pVar->recursive, 0, o_local, f_simple, 0 /* !target_var */);
2114 do_variable_definition_2(NILF, "kbsrc_cmds", pVar->value, pVar->value_length,
2115 !pVar->recursive, 0, o_local, f_simple, 0 /* !target_var */);
2116
2117 memcpy(pszSrc, "_OUTPUT", sizeof("_OUTPUT"));
2118 memcpy(pszDst, "_OUTPUT_", sizeof("_OUTPUT_"));
2119 pVar = kbuild_get_recursive_variable(pszSrcVar);
2120 pOutput = do_variable_definition_2(NILF, pszDstVar, pVar->value, pVar->value_length,
2121 !pVar->recursive, 0, o_local, f_simple, 0 /* !target_var */);
2122 pOutput = do_variable_definition_2(NILF, "kbsrc_output", pVar->value, pVar->value_length,
2123 !pVar->recursive, 0, o_local, f_simple, 0 /* !target_var */);
2124
2125 memcpy(pszSrc, "_OUTPUT_MAYBE", sizeof("_OUTPUT_MAYBE"));
2126 memcpy(pszDst, "_OUTPUT_MAYBE_", sizeof("_OUTPUT_MAYBE_"));
2127 pVar = kbuild_query_recursive_variable(pszSrcVar);
2128 if (pVar)
2129 {
2130 pOutputMaybe = do_variable_definition_2(NILF, pszDstVar, pVar->value, pVar->value_length,
2131 !pVar->recursive, 0, o_local, f_simple, 0 /* !target_var */);
2132 pOutputMaybe = do_variable_definition_2(NILF, "kbsrc_output_maybe", pVar->value, pVar->value_length,
2133 !pVar->recursive, 0, o_local, f_simple, 0 /* !target_var */);
2134 }
2135 else
2136 {
2137 pOutputMaybe = do_variable_definition_2(NILF, pszDstVar, "", 0, 1, 0, o_local, f_simple, 0 /* !target_var */);
2138 pOutputMaybe = do_variable_definition_2(NILF, "kbsrc_output_maybe", "", 0, 1, 0, o_local, f_simple, 0 /* !target_var */);
2139 }
2140
2141 memcpy(pszSrc, "_DEPEND", sizeof("_DEPEND"));
2142 memcpy(pszDst, "_DEPEND_", sizeof("_DEPEND_"));
2143 pVar = kbuild_get_recursive_variable(pszSrcVar);
2144 psz = pszVal = xmalloc(pVar->value_length + 1 + pDeps->value_length + 1 + pSource->value_length + 1);
2145 memcpy(psz, pVar->value, pVar->value_length); psz += pVar->value_length;
2146 *psz++ = ' ';
2147 memcpy(psz, pDeps->value, pDeps->value_length); psz += pDeps->value_length;
2148 *psz++ = ' ';
2149 memcpy(psz, pSource->value, pSource->value_length + 1);
2150 do_variable_definition_2(NILF, pszDstVar, pszVal, pVar->value_length + 1 + pDeps->value_length + 1 + pSource->value_length,
2151 !pVar->recursive && !pDeps->recursive && !pSource->recursive,
2152 NULL, o_local, f_simple, 0 /* !target_var */);
2153 do_variable_definition_2(NILF, "kbsrc_depend", pszVal, pVar->value_length + 1 + pDeps->value_length + 1 + pSource->value_length,
2154 !pVar->recursive && !pDeps->recursive && !pSource->recursive,
2155 pszVal, o_local, f_simple, 0 /* !target_var */);
2156
2157 memcpy(pszSrc, "_DEPORD", sizeof("_DEPORD"));
2158 memcpy(pszDst, "_DEPORD_", sizeof("_DEPORD_"));
2159 pVar = kbuild_get_recursive_variable(pszSrcVar);
2160 psz = pszVal = xmalloc(pVar->value_length + 1 + pDirDep->value_length + 1 + pOrderDeps->value_length + 1);
2161 memcpy(psz, pVar->value, pVar->value_length); psz += pVar->value_length;
2162 *psz++ = ' ';
2163 memcpy(psz, pDirDep->value, pDirDep->value_length); psz += pDirDep->value_length;
2164 *psz++ = ' ';
2165 memcpy(psz, pOrderDeps->value, pOrderDeps->value_length + 1);
2166 do_variable_definition_2(NILF, pszDstVar, pszVal,
2167 pVar->value_length + 1 + pDirDep->value_length + 1 + pOrderDeps->value_length,
2168 !pVar->recursive && !pDirDep->recursive && !pOrderDeps->recursive,
2169 NULL, o_local, f_simple, 0 /* !target_var */);
2170 do_variable_definition_2(NILF, "kbsrc_depord", pszVal,
2171 pVar->value_length + 1 + pDirDep->value_length + 1 + pOrderDeps->value_length,
2172 !pVar->recursive && !pDirDep->recursive && !pOrderDeps->recursive,
2173 pszVal, o_local, f_simple, 0 /* !target_var */);
2174
2175 /*
2176 _OUT_FILES += $($(target)_$(source)_OUTPUT_) $($(target)_$(source)_OUTPUT_MAYBE_)
2177 */
2178 pVar = kbuild_get_variable_n(ST("_OUT_FILES"));
2179 append_string_to_variable(pVar, pOutput->value, pOutput->value_length, 1 /* append */);
2180 if (pOutputMaybe->value_length)
2181 append_string_to_variable(pVar, pOutputMaybe->value, pOutputMaybe->value_length, 1 /* append */);
2182
2183 /*
2184 $(target)_2_OBJS += $(obj)
2185 */
2186 memcpy(pszDstVar + pTarget->value_length, "_2_OBJS", sizeof("_2_OBJS"));
2187 pVar = kbuild_query_recursive_variable_n(pszDstVar, pTarget->value_length + sizeof("_2_OBJS") - 1);
2188 fInstallOldVars |= iVer <= 2 && (!pVar || !pVar->value_length);
2189 if (pVar)
2190 {
2191 if (pVar->recursive)
2192 pVar = kbuild_simplify_variable(pVar);
2193 append_string_to_variable(pVar, pObj->value, pObj->value_length, 1 /* append */);
2194 }
2195 else
2196 define_variable_vl_global(pszDstVar, pTarget->value_length + sizeof("_2_OBJS") - 1,
2197 pObj->value, pObj->value_length,
2198 1 /* duplicate_value */,
2199 o_file,
2200 0 /* recursive */,
2201 NULL /* flocp */);
2202
2203 /*
2204 * Install legacy variables.
2205 */
2206 if (fInstallOldVars)
2207 {
2208 /* $(target)_OBJS_ = $($(target)_2_OBJS)*/
2209 memcpy(pszDstVar + pTarget->value_length, "_OBJS_", sizeof("_OBJS_"));
2210
2211 pszSrcVar[0] = '$';
2212 pszSrcVar[1] = '(';
2213 memcpy(pszSrcVar + 2, pTarget->value, pTarget->value_length);
2214 psz = pszSrcVar + 2 + pTarget->value_length;
2215 memcpy(psz, "_2_OBJS)", sizeof("_2_OBJS)"));
2216
2217 define_variable_vl_global(pszDstVar, pTarget->value_length + sizeof("_OBJS_") - 1,
2218 pszSrcVar, pTarget->value_length + sizeof("$(_2_OBJS)") - 1,
2219 1 /* duplicate_value */,
2220 o_file,
2221 1 /* recursive */,
2222 NULL /* flocp */);
2223 }
2224
2225 /*
2226 $(eval $(def_target_source_rule))
2227 */
2228 pVar = kbuild_get_recursive_variable("def_target_source_rule");
2229 pszVal = variable_expand_string_2 (o, pVar->value, pVar->value_length, &psz);
2230 assert(!((size_t)pszVal & 3));
2231
2232 install_variable_buffer(&pszSavedVarBuf, &cchSavedVarBuf);
2233 eval_buffer(pszVal, psz);
2234 restore_variable_buffer(pszSavedVarBuf, cchSavedVarBuf);
2235
2236 kbuild_put_sdks(&Sdks);
2237 (void)pszFuncName;
2238
2239 *pszVal = '\0';
2240 return pszVal;
2241}
2242
2243/*
2244
2245## Inherit one template property in a non-accumulative manner.
2246# @param $(prop) Property name
2247# @param $(target) Target name
2248# @todo fix the precedence order for some properties.
2249define def_inherit_template_one
2250ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop)
2251ifndef $(target)_$(prop)
2252$(target)_$(prop) := $(TEMPLATE_$($(target)_TEMPLATE)_$(prop))
2253endif
2254endif
2255ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg)
2256ifndef $(target)_$(prop).$(bld_trg)
2257$(target)_$(prop).$(bld_trg) := $(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg))
2258endif
2259endif
2260ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch)
2261ifndef $(target)_$(prop).$(bld_trg).$(bld_trg_arch)
2262$(target)_$(prop).$(bld_trg).$(bld_trg_arch) := $(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch))
2263endif
2264endif
2265ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch)
2266ifndef $(target)_$(prop).$(bld_trg_arch)
2267$(target)_$(prop).$(bld_trg_arch) := $(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch))
2268endif
2269endif
2270ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu)
2271ifndef $(target)_$(prop).$(bld_trg_cpu)
2272$(target)_$(prop).$(bld_trg_cpu) := $(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu))
2273endif
2274endif
2275endef
2276
2277## Inherit one template property in a non-accumulative manner, deferred expansion.
2278# @param 1: $(prop) Property name
2279# @param 2: $(target) Target name
2280# @todo fix the precedence order for some properties.
2281# @remark this define relies on double evaluation
2282define def_inherit_template_one_deferred
2283ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop)
2284ifndef $(target)_$(prop)
2285$(target)_$(prop) = $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop))
2286endif
2287endif
2288ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg)
2289ifndef $(target)_$(prop).$(bld_trg)
2290$(target)_$(prop).$(bld_trg) = $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg))
2291endif
2292endif
2293ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch)
2294ifndef $(target)_$(prop).$(bld_trg).$(bld_trg_arch)
2295$(target)_$(prop).$(bld_trg).$(bld_trg_arch) = $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch))
2296endif
2297endif
2298ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch)
2299ifndef $(target)_$(prop).$(bld_trg_arch)
2300$(target)_$(prop).$(bld_trg_arch) = $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch))
2301endif
2302endif
2303ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu)
2304ifndef $(target)_$(prop).$(bld_trg_cpu)
2305$(target)_$(prop).$(bld_trg_cpu) = $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu))
2306endif
2307endif
2308endef
2309
2310## Inherit one acculumlative template property where the 'most significant' items are at the left end.
2311# @param $(prop) Property name
2312# @param $(target) Target name
2313define def_inherit_template_one_accumulate_l
2314ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop)
2315 ifeq ($$(flavor $(target)_$(prop)),simple)
2316 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop))
2317 endif
2318$(target)_$(prop) += $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop))
2319endif
2320ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(KBUILD_TYPE)
2321 ifeq ($$(flavor $(target)_$(prop).$(KBUILD_TYPE)),simple)
2322 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(KBUILD_TYPE))
2323 endif
2324$(target)_$(prop).$(KBUILD_TYPE) += $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(KBUILD_TYPE))
2325endif
2326ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg)
2327 ifeq ($$(flavor $(target)_$(prop).$(bld_trg)),simple)
2328 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg))
2329 endif
2330$(target)_$(prop).$(bld_trg) += $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg))
2331endif
2332ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch)
2333 ifeq ($$(flavor $(target)_$(prop).$(bld_trg).$(bld_trg_arch)),simple)
2334 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg).$(bld_trg_arch))
2335 endif
2336$(target)_$(prop).$(bld_trg).$(bld_trg_arch) += $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch))
2337endif
2338ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu)
2339 ifeq ($$(flavor $(target)_$(prop).$(bld_trg_cpu)),simple)
2340 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg_cpu))
2341 endif
2342$(target)_$(prop).$(bld_trg_cpu) += $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu))
2343endif
2344ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch)
2345 ifeq ($$(flavor $(target)_$(prop).$(bld_trg_arch)),simple)
2346 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg_arch))
2347 endif
2348$(target)_$(prop).$(bld_trg_arch) += $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch))
2349endif
2350endef
2351
2352## Inherit one acculumlative template property where the 'most significant' items are at the right end.
2353# @param $(prop) Property name
2354# @param $(target) Target name
2355define def_inherit_template_one_accumulate_r
2356ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop)
2357 ifeq ($$(flavor $(target)_$(prop)),simple)
2358 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop))
2359 endif
2360$(target)_$(prop) <=$$(TEMPLATE_$($(target)_TEMPLATE)_$(prop))
2361endif
2362ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(KBUILD_TYPE)
2363 ifeq ($$(flavor $(target)_$(prop).$(KBUILD_TYPE)),simple)
2364 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(KBUILD_TYPE))
2365 endif
2366$(target)_$(prop).$(KBUILD_TYPE) <=$$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(KBUILD_TYPE))
2367endif
2368ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg)
2369 ifeq ($$(flavor $(target)_$(prop).$(bld_trg)),simple)
2370 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg))
2371 endif
2372$(target)_$(prop).$(bld_trg) <=$$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg))
2373endif
2374ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch)
2375 ifeq ($$(flavor $(target)_$(prop).$(bld_trg).$(bld_trg_arch)),simple)
2376 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg).$(bld_trg_arch))
2377 endif
2378$(target)_$(prop).$(bld_trg).$(bld_trg_arch) <=$$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch))
2379endif
2380ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu)
2381 ifeq ($$(flavor $(target)_$(prop).$(bld_trg_cpu)),simple)
2382 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg_cpu))
2383 endif
2384$(target)_$(prop).$(bld_trg_cpu) <=$$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu))
2385endif
2386ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch)
2387 ifeq ($$(flavor $(target)_$(prop).$(bld_trg_arch)),simple)
2388 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg_arch))
2389 endif
2390$(target)_$(prop).$(bld_trg_arch) <=$$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch))
2391endif
2392endef
2393
2394
2395## Inherit template properties for on target.
2396# @param $(target) Target name.
2397define def_inherit_template
2398# sanity check.
2399ifdef _$(target)_ALREADY_PROCESSED
2400 $(error kBuild: The target $(target) appears more than once in the target lists! Please correct the makefile(s))
2401endif
2402_$(target)_ALREADY_PROCESSED := 1
2403
2404# Inherit any default template.
2405ifdef TEMPLATE
2406ifeq ($($(target)_TEMPLATE),)
2407$(eval $(target)_TEMPLATE:=$(TEMPLATE))
2408endif
2409endif
2410# Expand the template if specified.
2411ifneq ($($(target)_TEMPLATE),)
2412$(foreach prop,$(PROPS_SINGLE),$(evalval def_inherit_template_one))
2413$(foreach prop,$(PROPS_DEFERRED),$(eval $(def_inherit_template_one_deferred))) # exploits the 2 evaluation, so no value!
2414$(foreach prop,$(PROPS_ACCUMULATE_L),$(eval $(def_inherit_template_one_accumulate_l))) # += works fine without value
2415$(foreach prop,$(PROPS_ACCUMULATE_R),$(eval $(def_inherit_template_one_accumulate_r))) # use <= (kmk addition)
2416endif
2417endef
2418
2419
2420Invoked like this:
2421 $(kb-exp-tmpl 1,$(_ALL_TARGET_TARGETS),$(KBUILD_TARGET),$(KBUILD_TARGET_ARCH),$(KBUILD_TARGET_CPU),$(KBUILD_TYPE))
2422*/
2423char *
2424func_kbuild_expand_template(char *o, char **argv, const char *pszFuncName)
2425{
2426 const char *pszVersion = argv[0];
2427 const char *pszBldTrg = argv[2];
2428 const char *pszBldTrgArch = argv[3];
2429 const char *pszBldTrgCpu = argv[4];
2430 const char *pszBldType = argv[5];
2431 size_t cchBldTrg = strlen(pszBldTrg);
2432 size_t cchBldTrgArch = strlen(pszBldTrgArch);
2433 size_t cchBldTrgCpu = strlen(pszBldTrgCpu);
2434 size_t cchBldType = strlen(pszBldType);
2435 size_t cchMaxBld = cchBldTrg + cchBldTrgArch + cchBldTrgCpu + cchBldType; /* too big, but so what. */
2436 struct kbet_key
2437 {
2438 unsigned int cch;
2439 char *psz;
2440 } aKeys[6];
2441 unsigned int const cKeys = 6;
2442 unsigned int iKey;
2443 struct variable *pDefTemplate;
2444 struct variable *pProps;
2445 struct kbet_prop
2446 {
2447 const char *pch;
2448 unsigned int cch;
2449 enum kbet_prop_enum { kPropSingle, kPropDeferred, kPropAccumulateL, kPropAccumulateR }
2450 enmType;
2451 } *paProps;
2452 unsigned int cProps;
2453 unsigned int iProp;
2454 size_t cchMaxProp;
2455 struct variable *pVarTrg;
2456 struct variable *pVarSrc;
2457 const char *pszIter;
2458 const char *pszTarget;
2459 unsigned int cchTarget;
2460 char *pszSrc = 0;
2461 char *pszSrcRef = 0;
2462 char *pszSrcBuf = 0;
2463 size_t cchSrcBuf = 0;
2464 char *pszTrg = 0;
2465 size_t cchTrg = 0;
2466
2467 /*
2468 * Validate input.
2469 */
2470 if (pszVersion[0] != '1' || pszVersion[1])
2471 fatal(NULL, "%s: Unsupported version `%s'", pszFuncName, pszVersion);
2472
2473 if (!cchBldTrg)
2474 fatal(NULL, "%s: missing bldtrg", pszFuncName);
2475
2476 if (!cchBldTrgArch)
2477 fatal(NULL, "%s: missing bld_trg_arch", pszFuncName);
2478
2479 if (!cchBldTrgCpu)
2480 fatal(NULL, "%s: missing bld_trg_cpu", pszFuncName);
2481
2482 if (!cchBldType)
2483 fatal(NULL, "%s: missing bld_type", pszFuncName);
2484
2485 /*
2486 * Prepare the keywords, prepending dots for quicker copying.
2487 * This allows for an inner loop when processing properties, saving code
2488 * at the expense of a few xmallocs.
2489 */
2490 /* the first entry is empty. */
2491 aKeys[0].cch = 0;
2492 aKeys[0].psz = NULL;
2493
2494 /* .$(bld_type) */
2495 aKeys[1].cch = cchBldType + 1;
2496 aKeys[1].psz = xmalloc (aKeys[1].cch + 1);
2497 aKeys[1].psz[0] = '.';
2498 memcpy(aKeys[1].psz + 1, pszBldType, cchBldType + 1);
2499
2500 /* .$(bld_trg) */
2501 aKeys[2].cch = cchBldTrg + 1;
2502 aKeys[2].psz = xmalloc (aKeys[2].cch + 1);
2503 aKeys[2].psz[0] = '.';
2504 memcpy(aKeys[2].psz + 1, pszBldTrg, cchBldTrg + 1);
2505
2506 /* .$(bld_trg).$(bld_trg_arch) */
2507 aKeys[3].cch = cchBldTrg + 1 + cchBldTrgArch + 1;
2508 aKeys[3].psz = xmalloc (aKeys[3].cch + 1);
2509 aKeys[3].psz[0] = '.';
2510 memcpy(aKeys[3].psz + 1, pszBldTrg, cchBldTrg);
2511 aKeys[3].psz[1 + cchBldTrg] = '.';
2512 memcpy(aKeys[3].psz + 1 + cchBldTrg + 1, pszBldTrgArch, cchBldTrgArch + 1);
2513
2514 /* .$(bld_trg_cpu) */
2515 aKeys[4].cch = cchBldTrgCpu + 1;
2516 aKeys[4].psz = xmalloc (aKeys[4].cch + 1);
2517 aKeys[4].psz[0] = '.';
2518 memcpy(aKeys[4].psz + 1, pszBldTrgCpu, cchBldTrgCpu + 1);
2519
2520 /* .$(bld_trg_arch) */
2521 aKeys[5].cch = cchBldTrgArch + 1;
2522 aKeys[5].psz = xmalloc (aKeys[5].cch + 1);
2523 aKeys[5].psz[0] = '.';
2524 memcpy(aKeys[5].psz + 1, pszBldTrgArch, cchBldTrgArch + 1);
2525
2526
2527 /*
2528 * Prepare the properties, folding them into an array.
2529 * This way we won't have to reparse them for each an every target, though
2530 * it comes at the expense of one or more heap calls.
2531 */
2532#define PROP_ALLOC_INC 128
2533 iProp = 0;
2534 cProps = PROP_ALLOC_INC;
2535 paProps = xmalloc(sizeof(*pProps) * cProps);
2536
2537 pProps = kbuild_get_variable_n(ST("PROPS_SINGLE"));
2538 pszIter = pProps->value;
2539 while ((paProps[iProp].pch = find_next_token(&pszIter, &paProps[iProp].cch)))
2540 {
2541 paProps[iProp].enmType = kPropSingle;
2542 if (++iProp >= cProps)
2543 {
2544 cProps += PROP_ALLOC_INC;
2545 paProps = xrealloc(paProps, sizeof(*paProps) * cProps);
2546 }
2547
2548 }
2549
2550 pProps = kbuild_get_variable_n(ST("PROPS_DEFERRED"));
2551 pszIter = pProps->value;
2552 while ((paProps[iProp].pch = find_next_token(&pszIter, &paProps[iProp].cch)))
2553 {
2554 paProps[iProp].enmType = kPropDeferred;
2555 if (++iProp >= cProps)
2556 {
2557 cProps += PROP_ALLOC_INC;
2558 paProps = xrealloc(paProps, sizeof(*paProps) * cProps);
2559 }
2560 }
2561
2562 pProps = kbuild_get_variable_n(ST("PROPS_ACCUMULATE_L"));
2563 pszIter = pProps->value;
2564 while ((paProps[iProp].pch = find_next_token(&pszIter, &paProps[iProp].cch)))
2565 {
2566 paProps[iProp].enmType = kPropAccumulateL;
2567 if (++iProp >= cProps)
2568 {
2569 cProps += PROP_ALLOC_INC;
2570 paProps = xrealloc(paProps, sizeof(*paProps) * cProps);
2571 }
2572 }
2573
2574 pProps = kbuild_get_variable_n(ST("PROPS_ACCUMULATE_R"));
2575 pszIter = pProps->value;
2576 while ((paProps[iProp].pch = find_next_token(&pszIter, &paProps[iProp].cch)))
2577 {
2578 paProps[iProp].enmType = kPropAccumulateR;
2579 if (++iProp >= cProps)
2580 {
2581 cProps += PROP_ALLOC_INC;
2582 paProps = xrealloc(paProps, sizeof(*paProps) * cProps);
2583 }
2584 }
2585#undef PROP_ALLOC_INC
2586 cProps = iProp;
2587
2588 /* find the max prop length. */
2589 cchMaxProp = paProps[0].cch;
2590 while (--iProp > 0)
2591 if (paProps[iProp].cch > cchMaxProp)
2592 cchMaxProp = paProps[iProp].cch;
2593
2594 /*
2595 * Query and prepare (strip) the default template
2596 * (given by the TEMPLATE variable).
2597 */
2598 pDefTemplate = kbuild_lookup_variable_n(ST("TEMPLATE"));
2599 if (pDefTemplate)
2600 {
2601 if ( pDefTemplate->value_length
2602 && ( isspace(pDefTemplate->value[0])
2603 || isspace(pDefTemplate->value[pDefTemplate->value_length - 1])))
2604 {
2605 unsigned int off;
2606 if (pDefTemplate->rdonly_val)
2607 fatal(NULL, "%s: TEMPLATE is read-only", pszFuncName);
2608
2609 /* head */
2610 for (off = 0; isspace(pDefTemplate->value[off]); off++)
2611 /* nothing */;
2612 if (off)
2613 {
2614 pDefTemplate->value_length -= off;
2615 memmove(pDefTemplate->value, pDefTemplate->value + off, pDefTemplate->value_length + 1);
2616 }
2617
2618 /* tail */
2619 off = pDefTemplate->value_length;
2620 while (off > 0 && isspace(pDefTemplate->value[off - 1]))
2621 off--;
2622 pDefTemplate->value_length = off;
2623 pDefTemplate->value[off] = '\0';
2624
2625 VARIABLE_CHANGED(pDefTemplate);
2626 }
2627
2628 if (!pDefTemplate->value_length)
2629 pDefTemplate = NULL;
2630 }
2631
2632 /*
2633 * Iterate the target list.
2634 */
2635 pszIter = argv[1];
2636 while ((pszTarget = find_next_token(&pszIter, &cchTarget)))
2637 {
2638 char *pszTrgProp, *pszSrcProp;
2639 char *pszTrgKey, *pszSrcKey;
2640 struct variable *pTmpl;
2641 const char *pszTmpl;
2642 size_t cchTmpl, cchMax;
2643
2644 /* resize the target buffer. */
2645 cchMax = cchTarget + cchMaxProp + cchMaxBld + 10;
2646 if (cchTrg < cchMax)
2647 {
2648 cchTrg = (cchMax + 31U) & ~(size_t)31;
2649 pszTrg = xrealloc(pszTrg, cchTrg);
2650 }
2651
2652 /*
2653 * Query the TEMPLATE property, if not found or zero-length fall back on the default.
2654 */
2655 memcpy(pszTrg, pszTarget, cchTarget);
2656 pszTrgProp = pszTrg + cchTarget;
2657 memcpy(pszTrgProp, "_TEMPLATE", sizeof("_TEMPLATE"));
2658 pszTrgProp++; /* after '_'. */
2659
2660 /** @todo Change this to a recursive lookup with simplification below. That
2661 * will allow target_TEMPLATE = $(NO_SUCH_TEMPLATE) instead of having
2662 * to use target_TEMPLATE = DUMMY */
2663 pTmpl = kbuild_lookup_variable_n(pszTrg, cchTarget + sizeof("_TEMPLATE") - 1);
2664 if (!pTmpl || !pTmpl->value_length)
2665 {
2666 if (!pDefTemplate)
2667 continue; /* no template */
2668 pszTmpl = pDefTemplate->value;
2669 cchTmpl = pDefTemplate->value_length;
2670 }
2671 else
2672 {
2673 pszTmpl = pTmpl->value;
2674 cchTmpl = pTmpl->value_length;
2675 while (isspace(*pszTmpl))
2676 cchTmpl--, pszTmpl++;
2677 if (!cchTmpl)
2678 continue; /* no template */
2679 }
2680
2681 /* resize the source buffer. */
2682 cchMax = sizeof("TEMPLATE_") + cchTmpl + cchMaxProp + cchMaxBld + 10 + sizeof(void *);
2683 if (cchSrcBuf < cchMax)
2684 {
2685 cchSrcBuf = (cchMax + 31U) & ~(size_t)31;
2686 pszSrcBuf = xrealloc(pszSrcBuf, cchSrcBuf);
2687 pszSrc = pszSrcBuf + sizeof(void *); assert(sizeof(void *) >= 2);
2688 pszSrcRef = pszSrc - 2;
2689 pszSrcRef[0] = '$';
2690 pszSrcRef[1] = '(';
2691 }
2692
2693 /* prepare the source buffer */
2694 memcpy(pszSrc, "TEMPLATE_", sizeof("TEMPLATE_") - 1);
2695 pszSrcProp = pszSrc + sizeof("TEMPLATE_") - 1;
2696 memcpy(pszSrcProp, pszTmpl, cchTmpl);
2697 pszSrcProp += cchTmpl;
2698 *pszSrcProp++ = '_';
2699
2700 /*
2701 * Process properties.
2702 * Note! The single and deferred are handled in the same way now.
2703 */
2704#define BY_REF_LIMIT 64 /*(cchSrcVar * 4 > 64 ? cchSrcVar * 4 : 64)*/
2705
2706 for (iProp = 0; iProp < cProps; iProp++)
2707 {
2708 memcpy(pszTrgProp, paProps[iProp].pch, paProps[iProp].cch);
2709 pszTrgKey = pszTrgProp + paProps[iProp].cch;
2710
2711 memcpy(pszSrcProp, paProps[iProp].pch, paProps[iProp].cch);
2712 pszSrcKey = pszSrcProp + paProps[iProp].cch;
2713
2714 for (iKey = 0; iKey < cKeys; iKey++)
2715 {
2716 char *pszTrgEnd;
2717 size_t cchSrcVar;
2718
2719 /* lookup source, skip ahead if it doesn't exist. */
2720 memcpy(pszSrcKey, aKeys[iKey].psz, aKeys[iKey].cch);
2721 cchSrcVar = pszSrcKey - pszSrc + aKeys[iKey].cch;
2722 pszSrc[cchSrcVar] = '\0';
2723 pVarSrc = kbuild_query_recursive_variable_n(pszSrc, cchSrcVar);
2724 if (!pVarSrc)
2725 continue;
2726
2727 /* lookup target, skip ahead if it exists. */
2728 memcpy(pszTrgKey, aKeys[iKey].psz, aKeys[iKey].cch);
2729 pszTrgEnd = pszTrgKey + aKeys[iKey].cch;
2730 *pszTrgEnd = '\0';
2731 pVarTrg = kbuild_query_recursive_variable_n(pszTrg, pszTrgEnd - pszTrg);
2732
2733 switch (paProps[iProp].enmType)
2734 {
2735 case kPropAccumulateL:
2736 case kPropAccumulateR:
2737 if (pVarTrg)
2738 {
2739 /* Append to existing variable. If the source is recursive,
2740 or we append by reference, we'll have to make sure the
2741 target is recusive as well. */
2742 if ( !pVarTrg->recursive
2743 && ( pVarSrc->value_length >= BY_REF_LIMIT
2744 || pVarSrc->recursive))
2745 pVarTrg->recursive = 1;
2746
2747 if (pVarSrc->value_length < BY_REF_LIMIT)
2748 append_string_to_variable(pVarTrg, pVarSrc->value, pVarSrc->value_length,
2749 paProps[iProp].enmType == kPropAccumulateL /* append */);
2750 else
2751 {
2752 pszSrc[cchSrcVar] = ')';
2753 pszSrc[cchSrcVar + 1] = '\0';
2754 append_string_to_variable(pVarTrg, pszSrcRef, 2 + cchSrcVar + 1,
2755 paProps[iProp].enmType == kPropAccumulateL /* append */);
2756 }
2757 break;
2758 }
2759 /* else: the target variable doesn't exist, create it. */
2760 /* fall thru */
2761
2762 case kPropSingle:
2763 case kPropDeferred:
2764 if (pVarTrg)
2765 continue; /* skip ahead if it already exists. */
2766
2767 /* copy the variable if its short, otherwise reference it. */
2768 if (pVarSrc->value_length < BY_REF_LIMIT)
2769 define_variable_vl_global(pszTrg, pszTrgEnd - pszTrg,
2770 pVarSrc->value, pVarSrc->value_length,
2771 1 /* duplicate_value */,
2772 o_file,
2773 pVarSrc->recursive,
2774 NULL /* flocp */);
2775 else
2776 {
2777 pszSrc[cchSrcVar] = ')';
2778 pszSrc[cchSrcVar + 1] = '\0';
2779 define_variable_vl_global(pszTrg, pszTrgEnd - pszTrg,
2780 pszSrcRef, 2 + cchSrcVar + 1,
2781 1 /* duplicate_value */,
2782 o_file,
2783 1 /* recursive */,
2784 NULL /* flocp */);
2785 }
2786 break;
2787
2788 }
2789
2790 } /* foreach key */
2791 } /* foreach prop */
2792#undef BY_REF_LIMIT
2793 } /* foreach target */
2794
2795 /*
2796 * Cleanup.
2797 */
2798 free(pszSrcBuf);
2799 free(pszTrg);
2800 free(paProps);
2801 for (iKey = 1; iKey < cKeys; iKey++)
2802 free(aKeys[iKey].psz);
2803
2804 return o;
2805}
2806
2807#endif /* KMK_HELPERS */
2808
Note: See TracBrowser for help on using the repository browser.

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