~robert-ancell/bake/osx-stat

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
/*
 * Copyright (C) 2011-2012 Robert Ancell <robert.ancell@gmail.com>
 *
 * This program 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. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

public class MonoModule : BuildModule
{
    public override bool can_generate_program_rules (Program program) throws Error
    {
        return can_generate_rules (program);
    }

    public override void generate_program_rules (Program program) throws Error
    {
        var binary_name = generate_compile_rules (program);
        if (program.install)
            program.recipe.add_install_rule (binary_name, program.install_directory);
    }

    public override bool can_generate_library_rules (Library library) throws Error
    {
        return can_generate_rules (library);
    }

    public override void generate_library_rules (Library library) throws Error
    {
        var binary_name = generate_compile_rules (library);
        if (library.install)
            library.recipe.add_install_rule (binary_name, Path.build_filename (library.install_directory, "cli", library.recipe.project_name));
    }

    private bool can_generate_rules (Compilable compilable) throws Error
    {
        var count = 0;
        foreach (var entry in compilable.get_sources ())
        {
            if (!entry.name.has_suffix (".cs"))
                return false;
            count++;
        }
        if (count == 0)
            return false;

        if (Environment.find_program_in_path ("gmcs") == null)
            return false;

        return true;
    }

    private string generate_compile_rules (Compilable compilable) throws Error
    {
        var recipe = compilable.recipe;

        var binary_name = "%s.exe".printf (compilable.name);
        if (compilable is Library)
            binary_name = "%s.dll".printf (compilable.name);

        var compile_flags = compilable.compile_flags;
        if (compile_flags == null)
            compile_flags = "";

        var rule = recipe.add_rule ();
        rule.add_output (binary_name);
        recipe.build_rule.add_input (binary_name);

        /* Compile */
        var command = "@gmcs";
        if (compile_flags != "")
            command += " " + compile_flags;
        if (compilable is Library)
            command += " -target:library";
        command += " -out:%s".printf (binary_name);
        foreach (var entry in compilable.get_sources ())
            if (entry.is_allowed)
                command += " %s".printf (entry.name);

        var compile_errors = new List<string> ();

        /* Link against libraries */
        var libraries = new List<TaggedEntry> ();
        try
        {
            libraries = compilable.get_tagged_list ("libraries");
        }
        catch (TaggedListError e)
        {
            compile_errors.append (e.message);
        }
        foreach (var library in libraries)
        {
            var local = false;
            foreach (var tag in library.tags)
            {
                if (tag == "local")
                    local = true;
                else
                    compile_errors.append ("Unknown tag (%s) for library %s".printf (tag, library.name));
            }

            /* Look for locally generated libraries */
            if (local)
            {
                var library_filename = "%s.dll".printf (library.name);
                var library_rule = recipe.toplevel.find_rule_recursive (library_filename);
                if (library_rule != null)
                {
                    var path = get_relative_path (recipe.dirname, Path.build_filename (library_rule.recipe.dirname, library_filename));
                    rule.add_input (path);
                    command += " -reference:%s".printf (path);
                }
                else
                    compile_errors.append ("Unable to find local library %s".printf (library.name));
            }
            else
            {
                command += " -reference:%s".printf (library.name);
            }
        }

        /* Embed resources */
        var resources = new List<TaggedEntry> ();
        try
        {
            resources = compilable.get_tagged_list ("resources");
        }
        catch (TaggedListError e)
        {
            compile_errors.append (e.message);
        }
        foreach (var resource in resources)
        {
            string? id = null;
            foreach (var tag in resource.tags)
            {
                if (tag.has_prefix ("id "))
                    id = strip (tag.substring (3));
                else
                    compile_errors.append ("Unknown tag (%s) for resource %s".printf (tag, resource.name));
            }

            rule.add_input (resource.name);
            command += " -resource:%s".printf (resource.name);
            if (id != null)
                command += ",%s".printf (id);
        }

        if (compile_errors.length () != 0)
        {
            if (compilable is Library)
                rule.add_error_command ("Unable to compile library %s:".printf (compilable.id));
            else
                rule.add_error_command ("Unable to compile program %s:".printf (compilable.id));
            foreach (var e in compile_errors)
                rule.add_error_command (" - %s".printf (e));
            rule.add_command ("@false");
            return binary_name;
        }

        /* Compile */
        foreach (var entry in compilable.get_sources ())
            if (entry.is_allowed)
                rule.add_input (entry.name);
        rule.add_status_command ("MONO-COMPILE %s".printf (binary_name));
        rule.add_command (command);

        if (compilable.gettext_domain != null)
        {
            // FIXME: We don't support gettext
            foreach (var entry in compilable.get_sources ())
                GettextModule.add_translatable_file (recipe, compilable.gettext_domain, "text/x-csharp", entry.name);
        }

        return binary_name;
    }
}