VirtualBox

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

Last change on this file since 3415 was 3415, checked in by bird, 4 years ago

kmk: Fixed mixup from r2433 where _TARGET_TOOL was extended but the builtin source handling functions wasn't. For instance, using _TOOLS, _TOOLS.amd64 and _TOOLS.x86, we would end up with the first when compiling and one of the latter two when linking. Also replaced a few legacy BUILD_PLATFORM* with KBUILD_HOST* variable names in the footers.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 103.1 KB
Line 
1/* $Id: kbuild.c 3415 2020-08-20 23:43:33Z 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 unsigned int cchInCur;
385 unsigned int cchMaxRelative = 0;
386 const char *pszInCur;
387
388 /*
389 * The first pass, count the relative paths.
390 */
391 const char *pszIterator = *ppsz;
392 const char * const pszEos = pszIterator + *pcch;
393 unsigned int cRelativePaths = 0;
394 assert(*pszEos == '\0');
395 while ((pszInCur = find_next_file_token(&pszIterator, pszEos, &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_file_token(&pszIterator, pszEos, &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 an non-empty variable when simplified and spaces skipped.
632 *
633 * This handy when emulating $(firstword )/$(lastword ) behaviour.
634 *
635 * @returns Pointer to the variable. NULL if not found.
636 * @param pszName The variable name.
637 * @param cchName The name length.
638 */
639MY_INLINE struct variable *
640kbuild_lookup_not_empty_variable_n(const char *pszName, size_t cchName)
641{
642 struct variable *pVar = kbuild_lookup_variable_n(pszName, cchName);
643 if (pVar && !pVar->recursive)
644 {
645 /*
646 * Skip spaces and make sure it's non-zero.
647 */
648 char *psz = pVar->value;
649 if (!ISSPACE(*psz))
650 { /* kind of likely */ }
651 else
652 do
653 psz++;
654 while (ISSPACE(*psz));
655
656 if (*psz)
657 { /*kind of likely */ }
658 else
659 pVar = NULL;
660 }
661 return pVar;
662}
663
664
665/**
666 * Looks up a variable.
667 * The value_length field is valid upon successful return.
668 *
669 * @returns Pointer to the variable. NULL if not found.
670 * @param pszName The variable name.
671 */
672MY_INLINE struct variable *
673kbuild_lookup_variable(const char *pszName)
674{
675 return kbuild_lookup_variable_n(pszName, strlen(pszName));
676}
677
678
679/**
680 * Looks up a variable and applies default a path to all relative paths.
681 * The value_length field is valid upon successful return.
682 *
683 * @returns Pointer to the variable. NULL if not found.
684 * @param pDefPath The default path.
685 * @param pszName The variable name.
686 * @param cchName The name length.
687 */
688MY_INLINE struct variable *
689kbuild_lookup_variable_defpath_n(struct variable *pDefPath, const char *pszName, size_t cchName)
690{
691 struct variable *pVar = kbuild_lookup_variable_n(pszName, cchName);
692 if (pVar && pDefPath)
693 {
694 assert(pVar->origin != o_automatic);
695#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
696 assert(!pVar->rdonly_val);
697#endif
698 kbuild_apply_defpath(pDefPath, &pVar->value, &pVar->value_length, &pVar->value_alloc_len, 1);
699 }
700 return pVar;
701}
702
703
704/**
705 * Looks up a variable and applies default a path to all relative paths.
706 * The value_length field is valid upon successful return.
707 *
708 * @returns Pointer to the variable. NULL if not found.
709 * @param pDefPath The default path.
710 * @param pszName The variable name.
711 */
712MY_INLINE struct variable *
713kbuild_lookup_variable_defpath(struct variable *pDefPath, const char *pszName)
714{
715 struct variable *pVar = kbuild_lookup_variable(pszName);
716 if (pVar && pDefPath)
717 {
718 assert(pVar->origin != o_automatic);
719#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
720 assert(!pVar->rdonly_val);
721#endif
722 kbuild_apply_defpath(pDefPath, &pVar->value, &pVar->value_length, &pVar->value_alloc_len, 1);
723 }
724 return pVar;
725}
726
727
728/**
729 * Gets the first defined property variable.
730 *
731 * When pBldType is given, additional property variations are consulted.
732 * See _TARGET_TOOL/r2433 in footer.kmk for the target-level difference.
733 * Similar extended property lookup is applied to the source part if the
734 * fExtendedSource parameter is non-zero (not done yet as it'll be expensive).
735 *
736 * Since r3415 this function will use $(target)_2_$(type)TOOL to cache the
737 * result of the target specific part of the lookup (_TARGET_TOOL).
738 */
739static struct variable *
740kbuild_first_prop(struct variable *pTarget, struct variable *pSource,
741 struct variable *pTool, struct variable *pType,
742 struct variable *pBldTrg, struct variable *pBldTrgArch, struct variable *pBldType,
743 const char *pszPropF1, char cchPropF1,
744 const char *pszPropF2, char cchPropF2,
745 int fExtendedSource,
746 const char *pszVarName)
747{
748 struct variable *pVar;
749 size_t cchBuf;
750 char *pszBuf;
751 char *psz, *psz1, *psz2, *psz3, *psz4, *pszEnd;
752 int fCacheIt = 1;
753
754 fExtendedSource = fExtendedSource && pBldType != NULL;
755
756 /* calc and allocate a too big name buffer. */
757 cchBuf = cchPropF2 + 1
758 + cchPropF1 + 1
759 + pTarget->value_length + 1
760 + pSource->value_length + 1
761 + (pTool ? pTool->value_length + 1 : 0)
762 + pType->value_length + 1
763 + pBldTrg->value_length + 1
764 + pBldTrgArch->value_length + 1
765 + (pBldType ? pBldType->value_length + 1 : 0);
766 pszBuf = xmalloc(cchBuf);
767
768#define ADD_VAR(pVar) do { my_memcpy(psz, (pVar)->value, (pVar)->value_length); psz += (pVar)->value_length; } while (0)
769#define ADD_STR(pszStr, cchStr) do { my_memcpy(psz, (pszStr), (cchStr)); psz += (cchStr); } while (0)
770#define ADD_CSTR(pszStr) do { my_memcpy(psz, pszStr, sizeof(pszStr) - 1); psz += sizeof(pszStr) - 1; } while (0)
771#define ADD_CH(ch) do { *psz++ = (ch); } while (0)
772
773 /*
774 * $(target)_$(source)_$(type)$(propf2).$(bld_trg).$(bld_trg_arch).$(bld_type)
775 * ...
776 * $(target)_$(source)_$(type)$(propf2)
777 */
778 psz = pszBuf;
779 ADD_VAR(pTarget);
780 ADD_CH('_');
781 ADD_VAR(pSource);
782 ADD_CH('_');
783 psz1 = psz;
784 ADD_VAR(pType);
785 ADD_STR(pszPropF2, cchPropF2);
786#define DO_VARIATIONS(a_fExtended) do { \
787 psz2 = psz; \
788 ADD_CH('.'); \
789 ADD_VAR(pBldTrg); \
790 psz3 = psz; \
791 ADD_CH('.'); \
792 ADD_VAR(pBldTrgArch); \
793 psz4 = psz; \
794 if ((a_fExtended) && pBldType) \
795 { \
796 ADD_CH('.'); \
797 ADD_VAR(pBldType); \
798 pVar = kbuild_lookup_not_empty_variable_n(pszBuf, psz - pszBuf); \
799 \
800 /* <lead>.$(bld_trg).$(bld_trg_arch) */ \
801 if (!pVar) \
802 pVar = kbuild_lookup_not_empty_variable_n(pszBuf, psz4 - pszBuf); \
803 \
804 /* <lead>.$(bld_trg).$(bld_type) */ \
805 if (!pVar) \
806 { \
807 psz = psz3 + 1; \
808 ADD_VAR(pBldType); \
809 pVar = kbuild_lookup_not_empty_variable_n(pszBuf, psz - pszBuf); \
810 } \
811 \
812 /* <lead>.$(bld_trg_arch) */ \
813 if (!pVar) \
814 { \
815 psz = psz2 + 1; \
816 ADD_VAR(pBldTrgArch); \
817 pVar = kbuild_lookup_not_empty_variable_n(pszBuf, psz - pszBuf); \
818 } \
819 \
820 /* <lead>.$(bld_trg) */ \
821 if (!pVar) \
822 { \
823 psz = psz2 + 1; \
824 ADD_VAR(pBldTrg); \
825 pVar = kbuild_lookup_not_empty_variable_n(pszBuf, psz - pszBuf); \
826 } \
827 \
828 /* <lead>.$(bld_type) */ \
829 if (!pVar) \
830 { \
831 psz = psz2 + 1; \
832 ADD_VAR(pBldType); \
833 pVar = kbuild_lookup_not_empty_variable_n(pszBuf, psz - pszBuf); \
834 } \
835 } \
836 else \
837 { \
838 /* <lead>.$(bld_trg).$(bld_trg_arch) */ \
839 pVar = kbuild_lookup_not_empty_variable_n(pszBuf, psz4 - pszBuf); \
840 \
841 /* <lead>.$(bld_trg) */ \
842 if (!pVar) \
843 pVar = kbuild_lookup_not_empty_variable_n(pszBuf, psz3 - pszBuf); \
844 } \
845 \
846 /* <lead> */ \
847 if (!pVar) \
848 pVar = kbuild_lookup_not_empty_variable_n(pszBuf, psz2 - pszBuf); \
849} while (0)
850 DO_VARIATIONS(fExtendedSource);
851
852 /*
853 * $(target)_$(source)_$(propf2).$(bld_trg).$(bld_trg_arch).$(bld_type) [omit $(type) prefix to $(propf2)]
854 * ...
855 * $(target)_$(source)_$(propf2)
856 */
857 if (!pVar)
858 {
859 psz = psz1; /* rewind to '$(target)_$(source)_' */
860 ADD_STR(pszPropF2, cchPropF2);
861 DO_VARIATIONS(fExtendedSource);
862 }
863
864 /*
865 * $(source)_$(type)$(propf2).$(bld_trg).$(bld_trg_arch).$(bld_type)
866 * ...
867 * $(source)_$(type)$(propf2)
868 */
869 if (!pVar)
870 {
871 psz = pszBuf;
872 ADD_VAR(pSource);
873 ADD_CH('_');
874 psz1 = psz;
875 ADD_VAR(pType);
876 ADD_STR(pszPropF2, cchPropF2);
877 DO_VARIATIONS(fExtendedSource);
878
879 /*
880 * $(source)_$(propf2).$(bld_trg).$(bld_trg_arch).$(bld_type) [omit $(type) prefix to $(propf2)]
881 * ...
882 * $(source)_$(propf2)
883 */
884 if (!pVar)
885 {
886 psz = psz1; /* rewind to '$(source)_' */
887 ADD_STR(pszPropF2, cchPropF2);
888 DO_VARIATIONS(fExtendedSource);
889 }
890 }
891
892 /*
893 * Check the cache: $(target)_2_$(type)$(propf2)
894 */
895 if (pVar)
896 fCacheIt = 0;
897 else if (fCacheIt)
898 {
899 psz = pszBuf;
900 ADD_VAR(pTarget);
901 ADD_STR("_2_", 3);
902 ADD_VAR(pType);
903 ADD_STR(pszPropF2, cchPropF2);
904 pVar = kbuild_lookup_variable_n(pszBuf, psz - pszBuf);
905
906 /* If found, this can be duplicated and returned directly. No value stripping
907 needed as we defined it (or at least should have) ourselves. */
908 if (pVar)
909 {
910 pVar = define_variable_vl(pszVarName, strlen(pszVarName), pVar->value, pVar->value_length,
911 1 /* duplicate */, o_local, 0 /* !recursive */);
912 free(pszBuf);
913 return pVar;
914 }
915 }
916
917 /*
918 * $(target)_$(type)$(propf2).$(bld_trg).$(bld_trg_arch).$(bld_type)
919 * ...
920 * $(target)_$(type)$(propf2)
921 */
922 if (!pVar)
923 {
924 psz = pszBuf;
925 ADD_VAR(pTarget);
926 ADD_CH('_');
927 psz1 = psz;
928 ADD_VAR(pType);
929 ADD_STR(pszPropF2, cchPropF2);
930 DO_VARIATIONS(1);
931
932 /*
933 * $(target)_$(propf2).$(bld_trg).$(bld_trg_arch).$(bld_type) [omit $(type) prefix to $(propf2)]
934 * ...
935 * $(target)_$(propf2)
936 */
937 if (!pVar)
938 {
939 psz = psz1; /* rewind to '$(target)_' */
940 ADD_STR(pszPropF2, cchPropF2);
941 DO_VARIATIONS(1);
942 }
943 }
944
945 /*
946 * TOOL_$(tool)_$(type)$(propf2).$(bld_trg).$(bld_trg_arch).$(bld_type)
947 * ...
948 * TOOL_$(tool)_$(type)$(propf2)
949 */
950 if (!pVar && pTool)
951 {
952 psz = pszBuf;
953 ADD_CSTR("TOOL_");
954 ADD_VAR(pTool);
955 ADD_CH('_');
956 psz1 = psz;
957 ADD_VAR(pType);
958 ADD_STR(pszPropF2, cchPropF2);
959 DO_VARIATIONS(1);
960
961 /*
962 * TOOL_$(tool)_$(propf2).$(bld_trg).$(bld_trg_arch).$(bld_type) [omit $(type) prefix to $(propf2)]
963 * ...
964 * TOOL_$(tool)_$(propf2)
965 */
966 if (!pVar)
967 {
968 psz = psz1; /* rewind to 'TOOL_$(tool)_' */
969 ADD_STR(pszPropF2, cchPropF2);
970 DO_VARIATIONS(1);
971 }
972 }
973
974 /*
975 * $(type)$(propf1).$(bld_trg).$(bld_trg_arch).$(bld_type)
976 * ...
977 * $(type)$(propf1)
978 */
979 if (!pVar)
980 {
981 psz = pszBuf;
982 ADD_VAR(pType);
983 ADD_STR(pszPropF1, cchPropF1);
984 DO_VARIATIONS(1);
985
986 /*
987 * $(propf1).$(bld_trg).$(bld_trg_arch).$(bld_type)
988 * ...
989 * $(propf1)
990 */
991 if (!pVar)
992 {
993 psz = pszBuf;
994 ADD_STR(pszPropF1, cchPropF1);
995 DO_VARIATIONS(1);
996 }
997 }
998
999 /*
1000 * Done!
1001 */
1002 if (pVar)
1003 {
1004 /* strip it */
1005 psz = pVar->value;
1006 pszEnd = psz + pVar->value_length;
1007 while (ISBLANK(*psz))
1008 psz++;
1009 while (pszEnd > psz && ISBLANK(pszEnd[-1]))
1010 pszEnd--;
1011 if (pszEnd > psz)
1012 {
1013 char chSaved = *pszEnd;
1014 *pszEnd = '\0';
1015 pVar = define_variable_vl(pszVarName, strlen(pszVarName), psz, pszEnd - psz,
1016 1 /* duplicate */, o_local, 0 /* !recursive */);
1017 *pszEnd = chSaved;
1018 }
1019 else
1020 pVar = NULL;
1021 }
1022
1023 /* Cache the result if needed. */
1024 if (fCacheIt)
1025 {
1026 psz = pszBuf;
1027 ADD_VAR(pTarget);
1028 ADD_STR("_2_", 3);
1029 ADD_VAR(pType);
1030 ADD_STR(pszPropF2, cchPropF2);
1031 define_variable_vl_global(pszBuf, psz - pszBuf, pVar ? pVar->value : "", pVar ? pVar->value_length : 0,
1032 1 /* duplicate */, o_file, 0 /* !recursive */, NILF);
1033 }
1034
1035#undef ADD_VAR
1036#undef ADD_STR
1037#undef ADD_CSTR
1038#undef ADD_CH
1039 free(pszBuf);
1040 return pVar;
1041}
1042
1043
1044/*
1045 *
1046_SOURCE_TOOL = $(strip $(firstword \
1047 $($(target)_$(source)_$(type)TOOL.$(bld_trg).$(bld_trg_arch)) \
1048 $($(target)_$(source)_$(type)TOOL.$(bld_trg)) \
1049 $($(target)_$(source)_$(type)TOOL) \
1050 $($(target)_$(source)_TOOL.$(bld_trg).$(bld_trg_arch)) \
1051 $($(target)_$(source)_TOOL.$(bld_trg)) \
1052 $($(target)_$(source)_TOOL) \
1053 $($(source)_$(type)TOOL.$(bld_trg).$(bld_trg_arch)) \
1054 $($(source)_$(type)TOOL.$(bld_trg)) \
1055 $($(source)_$(type)TOOL) \
1056 $($(source)_TOOL.$(bld_trg).$(bld_trg_arch)) \
1057 $($(source)_TOOL.$(bld_trg)) \
1058 $($(source)_TOOL) \
1059 $($(target)_$(type)TOOL.$(bld_trg).$(bld_trg_arch)) \
1060 $($(target)_$(type)TOOL.$(bld_trg)) \
1061 $($(target)_$(type)TOOL) \
1062 $($(target)_TOOL.$(bld_trg).$(bld_trg_arch)) \
1063 $($(target)_TOOL.$(bld_trg)) \
1064 $($(target)_TOOL) \
1065 \ - the rest depends on pBldType, see _TARGET_TOOL and kbuild_first_prop.
1066 $($(type)TOOL.$(bld_trg).$(bld_trg_arch)) \
1067 $($(type)TOOL.$(bld_trg)) \
1068 $($(type)TOOL) \
1069 $(TOOL.$(bld_trg).$(bld_trg_arch)) \
1070 $(TOOL.$(bld_trg)) \
1071 $(TOOL) ))
1072*/
1073static struct variable *
1074kbuild_get_source_tool(struct variable *pTarget, struct variable *pSource, struct variable *pType,
1075 struct variable *pBldTrg, struct variable *pBldTrgArch, struct variable *pBldType,
1076 const char *pszVarName)
1077{
1078 struct variable *pVar = kbuild_first_prop(pTarget, pSource, NULL, pType, pBldTrg, pBldTrgArch, pBldType,
1079 "TOOL", sizeof("TOOL") - 1,
1080 "TOOL", sizeof("TOOL") - 1,
1081 0 /*fExtendedSource*/, pszVarName);
1082 if (!pVar)
1083 OSS(fatal, NILF, _("no tool for source `%s' in target `%s'!"), pSource->value, pTarget->value);
1084 return pVar;
1085}
1086
1087
1088/**
1089 * Helper for func_kbuild_source_tool, func_kbuild_source_one, ++.
1090 */
1091static int kbuild_version_to_int(const char *pszVersion, int fStrict)
1092{
1093 int iVer = 0;
1094 if (pszVersion && pszVersion[0])
1095 {
1096 switch (pszVersion[0] | (pszVersion[1] << 8))
1097 {
1098 case '2': iVer = 2; break;
1099 case '3': iVer = 3; break;
1100 case '4': iVer = 4; break;
1101 case '5': iVer = 5; break;
1102 case '6': iVer = 6; break;
1103 case '7': iVer = 7; break;
1104 case '8': iVer = 8; break;
1105 case '9': iVer = 9; break;
1106 case '0': iVer = 0; break;
1107 case '1': iVer = 1; break;
1108 default:
1109 while (ISBLANK(*pszVersion))
1110 pszVersion++;
1111 if (*pszVersion)
1112 {
1113 char *pszEnd = NULL;
1114 long lVer;
1115 errno = 0;
1116 lVer = strtol(pszVersion, &pszEnd, 10);
1117 iVer = (int)lVer;
1118 if (fStrict)
1119 {
1120 if (lVer == 0 && errno != 0)
1121 OSN(fatal, NILF, _("invalid version argument '%s': errno=%d"), pszVersion, errno);
1122 else if (iVer != (int)lVer || iVer < 0)
1123 OS(fatal, NILF, _("version argument out of range '%s'"), pszVersion);
1124 else if (pszEnd)
1125 {
1126 while (ISBLANK(*pszEnd))
1127 pszEnd++;
1128 if (*pszEnd)
1129 OS(fatal, NILF, _("version is not numerical '%s'"), pszVersion);
1130 }
1131 }
1132 }
1133 break;
1134 }
1135 }
1136 return iVer;
1137}
1138
1139
1140/**
1141 * "kb-src-tool <varname> [ver=0]" - implements _SOURCE_TOOL.
1142 *
1143 * Since r3415 an extended set of keyword variations is used on the target, tool
1144 * and global properties.
1145 */
1146char *
1147func_kbuild_source_tool(char *o, char **argv, const char *pszFuncName)
1148{
1149 const int iVer = kbuild_version_to_int(argv[1], 1 /*strict*/);
1150 struct variable *pVar = kbuild_get_source_tool(kbuild_get_variable_n(ST("target")),
1151 kbuild_get_variable_n(ST("source")),
1152 kbuild_get_variable_n(ST("type")),
1153 kbuild_get_variable_n(ST("bld_trg")),
1154 kbuild_get_variable_n(ST("bld_trg_arch")),
1155 kbuild_get_variable_n(ST("bld_type")),
1156 argv[0]);
1157 if (pVar)
1158 o = variable_buffer_output(o, pVar->value, pVar->value_length);
1159 (void)pszFuncName; (void)iVer;
1160 return o;
1161}
1162
1163
1164/**
1165 * Similar to _TARGET_TOOL since r3415.
1166 */
1167static struct variable *
1168kbuild_get_object_suffix(struct variable *pTarget, struct variable *pSource,
1169 struct variable *pTool, struct variable *pType,
1170 struct variable *pBldTrg, struct variable *pBldTrgArch, struct variable *pBldType,
1171 const char *pszVarName)
1172{
1173 struct variable *pVar = kbuild_first_prop(pTarget, pSource, pTool, pType, pBldTrg, pBldTrgArch, pBldType,
1174 "SUFF_OBJ", sizeof("SUFF_OBJ") - 1,
1175 "OBJSUFF", sizeof("OBJSUFF") - 1,
1176 0 /*fExtendedSource*/, pszVarName);
1177 if (!pVar)
1178 OSS(fatal, NILF, _("no OBJSUFF attribute or SUFF_OBJ default for source `%s' in target `%s'!"),
1179 pSource->value, pTarget->value);
1180 return pVar;
1181}
1182
1183
1184/**
1185 * "kb-obj-suff <varname> [ver=0]"
1186 *
1187 * Since r3415 an extended set of keyword variations is used on the target, tool
1188 * and global properties.
1189 */
1190char *
1191func_kbuild_object_suffix(char *o, char **argv, const char *pszFuncName)
1192{
1193 const int iVer = kbuild_version_to_int(argv[1], 1 /*strict*/);
1194 struct variable *pVar = kbuild_get_object_suffix(kbuild_get_variable_n(ST("target")),
1195 kbuild_get_variable_n(ST("source")),
1196 kbuild_get_variable_n(ST("tool")),
1197 kbuild_get_variable_n(ST("type")),
1198 kbuild_get_variable_n(ST("bld_trg")),
1199 kbuild_get_variable_n(ST("bld_trg_arch")),
1200 kbuild_get_variable_n(ST("bld_type")),
1201 argv[0]);
1202 if (pVar)
1203 o = variable_buffer_output(o, pVar->value, pVar->value_length);
1204 (void)pszFuncName; (void)iVer;
1205 return o;
1206
1207}
1208
1209
1210/*
1211## Figure out where to put object files.
1212# @param $1 source file
1213# @param $2 normalized main target
1214# @remark There are two major hacks here:
1215# 1. Source files in the output directory are translated into a gen/ subdir.
1216# 2. Catch anyone specifying $(PATH_SUB_CURRENT)/sourcefile.c.
1217_OBJECT_BASE = $(PATH_TARGET)/$(2)/$(call no-root-slash,$(call no-drive,$(basename \
1218 $(patsubst $(PATH_ROOT)/%,%,$(patsubst $(PATH_SUB_CURRENT)/%,%,$(patsubst $(PATH_TARGET)/$(2)/%,gen/%,$(1)))))))
1219*/
1220static struct variable *
1221kbuild_get_object_base(struct variable *pTarget, struct variable *pSource, const char *pszVarName)
1222{
1223 struct variable *pPathTarget = kbuild_get_variable_n(ST("PATH_TARGET"));
1224 struct variable *pPathRoot = kbuild_get_variable_n(ST("PATH_ROOT"));
1225 struct variable *pPathSubCur = kbuild_get_variable_n(ST("PATH_SUB_CURRENT"));
1226 const char *pszSrcPrefix = NULL;
1227 size_t cchSrcPrefix = 0;
1228 size_t cchSrc = 0;
1229 const char *pszSrcEnd;
1230 char *pszSrc;
1231 char *pszResult;
1232 char *psz;
1233 char *pszDot;
1234 size_t cch;
1235
1236 /*
1237 * Strip the source filename of any unnecessary leading path and root specs.
1238 */
1239 if ( pSource->value_length > pPathTarget->value_length
1240 && !strncmp(pSource->value, pPathTarget->value, pPathTarget->value_length))
1241 {
1242 pszSrc = pSource->value + pPathTarget->value_length;
1243 pszSrcPrefix = "gen/";
1244 cchSrcPrefix = sizeof("gen/") - 1;
1245 if ( *pszSrc == '/'
1246 && !strncmp(pszSrc + 1, pTarget->value, pTarget->value_length)
1247 && ( pszSrc[pTarget->value_length + 1] == '/'
1248 || pszSrc[pTarget->value_length + 1] == '\0'))
1249 pszSrc += 1 + pTarget->value_length;
1250 }
1251 else if ( pSource->value_length > pPathRoot->value_length
1252 && !strncmp(pSource->value, pPathRoot->value, pPathRoot->value_length))
1253 {
1254 pszSrc = pSource->value + pPathRoot->value_length;
1255 if ( *pszSrc == '/'
1256 && !strncmp(pszSrc + 1, pPathSubCur->value, pPathSubCur->value_length)
1257 && ( pszSrc[pPathSubCur->value_length + 1] == '/'
1258 || pszSrc[pPathSubCur->value_length + 1] == '\0'))
1259 pszSrc += 1 + pPathSubCur->value_length;
1260 }
1261 else
1262 pszSrc = pSource->value;
1263
1264 /* skip root specification */
1265#ifdef HAVE_DOS_PATHS
1266 if (isalpha(pszSrc[0]) && pszSrc[1] == ':')
1267 pszSrc += 2;
1268#endif
1269 while (*pszSrc == '/'
1270#ifdef HAVE_DOS_PATHS
1271 || *pszSrc == '\\'
1272#endif
1273 )
1274 pszSrc++;
1275
1276 /* drop the source extension. */
1277 pszSrcEnd = pSource->value + pSource->value_length;
1278 for (;;)
1279 {
1280 pszSrcEnd--;
1281 if ( pszSrcEnd <= pszSrc
1282 || *pszSrcEnd == '/'
1283#ifdef HAVE_DOS_PATHS
1284 || *pszSrcEnd == '\\'
1285 || *pszSrcEnd == ':'
1286#endif
1287 )
1288 {
1289 pszSrcEnd = pSource->value + pSource->value_length;
1290 break;
1291 }
1292 if (*pszSrcEnd == '.')
1293 break;
1294 }
1295
1296 /*
1297 * Assemble the string on the heap and define the objbase variable
1298 * which we then return.
1299 */
1300 cchSrc = pszSrcEnd - pszSrc;
1301 cch = pPathTarget->value_length
1302 + 1 /* slash */
1303 + pTarget->value_length
1304 + 1 /* slash */
1305 + cchSrcPrefix
1306 + cchSrc
1307 + 1;
1308 psz = pszResult = xmalloc(cch);
1309
1310 memcpy(psz, pPathTarget->value, pPathTarget->value_length); psz += pPathTarget->value_length;
1311 *psz++ = '/';
1312 memcpy(psz, pTarget->value, pTarget->value_length); psz += pTarget->value_length;
1313 *psz++ = '/';
1314 if (pszSrcPrefix)
1315 {
1316 memcpy(psz, pszSrcPrefix, cchSrcPrefix);
1317 psz += cchSrcPrefix;
1318 }
1319 pszDot = psz;
1320 memcpy(psz, pszSrc, cchSrc); psz += cchSrc;
1321 *psz = '\0';
1322
1323 /* convert '..' path elements in the source to 'dt'. */
1324 while ((pszDot = memchr(pszDot, '.', psz - pszDot)) != NULL)
1325 {
1326 if ( pszDot[1] == '.'
1327 && ( pszDot == psz
1328 || pszDot[-1] == '/'
1329#ifdef HAVE_DOS_PATHS
1330 || pszDot[-1] == '\\'
1331 || pszDot[-1] == ':'
1332#endif
1333 )
1334 && ( !pszDot[2]
1335 || pszDot[2] == '/'
1336#ifdef HAVE_DOS_PATHS
1337 || pszDot[2] == '\\'
1338 || pszDot[2] == ':'
1339#endif
1340 )
1341 )
1342 {
1343 *pszDot++ = 'd';
1344 *pszDot++ = 't';
1345 }
1346 else
1347 pszDot++;
1348 }
1349
1350 /*
1351 * Define the variable in the current set and return it.
1352 */
1353 return define_variable_vl(pszVarName, strlen(pszVarName), pszResult, cch - 1,
1354 0 /* use pszResult */, o_local, 0 /* !recursive */);
1355}
1356
1357
1358/**
1359 * "kb-obj-base <var> [ver]" Implements _OBJECT_BASE.
1360 */
1361char *
1362func_kbuild_object_base(char *o, char **argv, const char *pszFuncName)
1363{
1364 const int iVer = kbuild_version_to_int(argv[1], 1 /*strict*/);
1365 struct variable *pVar = kbuild_get_object_base(kbuild_lookup_variable("target"),
1366 kbuild_lookup_variable("source"),
1367 argv[0]);
1368 if (pVar)
1369 o = variable_buffer_output(o, pVar->value, pVar->value_length);
1370 (void)pszFuncName; (void)iVer;
1371 return o;
1372}
1373
1374
1375struct kbuild_sdks
1376{
1377 char *apsz[4];
1378 struct variable *pa;
1379 unsigned c;
1380 unsigned iGlobal;
1381 unsigned cGlobal;
1382 unsigned iTarget;
1383 unsigned cTarget;
1384 unsigned iSource;
1385 unsigned cSource;
1386 unsigned iTargetSource;
1387 unsigned cTargetSource;
1388 unsigned int cchMax;
1389};
1390
1391
1392/* Fills in the SDK struct (remember to free it). */
1393static void
1394kbuild_get_sdks(struct kbuild_sdks *pSdks, struct variable *pTarget, struct variable *pSource,
1395 struct variable *pBldType, struct variable *pBldTrg, struct variable *pBldTrgArch)
1396{
1397 unsigned i;
1398 unsigned j;
1399 size_t cchTmp, cch;
1400 char *pszTmp;
1401 unsigned cchCur;
1402 char *pszCur;
1403 const char *pszIterator;
1404
1405 /** @todo rewrite this to avoid sprintf and allocated_varaible_expand_2. */
1406
1407 /* basic init. */
1408 pSdks->cchMax = 0;
1409 pSdks->pa = NULL;
1410 pSdks->c = 0;
1411 i = 0;
1412
1413 /* determin required tmp variable name space. */
1414 cchTmp = sizeof("$(__SDKS) $(__SDKS.) $(__SDKS.) $(__SDKS.) $(__SDKS..)")
1415 + (pTarget->value_length + pSource->value_length) * 5
1416 + pBldType->value_length
1417 + pBldTrg->value_length
1418 + pBldTrgArch->value_length
1419 + pBldTrg->value_length + pBldTrgArch->value_length;
1420 pszTmp = alloca(cchTmp);
1421
1422 /* the global sdks. */
1423 pSdks->iGlobal = i;
1424 pSdks->cGlobal = 0;
1425 cch = sprintf(pszTmp, "$(SDKS) $(SDKS.%s) $(SDKS.%s) $(SDKS.%s) $(SDKS.%s.%s)",
1426 pBldType->value,
1427 pBldTrg->value,
1428 pBldTrgArch->value,
1429 pBldTrg->value, pBldTrgArch->value);
1430 pszIterator = pSdks->apsz[0] = allocated_variable_expand_2(pszTmp, cch, NULL);
1431 while ((pszCur = find_next_token(&pszIterator, &cchCur)) != 0)
1432 pSdks->cGlobal++;
1433 i += pSdks->cGlobal;
1434
1435 /* the target sdks.*/
1436 pSdks->iTarget = i;
1437 pSdks->cTarget = 0;
1438 cch = sprintf(pszTmp, "$(%s_SDKS) $(%s_SDKS.%s) $(%s_SDKS.%s) $(%s_SDKS.%s) $(%s_SDKS.%s.%s)",
1439 pTarget->value,
1440 pTarget->value, pBldType->value,
1441 pTarget->value, pBldTrg->value,
1442 pTarget->value, pBldTrgArch->value,
1443 pTarget->value, pBldTrg->value, pBldTrgArch->value);
1444 pszIterator = pSdks->apsz[1] = allocated_variable_expand_2(pszTmp, cch, NULL);
1445 while ((pszCur = find_next_token(&pszIterator, &cchCur)) != 0)
1446 pSdks->cTarget++;
1447 i += pSdks->cTarget;
1448
1449 /* the source sdks.*/
1450 pSdks->iSource = i;
1451 pSdks->cSource = 0;
1452 cch = sprintf(pszTmp, "$(%s_SDKS) $(%s_SDKS.%s) $(%s_SDKS.%s) $(%s_SDKS.%s) $(%s_SDKS.%s.%s)",
1453 pSource->value,
1454 pSource->value, pBldType->value,
1455 pSource->value, pBldTrg->value,
1456 pSource->value, pBldTrgArch->value,
1457 pSource->value, pBldTrg->value, pBldTrgArch->value);
1458 pszIterator = pSdks->apsz[2] = allocated_variable_expand_2(pszTmp, cch, NULL);
1459 while ((pszCur = find_next_token(&pszIterator, &cchCur)) != 0)
1460 pSdks->cSource++;
1461 i += pSdks->cSource;
1462
1463 /* the target + source sdks. */
1464 pSdks->iTargetSource = i;
1465 pSdks->cTargetSource = 0;
1466 cch = sprintf(pszTmp, "$(%s_%s_SDKS) $(%s_%s_SDKS.%s) $(%s_%s_SDKS.%s) $(%s_%s_SDKS.%s) $(%s_%s_SDKS.%s.%s)",
1467 pTarget->value, pSource->value,
1468 pTarget->value, pSource->value, pBldType->value,
1469 pTarget->value, pSource->value, pBldTrg->value,
1470 pTarget->value, pSource->value, pBldTrgArch->value,
1471 pTarget->value, pSource->value, pBldTrg->value, pBldTrgArch->value);
1472 assert(cch < cchTmp); (void)cch;
1473 pszIterator = pSdks->apsz[3] = allocated_variable_expand_2(pszTmp, cch, NULL);
1474 while ((pszCur = find_next_token(&pszIterator, &cchCur)) != 0)
1475 pSdks->cTargetSource++;
1476 i += pSdks->cTargetSource;
1477
1478 pSdks->c = i;
1479 if (!i)
1480 return;
1481
1482 /*
1483 * Allocate the variable array and create the variables.
1484 */
1485 pSdks->pa = (struct variable *)xmalloc(sizeof(pSdks->pa[0]) * i);
1486 memset(pSdks->pa, 0, sizeof(pSdks->pa[0]) * i);
1487 for (i = j = 0; j < sizeof(pSdks->apsz) / sizeof(pSdks->apsz[0]); j++)
1488 {
1489 pszIterator = pSdks->apsz[j];
1490 while ((pszCur = find_next_token(&pszIterator, &cchCur)) != 0)
1491 {
1492 pSdks->pa[i].value = pszCur;
1493 pSdks->pa[i].value_length = cchCur;
1494 i++;
1495 }
1496 }
1497 assert(i == pSdks->c);
1498
1499 /* terminate them (find_next_token won't work if we terminate them in the previous loop). */
1500 while (i-- > 0)
1501 {
1502 pSdks->pa[i].value[pSdks->pa[i].value_length] = '\0';
1503
1504 /* calc the max variable length too. */
1505 if (pSdks->cchMax < (unsigned int)pSdks->pa[i].value_length)
1506 pSdks->cchMax = pSdks->pa[i].value_length;
1507 }
1508}
1509
1510
1511/* releases resources allocated in the kbuild_get_sdks. */
1512static void
1513kbuild_put_sdks(struct kbuild_sdks *pSdks)
1514{
1515 unsigned j;
1516 for (j = 0; j < sizeof(pSdks->apsz) / sizeof(pSdks->apsz[0]); j++)
1517 free(pSdks->apsz[j]);
1518 free(pSdks->pa);
1519}
1520
1521
1522/* this kind of stuff:
1523
1524defs := $(kb-src-exp defs)
1525 $(TOOL_$(tool)_DEFS)\
1526 $(TOOL_$(tool)_DEFS.$(bld_type))\
1527 $(TOOL_$(tool)_DEFS.$(bld_trg))\
1528 $(TOOL_$(tool)_DEFS.$(bld_trg_arch))\
1529 $(TOOL_$(tool)_DEFS.$(bld_trg).$(bld_trg_arch))\
1530 $(TOOL_$(tool)_DEFS.$(bld_trg_cpu))\
1531 $(TOOL_$(tool)_$(type)DEFS)\
1532 $(TOOL_$(tool)_$(type)DEFS.$(bld_type))\
1533 $(foreach sdk, $(SDKS.$(bld_trg)) \
1534 $(SDKS.$(bld_trg).$(bld_trg_arch)) \
1535 $(SDKS.$(bld_type)) \
1536 $(SDKS),\
1537 $(SDK_$(sdk)_DEFS)\
1538 $(SDK_$(sdk)_DEFS.$(bld_type))\
1539 $(SDK_$(sdk)_DEFS.$(bld_trg))\
1540 $(SDK_$(sdk)_DEFS.$(bld_trg_arch))\
1541 $(SDK_$(sdk)_DEFS.$(bld_trg).$(bld_trg_arch))\
1542 $(SDK_$(sdk)_DEFS.$(bld_trg_cpu))\
1543 $(SDK_$(sdk)_$(type)DEFS)\
1544 $(SDK_$(sdk)_$(type)DEFS.$(bld_type))\
1545 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg))\
1546 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_arch))\
1547 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1548 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_cpu)))\
1549 $(DEFS)\
1550 $(DEFS.$(bld_type))\
1551 $(DEFS.$(bld_trg))\
1552 $(DEFS.$(bld_trg_arch))\
1553 $(DEFS.$(bld_trg).$(bld_trg_arch))\
1554 $(DEFS.$(bld_trg_cpu))\
1555 $($(type)DEFS)\
1556 $($(type)DEFS.$(bld_type))\
1557 $($(type)DEFS.$(bld_trg))\
1558 $($(type)DEFS.$(bld_trg_arch))\
1559 $($(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1560 $($(type)DEFS.$(bld_trg_cpu))\
1561 $(foreach sdk, $($(target)_SDKS.$(bld_trg)) \
1562 $($(target)_SDKS.$(bld_trg).$(bld_trg_arch)) \
1563 $($(target)_SDKS.$(bld_type)) \
1564 $($(target)_SDKS),\
1565 $(SDK_$(sdk)_DEFS)\
1566 $(SDK_$(sdk)_DEFS.$(bld_type))\
1567 $(SDK_$(sdk)_DEFS.$(bld_trg))\
1568 $(SDK_$(sdk)_DEFS.$(bld_trg_arch))\
1569 $(SDK_$(sdk)_DEFS.$(bld_trg).$(bld_trg_arch))\
1570 $(SDK_$(sdk)_DEFS.$(bld_trg_cpu))\
1571 $(SDK_$(sdk)_$(type)DEFS)\
1572 $(SDK_$(sdk)_$(type)DEFS.$(bld_type))\
1573 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg))\
1574 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_arch))\
1575 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1576 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_cpu)))\
1577 $($(target)_DEFS)\
1578 $($(target)_DEFS.$(bld_type))\
1579 $($(target)_DEFS.$(bld_trg))\
1580 $($(target)_DEFS.$(bld_trg_arch))\
1581 $($(target)_DEFS.$(bld_trg).$(bld_trg_arch))\
1582 $($(target)_DEFS.$(bld_trg_cpu))\
1583 $($(target)_$(type)DEFS)\
1584 $($(target)_$(type)DEFS.$(bld_type))\
1585 $($(target)_$(type)DEFS.$(bld_trg))\
1586 $($(target)_$(type)DEFS.$(bld_trg_arch))\
1587 $($(target)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1588 $($(target)_$(type)DEFS.$(bld_trg_cpu))\
1589 $(foreach sdk, $($(source)_SDKS.$(bld_trg)) \
1590 $($(source)_SDKS.$(bld_trg).$(bld_trg_arch)) \
1591 $($(source)_SDKS.$(bld_type)) \
1592 $($(source)_SDKS),\
1593 $(SDK_$(sdk)_DEFS)\
1594 $(SDK_$(sdk)_DEFS.$(bld_type))\
1595 $(SDK_$(sdk)_DEFS.$(bld_trg))\
1596 $(SDK_$(sdk)_DEFS.$(bld_trg_arch))\
1597 $(SDK_$(sdk)_DEFS.$(bld_trg).$(bld_trg_arch))\
1598 $(SDK_$(sdk)_DEFS.$(bld_trg_cpu))\
1599 $(SDK_$(sdk)_$(type)DEFS)\
1600 $(SDK_$(sdk)_$(type)DEFS.$(bld_type))\
1601 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg))\
1602 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_arch))\
1603 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1604 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_cpu)))\
1605 $($(source)_DEFS)\
1606 $($(source)_DEFS.$(bld_type))\
1607 $($(source)_DEFS.$(bld_trg))\
1608 $($(source)_DEFS.$(bld_trg_arch))\
1609 $($(source)_DEFS.$(bld_trg).$(bld_trg_arch))\
1610 $($(source)_DEFS.$(bld_trg_cpu))\
1611 $($(source)_$(type)DEFS)\
1612 $($(source)_$(type)DEFS.$(bld_type))\
1613 $($(source)_$(type)DEFS.$(bld_trg))\
1614 $($(source)_$(type)DEFS.$(bld_trg_arch))\
1615 $($(source)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1616 $($(source)_$(type)DEFS.$(bld_trg_cpu))\
1617 $(foreach sdk, $($(target)_$(source)_SDKS.$(bld_trg)) \
1618 $($(target)_$(source)_SDKS.$(bld_trg).$(bld_trg_arch)) \
1619 $($(target)_$(source)_SDKS.$(bld_type)) \
1620 $($(target)_$(source)_SDKS),\
1621 $(SDK_$(sdk)_DEFS)\
1622 $(SDK_$(sdk)_DEFS.$(bld_type))\
1623 $(SDK_$(sdk)_DEFS.$(bld_trg))\
1624 $(SDK_$(sdk)_DEFS.$(bld_trg_arch))\
1625 $(SDK_$(sdk)_DEFS.$(bld_trg).$(bld_trg_arch))\
1626 $(SDK_$(sdk)_DEFS.$(bld_trg_cpu))\
1627 $(SDK_$(sdk)_$(type)DEFS)\
1628 $(SDK_$(sdk)_$(type)DEFS.$(bld_type))\
1629 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg))\
1630 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_arch))\
1631 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1632 $(SDK_$(sdk)_$(type)DEFS.$(bld_trg_cpu)))\
1633 $($(target)_$(source)_DEFS)\
1634 $($(target)_$(source)_DEFS.$(bld_type))\
1635 $($(target)_$(source)_DEFS.$(bld_trg))\
1636 $($(target)_$(source)_DEFS.$(bld_trg_arch))\
1637 $($(target)_$(source)_DEFS.$(bld_trg).$(bld_trg_arch))\
1638 $($(target)_$(source)_DEFS.$(bld_trg_cpu))\
1639 $($(target)_$(source)_$(type)DEFS)\
1640 $($(target)_$(source)_$(type)DEFS.$(bld_type))\
1641 $($(target)_$(source)_$(type)DEFS.$(bld_trg))\
1642 $($(target)_$(source)_$(type)DEFS.$(bld_trg_arch))\
1643 $($(target)_$(source)_$(type)DEFS.$(bld_trg).$(bld_trg_arch))\
1644 $($(target)_$(source)_$(type)DEFS.$(bld_trg_cpu))
1645*/
1646static struct variable *
1647kbuild_collect_source_prop(struct variable *pTarget, struct variable *pSource,
1648 struct variable *pTool, struct kbuild_sdks *pSdks,
1649 struct variable *pType, struct variable *pBldType,
1650 struct variable *pBldTrg, struct variable *pBldTrgArch, struct variable *pBldTrgCpu,
1651 struct variable *pDefPath,
1652 const char *pszProp, size_t cchProp,
1653 const char *pszVarName, size_t cchVarName,
1654 int iDirection)
1655{
1656 struct variable *pVar;
1657 unsigned iSdk, iSdkEnd;
1658 int cVars, iVar;
1659 size_t cchTotal, cchBuf;
1660 char *pszResult, *pszBuf, *psz, *psz2, *psz3;
1661 struct
1662 {
1663 struct variable *pVar;
1664 unsigned int cchExp;
1665 char *pszExp;
1666 } *paVars;
1667
1668 assert(iDirection == 1 || iDirection == -1);
1669
1670 /*
1671 * Calc and allocate a too big name buffer.
1672 */
1673 cchBuf = cchProp + 1
1674 + pTarget->value_length + 1
1675 + pSource->value_length + 1
1676 + pSdks->cchMax + 1
1677 + (pTool ? pTool->value_length + 1 : 0)
1678 + pType->value_length + 1
1679 + pBldTrg->value_length + 1
1680 + pBldTrgArch->value_length + 1
1681 + pBldTrgCpu->value_length + 1
1682 + pBldType->value_length + 1;
1683 pszBuf = xmalloc(cchBuf);
1684
1685 /*
1686 * Get the variables.
1687 *
1688 * The compiler will get a heart attack when it sees this code ... ;-)
1689 */
1690 cVars = 12 * (pSdks->c + 5);
1691 paVars = alloca(cVars * sizeof(paVars[0]));
1692
1693 iVar = 0;
1694 cchTotal = 0;
1695
1696#define ADD_VAR(pVar) do { my_memcpy(psz, (pVar)->value, (pVar)->value_length); psz += (pVar)->value_length; } while (0)
1697#define ADD_STR(pszStr, cchStr) do { my_memcpy(psz, (pszStr), (cchStr)); psz += (cchStr); } while (0)
1698#define ADD_CSTR(pszStr) do { my_memcpy(psz, pszStr, sizeof(pszStr) - 1); psz += sizeof(pszStr) - 1; } while (0)
1699#define ADD_CH(ch) do { *psz++ = (ch); } while (0)
1700#define DO_VAR_LOOKUP() \
1701 do { \
1702 pVar = kbuild_lookup_variable_n(pszBuf, psz - pszBuf); \
1703 if (pVar) \
1704 { \
1705 paVars[iVar].pVar = pVar; \
1706 if ( !pVar->recursive \
1707 || IS_VARIABLE_RECURSIVE_WITHOUT_DOLLAR(pVar)) \
1708 { \
1709 paVars[iVar].pszExp = pVar->value; \
1710 paVars[iVar].cchExp = pVar->value_length; \
1711 if (pDefPath && paVars[iVar].cchExp) \
1712 kbuild_apply_defpath(pDefPath, &paVars[iVar].pszExp, &paVars[iVar].cchExp, NULL, 0); \
1713 if (paVars[iVar].cchExp) \
1714 { \
1715 cchTotal += paVars[iVar].cchExp + 1; \
1716 iVar++; \
1717 } \
1718 } \
1719 else \
1720 { \
1721 paVars[iVar].pszExp = allocated_variable_expand_2(pVar->value, pVar->value_length, &paVars[iVar].cchExp); \
1722 if (pDefPath && paVars[iVar].cchExp) \
1723 kbuild_apply_defpath(pDefPath, &paVars[iVar].pszExp, &paVars[iVar].cchExp, NULL, 1); \
1724 if (paVars[iVar].cchExp) \
1725 { \
1726 cchTotal += paVars[iVar].cchExp + 1; \
1727 iVar++; \
1728 } \
1729 else \
1730 free(paVars[iVar].pszExp); \
1731 } \
1732 } \
1733 } while (0)
1734#define DO_SINGLE_PSZ3_VARIATION() \
1735 do { \
1736 DO_VAR_LOOKUP(); \
1737 \
1738 ADD_CH('.'); \
1739 psz3 = psz; \
1740 ADD_VAR(pBldType); \
1741 DO_VAR_LOOKUP(); \
1742 \
1743 psz = psz3; \
1744 ADD_VAR(pBldTrg); \
1745 DO_VAR_LOOKUP(); \
1746 \
1747 psz = psz3; \
1748 ADD_VAR(pBldTrgArch); \
1749 DO_VAR_LOOKUP(); \
1750 \
1751 psz = psz3; \
1752 ADD_VAR(pBldTrg); \
1753 ADD_CH('.'); \
1754 ADD_VAR(pBldTrgArch); \
1755 DO_VAR_LOOKUP(); \
1756 \
1757 psz = psz3; \
1758 ADD_VAR(pBldTrgCpu); \
1759 DO_VAR_LOOKUP(); \
1760 } while (0)
1761
1762#define DO_DOUBLE_PSZ2_VARIATION() \
1763 do { \
1764 psz2 = psz; \
1765 ADD_STR(pszProp, cchProp); \
1766 DO_SINGLE_PSZ3_VARIATION(); \
1767 \
1768 /* add prop before type */ \
1769 psz = psz2; \
1770 ADD_VAR(pType); \
1771 ADD_STR(pszProp, cchProp); \
1772 DO_SINGLE_PSZ3_VARIATION(); \
1773 } while (0)
1774
1775 /* the tool (lowest priority). */
1776 psz = pszBuf;
1777 ADD_CSTR("TOOL_");
1778 ADD_VAR(pTool);
1779 ADD_CH('_');
1780 DO_DOUBLE_PSZ2_VARIATION();
1781
1782
1783 /* the global sdks. */
1784 iSdkEnd = iDirection == 1 ? pSdks->iGlobal + pSdks->cGlobal : pSdks->iGlobal - 1;
1785 for (iSdk = iDirection == 1 ? pSdks->iGlobal : pSdks->iGlobal + pSdks->cGlobal - 1;
1786 iSdk != iSdkEnd;
1787 iSdk += iDirection)
1788 {
1789 struct variable *pSdk = &pSdks->pa[iSdk];
1790 psz = pszBuf;
1791 ADD_CSTR("SDK_");
1792 ADD_VAR(pSdk);
1793 ADD_CH('_');
1794 DO_DOUBLE_PSZ2_VARIATION();
1795 }
1796
1797 /* the globals. */
1798 psz = pszBuf;
1799 DO_DOUBLE_PSZ2_VARIATION();
1800
1801 /* the target sdks. */
1802/** @todo these can be cached in a type specific _2_ variable */
1803 iSdkEnd = iDirection == 1 ? pSdks->iTarget + pSdks->cTarget : pSdks->iTarget - 1;
1804 for (iSdk = iDirection == 1 ? pSdks->iTarget : pSdks->iTarget + pSdks->cTarget - 1;
1805 iSdk != iSdkEnd;
1806 iSdk += iDirection)
1807 {
1808 struct variable *pSdk = &pSdks->pa[iSdk];
1809 psz = pszBuf;
1810 ADD_CSTR("SDK_");
1811 ADD_VAR(pSdk);
1812 ADD_CH('_');
1813 DO_DOUBLE_PSZ2_VARIATION();
1814 }
1815
1816
1817 /* the target. */
1818/** @todo these can be cached in a type specific _2_ variable */
1819 psz = pszBuf;
1820 ADD_VAR(pTarget);
1821 ADD_CH('_');
1822 DO_DOUBLE_PSZ2_VARIATION();
1823
1824 /* the source sdks. */
1825 iSdkEnd = iDirection == 1 ? pSdks->iSource + pSdks->cSource : pSdks->iSource - 1;
1826 for (iSdk = iDirection == 1 ? pSdks->iSource : pSdks->iSource + pSdks->cSource - 1;
1827 iSdk != iSdkEnd;
1828 iSdk += iDirection)
1829 {
1830 struct variable *pSdk = &pSdks->pa[iSdk];
1831 psz = pszBuf;
1832 ADD_CSTR("SDK_");
1833 ADD_VAR(pSdk);
1834 ADD_CH('_');
1835 DO_DOUBLE_PSZ2_VARIATION();
1836 }
1837
1838 /* the source. */
1839 psz = pszBuf;
1840 ADD_VAR(pSource);
1841 ADD_CH('_');
1842 DO_DOUBLE_PSZ2_VARIATION();
1843
1844 /* the target + source sdks. */
1845 iSdkEnd = iDirection == 1 ? pSdks->iTargetSource + pSdks->cTargetSource : pSdks->iTargetSource - 1;
1846 for (iSdk = iDirection == 1 ? pSdks->iTargetSource : pSdks->iTargetSource + pSdks->cTargetSource - 1;
1847 iSdk != iSdkEnd;
1848 iSdk += iDirection)
1849 {
1850 struct variable *pSdk = &pSdks->pa[iSdk];
1851 psz = pszBuf;
1852 ADD_CSTR("SDK_");
1853 ADD_VAR(pSdk);
1854 ADD_CH('_');
1855 DO_DOUBLE_PSZ2_VARIATION();
1856 }
1857
1858 /* the target + source. */
1859 psz = pszBuf;
1860 ADD_VAR(pTarget);
1861 ADD_CH('_');
1862 ADD_VAR(pSource);
1863 ADD_CH('_');
1864 DO_DOUBLE_PSZ2_VARIATION();
1865
1866 free(pszBuf);
1867
1868 assert(iVar <= cVars);
1869 cVars = iVar;
1870
1871 /*
1872 * Construct the result value.
1873 */
1874 if (!cVars || !cchTotal)
1875 pVar = define_variable_vl(pszVarName, cchVarName, "", 0,
1876 1 /* duplicate value */ , o_local, 0 /* !recursive */);
1877 else
1878 {
1879 psz = pszResult = xmalloc(cchTotal + 1);
1880 if (iDirection == 1)
1881 {
1882 for (iVar = 0; iVar < cVars; iVar++)
1883 {
1884 my_memcpy(psz, paVars[iVar].pszExp, paVars[iVar].cchExp);
1885 psz += paVars[iVar].cchExp;
1886 *psz++ = ' ';
1887 if (paVars[iVar].pszExp != paVars[iVar].pVar->value)
1888 free(paVars[iVar].pszExp);
1889 }
1890 }
1891 else
1892 {
1893 iVar = cVars;
1894 while (iVar-- > 0)
1895 {
1896 my_memcpy(psz, paVars[iVar].pszExp, paVars[iVar].cchExp);
1897 psz += paVars[iVar].cchExp;
1898 *psz++ = ' ';
1899 if (paVars[iVar].pszExp != paVars[iVar].pVar->value)
1900 free(paVars[iVar].pszExp);
1901 }
1902
1903 }
1904 assert(psz != pszResult);
1905 assert(cchTotal == (size_t)(psz - pszResult));
1906 psz[-1] = '\0';
1907 cchTotal--;
1908
1909 pVar = define_variable_vl(pszVarName, cchVarName, pszResult, cchTotal,
1910 0 /* take pszResult */ , o_local, 0 /* !recursive */);
1911 }
1912
1913 return pVar;
1914
1915#undef ADD_VAR
1916#undef ADD_STR
1917#undef ADD_CSTR
1918#undef ADD_CH
1919#undef DO_VAR_LOOKUP
1920#undef DO_DOUBLE_PSZ2_VARIATION
1921#undef DO_SINGLE_PSZ3_VARIATION
1922}
1923
1924
1925/* "kb-src-prop <prop> <var> <dir> [defpath] [ver]"
1926 get a source property. */
1927char *
1928func_kbuild_source_prop(char *o, char **argv, const char *pszFuncName)
1929{
1930 struct variable *pTarget = kbuild_get_variable_n(ST("target"));
1931 struct variable *pSource = kbuild_get_variable_n(ST("source"));
1932 struct variable *pDefPath = NULL;
1933 struct variable *pType = kbuild_get_variable_n(ST("type"));
1934 struct variable *pTool = kbuild_get_variable_n(ST("tool"));
1935 struct variable *pBldType = kbuild_get_variable_n(ST("bld_type"));
1936 struct variable *pBldTrg = kbuild_get_variable_n(ST("bld_trg"));
1937 struct variable *pBldTrgArch = kbuild_get_variable_n(ST("bld_trg_arch"));
1938 struct variable *pBldTrgCpu = kbuild_get_variable_n(ST("bld_trg_cpu"));
1939 struct variable *pVar;
1940 struct kbuild_sdks Sdks;
1941 int iVer = 0;
1942 int iDirection;
1943 if (!strcmp(argv[2], "left-to-right"))
1944 iDirection = 1;
1945 else if (!strcmp(argv[2], "right-to-left"))
1946 iDirection = -1;
1947 else
1948 OS(fatal, NILF, _("incorrect direction argument `%s'!"), argv[2]);
1949 if (argv[3])
1950 {
1951 const char *psz = argv[3];
1952 while (ISSPACE(*psz))
1953 psz++;
1954 if (*psz)
1955 pDefPath = kbuild_get_variable_n(ST("defpath"));
1956 if (argv[4])
1957 iVer = kbuild_version_to_int(argv[4], 1 /*strict*/);
1958 }
1959
1960 kbuild_get_sdks(&Sdks, pTarget, pSource, pBldType, pBldTrg, pBldTrgArch);
1961
1962 pVar = kbuild_collect_source_prop(pTarget, pSource, pTool, &Sdks, pType,
1963 pBldType, pBldTrg, pBldTrgArch, pBldTrgCpu,
1964 pDefPath,
1965 argv[0], strlen(argv[0]),
1966 argv[1], strlen(argv[1]),
1967 iDirection);
1968 if (pVar)
1969 o = variable_buffer_output(o, pVar->value, pVar->value_length);
1970
1971 kbuild_put_sdks(&Sdks);
1972 (void)pszFuncName; (void)iVer;
1973 return o;
1974}
1975
1976
1977/*
1978dep := $(obj)$(SUFF_DEP)
1979obj := $(outbase)$(objsuff)
1980dirdep := $(call DIRDEP,$(dir $(outbase)))
1981PATH_$(target)_$(source) := $(patsubst %/,%,$(dir $(outbase)))
1982*/
1983static struct variable *
1984kbuild_set_object_name_and_dep_and_dirdep_and_PATH_target_source(struct variable *pTarget, struct variable *pSource,
1985 struct variable *pOutBase, struct variable *pObjSuff,
1986 const char *pszVarName, struct variable **ppDep,
1987 struct variable **ppDirDep)
1988{
1989 struct variable *pDepSuff = kbuild_get_variable_n(ST("SUFF_DEP"));
1990 struct variable *pObj;
1991 size_t cch = pOutBase->value_length + pObjSuff->value_length + pDepSuff->value_length + 1;
1992 char *pszResult = alloca(cch);
1993 char *pszName, *psz;
1994
1995 /*
1996 * dep.
1997 */
1998 psz = pszResult;
1999 memcpy(psz, pOutBase->value, pOutBase->value_length); psz += pOutBase->value_length;
2000 memcpy(psz, pObjSuff->value, pObjSuff->value_length); psz += pObjSuff->value_length;
2001 memcpy(psz, pDepSuff->value, pDepSuff->value_length + 1);
2002 *ppDep = define_variable_vl("dep", 3, pszResult, cch - 1, 1 /*dup*/, o_local, 0 /* !recursive */);
2003
2004 /*
2005 * obj
2006 */
2007 *psz = '\0';
2008 pObj = define_variable_vl(pszVarName, strlen(pszVarName), pszResult, psz - pszResult,
2009 1/* dup */, o_local, 0 /* !recursive */);
2010
2011 /*
2012 * PATH_$(target)_$(source) - this is global!
2013 */
2014 /* calc variable name. */
2015 cch = sizeof("PATH_")-1 + pTarget->value_length + sizeof("_")-1 + pSource->value_length;
2016 psz = pszName = alloca(cch + 1);
2017 memcpy(psz, "PATH_", sizeof("PATH_") - 1); psz += sizeof("PATH_") - 1;
2018 memcpy(psz, pTarget->value, pTarget->value_length); psz += pTarget->value_length;
2019 *psz++ = '_';
2020 memcpy(psz, pSource->value, pSource->value_length + 1);
2021
2022 /* strip off the filename. */
2023 psz = pszResult + pOutBase->value_length;
2024 for (;;)
2025 {
2026 psz--;
2027 if (psz <= pszResult)
2028 OS(fatal, NULL, "whut!?! no path? result=`%s'", pszResult);
2029#ifdef HAVE_DOS_PATHS
2030 if (*psz == ':')
2031 {
2032 psz++;
2033 break;
2034 }
2035#endif
2036 if ( *psz == '/'
2037#ifdef HAVE_DOS_PATHS
2038 || *psz == '\\'
2039#endif
2040 )
2041 {
2042 while ( psz - 1 > pszResult
2043 && psz[-1] == '/'
2044#ifdef HAVE_DOS_PATHS
2045 || psz[-1] == '\\'
2046#endif
2047 )
2048 psz--;
2049#ifdef HAVE_DOS_PATHS
2050 if (psz == pszResult + 2 && pszResult[1] == ':')
2051 psz++;
2052#endif
2053 break;
2054 }
2055 }
2056 *psz = '\0';
2057
2058 /* set global variable */
2059 define_variable_vl_global(pszName, cch, pszResult, psz - pszResult, 1/*dup*/, o_file, 0 /* !recursive */, NILF);
2060
2061 /*
2062 * dirdep
2063 */
2064 if ( psz[-1] != '/'
2065#ifdef HAVE_DOS_PATHS
2066 && psz[-1] != '\\'
2067 && psz[-1] != ':'
2068#endif
2069 )
2070 {
2071 *psz++ = '/';
2072 *psz = '\0';
2073 }
2074 *ppDirDep = define_variable_vl("dirdep", 6, pszResult, psz - pszResult, 1/*dup*/, o_local, 0 /* !recursive */);
2075
2076 return pObj;
2077}
2078
2079
2080/**
2081 *
2082 *
2083 * Setup the base variables for def_target_source_c_cpp_asm_new:
2084 * @code
2085
2086X := $(kb-src-tool tool)
2087x := $(kb-obj-base outbase)
2088x := $(kb-obj-suff objsuff)
2089obj := $(outbase)$(objsuff)
2090PATH_$(target)_$(source) := $(patsubst %/,%,$(dir $(outbase)))
2091
2092x := $(kb-src-prop DEFS,defs,left-to-right)
2093x := $(kb-src-prop INCS,incs,right-to-left)
2094x := $(kb-src-prop FLAGS,flags,right-to-left)
2095
2096x := $(kb-src-prop DEPS,deps,left-to-right)
2097dirdep := $(call DIRDEP,$(dir $(outbase)))
2098dep := $(obj)$(SUFF_DEP)
2099 * @endcode
2100 *
2101 * argv[0] is the function version. Prior to r1792 (early 0.1.5) this
2102 * was undefined and footer.kmk always passed an empty string.
2103 *
2104 * Version 2, as implemented in r1797, will make use of the async
2105 * includedep queue feature. This means the files will be read by one or
2106 * more background threads, leaving the eval'ing to be done later on by
2107 * the main thread (in snap_deps).
2108 *
2109 * Version 3, as implemented in rXXXX, will check
2110 * TOOL_$(tool)_COMPILE_$(type)_USES_KOBJCACHE and use
2111 * def_target_source_rule_v3plus_objcache if it's non-empty or
2112 * def_target_source_rule_v3plus if it's empty (version 2 and older will use
2113 * def_target_source_rule). Version 3 will also skip defining several
2114 * properties that it considers legacy (todo: which).
2115 *
2116 * Version 4, as implemented in r3415, will use the extended tool resolution at
2117 * the target level (not source) as implemented by the _TARGET_TOOL update in
2118 * r2433.
2119 *
2120 * With r3415 the $(target)_2_$(type)TOOL will be used for caching purposes
2121 * regardless of version.
2122 */
2123char *
2124func_kbuild_source_one(char *o, char **argv, const char *pszFuncName)
2125{
2126 static int s_fNoCompileDepsDefined = -1;
2127 struct variable *pTarget = kbuild_get_variable_n(ST("target"));
2128 struct variable *pSource = kbuild_get_variable_n(ST("source"));
2129 struct variable *pDefPath = kbuild_get_variable_n(ST("defpath"));
2130 struct variable *pType = kbuild_get_variable_n(ST("type"));
2131 struct variable *pBldType = kbuild_get_variable_n(ST("bld_type"));
2132 struct variable *pBldTrg = kbuild_get_variable_n(ST("bld_trg"));
2133 struct variable *pBldTrgArch= kbuild_get_variable_n(ST("bld_trg_arch"));
2134 struct variable *pBldTrgCpu = kbuild_get_variable_n(ST("bld_trg_cpu"));
2135 const int iVer = kbuild_version_to_int(argv[0], 0 /*strict*/);
2136 struct variable *pTool = kbuild_get_source_tool(pTarget, pSource, pType, pBldTrg, pBldTrgArch,
2137 iVer >= 4 ? pBldType : NULL, "tool");
2138 struct variable *pOutBase = kbuild_get_object_base(pTarget, pSource, "outbase");
2139 struct variable *pObjSuff = kbuild_get_object_suffix(pTarget, pSource, pTool, pType, pBldTrg, pBldTrgArch,
2140 iVer >= 4 ? pBldType : NULL, "objsuff");
2141 struct variable *pDeps, *pOrderDeps, *pDirDep, *pDep, *pVar, *pOutput, *pOutputMaybe;
2142#if 0 /* not used */
2143 struct variable *pDefs, *pIncs, *pFlags;
2144#endif
2145 struct variable *pObj = kbuild_set_object_name_and_dep_and_dirdep_and_PATH_target_source(pTarget, pSource, pOutBase, pObjSuff, "obj", &pDep, &pDirDep);
2146 int fInstallOldObjsVar = 0;
2147 char *pszDstVar, *pszDst, *pszSrcVar, *pszSrc, *pszVal, *psz;
2148 char *pszSavedVarBuf;
2149 unsigned cchSavedVarBuf;
2150 size_t cch;
2151 struct kbuild_sdks Sdks;
2152
2153 /*
2154 * Gather properties.
2155 */
2156 kbuild_get_sdks(&Sdks, pTarget, pSource, pBldType, pBldTrg, pBldTrgArch);
2157
2158 if (pDefPath && !pDefPath->value_length)
2159 pDefPath = NULL;
2160
2161
2162 /*pDefs =*/ kbuild_collect_source_prop(pTarget, pSource, pTool, &Sdks, pType, pBldType, pBldTrg, pBldTrgArch, pBldTrgCpu, NULL,
2163 ST("DEFS"), ST("defs"), 1 /* left-to-right */);
2164 /*pIncs =*/ kbuild_collect_source_prop(pTarget, pSource, pTool, &Sdks, pType, pBldType, pBldTrg, pBldTrgArch, pBldTrgCpu, pDefPath,
2165 ST("INCS"), ST("incs"), -1 /* right-to-left */);
2166 /*pFlags =*/ kbuild_collect_source_prop(pTarget, pSource, pTool, &Sdks, pType, pBldType, pBldTrg, pBldTrgArch, pBldTrgCpu, NULL,
2167 ST("FLAGS"), ST("flags"), 1 /* left-to-right */);
2168 pDeps = kbuild_collect_source_prop(pTarget, pSource, pTool, &Sdks, pType, pBldType, pBldTrg, pBldTrgArch, pBldTrgCpu, pDefPath,
2169 ST("DEPS"), ST("deps"), 1 /* left-to-right */);
2170 pOrderDeps = kbuild_collect_source_prop(pTarget, pSource, pTool, &Sdks, pType, pBldType, pBldTrg, pBldTrgArch, pBldTrgCpu, pDefPath,
2171 ST("ORDERDEPS"), ST("orderdeps"), 1 /* left-to-right */);
2172
2173 /*
2174 * If we've got a default path, we must expand the source now.
2175 * If we do this too early, "<source>_property = stuff" won't work becuase
2176 * our 'source' value isn't what the user expects.
2177 */
2178 if (pDefPath)
2179 {
2180 /** @todo assert(pSource->origin != o_automatic); We're changing 'source'
2181 * from the foreach loop! */
2182#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2183 assert(!pSource->rdonly_val);
2184#endif
2185 kbuild_apply_defpath(pDefPath, &pSource->value, &pSource->value_length, &pSource->value_alloc_len, 1 /* can free */);
2186 }
2187
2188 /*
2189 # dependencies
2190 ifndef NO_COMPILE_DEPS
2191 _DEPFILES_INCLUDED += $(dep)
2192 $(if $(wildcard $(dep)),$(eval include $(dep)))
2193 endif
2194 */
2195 if (s_fNoCompileDepsDefined == -1)
2196 s_fNoCompileDepsDefined = kbuild_lookup_variable_n(ST("NO_COMPILE_DEPS")) != NULL;
2197 if (!s_fNoCompileDepsDefined)
2198 {
2199 pVar = kbuild_query_recursive_variable_n("_DEPFILES_INCLUDED", sizeof("_DEPFILES_INCLUDED") - 1);
2200 if (pVar)
2201 {
2202 if (pVar->recursive)
2203 pVar = kbuild_simplify_variable(pVar);
2204 append_string_to_variable(pVar, pDep->value, pDep->value_length, 1 /* append */);
2205 }
2206 else
2207 define_variable_vl_global("_DEPFILES_INCLUDED", sizeof("_DEPFILES_INCLUDED") - 1,
2208 pDep->value, pDep->value_length,
2209 1 /* duplicate_value */,
2210 o_file,
2211 0 /* recursive */,
2212 NULL /* flocp */);
2213
2214 eval_include_dep(pDep->value, NILF, iVer >= 2 ? incdep_queue : incdep_read_it);
2215 }
2216
2217 /*
2218 # call the tool
2219 $(target)_$(source)_CMDS_ := $(TOOL_$(tool)_COMPILE_$(type)_CMDS)
2220 $(target)_$(source)_OUTPUT_ := $(TOOL_$(tool)_COMPILE_$(type)_OUTPUT)
2221 $(target)_$(source)_OUTPUT_MAYBE_ := $(TOOL_$(tool)_COMPILE_$(type)_OUTPUT_MAYBE)
2222 $(target)_$(source)_DEPEND_ := $(TOOL_$(tool)_COMPILE_$(type)_DEPEND) $(deps) $(source)
2223 $(target)_$(source)_DEPORD_ := $(TOOL_$(tool)_COMPILE_$(type)_DEPORD) $(dirdep)
2224 */
2225 /** @todo Make all these local variables, if someone needs the info later it
2226 * can be recalculated. (Ticket #80.) */
2227 cch = sizeof("TOOL_") + pTool->value_length + sizeof("_COMPILE_") + pType->value_length + sizeof("_USES_KOBJCACHE");
2228 if (cch < pTarget->value_length + sizeof("$(_2_OBJS)"))
2229 cch = pTarget->value_length + sizeof("$(_2_OBJS)");
2230 psz = pszSrcVar = alloca(cch);
2231 memcpy(psz, "TOOL_", sizeof("TOOL_") - 1); psz += sizeof("TOOL_") - 1;
2232 memcpy(psz, pTool->value, pTool->value_length); psz += pTool->value_length;
2233 memcpy(psz, "_COMPILE_", sizeof("_COMPILE_") - 1); psz += sizeof("_COMPILE_") - 1;
2234 memcpy(psz, pType->value, pType->value_length); psz += pType->value_length;
2235 pszSrc = psz;
2236
2237 cch = pTarget->value_length + 1 + pSource->value_length + sizeof("_OUTPUT_MAYBE_");
2238 psz = pszDstVar = alloca(cch);
2239 memcpy(psz, pTarget->value, pTarget->value_length); psz += pTarget->value_length;
2240 *psz++ = '_';
2241 memcpy(psz, pSource->value, pSource->value_length); psz += pSource->value_length;
2242 pszDst = psz;
2243
2244 memcpy(pszSrc, "_CMDS", sizeof("_CMDS"));
2245 memcpy(pszDst, "_CMDS_", sizeof("_CMDS_"));
2246 pVar = kbuild_get_recursive_variable(pszSrcVar);
2247 if (iVer <= 2)
2248 do_variable_definition_2(NILF, pszDstVar, pVar->value, pVar->value_length,
2249 !pVar->recursive, 0, o_local, f_simple, 0 /* !target_var */);
2250 do_variable_definition_2(NILF, "kbsrc_cmds", pVar->value, pVar->value_length,
2251 !pVar->recursive, 0, o_local, f_simple, 0 /* !target_var */);
2252
2253 memcpy(pszSrc, "_OUTPUT", sizeof("_OUTPUT"));
2254 memcpy(pszDst, "_OUTPUT_", sizeof("_OUTPUT_"));
2255 pVar = kbuild_get_recursive_variable(pszSrcVar);
2256 if (iVer <= 2)
2257 pOutput = do_variable_definition_2(NILF, pszDstVar, pVar->value, pVar->value_length,
2258 !pVar->recursive, 0, o_local, f_simple, 0 /* !target_var */);
2259 pOutput = do_variable_definition_2(NILF, "kbsrc_output", pVar->value, pVar->value_length,
2260 !pVar->recursive, 0, o_local, f_simple, 0 /* !target_var */);
2261
2262 memcpy(pszSrc, "_OUTPUT_MAYBE", sizeof("_OUTPUT_MAYBE"));
2263 memcpy(pszDst, "_OUTPUT_MAYBE_", sizeof("_OUTPUT_MAYBE_"));
2264 pVar = kbuild_query_recursive_variable(pszSrcVar);
2265 if (pVar)
2266 {
2267 if (iVer <= 2)
2268 pOutputMaybe = do_variable_definition_2(NILF, pszDstVar, pVar->value, pVar->value_length,
2269 !pVar->recursive, 0, o_local, f_simple, 0 /* !target_var */);
2270 pOutputMaybe = do_variable_definition_2(NILF, "kbsrc_output_maybe", pVar->value, pVar->value_length,
2271 !pVar->recursive, 0, o_local, f_simple, 0 /* !target_var */);
2272 }
2273 else
2274 {
2275 if (iVer <= 2)
2276 pOutputMaybe = do_variable_definition_2(NILF, pszDstVar, "", 0, 1, 0, o_local, f_simple, 0 /* !target_var */);
2277 pOutputMaybe = do_variable_definition_2(NILF, "kbsrc_output_maybe", "", 0, 1, 0, o_local, f_simple, 0 /* !target_var */);
2278 }
2279
2280 memcpy(pszSrc, "_DEPEND", sizeof("_DEPEND"));
2281 memcpy(pszDst, "_DEPEND_", sizeof("_DEPEND_"));
2282 pVar = kbuild_get_recursive_variable(pszSrcVar);
2283 psz = pszVal = xmalloc(pVar->value_length + 1 + pDeps->value_length + 1 + pSource->value_length + 1);
2284 memcpy(psz, pVar->value, pVar->value_length); psz += pVar->value_length;
2285 *psz++ = ' ';
2286 memcpy(psz, pDeps->value, pDeps->value_length); psz += pDeps->value_length;
2287 *psz++ = ' ';
2288 memcpy(psz, pSource->value, pSource->value_length + 1);
2289 if (iVer <= 2)
2290 do_variable_definition_2(NILF, pszDstVar, pszVal, pVar->value_length + 1 + pDeps->value_length + 1 + pSource->value_length,
2291 !pVar->recursive && !pDeps->recursive && !pSource->recursive,
2292 NULL, o_local, f_simple, 0 /* !target_var */);
2293 do_variable_definition_2(NILF, "kbsrc_depend", pszVal, pVar->value_length + 1 + pDeps->value_length + 1 + pSource->value_length,
2294 !pVar->recursive && !pDeps->recursive && !pSource->recursive,
2295 pszVal, o_local, f_simple, 0 /* !target_var */);
2296
2297 memcpy(pszSrc, "_DEPORD", sizeof("_DEPORD"));
2298 memcpy(pszDst, "_DEPORD_", sizeof("_DEPORD_"));
2299 pVar = kbuild_get_recursive_variable(pszSrcVar);
2300 psz = pszVal = xmalloc(pVar->value_length + 1 + pDirDep->value_length + 1 + pOrderDeps->value_length + 1);
2301 memcpy(psz, pVar->value, pVar->value_length); psz += pVar->value_length;
2302 *psz++ = ' ';
2303 memcpy(psz, pDirDep->value, pDirDep->value_length); psz += pDirDep->value_length;
2304 *psz++ = ' ';
2305 memcpy(psz, pOrderDeps->value, pOrderDeps->value_length + 1);
2306 if (iVer <= 2)
2307 do_variable_definition_2(NILF, pszDstVar, pszVal,
2308 pVar->value_length + 1 + pDirDep->value_length + 1 + pOrderDeps->value_length,
2309 !pVar->recursive && !pDirDep->recursive && !pOrderDeps->recursive,
2310 NULL, o_local, f_simple, 0 /* !target_var */);
2311 do_variable_definition_2(NILF, "kbsrc_depord", pszVal,
2312 pVar->value_length + 1 + pDirDep->value_length + 1 + pOrderDeps->value_length,
2313 !pVar->recursive && !pDirDep->recursive && !pOrderDeps->recursive,
2314 pszVal, o_local, f_simple, 0 /* !target_var */);
2315
2316 /*
2317 _OUT_FILES += $($(target)_$(source)_OUTPUT_) $($(target)_$(source)_OUTPUT_MAYBE_)
2318 */
2319 pVar = kbuild_get_variable_n(ST("_OUT_FILES"));
2320 append_string_to_variable(pVar, pOutput->value, pOutput->value_length, 1 /* append */);
2321 if (pOutputMaybe->value_length)
2322 append_string_to_variable(pVar, pOutputMaybe->value, pOutputMaybe->value_length, 1 /* append */);
2323
2324 /*
2325 $(target)_2_OBJS += $(obj)
2326 */
2327 memcpy(pszDstVar + pTarget->value_length, "_2_OBJS", sizeof("_2_OBJS"));
2328 pVar = kbuild_query_recursive_variable_n(pszDstVar, pTarget->value_length + sizeof("_2_OBJS") - 1);
2329 fInstallOldObjsVar |= iVer <= 2 && (!pVar || !pVar->value_length);
2330 if (pVar)
2331 {
2332 if (pVar->recursive)
2333 pVar = kbuild_simplify_variable(pVar);
2334 append_string_to_variable(pVar, pObj->value, pObj->value_length, 1 /* append */);
2335 }
2336 else
2337 define_variable_vl_global(pszDstVar, pTarget->value_length + sizeof("_2_OBJS") - 1,
2338 pObj->value, pObj->value_length,
2339 1 /* duplicate_value */,
2340 o_file,
2341 0 /* recursive */,
2342 NULL /* flocp */);
2343
2344 /*
2345 * Install legacy variable.
2346 */
2347 if (fInstallOldObjsVar)
2348 {
2349 /* $(target)_OBJS_ = $($(target)_2_OBJS)*/
2350 memcpy(pszDstVar + pTarget->value_length, "_OBJS_", sizeof("_OBJS_"));
2351
2352 pszSrcVar[0] = '$';
2353 pszSrcVar[1] = '(';
2354 memcpy(pszSrcVar + 2, pTarget->value, pTarget->value_length);
2355 psz = pszSrcVar + 2 + pTarget->value_length;
2356 memcpy(psz, "_2_OBJS)", sizeof("_2_OBJS)"));
2357
2358 define_variable_vl_global(pszDstVar, pTarget->value_length + sizeof("_OBJS_") - 1,
2359 pszSrcVar, pTarget->value_length + sizeof("$(_2_OBJS)") - 1,
2360 1 /* duplicate_value */,
2361 o_file,
2362 1 /* recursive */,
2363 NULL /* flocp */);
2364 }
2365
2366 /*
2367 $(eval $(def_target_source_rule))
2368 */
2369 if (iVer > 2)
2370 {
2371 /*ifneq ($(TOOL_$(tool)_COMPILE_$(type)_USES_KOBJCACHE),)*/
2372 int fUsesObjCache = 0;
2373 memcpy(pszSrc, "_USES_KOBJCACHE", sizeof("_USES_KOBJCACHE"));
2374 pVar = lookup_variable(pszSrcVar, pszSrc + sizeof("_USES_KOBJCACHE") - 1 - pszSrcVar);
2375 if (pVar)
2376 {
2377 if ( !pVar->recursive
2378 || IS_VARIABLE_RECURSIVE_WITHOUT_DOLLAR(pVar))
2379 fUsesObjCache = pVar->value_length > 0;
2380 else
2381 {
2382 unsigned int cchTmp = 0;
2383 char *pszTmp = allocated_variable_expand_2(pVar->value, pVar->value_length, &cchTmp);
2384 free(pszTmp);
2385 fUsesObjCache = cchTmp > 0;
2386 }
2387 }
2388 if (!fUsesObjCache)
2389 pVar = kbuild_get_recursive_variable("def_target_source_rule_v3plus");
2390 else
2391 pVar = kbuild_get_recursive_variable("def_target_source_rule_v3plus_objcache");
2392 }
2393 else
2394 pVar = kbuild_get_recursive_variable("def_target_source_rule");
2395 pszVal = variable_expand_string_2(o, pVar->value, pVar->value_length, &psz);
2396 assert(!((size_t)pszVal & 3));
2397
2398 install_variable_buffer(&pszSavedVarBuf, &cchSavedVarBuf);
2399 eval_buffer(pszVal, NULL, psz);
2400 restore_variable_buffer(pszSavedVarBuf, cchSavedVarBuf);
2401
2402 kbuild_put_sdks(&Sdks);
2403 (void)pszFuncName;
2404
2405 *pszVal = '\0';
2406 return pszVal;
2407}
2408
2409/*
2410
2411## Inherit one template property in a non-accumulative manner.
2412# @param $(prop) Property name
2413# @param $(target) Target name
2414# @todo fix the precedence order for some properties.
2415define def_inherit_template_one
2416ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop)
2417ifndef $(target)_$(prop)
2418$(target)_$(prop) := $(TEMPLATE_$($(target)_TEMPLATE)_$(prop))
2419endif
2420endif
2421ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg)
2422ifndef $(target)_$(prop).$(bld_trg)
2423$(target)_$(prop).$(bld_trg) := $(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg))
2424endif
2425endif
2426ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch)
2427ifndef $(target)_$(prop).$(bld_trg).$(bld_trg_arch)
2428$(target)_$(prop).$(bld_trg).$(bld_trg_arch) := $(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch))
2429endif
2430endif
2431ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch)
2432ifndef $(target)_$(prop).$(bld_trg_arch)
2433$(target)_$(prop).$(bld_trg_arch) := $(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch))
2434endif
2435endif
2436ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu)
2437ifndef $(target)_$(prop).$(bld_trg_cpu)
2438$(target)_$(prop).$(bld_trg_cpu) := $(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu))
2439endif
2440endif
2441endef
2442
2443## Inherit one template property in a non-accumulative manner, deferred expansion.
2444# @param 1: $(prop) Property name
2445# @param 2: $(target) Target name
2446# @todo fix the precedence order for some properties.
2447# @remark this define relies on double evaluation
2448define def_inherit_template_one_deferred
2449ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop)
2450ifndef $(target)_$(prop)
2451$(target)_$(prop) = $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop))
2452endif
2453endif
2454ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg)
2455ifndef $(target)_$(prop).$(bld_trg)
2456$(target)_$(prop).$(bld_trg) = $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg))
2457endif
2458endif
2459ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch)
2460ifndef $(target)_$(prop).$(bld_trg).$(bld_trg_arch)
2461$(target)_$(prop).$(bld_trg).$(bld_trg_arch) = $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch))
2462endif
2463endif
2464ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch)
2465ifndef $(target)_$(prop).$(bld_trg_arch)
2466$(target)_$(prop).$(bld_trg_arch) = $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch))
2467endif
2468endif
2469ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu)
2470ifndef $(target)_$(prop).$(bld_trg_cpu)
2471$(target)_$(prop).$(bld_trg_cpu) = $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu))
2472endif
2473endif
2474endef
2475
2476## Inherit one acculumlative template property where the 'most significant' items are at the left end.
2477# @param $(prop) Property name
2478# @param $(target) Target name
2479define def_inherit_template_one_accumulate_l
2480ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop)
2481 ifeq ($$(flavor $(target)_$(prop)),simple)
2482 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop))
2483 endif
2484$(target)_$(prop) += $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop))
2485endif
2486ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(KBUILD_TYPE)
2487 ifeq ($$(flavor $(target)_$(prop).$(KBUILD_TYPE)),simple)
2488 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(KBUILD_TYPE))
2489 endif
2490$(target)_$(prop).$(KBUILD_TYPE) += $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(KBUILD_TYPE))
2491endif
2492ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg)
2493 ifeq ($$(flavor $(target)_$(prop).$(bld_trg)),simple)
2494 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg))
2495 endif
2496$(target)_$(prop).$(bld_trg) += $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg))
2497endif
2498ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch)
2499 ifeq ($$(flavor $(target)_$(prop).$(bld_trg).$(bld_trg_arch)),simple)
2500 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg).$(bld_trg_arch))
2501 endif
2502$(target)_$(prop).$(bld_trg).$(bld_trg_arch) += $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch))
2503endif
2504ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu)
2505 ifeq ($$(flavor $(target)_$(prop).$(bld_trg_cpu)),simple)
2506 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg_cpu))
2507 endif
2508$(target)_$(prop).$(bld_trg_cpu) += $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu))
2509endif
2510ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch)
2511 ifeq ($$(flavor $(target)_$(prop).$(bld_trg_arch)),simple)
2512 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg_arch))
2513 endif
2514$(target)_$(prop).$(bld_trg_arch) += $$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch))
2515endif
2516endef
2517
2518## Inherit one acculumlative template property where the 'most significant' items are at the right end.
2519# @param $(prop) Property name
2520# @param $(target) Target name
2521define def_inherit_template_one_accumulate_r
2522ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop)
2523 ifeq ($$(flavor $(target)_$(prop)),simple)
2524 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop))
2525 endif
2526$(target)_$(prop) <=$$(TEMPLATE_$($(target)_TEMPLATE)_$(prop))
2527endif
2528ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(KBUILD_TYPE)
2529 ifeq ($$(flavor $(target)_$(prop).$(KBUILD_TYPE)),simple)
2530 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(KBUILD_TYPE))
2531 endif
2532$(target)_$(prop).$(KBUILD_TYPE) <=$$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(KBUILD_TYPE))
2533endif
2534ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg)
2535 ifeq ($$(flavor $(target)_$(prop).$(bld_trg)),simple)
2536 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg))
2537 endif
2538$(target)_$(prop).$(bld_trg) <=$$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg))
2539endif
2540ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch)
2541 ifeq ($$(flavor $(target)_$(prop).$(bld_trg).$(bld_trg_arch)),simple)
2542 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg).$(bld_trg_arch))
2543 endif
2544$(target)_$(prop).$(bld_trg).$(bld_trg_arch) <=$$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg).$(bld_trg_arch))
2545endif
2546ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu)
2547 ifeq ($$(flavor $(target)_$(prop).$(bld_trg_cpu)),simple)
2548 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg_cpu))
2549 endif
2550$(target)_$(prop).$(bld_trg_cpu) <=$$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_cpu))
2551endif
2552ifdef TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch)
2553 ifeq ($$(flavor $(target)_$(prop).$(bld_trg_arch)),simple)
2554 $$(evalcall2 def_simple_2_recursive,$(target)_$(prop).$(bld_trg_arch))
2555 endif
2556$(target)_$(prop).$(bld_trg_arch) <=$$(TEMPLATE_$($(target)_TEMPLATE)_$(prop).$(bld_trg_arch))
2557endif
2558endef
2559
2560
2561## Inherit template properties for on target.
2562# @param $(target) Target name.
2563define def_inherit_template
2564# sanity check.
2565ifdef _$(target)_ALREADY_PROCESSED
2566 $(error kBuild: The target $(target) appears more than once in the target lists! Please correct the makefile(s))
2567endif
2568_$(target)_ALREADY_PROCESSED := 1
2569
2570# Inherit any default template.
2571ifdef TEMPLATE
2572ifeq ($($(target)_TEMPLATE),)
2573$(eval $(target)_TEMPLATE:=$(TEMPLATE))
2574endif
2575endif
2576# Expand the template if specified.
2577ifneq ($($(target)_TEMPLATE),)
2578$(foreach prop,$(PROPS_SINGLE),$(evalval def_inherit_template_one))
2579$(foreach prop,$(PROPS_DEFERRED),$(eval $(def_inherit_template_one_deferred))) # exploits the 2 evaluation, so no value!
2580$(foreach prop,$(PROPS_ACCUMULATE_L),$(eval $(def_inherit_template_one_accumulate_l))) # += works fine without value
2581$(foreach prop,$(PROPS_ACCUMULATE_R),$(eval $(def_inherit_template_one_accumulate_r))) # use <= (kmk addition)
2582endif
2583endef
2584
2585
2586Invoked like this:
2587 $(kb-exp-tmpl 1,$(_ALL_TARGET_TARGETS),$(KBUILD_TARGET),$(KBUILD_TARGET_ARCH),$(KBUILD_TARGET_CPU),$(KBUILD_TYPE))
2588*/
2589char *
2590func_kbuild_expand_template(char *o, char **argv, const char *pszFuncName)
2591{
2592 const char *pszVersion = argv[0];
2593 const char *pszBldTrg = argv[2];
2594 const char *pszBldTrgArch = argv[3];
2595 const char *pszBldTrgCpu = argv[4];
2596 const char *pszBldType = argv[5];
2597 size_t cchBldTrg = strlen(pszBldTrg);
2598 size_t cchBldTrgArch = strlen(pszBldTrgArch);
2599 size_t cchBldTrgCpu = strlen(pszBldTrgCpu);
2600 size_t cchBldType = strlen(pszBldType);
2601 size_t cchMaxBld = cchBldTrg + cchBldTrgArch + cchBldTrgCpu + cchBldType; /* too big, but so what. */
2602 struct kbet_key
2603 {
2604 unsigned int cch;
2605 char *psz;
2606 } aKeys[6];
2607 unsigned int const cKeys = 6;
2608 unsigned int iKey;
2609 struct variable *pDefTemplate;
2610 struct variable *pProps;
2611 struct kbet_prop
2612 {
2613 const char *pch;
2614 unsigned int cch;
2615 enum kbet_prop_enum { kPropSingle, kPropDeferred, kPropAccumulateL, kPropAccumulateR }
2616 enmType;
2617 } *paProps;
2618 unsigned int cProps;
2619 unsigned int iProp;
2620 size_t cchMaxProp;
2621 struct variable *pVarTrg;
2622 struct variable *pVarSrc;
2623 const char *pszIter;
2624 const char *pszTarget;
2625 unsigned int cchTarget;
2626 char *pszSrc = 0;
2627 char *pszSrcRef = 0;
2628 char *pszSrcBuf = 0;
2629 size_t cchSrcBuf = 0;
2630 char *pszTrg = 0;
2631 size_t cchTrg = 0;
2632
2633 /*
2634 * Validate input.
2635 */
2636 if (pszVersion[0] != '1' || pszVersion[1])
2637 OSS(fatal, NULL, "%s: Unsupported version `%s'", pszFuncName, pszVersion);
2638
2639 if (!cchBldTrg)
2640 OS(fatal, NULL, "%s: missing bldtrg", pszFuncName);
2641
2642 if (!cchBldTrgArch)
2643 OS(fatal, NULL, "%s: missing bld_trg_arch", pszFuncName);
2644
2645 if (!cchBldTrgCpu)
2646 OS(fatal, NULL, "%s: missing bld_trg_cpu", pszFuncName);
2647
2648 if (!cchBldType)
2649 OS(fatal, NULL, "%s: missing bld_type", pszFuncName);
2650
2651 /*
2652 * Prepare the keywords, prepending dots for quicker copying.
2653 * This allows for an inner loop when processing properties, saving code
2654 * at the expense of a few xmallocs.
2655 */
2656 /* the first entry is empty. */
2657 aKeys[0].cch = 0;
2658 aKeys[0].psz = NULL;
2659
2660 /* .$(bld_type) */
2661 aKeys[1].cch = cchBldType + 1;
2662 aKeys[1].psz = xmalloc (aKeys[1].cch + 1);
2663 aKeys[1].psz[0] = '.';
2664 memcpy(aKeys[1].psz + 1, pszBldType, cchBldType + 1);
2665
2666 /* .$(bld_trg) */
2667 aKeys[2].cch = cchBldTrg + 1;
2668 aKeys[2].psz = xmalloc (aKeys[2].cch + 1);
2669 aKeys[2].psz[0] = '.';
2670 memcpy(aKeys[2].psz + 1, pszBldTrg, cchBldTrg + 1);
2671
2672 /* .$(bld_trg).$(bld_trg_arch) */
2673 aKeys[3].cch = cchBldTrg + 1 + cchBldTrgArch + 1;
2674 aKeys[3].psz = xmalloc (aKeys[3].cch + 1);
2675 aKeys[3].psz[0] = '.';
2676 memcpy(aKeys[3].psz + 1, pszBldTrg, cchBldTrg);
2677 aKeys[3].psz[1 + cchBldTrg] = '.';
2678 memcpy(aKeys[3].psz + 1 + cchBldTrg + 1, pszBldTrgArch, cchBldTrgArch + 1);
2679
2680 /* .$(bld_trg_cpu) */
2681 aKeys[4].cch = cchBldTrgCpu + 1;
2682 aKeys[4].psz = xmalloc (aKeys[4].cch + 1);
2683 aKeys[4].psz[0] = '.';
2684 memcpy(aKeys[4].psz + 1, pszBldTrgCpu, cchBldTrgCpu + 1);
2685
2686 /* .$(bld_trg_arch) */
2687 aKeys[5].cch = cchBldTrgArch + 1;
2688 aKeys[5].psz = xmalloc (aKeys[5].cch + 1);
2689 aKeys[5].psz[0] = '.';
2690 memcpy(aKeys[5].psz + 1, pszBldTrgArch, cchBldTrgArch + 1);
2691
2692
2693 /*
2694 * Prepare the properties, folding them into an array.
2695 * This way we won't have to reparse them for each an every target, though
2696 * it comes at the expense of one or more heap calls.
2697 */
2698#define PROP_ALLOC_INC 128
2699 iProp = 0;
2700 cProps = PROP_ALLOC_INC;
2701 paProps = xmalloc(sizeof(*pProps) * cProps);
2702
2703 pProps = kbuild_get_variable_n(ST("PROPS_SINGLE"));
2704 pszIter = pProps->value;
2705 while ((paProps[iProp].pch = find_next_token(&pszIter, &paProps[iProp].cch)))
2706 {
2707 paProps[iProp].enmType = kPropSingle;
2708 if (++iProp >= cProps)
2709 {
2710 cProps += PROP_ALLOC_INC;
2711 paProps = xrealloc(paProps, sizeof(*paProps) * cProps);
2712 }
2713
2714 }
2715
2716 pProps = kbuild_get_variable_n(ST("PROPS_DEFERRED"));
2717 pszIter = pProps->value;
2718 while ((paProps[iProp].pch = find_next_token(&pszIter, &paProps[iProp].cch)))
2719 {
2720 paProps[iProp].enmType = kPropDeferred;
2721 if (++iProp >= cProps)
2722 {
2723 cProps += PROP_ALLOC_INC;
2724 paProps = xrealloc(paProps, sizeof(*paProps) * cProps);
2725 }
2726 }
2727
2728 pProps = kbuild_get_variable_n(ST("PROPS_ACCUMULATE_L"));
2729 pszIter = pProps->value;
2730 while ((paProps[iProp].pch = find_next_token(&pszIter, &paProps[iProp].cch)))
2731 {
2732 paProps[iProp].enmType = kPropAccumulateL;
2733 if (++iProp >= cProps)
2734 {
2735 cProps += PROP_ALLOC_INC;
2736 paProps = xrealloc(paProps, sizeof(*paProps) * cProps);
2737 }
2738 }
2739
2740 pProps = kbuild_get_variable_n(ST("PROPS_ACCUMULATE_R"));
2741 pszIter = pProps->value;
2742 while ((paProps[iProp].pch = find_next_token(&pszIter, &paProps[iProp].cch)))
2743 {
2744 paProps[iProp].enmType = kPropAccumulateR;
2745 if (++iProp >= cProps)
2746 {
2747 cProps += PROP_ALLOC_INC;
2748 paProps = xrealloc(paProps, sizeof(*paProps) * cProps);
2749 }
2750 }
2751#undef PROP_ALLOC_INC
2752 cProps = iProp;
2753
2754 /* find the max prop length. */
2755 cchMaxProp = paProps[0].cch;
2756 while (--iProp > 0)
2757 if (paProps[iProp].cch > cchMaxProp)
2758 cchMaxProp = paProps[iProp].cch;
2759
2760 /*
2761 * Query and prepare (strip) the default template
2762 * (given by the TEMPLATE variable).
2763 */
2764 pDefTemplate = kbuild_lookup_variable_n(ST("TEMPLATE"));
2765 if (pDefTemplate)
2766 {
2767 if ( pDefTemplate->value_length
2768 && ( ISSPACE(pDefTemplate->value[0])
2769 || ISSPACE(pDefTemplate->value[pDefTemplate->value_length - 1])))
2770 {
2771 unsigned int off;
2772 if (pDefTemplate->rdonly_val)
2773 OS(fatal, NULL, "%s: TEMPLATE is read-only", pszFuncName);
2774
2775 /* head */
2776 for (off = 0; ISSPACE(pDefTemplate->value[off]); off++)
2777 /* nothing */;
2778 if (off)
2779 {
2780 pDefTemplate->value_length -= off;
2781 memmove(pDefTemplate->value, pDefTemplate->value + off, pDefTemplate->value_length + 1);
2782 }
2783
2784 /* tail */
2785 off = pDefTemplate->value_length;
2786 while (off > 0 && ISSPACE(pDefTemplate->value[off - 1]))
2787 off--;
2788 pDefTemplate->value_length = off;
2789 pDefTemplate->value[off] = '\0';
2790
2791 VARIABLE_CHANGED(pDefTemplate);
2792 }
2793
2794 if (!pDefTemplate->value_length)
2795 pDefTemplate = NULL;
2796 }
2797
2798 /*
2799 * Iterate the target list.
2800 */
2801 pszIter = argv[1];
2802 while ((pszTarget = find_next_token(&pszIter, &cchTarget)))
2803 {
2804 char *pszTrgProp, *pszSrcProp;
2805 char *pszTrgKey, *pszSrcKey;
2806 struct variable *pTmpl;
2807 const char *pszTmpl;
2808 size_t cchTmpl, cchMax;
2809
2810 /* resize the target buffer. */
2811 cchMax = cchTarget + cchMaxProp + cchMaxBld + 10;
2812 if (cchTrg < cchMax)
2813 {
2814 cchTrg = (cchMax + 31U) & ~(size_t)31;
2815 pszTrg = xrealloc(pszTrg, cchTrg);
2816 }
2817
2818 /*
2819 * Query the TEMPLATE property, if not found or zero-length fall back on the default.
2820 */
2821 memcpy(pszTrg, pszTarget, cchTarget);
2822 pszTrgProp = pszTrg + cchTarget;
2823 memcpy(pszTrgProp, "_TEMPLATE", sizeof("_TEMPLATE"));
2824 pszTrgProp++; /* after '_'. */
2825
2826 /** @todo Change this to a recursive lookup with simplification below. That
2827 * will allow target_TEMPLATE = $(NO_SUCH_TEMPLATE) instead of having
2828 * to use target_TEMPLATE = DUMMY */
2829 pTmpl = kbuild_lookup_variable_n(pszTrg, cchTarget + sizeof("_TEMPLATE") - 1);
2830 if (!pTmpl || !pTmpl->value_length)
2831 {
2832 if (!pDefTemplate)
2833 continue; /* no template */
2834 pszTmpl = pDefTemplate->value;
2835 cchTmpl = pDefTemplate->value_length;
2836 }
2837 else
2838 {
2839 pszTmpl = pTmpl->value;
2840 cchTmpl = pTmpl->value_length;
2841 while (ISSPACE(*pszTmpl))
2842 cchTmpl--, pszTmpl++;
2843 if (!cchTmpl)
2844 continue; /* no template */
2845 }
2846
2847 /* resize the source buffer. */
2848 cchMax = sizeof("TEMPLATE_") + cchTmpl + cchMaxProp + cchMaxBld + 10 + sizeof(void *);
2849 if (cchSrcBuf < cchMax)
2850 {
2851 cchSrcBuf = (cchMax + 31U) & ~(size_t)31;
2852 pszSrcBuf = xrealloc(pszSrcBuf, cchSrcBuf);
2853 pszSrc = pszSrcBuf + sizeof(void *); assert(sizeof(void *) >= 2);
2854 pszSrcRef = pszSrc - 2;
2855 pszSrcRef[0] = '$';
2856 pszSrcRef[1] = '(';
2857 }
2858
2859 /* prepare the source buffer */
2860 memcpy(pszSrc, "TEMPLATE_", sizeof("TEMPLATE_") - 1);
2861 pszSrcProp = pszSrc + sizeof("TEMPLATE_") - 1;
2862 memcpy(pszSrcProp, pszTmpl, cchTmpl);
2863 pszSrcProp += cchTmpl;
2864 *pszSrcProp++ = '_';
2865
2866 /*
2867 * Process properties.
2868 * Note! The single and deferred are handled in the same way now.
2869 */
2870#define BY_REF_LIMIT 64 /*(cchSrcVar * 4 > 64 ? cchSrcVar * 4 : 64)*/
2871
2872 for (iProp = 0; iProp < cProps; iProp++)
2873 {
2874 memcpy(pszTrgProp, paProps[iProp].pch, paProps[iProp].cch);
2875 pszTrgKey = pszTrgProp + paProps[iProp].cch;
2876
2877 memcpy(pszSrcProp, paProps[iProp].pch, paProps[iProp].cch);
2878 pszSrcKey = pszSrcProp + paProps[iProp].cch;
2879
2880 for (iKey = 0; iKey < cKeys; iKey++)
2881 {
2882 char *pszTrgEnd;
2883 size_t cchSrcVar;
2884
2885 /* lookup source, skip ahead if it doesn't exist. */
2886 memcpy(pszSrcKey, aKeys[iKey].psz, aKeys[iKey].cch);
2887 cchSrcVar = pszSrcKey - pszSrc + aKeys[iKey].cch;
2888 pszSrc[cchSrcVar] = '\0';
2889 pVarSrc = kbuild_query_recursive_variable_n(pszSrc, cchSrcVar);
2890 if (!pVarSrc)
2891 continue;
2892
2893 /* lookup target, skip ahead if it exists. */
2894 memcpy(pszTrgKey, aKeys[iKey].psz, aKeys[iKey].cch);
2895 pszTrgEnd = pszTrgKey + aKeys[iKey].cch;
2896 *pszTrgEnd = '\0';
2897 pVarTrg = kbuild_query_recursive_variable_n(pszTrg, pszTrgEnd - pszTrg);
2898
2899 switch (paProps[iProp].enmType)
2900 {
2901 case kPropAccumulateL:
2902 case kPropAccumulateR:
2903 if (pVarTrg)
2904 {
2905 /* Append to existing variable. If the source is recursive,
2906 or we append by reference, we'll have to make sure the
2907 target is recusive as well. */
2908 if ( !pVarTrg->recursive
2909 && ( pVarSrc->value_length >= BY_REF_LIMIT
2910 || pVarSrc->recursive))
2911 pVarTrg->recursive = 1;
2912
2913 if (pVarSrc->value_length < BY_REF_LIMIT)
2914 append_string_to_variable(pVarTrg, pVarSrc->value, pVarSrc->value_length,
2915 paProps[iProp].enmType == kPropAccumulateL /* append */);
2916 else
2917 {
2918 pszSrc[cchSrcVar] = ')';
2919 pszSrc[cchSrcVar + 1] = '\0';
2920 append_string_to_variable(pVarTrg, pszSrcRef, 2 + cchSrcVar + 1,
2921 paProps[iProp].enmType == kPropAccumulateL /* append */);
2922 }
2923 break;
2924 }
2925 /* else: the target variable doesn't exist, create it. */
2926 /* fall thru */
2927
2928 case kPropSingle:
2929 case kPropDeferred:
2930 if (pVarTrg)
2931 continue; /* skip ahead if it already exists. */
2932
2933 /* copy the variable if its short, otherwise reference it. */
2934 if (pVarSrc->value_length < BY_REF_LIMIT)
2935 define_variable_vl_global(pszTrg, pszTrgEnd - pszTrg,
2936 pVarSrc->value, pVarSrc->value_length,
2937 1 /* duplicate_value */,
2938 o_file,
2939 pVarSrc->recursive,
2940 NULL /* flocp */);
2941 else
2942 {
2943 pszSrc[cchSrcVar] = ')';
2944 pszSrc[cchSrcVar + 1] = '\0';
2945 define_variable_vl_global(pszTrg, pszTrgEnd - pszTrg,
2946 pszSrcRef, 2 + cchSrcVar + 1,
2947 1 /* duplicate_value */,
2948 o_file,
2949 1 /* recursive */,
2950 NULL /* flocp */);
2951 }
2952 break;
2953
2954 }
2955
2956 } /* foreach key */
2957 } /* foreach prop */
2958#undef BY_REF_LIMIT
2959 } /* foreach target */
2960
2961 /*
2962 * Cleanup.
2963 */
2964 free(pszSrcBuf);
2965 free(pszTrg);
2966 free(paProps);
2967 for (iKey = 1; iKey < cKeys; iKey++)
2968 free(aKeys[iKey].psz);
2969
2970 return o;
2971}
2972
2973#endif /* KMK_HELPERS */
2974
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