Skip to content

Commit

Permalink
kernel plugin: kernel targets depending on debarch (#689)
Browse files Browse the repository at this point in the history
This allows using the same snapcraft.yaml to build kernels
for different arches as they have different image targets.

LP: #1604801

Signed-off-by: Sergio Schvezov <sergio.schvezov@ubuntu.com>
  • Loading branch information
sergiusens committed Aug 5, 2016
1 parent 542ae96 commit f0a19eb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
47 changes: 34 additions & 13 deletions snapcraft/plugins/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
The following kernel specific options are provided by this plugin:
- kernel-image-target:
(string; default: bzImage)
the kernel image make target to build; maps to make target.
(yaml object or string; default: bzImage)
the default target is bzImage and can be set to any specific
target.
For more complex cases where one would want to use
the same snapcraft.yaml to target multiple architectures a
yaml object can be used. This yaml object would be a map of
debian architecture and kernel image build targets.
- kernel-initrd-modules:
(array of string)
Expand Down Expand Up @@ -77,7 +82,10 @@ def schema(cls):
schema = super().schema()

schema['properties']['kernel-image-target'] = {
'type': 'string',
'oneOf': [
{'type': 'string'},
{'type': 'object'},
],
'default': 'bzImage',
}

Expand Down Expand Up @@ -138,14 +146,7 @@ def compression_cmd(self):
def __init__(self, name, options, project):
super().__init__(name, options, project)

self.make_targets = [self.options.kernel_image_target, 'modules']
self.dtbs = ['{}.dtb'.format(i)
for i in self.options.kernel_device_trees]
if self.dtbs:
self.make_targets.extend(self.dtbs)
self.make_install_targets = [
'modules_install', 'INSTALL_MOD_PATH={}'.format(self.installdir)]
self.make_install_targets.extend(self._get_fw_install_targets())
self._set_kernel_targets()

self.os_snap = os.path.join(self.sourcedir, 'os.snap')
self.kernel_release = ''
Expand All @@ -157,6 +158,26 @@ def enable_cross_compilation(self):
self.project.kernel_arch))
self.make_cmd.append('CROSS_COMPILE={}'.format(
self.project.cross_compiler_prefix))
# by enabling cross compilation, the kernel_arch and deb_arch
# from the project options have effectively changed so we reset
# kernel targets.
self._set_kernel_targets()

def _set_kernel_targets(self):
if isinstance(self.options.kernel_image_target, str):
self.kernel_image_target = self.options.kernel_image_target
elif self.project.deb_arch in self.options.kernel_image_target:
self.kernel_image_target = \
self.options.kernel_image_target[self.project.deb_arch]

self.make_targets = [self.kernel_image_target, 'modules']
self.dtbs = ['{}.dtb'.format(i)
for i in self.options.kernel_device_trees]
if self.dtbs:
self.make_targets.extend(self.dtbs)
self.make_install_targets = [
'modules_install', 'INSTALL_MOD_PATH={}'.format(self.installdir)]
self.make_install_targets.extend(self._get_fw_install_targets())

def _get_fw_install_targets(self):
if not self.options.kernel_with_firmware:
Expand Down Expand Up @@ -279,9 +300,9 @@ def _get_build_arch_dir(self):

def _copy_vmlinuz(self):
kernel = '{}-{}'.format(
self.options.kernel_image_target, self.kernel_release)
self.kernel_image_target, self.kernel_release)
src = os.path.join(self._get_build_arch_dir(),
self.options.kernel_image_target)
self.kernel_image_target)
dst = os.path.join(self.installdir, kernel)
if not os.path.exists(src):
raise ValueError(
Expand Down
36 changes: 35 additions & 1 deletion snapcraft/tests/test_plugin_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def test_schema(self):
self.assertTrue(properties[prop]['uniqueItems'])

self.assertEqual(
properties['kernel-image-target']['type'], 'string')
properties['kernel-image-target']['oneOf'],
[{'type': 'string'}, {'type': 'object'}])
self.assertEqual(
properties['kernel-image-target']['default'], 'bzImage')

Expand Down Expand Up @@ -820,6 +821,39 @@ def test_enable_cross_compilation(self):
plugin.make_cmd,
['make', '-j2', 'ARCH=arm64', 'CROSS_COMPILE=aarch64-linux-gnu-'])

def test_kernel_image_target_as_map(self):
self.options.kernel_image_target = {'arm64': 'Image'}
project_options = snapcraft.ProjectOptions(target_deb_arch='arm64')
plugin = kernel.KernelPlugin('test-part', self.options,
project_options)

self.assertEqual(plugin.make_targets, ['Image', 'modules'])

def test_kernel_image_target_as_string(self):
self.options.kernel_image_target = 'Image'
project_options = snapcraft.ProjectOptions(target_deb_arch='arm64')
plugin = kernel.KernelPlugin('test-part', self.options,
project_options)

self.assertEqual(plugin.make_targets, ['Image', 'modules'])

def test_kernel_image_target_non_existent(self):
class Options:
build_parameters = []
kconfigfile = None
kdefconfig = []
kconfigs = []
kernel_with_firmware = True
kernel_initrd_modules = []
kernel_initrd_firmware = []
kernel_device_trees = []
kernel_initrd_compression = 'gz'
project_options = snapcraft.ProjectOptions(target_deb_arch='arm64')
plugin = kernel.KernelPlugin('test-part', self.options,
project_options)

self.assertEqual(plugin.make_targets, ['bzImage', 'modules'])

@mock.patch.object(storeapi.StoreClient, 'download')
def test_pull(self, download_mock):
plugin = kernel.KernelPlugin('test-part', self.options,
Expand Down

0 comments on commit f0a19eb

Please sign in to comment.