summaryrefslogtreecommitdiff
path: root/morphlib/buildsystem.py
blob: a048d8818699ff45bd9825781196eef330f9cd44 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Copyright (C) 2012  Codethink Limited
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
# 
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import os


class BuildSystem(object):

    '''An abstraction of an upstream build system.
    
    Some build systems are well known: autotools, for example.
    Others are purely manual: there's a set of commands to run that
    are specific for that project, and (almost) no other project uses them.
    The Linux kernel would be an example of that.
    
    This class provides an abstraction for these, including a method
    to autodetect well known build systems.
    
    '''
    
    def __init__(self):
        self.configure_commands = []
        self.build_commands = []
        self.test_commands = []
        self.install_commands = []
        
    def __getitem__(self, key):
        key = '_'.join(key.split('-'))
        return getattr(self, key)
        
    def get_morphology_text(self, name):
        '''Return the text of an autodetected chunk morphology.'''
        
        return '''
            {
                "name": "%(name)s",
                "kind": "chunk",
                "build-system": "%(bs)s"
            }
        ''' % {
            'name': name,
            'bs': self.name,
        }
    
    def used_by_project(self, srcdir):
        '''Does project at ``srcdir`` use this build system?'''
        raise NotImplementedError() # pragma: no cover
        

class ManualBuildSystem(BuildSystem):

    '''A manual build system where the morphology must specify all commands.'''

    name = 'manual'
    
    def used_by_project(self, srcdir):
        return False


class DummyBuildSystem(BuildSystem):

    '''A dummy build system, useful for debugging morphologies.'''

    name = 'dummy'
    
    def __init__(self):
        self.configure_commands = ['echo dummy configure']
        self.build_commands = ['echo dummy build']
        self.test_commands = ['echo dummy test']
        self.install_commands = ['echo dummy install']

    def used_by_project(self, srcdir):
        return False


class AutotoolsBuildSystem(BuildSystem):

    '''The automake/autoconf/libtool holy trinity.'''

    name = 'autotools'
    
    def __init__(self):
        self.configure_commands = [
            'if [ -e autogen.sh ]; then ./autogen.sh; ' +
            'elif [ ! -e ./configure ]; then autoreconf -ivf; fi',
            './configure --prefix="$PREFIX"',
        ]
        self.build_commands = [
            'make',
        ]
        self.test_commands = [
        ]
        self.install_commands = [
            'make DESTDIR="$DESTDIR" install',
        ]

    def used_by_project(self, srcdir):
        indicators = [
            'autogen.sh',
            'configure.ac',
            'configure.in',
            'configure.in.in',
        ]
        
        return any(os.path.exists(os.path.join(srcdir, x))
                   for x in indicators)


build_systems = [
    ManualBuildSystem(),
    AutotoolsBuildSystem(),
    DummyBuildSystem(),
]
    

def detect_build_system(srcdir):
    '''Automatically detect the build system, if possible.
    
    If the build system cannot be detected automatically, return None.
    
    '''
    
    for bs in build_systems:
        if bs.used_by_project(srcdir):
            return bs
    return None


def lookup_build_system(name):
    '''Return build system that corresponds to the name.
    
    If the name does not match any build system, raise ``KeyError``.
    
    '''
    
    for bs in build_systems:
        if bs.name == name:
            return bs
    raise KeyError('Unknown build system: %s' % name)