summaryrefslogtreecommitdiff
path: root/gen_makefile.py
blob: 09407392c78305d3b909163cf49a156a292a22a9 (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
#!/usr/bin/env python

## Copyright (c) 2015 Olivier Gayot <duskcoder@gmail.com>
## 
## 
## Permission is hereby granted, free of charge, to any person obtaining a
## copy of this software and associated documentation files (the "Software"), to
## deal in the Software without restriction, including without limitation the
## rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
## sell copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
## 
## The above copyright notice and this permission notice shall be included in
## all copies or substantial portions of the Software.
## 
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.


class makefile_generator():
    def __init__(self):
        self._binary = 'a.out'
        self.phony = []

    def append_variable(self, name, value=''):
        print('%s%.*s= %s' % (name, 16 - len(name), '               ', value))

    def append_separator(self):
        print('')

    def append_rule(self, target, dep='', cmds={}):
        print('%s: %s' % (target, dep))

        for cmd in cmds:
            print('\t%s' % (cmd))

        self.append_separator()

    def append_static_rule(self, target, dep='', cmds={}):
        self.append_rule(target, dep, cmds)
        self.phony.append(target)

    def gen(self):
        # mandatory input
        self.append_variable('NAME', self._binary)
        self.append_variable('SRC', '$(wildcard *.%s)' % (self._extension))
        self.append_separator()

        # optional input
        self.append_variable('CPPFLAGS')
        self.append_variable('LDFLAGS')
        self.append_variable(self._compiler_flags_var, self._compiler_flags)
        self.append_separator()
        self.append_variable(self._compiler_var, self._compiler)
        self.append_separator()

        # make use of implicit rules
        self.append_variable('OBJ', '$(SRC:.%s=.o)' % (self._extension))
        self.append_variable('DEP', '$(SRC:.%s=.d)' % (self._extension))
        self.append_separator()

        # first rule to be evaluated
        self.append_static_rule('all', '$(NAME)')

        # linkage
        self.append_rule('$(NAME)', '$(OBJ)', {
            '$(%s) -o "$@" $^ $(LDFLAGS)' % (self._compiler_var)
            })

        # include dependencies
        print('-include $(DEP)')
        self.append_separator()

        # rule to generate dependencies 
        self.append_rule('%.d', '%%.%s' % (self._extension), {
            '$(%s) -MM $(CPPFLAGS) $(%s) $< -MF $@ -MT "$*.o $@"' %
            (self._compiler_var, self._compiler_flags_var),
            })

        self.append_static_rule('clean', cmds={
            '$(RM) $(OBJ)',
            '$(RM) $(NAME)',
            })
        self.append_static_rule('mrproper', 'clean', {
            '$(RM) $(DEP)',
            })
        self.append_static_rule('distclean', 'mrproper', {
            '$(RM) $(addsuffix ~,$(SRC))'
            })

        # phony rule
        self.append_rule('.PHONY', ' '.join(self.phony))

class makefile_generator_c(makefile_generator):
    def __init__(self):
        self._compiler_var = 'CC'
        self._compiler_flags_var = 'CFLAGS'
        self._compiler = 'gcc'
        self._compiler_flags = '-W -Wall -Wextra -std=gnu99'
        self._extension = 'c'

        super().__init__()

class makefile_generator_cpp(makefile_generator):
    def __init__(self):
        self._compiler_var = 'CXX'
        self._compiler_flags_var = 'CXXFLAGS'
        self._compiler = 'g++'
        self._compiler_flags = '-W -Wall -Wextra -Weffc++'
        self._extension = 'cpp'

        super().__init__()

def main():
    generator = makefile_generator_c()

    generator.gen()

if __name__ == '__main__':
    main()