Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/devbot/build.py
blob: 6380c6e44cadd31a6d347d630aca507669a7436f (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import fnmatch
import re
import os
import multiprocessing
import shutil
import subprocess
from distutils.sysconfig import parse_makefile

from devbot import command
from devbot import config
from devbot import state
from devbot import utils
from devbot import release
from devbot import git

_builders = {}
_distributors = {}


def build_one(module_name):
    for module in config.load_modules():
        if module.name == module_name:
            return _build_module(module)

    return False


def pull_one(module_name):
    for module in config.load_modules():
        if module.name == module_name:
            return _pull_module(module)

    return False


def pull(lazy=False):
    to_pull = []
    for module in config.load_modules():
        git_module = git.get_module(module)
        if not lazy or not os.path.exists(git_module.local):
            to_pull.append(module)

    if to_pull:
        print "\n= Pulling =\n"

    for module in to_pull:
        if not _pull_module(module):
            return False

    return True


def build(full=False):
    if full or state.full_build_is_required():
        state.clean(build_only=True)
        clean()

    state.full_build_touch()

    pull(lazy=True)

    to_build = []
    for module in config.load_modules():
        if not state.built_module_is_unchanged(module):
            to_build.append(module)

    if not to_build:
        return True

    print "\n= Building =\n"

    _ccache_reset()

    for module in to_build:
        if not _build_module(module):
            return False

    _ccache_print_stats()

    return True


def distribute():
    for module in config.load_modules():
        if module.distribute:
            if not _distribute_module(module):
                break

    return True


def clean():
    print "\n= Clean =\n"

    print "* Emptying install directory"
    _empty_dir(config.install_dir)

    print "* Emptying build directory"
    _empty_dir(config.get_build_dir())

    for module in config.load_modules():
        if not module.out_of_source:
            if git.get_module(module).clean():
                print "* Cleaning %s" % module.name


def _ccache_reset():
    subprocess.check_call(["ccache", "-z"], stdout=utils.devnull)


def _ccache_print_stats():
    print "\n= ccache statistics =\n"
    subprocess.check_call(["ccache", "-s"])


def _unlink_libtool_files():
    def func(arg, dirname, fnames):
        for fname in fnmatch.filter(fnames, "*.la"):
            os.unlink(os.path.join(dirname, fname))

    os.path.walk(config.lib_dir, func, None)


def _pull_module(module):
    print "* Pulling %s" % module.name

    git_module = git.get_module(module)

    try:
        git_module.update()
    except subprocess.CalledProcessError:
        return False

    return True


def _eval_option(option):
    return eval(option, {"prefix": config.prefix_dir})


def _build_autotools(module, log):
    # Workaround for aclocal 1.11 (fixed in 1.12)
    aclocal_path = os.path.join(config.share_dir, "aclocal")
    utils.ensure_dir(aclocal_path)

    makefile_path = os.path.join(module.get_build_dir(), "Makefile")

    if not os.path.exists(makefile_path):
        configure = os.path.join(module.get_source_dir(), "autogen.sh")

        args = [configure,
                "--prefix", config.prefix_dir,
                "--libdir", config.lib_dir]
        args.extend(module.options)

        for option in module.options_evaluated:
            args.append(_eval_option(option))

        command.run(args, log)

    jobs = multiprocessing.cpu_count() * 2

    command.run(["make", "-j", "%d" % jobs], log)
    command.run(["make", "install"], log)

    _unlink_libtool_files()

_builders["autotools"] = _build_autotools


def _build_activity(module, log):
    setup = os.path.join(module.get_source_dir(), "setup.py")
    command.run([setup, "install", "--prefix", config.prefix_dir], log)

_builders["activity"] = _build_activity


def _distribute_autotools(module):
    makefile = parse_makefile("Makefile")
    filename = makefile["DIST_ARCHIVES"]
    version = makefile["VERSION"]

    git_module = git.get_module(module)

    version_revision = None
    description = git_module.describe()
    if description != "v%s" % version:
        match = re.match(r"(v[\d\.]+)", description)
        if match is None:
            print "No version tag was found"
            return False
        else:
            version_revision = match.groups()[0]

    if version_revision is not None:
        git_module.checkout(version_revision)

    command.run(["make", "distcheck"])

    result = False

    if not release.exists(module, filename):
        path = os.path.join(os.getcwd(), filename)
        if release.upload(module, path):
            annotation = git_module.get_annotation("v%s" % version)
            result = release.announce(module, filename, version, annotation)
    else:
        print "Release already uploaded"

    if version_revision is not None:
        git_module.checkout()

    return result

_distributors["autotools"] = _distribute_autotools


def _build_module(module, log=None):
    print "* Building %s" % module.name

    source_dir = module.get_source_dir()

    if not os.path.exists(source_dir):
        print "Source directory does not exist. Please pull the sources " \
              "before building."
        return False

    if module.out_of_source:
        build_dir = module.get_build_dir()

        if not os.path.exists(build_dir):
            os.mkdir(build_dir)

        os.chdir(build_dir)
    else:
        os.chdir(source_dir)

    try:
        build_system = module.get_build_system()
        if build_system is None:
            return False

        _builders[build_system](module, log)
    except subprocess.CalledProcessError:
        return False

    state.built_module_touch(module)

    return True


def _distribute_module(module, log=None):
    print "\n=== Distribute %s ===\n" % module.name

    build_dir = module.get_build_dir()

    if not os.path.exists(build_dir):
        print "Build directory does not exist. Please build before " \
              "distributing."
        return False

    os.chdir(build_dir)

    try:
        build_system = module.get_build_system()
        if build_system is None:
            return False

        _distributors[build_system](module)
    except subprocess.CalledProcessError:
        return False

    return True


def _empty_dir(dir_path):
    shutil.rmtree(dir_path, ignore_errors=True)
    os.mkdir(dir_path)