Carles Cufi | 31bdad5 | 2019-04-26 21:53:02 +0200 | [diff] [blame] | 1 | # Copyright 2018 (c) Foundries.io. |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | |
| 5 | '''Common definitions for building Zephyr applications. |
| 6 | |
| 7 | This provides some default settings and convenience wrappers for |
| 8 | building Zephyr applications needed by multiple commands. |
| 9 | |
| 10 | See build.py for the build command itself. |
| 11 | ''' |
| 12 | |
Jan Van Winkel | 4d975db | 2019-05-04 14:44:56 +0200 | [diff] [blame] | 13 | import zcmake |
Carles Cufi | 31bdad5 | 2019-04-26 21:53:02 +0200 | [diff] [blame] | 14 | import os |
Carles Cufi | 41f1f64 | 2019-06-17 11:47:11 +0200 | [diff] [blame] | 15 | from pathlib import Path |
Carles Cufi | 31bdad5 | 2019-04-26 21:53:02 +0200 | [diff] [blame] | 16 | from west import log |
Carles Cufi | 98980c6 | 2019-06-05 16:04:29 +0200 | [diff] [blame] | 17 | from west.configuration import config |
| 18 | from west.util import escapes_directory |
Carles Cufi | 31bdad5 | 2019-04-26 21:53:02 +0200 | [diff] [blame] | 19 | |
| 20 | DEFAULT_BUILD_DIR = 'build' |
| 21 | '''Name of the default Zephyr build directory.''' |
| 22 | |
| 23 | DEFAULT_CMAKE_GENERATOR = 'Ninja' |
| 24 | '''Name of the default CMake generator.''' |
| 25 | |
Carles Cufi | 98980c6 | 2019-06-05 16:04:29 +0200 | [diff] [blame] | 26 | FIND_BUILD_DIR_DESCRIPTION = '''\ |
Martí Bolívar | 6e4c2b9 | 2020-06-25 12:58:58 -0700 | [diff] [blame] | 27 | If the build directory is not given, the default is {}/ unless the |
Martí Bolívar | eb95bed | 2020-02-10 06:40:29 -0800 | [diff] [blame] | 28 | build.dir-fmt configuration variable is set. The current directory is |
| 29 | checked after that. If either is a Zephyr build directory, it is used. |
Carles Cufi | 98980c6 | 2019-06-05 16:04:29 +0200 | [diff] [blame] | 30 | '''.format(DEFAULT_BUILD_DIR) |
Carles Cufi | 31bdad5 | 2019-04-26 21:53:02 +0200 | [diff] [blame] | 31 | |
Carles Cufi | 41f1f64 | 2019-06-17 11:47:11 +0200 | [diff] [blame] | 32 | def _resolve_build_dir(fmt, guess, cwd, **kwargs): |
Carles Cufi | 98980c6 | 2019-06-05 16:04:29 +0200 | [diff] [blame] | 33 | # Remove any None values, we do not want 'None' as a string |
| 34 | kwargs = {k: v for k, v in kwargs.items() if v is not None} |
| 35 | # Check if source_dir is below cwd first |
| 36 | source_dir = kwargs.get('source_dir') |
| 37 | if source_dir: |
| 38 | if escapes_directory(cwd, source_dir): |
| 39 | kwargs['source_dir'] = os.path.relpath(source_dir, cwd) |
| 40 | else: |
| 41 | # no meaningful relative path possible |
| 42 | kwargs['source_dir'] = '' |
Carles Cufi | 31bdad5 | 2019-04-26 21:53:02 +0200 | [diff] [blame] | 43 | |
Carles Cufi | 41f1f64 | 2019-06-17 11:47:11 +0200 | [diff] [blame] | 44 | try: |
| 45 | return fmt.format(**kwargs) |
| 46 | except KeyError: |
| 47 | if not guess: |
| 48 | return None |
Carles Cufi | 98980c6 | 2019-06-05 16:04:29 +0200 | [diff] [blame] | 49 | |
Carles Cufi | 41f1f64 | 2019-06-17 11:47:11 +0200 | [diff] [blame] | 50 | # Guess the build folder by iterating through all sub-folders from the |
| 51 | # root of the format string and trying to resolve. If resolving fails, |
| 52 | # proceed to iterate over subfolders only if there is a single folder |
| 53 | # present on each iteration. |
| 54 | parts = Path(fmt).parts |
| 55 | b = Path('.') |
| 56 | for p in parts: |
| 57 | # default to cwd in the first iteration |
| 58 | curr = b |
| 59 | b = b.joinpath(p) |
| 60 | try: |
| 61 | # if fmt is an absolute path, the first iteration will always |
| 62 | # resolve '/' |
| 63 | b = Path(str(b).format(**kwargs)) |
| 64 | except KeyError: |
| 65 | # Missing key, check sub-folders and match if a single one exists |
| 66 | while True: |
Carles Cufi | 4a50444 | 2019-09-06 15:18:41 +0200 | [diff] [blame] | 67 | if not curr.exists(): |
| 68 | return None |
Carles Cufi | 41f1f64 | 2019-06-17 11:47:11 +0200 | [diff] [blame] | 69 | dirs = [f for f in curr.iterdir() if f.is_dir()] |
| 70 | if len(dirs) != 1: |
| 71 | return None |
| 72 | curr = dirs[0] |
| 73 | if is_zephyr_build(str(curr)): |
| 74 | return str(curr) |
| 75 | return str(b) |
| 76 | |
| 77 | def find_build_dir(dir, guess=False, **kwargs): |
Carles Cufi | 31bdad5 | 2019-04-26 21:53:02 +0200 | [diff] [blame] | 78 | '''Heuristic for finding a build directory. |
| 79 | |
Carles Cufi | 98980c6 | 2019-06-05 16:04:29 +0200 | [diff] [blame] | 80 | The default build directory is computed by reading the build.dir-fmt |
| 81 | configuration option, defaulting to DEFAULT_BUILD_DIR if not set. It might |
| 82 | be None if the build.dir-fmt configuration option is set but cannot be |
| 83 | resolved. |
| 84 | If the given argument is truthy, it is returned. Otherwise, if |
| 85 | the default build folder is a build directory, it is returned. |
| 86 | Next, if the current working directory is a build directory, it is |
| 87 | returned. Finally, the default build directory is returned (may be None). |
| 88 | ''' |
| 89 | |
Carles Cufi | 31bdad5 | 2019-04-26 21:53:02 +0200 | [diff] [blame] | 90 | if dir: |
| 91 | build_dir = dir |
| 92 | else: |
| 93 | cwd = os.getcwd() |
Carles Cufi | 98980c6 | 2019-06-05 16:04:29 +0200 | [diff] [blame] | 94 | default = config.get('build', 'dir-fmt', fallback=DEFAULT_BUILD_DIR) |
Carles Cufi | 41f1f64 | 2019-06-17 11:47:11 +0200 | [diff] [blame] | 95 | default = _resolve_build_dir(default, guess, cwd, **kwargs) |
| 96 | log.dbg('config dir-fmt: {}'.format(default), level=log.VERBOSE_EXTREME) |
Carles Cufi | 98980c6 | 2019-06-05 16:04:29 +0200 | [diff] [blame] | 97 | if default and is_zephyr_build(default): |
Carles Cufi | 49c4b1c | 2019-06-03 12:43:38 +0200 | [diff] [blame] | 98 | build_dir = default |
| 99 | elif is_zephyr_build(cwd): |
Carles Cufi | 31bdad5 | 2019-04-26 21:53:02 +0200 | [diff] [blame] | 100 | build_dir = cwd |
| 101 | else: |
Carles Cufi | 98980c6 | 2019-06-05 16:04:29 +0200 | [diff] [blame] | 102 | build_dir = default |
| 103 | log.dbg('build dir: {}'.format(build_dir), level=log.VERBOSE_EXTREME) |
| 104 | if build_dir: |
| 105 | return os.path.abspath(build_dir) |
| 106 | else: |
| 107 | return None |
Carles Cufi | 31bdad5 | 2019-04-26 21:53:02 +0200 | [diff] [blame] | 108 | |
| 109 | def is_zephyr_build(path): |
| 110 | '''Return true if and only if `path` appears to be a valid Zephyr |
| 111 | build directory. |
| 112 | |
| 113 | "Valid" means the given path is a directory which contains a CMake |
Martí Bolívar | f752c5e | 2020-10-06 16:36:24 -0700 | [diff] [blame] | 114 | cache with a 'ZEPHYR_BASE' or 'ZEPHYR_TOOLCHAIN_VARIANT' variable. |
| 115 | |
| 116 | (The check for ZEPHYR_BASE introduced sometime after Zephyr 2.4 to |
| 117 | fix https://github.com/zephyrproject-rtos/zephyr/issues/28876; we |
| 118 | keep support for the second variable around for compatibility with |
| 119 | versions 2.2 and earlier, which didn't have ZEPHYR_BASE in cache. |
| 120 | The cached ZEPHYR_BASE was added in |
| 121 | https://github.com/zephyrproject-rtos/zephyr/pull/23054.) |
Carles Cufi | 31bdad5 | 2019-04-26 21:53:02 +0200 | [diff] [blame] | 122 | ''' |
| 123 | try: |
Jan Van Winkel | 4d975db | 2019-05-04 14:44:56 +0200 | [diff] [blame] | 124 | cache = zcmake.CMakeCache.from_build_dir(path) |
Carles Cufi | 31bdad5 | 2019-04-26 21:53:02 +0200 | [diff] [blame] | 125 | except FileNotFoundError: |
| 126 | cache = {} |
| 127 | |
Martí Bolívar | f752c5e | 2020-10-06 16:36:24 -0700 | [diff] [blame] | 128 | if 'ZEPHYR_BASE' in cache or 'ZEPHYR_TOOLCHAIN_VARIANT' in cache: |
| 129 | log.dbg(f'{path} is a zephyr build directory', |
Carles Cufi | 31bdad5 | 2019-04-26 21:53:02 +0200 | [diff] [blame] | 130 | level=log.VERBOSE_EXTREME) |
| 131 | return True |
Martí Bolívar | f752c5e | 2020-10-06 16:36:24 -0700 | [diff] [blame] | 132 | |
| 133 | log.dbg(f'{path} is NOT a valid zephyr build directory', |
| 134 | level=log.VERBOSE_EXTREME) |
| 135 | return False |