summaryrefslogtreecommitdiff
path: root/attoconf/lib/make.py
blob: 7e2c9d36e88d0ec233536f54a2cb09cfdf35a4e9 (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
#   Copyright 2013 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

from ..classy import ClassyProject
from ..version import string as version_string

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' % version_string)
            build.vars['SRC_DIR'] = (build.relative_source(), 'special')
            for var in build.project.order:
                if var is None:
                    out.write('\n')
                    continue
                val, origin = build.vars[var]
                out.write('%s = %s # %s\n' % (var, val, origin))
            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__ = ()
    @classmethod
    def slots(cls):
        return super(Make, cls).slots() + ('make_in', 'make_out')

    def __init__(self, srcdir):
        super(Make, self).__init__(srcdir)
        self.set_make_infile('Makefile.in')
        self.set_make_outfile('Makefile') # 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()
        self.order.insert(0, 'SRC_DIR')
        self.checks.append(MakeHook(self.make_in, self.make_out))