summaryrefslogtreecommitdiffstats
path: root/tools/wixl/wixl.vala
blob: 24dd179aad2baae97919111bed1f767a004ff693 (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
using Posix;

namespace Wixl {

    static bool version;
    static bool verbose;
    static bool preproc;
    static string output;
    [CCode (array_length = false, array_null_terminated = true)]
    static string[] files;
    [CCode (array_length = false, array_null_terminated = true)]
    static string[] defines;
    [CCode (array_length = false, array_null_terminated = true)]
    static string[] opt_includedirs;

    static string[] includedirs;
    static string wxidir;
    static Arch arch = Arch.X86;

    private const OptionEntry[] options = {
        { "version", 0, 0, OptionArg.NONE, ref version, N_("Display version number"), null },
        { "verbose", 'v', 0, OptionArg.NONE, ref verbose, N_("Verbose output"), null },
        { "output", 'o', 0, OptionArg.FILENAME, ref output, N_("Output file"), null },
        { "define", 'D', 0, OptionArg.STRING_ARRAY, ref defines, N_("Define variable"), null },
        { "arch", 'a', 0, OptionArg.CALLBACK, (void*)parse_arch, N_("Target architecture"), null },
        { "includedir", 'I', 0, OptionArg.STRING_ARRAY, ref opt_includedirs, N_("Include directory"), null },
        { "wxidir", 0, 0, OptionArg.STRING, ref wxidir, N_("System include directory"), null },
        { "only-preproc", 'E', 0, OptionArg.NONE, ref preproc, N_("Stop after the preprocessing stage"), null },
        { "", 0, 0, OptionArg.FILENAME_ARRAY, ref files, null, N_("INPUT_FILE...") },
        { null }
    };

    bool parse_arch (string option_name, string value, void *data) throws OptionError {
        try {
            arch = Arch.from_string (value);
        } catch (GLib.Error e) {
            throw new OptionError.BAD_VALUE (_("arch of type '%s' is not supported").printf (value));
        }

        return true;
    }

    int main (string[] args) {
        Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
        Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
        Intl.textdomain (Config.GETTEXT_PACKAGE);
        GLib.Environment.set_application_name ("wixl");

        var parameter_string = _("- a msi building tool");
        var opt_context = new OptionContext (parameter_string);
        opt_context.set_help_enabled (true);
        opt_context.add_main_entries (options, null);

        wxidir = Config.PKGDATADIR + "/include";

        try {
            opt_context.parse (ref args);
        } catch (OptionError.BAD_VALUE err) {
            GLib.stderr.printf (err.message + "\n");
            exit (1);
        } catch (OptionError error) {
            warning (error.message);
        }

        /* fixme vala, does not support += on arrays without length.  */
        includedirs = opt_includedirs;
        includedirs += wxidir;

        if (version) {
            GLib.stdout.printf ("%s\n", Config.PACKAGE_VERSION);
            exit (0);
        }

        if (files.length < 1) {
            GLib.stderr.printf (_("Please specify input files.\n"));
            exit (1);
        }

        if (output == null && !preproc) {
            if (files[0].has_suffix (".wxs"))
                output = files[0].slice (0, -4) + ".msi";
            else {
                GLib.stderr.printf (_("Please specify the output file.\n"));
                exit (1);
            }
        }

        try {
            var builder = new WixBuilder (includedirs, arch);

            foreach (var d in defines) {
                var def = d.split ("=", 2);
                var name = def[0];
                var value = def.length == 2 ? def[1] : "1";
                builder.define_variable (name, value);
            }

            foreach (var arg in files) {
                if (verbose)
                    print ("Loading %s...\n", arg);
                var file = File.new_for_commandline_arg (arg);
                builder.load_file (file, preproc);
                builder.add_path (file.get_parent ().get_path ());
            }

            if (preproc)
                return 0;

            if (verbose)
                print ("Building %s...\n", output);
            var msi = builder.build ();
            if (verbose)
                print ("Writing %s...\n", output);
            msi.build (output);
        } catch (GLib.Error error) {
            printerr (error.message + "\n");
            return 1;
        }

        return 0;
    }

} // Wixl