VirtualBox

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

Last change on this file since 3140 was 3140, checked in by bird, 7 years ago

kmk: Merged in changes from GNU make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6 / https://git.savannah.gnu.org/git/make.git).

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

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