summaryrefslogtreecommitdiff
path: root/attoconf/lib/make.py
blob: f815cd0378060b1e256799b72d4fad30adb7f2ed (plain) (blame)
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
#   Copyright 2013-2014 Ben Longbons <b.r.longbons@gmail.com>
#
#   This file is part of attoconf.
#
#   attoconf 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, either version 3 of the License, or
#   (at your option) any later version.
#
#   attoconf 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 attoconf.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function, division, absolute_import

import os
import sys

from ..classy import ClassyProject
from ..version import full_version

blacklist = frozenset(''.join(chr(i) for i in range(0x20)) + '#$')
def validate(s):
    return s == s.strip() and not frozenset(s) & blacklist


class MakeHook(object):
    __slots__ = ('infile', 'outfile')
    def __init__(self, infile, outfile):
        self.infile = infile
        self.outfile = outfile

    def __call__(self, build):
        if self.outfile is None:
            # if there are multiple backends
            print('Skipping generation of a makefile')
            return
        with open(os.path.join(build.builddir, self.outfile), 'w') as out:
            print('Generating a makefile ...')
            out.write('# This part was generated by %s\n' % full_version)
            build.vars['SRC_DIR'] = build.relative_source()
            for var in build.project.order:
                if var is None:
                    out.write('\n')
                    continue
                val = build.vars[var]
                out.write('%s = %s\n' % (var, val))
            if self.infile is not None:
                out.write('# The rest was copied from %s\n' % self.infile)
                infile = os.path.join(build.project.srcdir, self.infile)
                with open(infile) as in_:
                    for line in in_:
                        assert line.endswith('\n')
                        out.write(line)


class Make(ClassyProject):
    ''' Post hook to generate a Makefile from Makefile.in
    '''
    __slots__ = ()
    _merge_slots_ = ('make_in', 'make_out')

    # compatibility with attoconf < 0.7
    def __init__(self, srcdir,
            make_infile='Makefile.in',
            make_outfile='Makefile',
            **kwargs):
        super(Make, self).__init__(srcdir=srcdir, **kwargs)
        self.set_make_infile(make_infile)
        self.set_make_outfile(make_outfile) # relative to build dir

    def set_make_infile(self, ipath):
        self.make_in = ipath

    def set_make_outfile(self, opath):
        self.make_out = opath

    def post(self):
        super(Make, self).post()
        if 'SRC_DIR' in self.order:
            sys.exit('ERROR: Incompatible generator hooks!')
        self.order.insert(0, 'SRC_DIR')
        self.checks.append(MakeHook(self.make_in, self.make_out))