1# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
2# vim: set filetype=python:
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7# For more complex and repetitive things, we can create templates
8@template
9def check_compiler_flag(flag):
10    @depends(is_gcc)
11    def check(value):
12        if value:
13            return [flag]
14
15    set_config("CFLAGS", check)
16    return check
17
18
19check_compiler_flag("-Werror=foobar")
20
21# Normal functions can be used in @depends functions.
22def fortytwo():
23    return 42
24
25
26def twentyone():
27    yield 21
28
29
30@depends(is_gcc)
31def check(value):
32    if value:
33        return fortytwo()
34
35
36set_config("TEMPLATE_VALUE", check)
37
38
39@depends(is_gcc)
40def check(value):
41    if value:
42        for val in twentyone():
43            return val
44
45
46set_config("TEMPLATE_VALUE_2", check)
47
48# Normal functions can use @imports too to import modules.
49@imports("sys")
50def platform():
51    return sys.platform
52
53
54option("--enable-imports-in-template", help="Imports in template")
55
56
57@depends("--enable-imports-in-template")
58def check(value):
59    if value:
60        return platform()
61
62
63set_config("PLATFORM", check)
64
65
66@template
67def indirectly_define_option(*args, **kwargs):
68    option(*args, **kwargs)
69