blob: ca4996c7ace7a97c541f2f075ff8f5c4b73ed161 [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#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License version 2 as
7# published by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18import re
19import logging
20from recipetool.create import RecipeHandler, read_pkgconfig_provides, validate_pv
21
22logger = logging.getLogger('recipetool')
23
24tinfoil = None
25
26def tinfoil_init(instance):
27 global tinfoil
28 tinfoil = instance
29
30
31class KernelRecipeHandler(RecipeHandler):
32 def process(self, srctree, classes, lines_before, lines_after, handled, extravalues):
33 import bb.process
34 if 'buildsystem' in handled:
35 return False
36
37 for tell in ['arch', 'firmware', 'Kbuild', 'Kconfig']:
38 if not os.path.exists(os.path.join(srctree, tell)):
39 return False
40
41 handled.append('buildsystem')
42 del lines_after[:]
43 del classes[:]
Brad Bishop6e60e8b2018-02-01 10:27:11 -050044 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 -050045 def handle_var(varname, origvalue, op, newlines):
46 if varname in ['SRCREV', 'SRCREV_machine']:
47 while newlines[-1].startswith('#'):
48 del newlines[-1]
49 try:
50 stdout, _ = bb.process.run('git rev-parse HEAD', cwd=srctree, shell=True)
51 except bb.process.ExecutionError as e:
52 stdout = None
53 if stdout:
54 return stdout.strip(), op, 0, True
55 elif varname == 'LINUX_VERSION':
56 makefile = os.path.join(srctree, 'Makefile')
57 if os.path.exists(makefile):
58 kversion = -1
59 kpatchlevel = -1
60 ksublevel = -1
61 kextraversion = ''
Patrick Williamsc0f7c042017-02-23 20:41:17 -060062 with open(makefile, 'r', errors='surrogateescape') as f:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050063 for i, line in enumerate(f):
64 if i > 10:
65 break
66 if line.startswith('VERSION ='):
67 kversion = int(line.split('=')[1].strip())
68 elif line.startswith('PATCHLEVEL ='):
69 kpatchlevel = int(line.split('=')[1].strip())
70 elif line.startswith('SUBLEVEL ='):
71 ksublevel = int(line.split('=')[1].strip())
72 elif line.startswith('EXTRAVERSION ='):
73 kextraversion = line.split('=')[1].strip()
74 version = ''
75 if kversion > -1 and kpatchlevel > -1:
76 version = '%d.%d' % (kversion, kpatchlevel)
77 if ksublevel > -1:
78 version += '.%d' % ksublevel
79 version += kextraversion
80 if version:
81 return version, op, 0, True
82 elif varname == 'SRC_URI':
83 while newlines[-1].startswith('#'):
84 del newlines[-1]
85 elif varname == 'COMPATIBLE_MACHINE':
86 while newlines[-1].startswith('#'):
87 del newlines[-1]
Brad Bishop6e60e8b2018-02-01 10:27:11 -050088 machine = tinfoil.config_data.getVar('MACHINE')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050089 return machine, op, 0, True
90 return origvalue, op, 0, True
91 with open(template, 'r') as f:
92 varlist = ['SRCREV', 'SRCREV_machine', 'SRC_URI', 'LINUX_VERSION', 'COMPATIBLE_MACHINE']
93 (_, newlines) = bb.utils.edit_metadata(f, varlist, handle_var)
94 lines_before[:] = [line.rstrip('\n') for line in newlines]
95
96 return True
97
98def register_recipe_handlers(handlers):
99 handlers.append((KernelRecipeHandler(), 100))