Skip to content

Commit

Permalink
Merge pull request #347 from kyrofa/backport/cmake_find_in_stage
Browse files Browse the repository at this point in the history
Backport to 1.x: CMake plugin: Automatically find staged libs and files.
  • Loading branch information
Kyle Fazzari committed Feb 29, 2016
2 parents e8db13a + 449eed3 commit c9276c0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
21 changes: 19 additions & 2 deletions snapcraft/plugins/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import shutil

import snapcraft.plugins.make
from snapcraft import common


class CMakePlugin(snapcraft.plugins.make.MakePlugin):
Expand Down Expand Up @@ -69,5 +70,21 @@ def build(self):
sourcedir = self.sourcedir

self.run(['cmake', sourcedir, '-DCMAKE_INSTALL_PREFIX='] +
self.options.configflags)
self.run(['make', 'install', 'DESTDIR=' + self.installdir])
self.options.configflags, env=self._build_environment())
self.run(['make', 'install', 'DESTDIR=' + self.installdir],
env=self._build_environment())

def _build_environment(self):
env = os.environ.copy()
env['CMAKE_PREFIX_PATH'] = '$CMAKE_PREFIX_PATH:{}'.format(
common.get_stagedir())
env['CMAKE_INCLUDE_PATH'] = '$CMAKE_INCLUDE_PATH:' + ':'.join(
['{0}/include', '{0}/usr/include', '{0}/include/{1}',
'{0}/usr/include/{1}']).format(common.get_stagedir(),
common.get_arch_triplet())
env['CMAKE_LIBRARY_PATH'] = '$CMAKE_LIBRARY_PATH:' + ':'.join(
['{0}/lib', '{0}/usr/lib', '{0}/lib/{1}',
'{0}/usr/lib/{1}']).format(common.get_stagedir(),
common.get_arch_triplet())

return env
48 changes: 43 additions & 5 deletions snapcraft/tests/test_plugin_cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

from unittest import mock

from snapcraft import tests
from snapcraft.plugins import cmake
from snapcraft import (
common,
tests,
)


class CMakeTestCase(tests.TestCase):
Expand All @@ -46,10 +49,10 @@ class Options:

self.run_mock.assert_has_calls([
mock.call(['cmake', plugin.sourcedir, '-DCMAKE_INSTALL_PREFIX='],
cwd=plugin.builddir),
cwd=plugin.builddir, env=mock.ANY),
mock.call(['make', 'install',
'DESTDIR={}'.format(plugin.installdir)],
cwd=plugin.builddir)])
cwd=plugin.builddir, env=mock.ANY)])

def test_build_referencing_sourcedir_with_subdir(self):
class Options:
Expand All @@ -64,7 +67,42 @@ class Options:
plugin.sourcedir, plugin.options.source_subdir)
self.run_mock.assert_has_calls([
mock.call(['cmake', sourcedir, '-DCMAKE_INSTALL_PREFIX='],
cwd=plugin.builddir),
cwd=plugin.builddir, env=mock.ANY),
mock.call(['make', 'install',
'DESTDIR={}'.format(plugin.installdir)],
cwd=plugin.builddir)])
cwd=plugin.builddir, env=mock.ANY)])

def test_build_environment(self):
class Options:
configflags = []

plugin = cmake.CMakePlugin('test-part', Options())
os.makedirs(plugin.builddir)
plugin.build()

expected = {}

expected['CMAKE_PREFIX_PATH'] = '$CMAKE_PREFIX_PATH:{}'.format(
common.get_stagedir())
expected['CMAKE_INCLUDE_PATH'] = '$CMAKE_INCLUDE_PATH:' + ':'.join(
['{0}/include', '{0}/usr/include', '{0}/include/{1}',
'{0}/usr/include/{1}']).format(common.get_stagedir(),
common.get_arch_triplet())
expected['CMAKE_LIBRARY_PATH'] = '$CMAKE_LIBRARY_PATH:' + ':'.join(
['{0}/lib', '{0}/usr/lib', '{0}/lib/{1}',
'{0}/usr/lib/{1}']).format(common.get_stagedir(),
common.get_arch_triplet())

self.assertEqual(2, self.run_mock.call_count)
for call_args in self.run_mock.call_args_list:
environment = call_args[1]['env']
for variable, value in expected.items():
self.assertTrue(
variable in environment,
'Expected variable "{}" to be in environment'.format(
variable))

self.assertEqual(
environment[variable], value,
'Expected ${}={}, but it was {}'.format(
variable, value, environment[variable]))

0 comments on commit c9276c0

Please sign in to comment.