Skip to content

Commit

Permalink
waf plugin: enable cross-compilation support (canonical#1397)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalikiana authored and Christian committed Aug 3, 2017
1 parent 2d43aa7 commit edd869a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 2 deletions.
14 changes: 14 additions & 0 deletions integration_tests/plugins/test_waf_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,24 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os

import snapcraft
import integration_tests
from snapcraft.tests.matchers import HasArchitecture


class WafPluginTestCase(integration_tests.TestCase):

def test_build_waf_plugin(self):
self.run_snapcraft('build', 'waf-with-configflags')

def test_cross_compiling(self):
if snapcraft.ProjectOptions().deb_arch != 'amd64':
self.skipTest('The test only handles amd64 to arm64')

self.run_snapcraft(['build', '--target-arch=arm64'],
'waf-with-configflags')
binary = os.path.join(self.parts_dir, 'waf-project',
'install', 'usr', 'local', 'bin', 'myprogram')
self.assertThat(binary, HasArchitecture('aarch64'))
3 changes: 3 additions & 0 deletions integration_tests/snaps/waf-with-configflags/program/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#include "abc.h"

#include <stdio.h>

int main() {
printf("Hello Waf!\n");
return 0;
}
6 changes: 5 additions & 1 deletion integration_tests/snaps/waf-with-configflags/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ description: |
This is a basic waf snap with configflags.
confinement: strict

build-packages: [gcc, libc6-dev]
apps:
waf-with-configflags:
command: usr/local/bin/myprogram

build-packages: [gcc]
# so far there is no packaged waf, projects usually carry it in their git
# for versioning, the plugin as well as this integration test reflect this

Expand Down
9 changes: 9 additions & 0 deletions manual-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@
5. Run `autotools-hello`.


# Test cross-compilation with Waf

1. Go to integration_tests/snaps/waf-with-configflags.
2. Run `snapcraft snap --target-arch=armhf`.
3. Copy the snap to a Raspberry Pi.
4. Install the snap.
5. Run `waf-with-configflags`.


# Test the PC kernel.

1. Get the PC kernel source:
Expand Down
16 changes: 15 additions & 1 deletion snapcraft/plugins/waf.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def schema(cls):
def __init__(self, name, options, project):
super().__init__(name, options, project)
self.build_packages.extend([
'python-dev',
'python-dev:native',
])

@classmethod
Expand All @@ -65,6 +65,20 @@ def get_build_properties(cls):
# change in the YAML Snapcraft will consider the build step dirty.
return ['configflags']

def env(self, root):
env = super().env(root)
if self.project.is_cross_compiling:
env.extend([
'CC={}-gcc'.format(self.project.arch_triplet),
'CXX={}-g++'.format(self.project.arch_triplet),
])
return env

def enable_cross_compilation(self):
# Let snapcraft know that this plugin can cross-compile
# If the method isn't implemented an exception is raised
pass

def build(self):
super().build()
self.run(['./waf', 'distclean'])
Expand Down
45 changes: 45 additions & 0 deletions snapcraft/tests/plugins/test_waf.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,48 @@ def test_build_with_destdir(self, run_mock):
mock.call(['./waf', 'install',
'--destdir={}'.format(plugin.installdir)])
])


class WafCrossCompilePluginTestCase(tests.TestCase):

scenarios = [
('armv7l', dict(deb_arch='armhf')),
('aarch64', dict(deb_arch='arm64')),
('i386', dict(deb_arch='i386')),
('x86_64', dict(deb_arch='amd64')),
('ppc64le', dict(deb_arch='ppc64el')),
]

def setUp(self):
super().setUp()

class Options:
configflags = []

self.options = Options()
self.project_options = snapcraft.ProjectOptions(
target_deb_arch=self.deb_arch)

patcher = mock.patch('snapcraft.internal.common.run')
self.run_mock = patcher.start()
self.addCleanup(patcher.stop)

patcher = mock.patch('snapcraft.ProjectOptions.is_cross_compiling')
patcher.start()
self.addCleanup(patcher.stop)

patcher = mock.patch.dict(os.environ, {})
self.env_mock = patcher.start()
self.addCleanup(patcher.stop)

def test_cross_compile(self):
plugin = waf.WafPlugin('test-part', self.options,
self.project_options)
# This shouldn't raise an exception
plugin.enable_cross_compilation()

env = plugin.env(plugin.sourcedir)
self.assertIn('CC={}-gcc'.format(
self.project_options.arch_triplet), env)
self.assertIn('CXX={}-g++'.format(
self.project_options.arch_triplet), env)

0 comments on commit edd869a

Please sign in to comment.