summaryrefslogtreecommitdiffstats
path: root/snap/plugins/x-gyp-cmake.py
blob: 94a5ba1a890472573bbd9e0ca369befd0dfce840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Author: Marco Trevisan <marco@ubuntu.com>
# Copyright (C) 2017-2018 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# 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

from snapcraft.plugins import cmake


class GypCMakePlugin(cmake.CMakePlugin):
    """A basic plugin for snapcraft that generates CMake files from gyp"""

    @classmethod
    def schema(cls):
        schema = super().schema()

        schema['properties']['gyp-file'] = {
            'type': 'string'
        }

        schema['properties']['build-type'] = {
            'type': 'string',
            'default': 'Release',
            'enum': ['Debug', 'Release'],
        }

        schema['properties']['environment'] = {
            'type': 'array',
            'minitems': 0,
            'uniqueItems': True,
            'items': {
                'type': 'object',
                'minitems': 0,
                'uniqueItems': True,
                'items': {
                    'type': 'string',
                },
            },
            'default': [],
        }

        schema['required'].append('gyp-file')

        schema['build-properties'].extend([
            'build-type',
            'gyp-file',
        ])

        return schema

    def __init__(self, name, options, project):
        super().__init__(name, options, project)
        self.build_packages.extend([
            'binutils',
            'python',
        ])
        self.builddir = os.path.join(
            self.build_basedir, 'out', self.options.build_type)

    def build(self):
        env = self._build_environment()
        gyp_path = os.path.join(self.sourcedir, os.path.dirname(self.options.gyp_file))

        for environ in self.options.environment:
            [env_name] = list(environ)
            env[env_name] = str(environ[env_name])

        if not os.path.exists(os.path.join(self.builddir, 'CMakeLists.txt')):
            gyp_command = ['gyp'] + self.options.configflags + ['--format=cmake']
            gyp_command.append('--generator-output={}'.format(self.build_basedir))
            gyp_command.append(os.path.basename(self.options.gyp_file))
            self.run(gyp_command, cwd=gyp_path)

        if not os.path.exists(os.path.join(self.builddir, 'Makefile')):
            self.run(['cmake', '.'], env=env)

        self.make(env=env)

        if self.options.artifacts and self.options.build_type == 'Release':
            for artifact in self.options.artifacts:
                dest = os.path.join(self.installdir, artifact)
                if os.path.isfile(dest):
                    mime_type = self.run_output(
                        'file --mime-type -b {}'.format(dest).split())
                    if 'application/x-executable' in mime_type:
                        self.run(['strip', dest])