blob: 5740589a683d0876800d055207b46df356c77b0e [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001# Recipe creation tool - kernel support plugin
2#
3# Copyright (C) 2016 Intel Corporation
4#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05006#
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05007
8import re
9import logging
10from recipetool.create import RecipeHandler, read_pkgconfig_provides, validate_pv
11
12logger = logging.getLogger('recipetool')
13
14tinfoil = None
15
16def tinfoil_init(instance):
17 global tinfoil
18 tinfoil = instance
19
20
21class 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 Bishop6e60e8b2018-02-01 10:27:11 -050034 template = os.path.join(tinfoil.config_data.getVar('COREBASE'), 'meta-skeleton', 'recipes-kernel', 'linux', 'linux-yocto-custom.bb')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050035 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 Williamsc0f7c042017-02-23 20:41:17 -060052 with open(makefile, 'r', errors='surrogateescape') as f:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050053 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 Bishop6e60e8b2018-02-01 10:27:11 -050078 machine = tinfoil.config_data.getVar('MACHINE')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050079 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
88def register_recipe_handlers(handlers):
89 handlers.append((KernelRecipeHandler(), 100))