Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | # Recipe creation tool - kernel support plugin |
| 2 | # |
| 3 | # Copyright (C) 2016 Intel Corporation |
| 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 6 | # |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 7 | |
| 8 | import re |
| 9 | import logging |
| 10 | from recipetool.create import RecipeHandler, read_pkgconfig_provides, validate_pv |
| 11 | |
| 12 | logger = logging.getLogger('recipetool') |
| 13 | |
| 14 | tinfoil = None |
| 15 | |
| 16 | def tinfoil_init(instance): |
| 17 | global tinfoil |
| 18 | tinfoil = instance |
| 19 | |
| 20 | |
| 21 | class KernelRecipeHandler(RecipeHandler): |
| 22 | def process(self, srctree, classes, lines_before, lines_after, handled, extravalues): |
| 23 | import bb.process |
| 24 | if 'buildsystem' in handled: |
| 25 | return False |
| 26 | |
| 27 | for tell in ['arch', 'firmware', 'Kbuild', 'Kconfig']: |
| 28 | if not os.path.exists(os.path.join(srctree, tell)): |
| 29 | return False |
| 30 | |
| 31 | handled.append('buildsystem') |
| 32 | del lines_after[:] |
| 33 | del classes[:] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 34 | template = os.path.join(tinfoil.config_data.getVar('COREBASE'), 'meta-skeleton', 'recipes-kernel', 'linux', 'linux-yocto-custom.bb') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 35 | def handle_var(varname, origvalue, op, newlines): |
| 36 | if varname in ['SRCREV', 'SRCREV_machine']: |
| 37 | while newlines[-1].startswith('#'): |
| 38 | del newlines[-1] |
| 39 | try: |
| 40 | stdout, _ = bb.process.run('git rev-parse HEAD', cwd=srctree, shell=True) |
| 41 | except bb.process.ExecutionError as e: |
| 42 | stdout = None |
| 43 | if stdout: |
| 44 | return stdout.strip(), op, 0, True |
| 45 | elif varname == 'LINUX_VERSION': |
| 46 | makefile = os.path.join(srctree, 'Makefile') |
| 47 | if os.path.exists(makefile): |
| 48 | kversion = -1 |
| 49 | kpatchlevel = -1 |
| 50 | ksublevel = -1 |
| 51 | kextraversion = '' |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 52 | with open(makefile, 'r', errors='surrogateescape') as f: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 53 | for i, line in enumerate(f): |
| 54 | if i > 10: |
| 55 | break |
| 56 | if line.startswith('VERSION ='): |
| 57 | kversion = int(line.split('=')[1].strip()) |
| 58 | elif line.startswith('PATCHLEVEL ='): |
| 59 | kpatchlevel = int(line.split('=')[1].strip()) |
| 60 | elif line.startswith('SUBLEVEL ='): |
| 61 | ksublevel = int(line.split('=')[1].strip()) |
| 62 | elif line.startswith('EXTRAVERSION ='): |
| 63 | kextraversion = line.split('=')[1].strip() |
| 64 | version = '' |
| 65 | if kversion > -1 and kpatchlevel > -1: |
| 66 | version = '%d.%d' % (kversion, kpatchlevel) |
| 67 | if ksublevel > -1: |
| 68 | version += '.%d' % ksublevel |
| 69 | version += kextraversion |
| 70 | if version: |
| 71 | return version, op, 0, True |
| 72 | elif varname == 'SRC_URI': |
| 73 | while newlines[-1].startswith('#'): |
| 74 | del newlines[-1] |
| 75 | elif varname == 'COMPATIBLE_MACHINE': |
| 76 | while newlines[-1].startswith('#'): |
| 77 | del newlines[-1] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 78 | machine = tinfoil.config_data.getVar('MACHINE') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 79 | return machine, op, 0, True |
| 80 | return origvalue, op, 0, True |
| 81 | with open(template, 'r') as f: |
| 82 | varlist = ['SRCREV', 'SRCREV_machine', 'SRC_URI', 'LINUX_VERSION', 'COMPATIBLE_MACHINE'] |
| 83 | (_, newlines) = bb.utils.edit_metadata(f, varlist, handle_var) |
| 84 | lines_before[:] = [line.rstrip('\n') for line in newlines] |
| 85 | |
| 86 | return True |
| 87 | |
| 88 | def register_recipe_handlers(handlers): |
| 89 | handlers.append((KernelRecipeHandler(), 100)) |