aboutsummaryrefslogtreecommitdiff
path: root/Build.PL
blob: d03a5dc06965eba4fa719210e3d74178a35557f4 (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
use v5.38.0;
use Module::Build;

my @types = (
    [ 'Gtk::Application',    'GtkApplication',    'GTK_APPLICATION' ],
    [ 'Gio::Application',    'GApplication',      'G_APPLICATION' ],
    [ 'Gtk::Box',            'GtkBox',            'GTK_BOX' ],
    [ 'Gtk::Window',         'GtkWindow',         'GTK_WINDOW' ],
    [ 'Gtk::ScrolledWindow', 'GtkScrolledWindow', 'GTK_SCROLLED_WINDOW' ],
    [
        'Gtk::ApplicationWindow', 'GtkApplicationWindow',
        'GTK_APPLICATION_WINDOW'
    ],
    [ 'Gtk::Overlay',     'GtkOverlay',     'GTK_OVERLAY' ],
    [ 'G::Object',        'GObject',        'G_OBJECT' ],
    [ 'Gtk::Widget',      'GtkWidget',      'GTK_WIDGET' ],
    [ 'Gtk::Button',      'GtkButton',      'GTK_BUTTON' ],
    [ 'Gtk::CheckButton', 'GtkCheckButton', 'GTK_CHECK_BUTTON' ],
    [ 'Gtk::Entry',       'GtkEntry',       'GTK_ENTRY' ],
    [ 'Gtk::Dropdown',    'GtkDropDown',    'GTK_DROP_DOWN' ],
    [ 'Gtk::Picture',     'GtkPicture',     'GTK_PICTURE' ],
    [ 'Gio::File',        'GFile',          'G_FILE' ],
    [ 'Gdk::Texture',     'GdkTexture',     'GDK_TEXTURE' ],
    [ 'Gdk::Display',     'GdkDisplay',     'GDK_DISPLAY' ],
    [ 'Gtk::CssProvider', 'GtkCssProvider', 'GTK_CSS_PROVIDER' ],
    [ 'Gtk::Grid',        'GtkGrid',        'GTK_GRID' ],
    [ 'Gtk::Label',       'GtkLabel',       'GTK_LABEL' ],
    [ 'Gtk::Editable',    'GtkEditable',    'GTK_EDITABLE' ],
    [ 'Gtk::AlertDialog', 'GtkAlertDialog', 'GTK_ALERT_DIALOG' ],
);

my @constants = (
    ['GTK_ALIGN_FILL'],           ['GTK_ALIGN_START'],
    ['GTK_ALIGN_END'],            ['GTK_ALIGN_CENTER'],
    ['GTK_ORIENTATION_VERTICAL'], ['GTK_STYLE_PROVIDER_PRIORITY_APPLICATION'],
    ['G_BINDING_SYNC_CREATE'],    ['G_BINDING_DEFAULT'],
);

my %parentships = (
    'Gio::Application'       => [ 'G::Object', ],
    'G::File'                => [ 'G::Object', ],
    'Gdk::Texture'           => [ 'G::Object', ],
    'Gdk::Display'           => [ 'G::Object', ],
    'Gtk::Widget'            => [ 'G::Object', ],
    'Gtk::CssProvider'       => [ 'G::Object', ],
    'Gtk::AlertDialog'       => [ 'G::Object', ],
    'Gtk::Application'       => [ 'Gio::Application', ],
    'Gtk::Overlay'           => ['Gtk::Widget'],
    'Gtk::Window'            => [ 'Gtk::Widget', ],
    'Gtk::Dropdown'          => [ 'Gtk::Widget', ],
    'Gtk::ScrolledWindow'    => [ 'Gtk::Widget', ],
    'Gtk::Entry'             => [ 'Gtk::Widget', 'Gtk::Editable', ],
    'Gtk::Picture'           => [ 'Gtk::Widget', ],
    'Gtk::Grid'              => [ 'Gtk::Widget', ],
    'Gtk::Button'            => [ 'Gtk::Widget', ],
    'Gtk::CheckButton'       => [ 'Gtk::Widget', ],
    'Gtk::Box'               => [ 'Gtk::Widget', ],
    'Gtk::Label'             => [ 'Gtk::Widget', ],
    'Gtk::ApplicationWindow' => [ 'Gtk::Window', ],
);

generate_typemap();
generate_typedefs();
generate_parentships();
generate_constants_xsi();

sub generate_constants_xsi {
    open my $fh, '>', 'lib/AlgaOS/Constants.xsi'
      or die "Cannot create Constants.xsi: $!";
    say $fh "MODULE = AlgaOS::Updater PACKAGE = AlgaOS::Updater::Constants";

    say $fh "";

    for my $c (@constants) {
        my ($c_name) = @$c;

        say $fh <<"XS";
unsigned int
$c_name(...)
CODE:
    RETVAL = (unsigned int) $c_name;
OUTPUT:
    RETVAL
XS
    }

    say $fh <<"XS";
AlgaOS::Updater::Constants
new(...)
    CODE:
        RETVAL = malloc(sizeof *RETVAL);
        *RETVAL = 1;
    OUTPUT:
        RETVAL

void
DESTROY(AlgaOS::Updater::Constants self)
    CODE:
        free(self);
XS
}

sub generate_parentships {
    open my $fh, '>', 'lib/AlgaOS/Updater/Parents.pm'
      or die "Cannot create parentships: $!";
    for my $child ( keys %parentships ) {
        say $fh "package $child;";
        say $fh "use v5.38.0;";
        say $fh "use vars qw/\@ISA/;";
        say $fh "our \@ISA = qw/@{[join ' ', @{$parentships{$child}}]}/;";
    }
}

my $build = Module::Build->new(
    module_name => 'AlgaOS::Updater',
    license     => 'perl',
    requires    => {
        'perl'               => '5.38.2',
        'ExtUtils::CBuilder' => 0,
        'Crypt::URandom'     => 0,
        'Moo'                => 0,
        'PBKDF2::Tiny'       => 0,
        'JSON'               => 0,
	'File::ShareDir'     => 0,
    },
    script_files => [
        'scripts/algaos-updater',
    ],
    share_dir => 'share',
    extra_compiler_flags => scalar `pkg-config --cflags gtk4` . " -I$ENV{PWD}",
    extra_linker_flags   => scalar `pkg-config --libs gtk4`,
);

$build->create_build_script;

sub generate_typemap {
    open my $pre_fh, '<', 'pre_typemap';
    local $/ = undef;
    my $pre_file = <$pre_fh> // '';
    open my $fh, '>', 'typemap'
      or die "Cannot create typemap: $!";

    say $fh "TYPEMAP";
    say $fh "
$pre_file
    ";
    for my $row (@types) {
        my $perl_class = $row->[0];
        my $c_constant = $row->[1];
        my $c_class    = $row->[2];
        say $fh "
$perl_class T_PTROBJ_GOBJECT_$c_class

$c_constant * T_PTROBJ_GOBJECT_$c_class
";
    }
    say $fh "INPUT";
    for my $row (@types) {
        my $perl_class         = $row->[0];
        my $c_constant         = $row->[1];
        my $c_class            = $row->[2];
        my $c_class_lower_case = lc($c_class);
        say $fh "
T_PTROBJ_GOBJECT_$c_class
    if (sv_isobject(\$arg)) {
        IV integer_pointer = SvIV(SvRV(\$arg));
        void *void_object = INT2PTR(void *, integer_pointer);
        if (G_IS_OBJECT(void_object)) {
            GObject *gobject = G_OBJECT (void_object);
            if (G_TYPE_CHECK_INSTANCE_TYPE(gobject, ${c_class_lower_case}_get_type())) {
                \$var = $c_class (gobject);
            } else {
                croak(\"Not implementing $c_class\");
            }
        } else {
            croak(\"Expected gobject\");
        }
    } else {
        croak(\"Expected $perl_class object\");
    }
";
    }

    say $fh "OUTPUT";

    for my $row (@types) {
        my $perl_class = $row->[0];
        my $c_class    = $row->[2];

        say $fh "
T_PTROBJ_GOBJECT_$c_class
    sv_setref_pv(\$arg, \"$perl_class\", (void *)\$var);
";
    }
}

sub generate_typedefs {
    open my $pre_fh, '<', 'pre_typedef.h';
    local $/ = undef;
    my $pre_file = <$pre_fh> // '';
    open my $fh, '>', 'typedefs.h'
      or die "Cannot create typedefs.h: $!";
    say $fh $pre_file;
    for my $row (@types) {
        my $perl_class         = $row->[0];
        my $c_constant         = $row->[1];
        my $c_class            = $row->[2];
        my $c_class_lower_case = lc($c_class);
        say $fh "typedef $c_constant *" . ( $perl_class =~ s/::/__/gr ) . ';';
    }
}