3 # OCaml bindings for libvirt.
4 # (C) Copyright 2007-2008 Richard W.M. Jones, Red Hat Inc.
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 # This generates libvirt_c.c (the core of the bindings). You don't
22 # need to run this program unless you are extending the bindings
23 # themselves (eg. because libvirt has been extended).
27 #----------------------------------------------------------------------
29 # The functions in the libvirt API that we can generate.
32 { name => "virConnectGetHostname", sig => "conn : string", weak => 1 },
33 { name => "virConnectGetURI", sig => "conn : string", weak => 1 },
34 { name => "virConnectGetType", sig => "conn : static string" },
35 { name => "virConnectNumOfDomains", sig => "conn : int" },
36 { name => "virConnectListDomains", sig => "conn, int : int array" },
37 { name => "virConnectNumOfDefinedDomains", sig => "conn : int" },
38 { name => "virConnectListDefinedDomains",
39 sig => "conn, int : string array" },
40 { name => "virConnectNumOfNetworks", sig => "conn : int" },
41 { name => "virConnectListNetworks", sig => "conn, int : string array" },
42 { name => "virConnectNumOfDefinedNetworks", sig => "conn : int" },
43 { name => "virConnectListDefinedNetworks",
44 sig => "conn, int : string array" },
45 { name => "virConnectNumOfStoragePools", sig => "conn : int", weak => 1 },
46 { name => "virConnectListStoragePools",
47 sig => "conn, int : string array", weak => 1 },
48 { name => "virConnectNumOfDefinedStoragePools",
49 sig => "conn : int", weak => 1 },
50 { name => "virConnectListDefinedStoragePools",
51 sig => "conn, int : string array", weak => 1 },
52 { name => "virConnectGetCapabilities", sig => "conn : string" },
54 { name => "virDomainLookupByName", sig => "conn, string : dom" },
55 { name => "virDomainLookupByUUIDString", sig => "conn, string : dom" },
56 { name => "virDomainGetName", sig => "dom : static string" },
57 { name => "virDomainGetOSType", sig => "dom : string" },
58 { name => "virDomainGetXMLDesc", sig => "dom, 0 : string" },
59 { name => "virDomainGetUUID", sig => "dom : uuid" },
60 { name => "virDomainGetUUIDString", sig => "dom : uuid string" },
61 { name => "virDomainSuspend", sig => "dom : unit" },
62 { name => "virDomainResume", sig => "dom : unit" },
63 { name => "virDomainShutdown", sig => "dom : unit" },
64 { name => "virDomainReboot", sig => "dom, 0 : unit" },
65 { name => "virDomainUndefine", sig => "dom : unit" },
66 { name => "virDomainCreate", sig => "dom : unit" },
67 { name => "virDomainGetAutostart", sig => "dom : bool" },
68 { name => "virDomainSetAutostart", sig => "dom, bool : unit" },
70 { name => "virNetworkLookupByName", sig => "conn, string : net" },
71 { name => "virNetworkLookupByUUIDString", sig => "conn, string : net" },
72 { name => "virNetworkGetName", sig => "net : static string" },
73 { name => "virNetworkGetXMLDesc", sig => "net, 0 : string" },
74 { name => "virNetworkGetBridgeName", sig => "net : string" },
75 { name => "virNetworkGetUUID", sig => "net : uuid" },
76 { name => "virNetworkGetUUIDString", sig => "net : uuid string" },
77 { name => "virNetworkUndefine", sig => "net : unit" },
78 { name => "virNetworkCreate", sig => "net : unit" },
79 { name => "virNetworkGetAutostart", sig => "net : bool" },
80 { name => "virNetworkSetAutostart", sig => "net, bool : unit" },
82 { name => "virStoragePoolLookupByName",
83 sig => "conn, string : pool", weak => 1 },
84 { name => "virStoragePoolLookupByUUIDString",
85 sig => "conn, string : pool", weak => 1 },
86 { name => "virStoragePoolGetName",
87 sig => "pool : static string", weak => 1 },
88 { name => "virStoragePoolGetXMLDesc",
89 sig => "pool, 0 : string", weak => 1 },
90 { name => "virStoragePoolGetUUID",
91 sig => "pool : uuid", weak => 1 },
92 { name => "virStoragePoolGetUUIDString",
93 sig => "pool : uuid string", weak => 1 },
94 { name => "virStoragePoolUndefine",
95 sig => "pool : unit", weak => 1 },
96 { name => "virStoragePoolCreate",
97 sig => "pool : unit", weak => 1 },
98 { name => "virStoragePoolShutdown",
99 sig => "pool : unit", weak => 1 },
100 { name => "virStoragePoolRefresh",
101 sig => "pool, 0U : unit", weak => 1 },
102 { name => "virStoragePoolGetAutostart",
103 sig => "pool : bool", weak => 1 },
104 { name => "virStoragePoolSetAutostart",
105 sig => "pool, bool : unit", weak => 1 },
107 # { name => "virStorageVolLookupByName", XXX see libvir-list posting
108 # sig => "pool, string : vol", weak => 1 },
109 { name => "virStorageVolLookupByKey",
110 sig => "conn, string : vol", weak => 1 },
111 { name => "virStorageVolLookupByPath",
112 sig => "conn, string : vol", weak => 1 },
113 { name => "virStorageVolGetXMLDesc",
114 sig => "vol, 0 : string", weak => 1 },
115 { name => "virStorageVolGetPath",
116 sig => "vol : string", weak => 1 },
117 { name => "virStorageVolGetKey",
118 sig => "vol : static string", weak => 1 },
119 { name => "virStorageVolGetName",
120 sig => "vol : static string", weak => 1 },
124 # Functions we haven't implemented anywhere yet.
125 # We create stubs for these, but they need to either be moved ^^ so they
126 # are auto-generated or implementations written in libvirt_c_oneoffs.c.
128 my @unimplemented = (
129 "ocaml_libvirt_domain_create_job",
130 "ocaml_libvirt_domain_core_dump_job",
131 "ocaml_libvirt_domain_restore_job",
132 "ocaml_libvirt_domain_save_job",
133 "ocaml_libvirt_connect_create_linux_job",
134 "ocaml_libvirt_network_create_job",
135 "ocaml_libvirt_network_create_xml_job",
136 "ocaml_libvirt_storage_pool_get_info",
137 "ocaml_libvirt_storage_pool_free", #?
138 "ocaml_libvirt_storage_pool_destroy", #?
139 "ocaml_libvirt_storage_pool_define_xml",
140 "ocaml_libvirt_storage_pool_create_xml",
141 "ocaml_libvirt_storage_pool_lookup_by_uuid",
142 "ocaml_libvirt_storage_vol_lookup_by_name", # XXX
143 "ocaml_libvirt_storage_vol_free", #?
144 "ocaml_libvirt_storage_vol_destroy", #?
145 "ocaml_libvirt_storage_vol_create_xml",
146 "ocaml_libvirt_storage_vol_get_info",
147 "ocaml_libvirt_pool_of_volume",
148 "ocaml_libvirt_job_cancel",
149 "ocaml_libvirt_job_get_network",
150 "ocaml_libvirt_job_get_domain",
151 "ocaml_libvirt_job_get_info",
154 #----------------------------------------------------------------------
156 # Open the output file.
158 my $filename = "libvirt_c.c";
159 open F, ">$filename" or die "$filename: $!";
161 # Write the prologue.
164 /* !!! WARNING WARNING WARNING WARNING WARNING WARNING WARNING !!!
166 * THIS FILE IS AUTOMATICALLY GENERATED BY 'generator.pl'.
168 * Any changes you make to this file may be overwritten.
171 /* OCaml bindings for libvirt.
172 * (C) Copyright 2007-2008 Richard W.M. Jones, Red Hat Inc.
173 * http://libvirt.org/
175 * This library is free software; you can redistribute it and/or
176 * modify it under the terms of the GNU Lesser General Public
177 * License as published by the Free Software Foundation; either
178 * version 2 of the License, or (at your option) any later version.
180 * This library is distributed in the hope that it will be useful,
181 * but WITHOUT ANY WARRANTY; without even the implied warranty of
182 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
183 * Lesser General Public License for more details.
185 * You should have received a copy of the GNU Lesser General Public
186 * License along with this library; if not, write to the Free Software
187 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
196 #include <libvirt/libvirt.h>
197 #include <libvirt/virterror.h>
199 #include <caml/config.h>
200 #include <caml/alloc.h>
201 #include <caml/callback.h>
202 #include <caml/custom.h>
203 #include <caml/fail.h>
204 #include <caml/memory.h>
205 #include <caml/misc.h>
206 #include <caml/mlvalues.h>
207 #include <caml/signals.h>
209 #include "libvirt_c_prologue.c"
211 #include "libvirt_c_oneoffs.c"
215 #----------------------------------------------------------------------
217 sub camel_case_to_underscores
221 $name =~ s/([A-Z][a-z]+|XML|URI|OS|UUID)/$1,/g;
222 my @subs = split (/,/, $name);
223 @subs = map { lc($_) } @subs;
227 # Helper functions dealing with signatures.
229 sub short_name_to_c_type
233 if ($_ eq "conn") { "virConnectPtr" }
234 elsif ($_ eq "dom") { "virDomainPtr" }
235 elsif ($_ eq "net") { "virNetworkPtr" }
236 elsif ($_ eq "pool") { "virStoragePoolPtr" }
237 elsif ($_ eq "vol") { "virStorageVolPtr" }
239 die "unknown short name $_"
248 if ($sig =~ /^(\w+) : string$/) {
249 my $c_type = short_name_to_c_type ($1);
250 "char *$c_name ($c_type $1)"
251 } elsif ($sig =~ /^(\w+) : static string$/) {
252 my $c_type = short_name_to_c_type ($1);
253 "const char *$c_name ($c_type $1)"
254 } elsif ($sig =~ /^(\w+) : int$/) {
255 my $c_type = short_name_to_c_type ($1);
256 "int $c_name ($c_type $1)"
257 } elsif ($sig =~ /^(\w+) : uuid$/) {
258 my $c_type = short_name_to_c_type ($1);
259 "int $c_name ($c_type $1, unsigned char *)"
260 } elsif ($sig =~ /^(\w+) : uuid string$/) {
261 my $c_type = short_name_to_c_type ($1);
262 "int $c_name ($c_type $1, char *)"
263 } elsif ($sig =~ /^(\w+) : bool$/) {
264 my $c_type = short_name_to_c_type ($1);
265 "int $c_name ($c_type $1, int *r)"
266 } elsif ($sig =~ /^(\w+), bool : unit$/) {
267 my $c_type = short_name_to_c_type ($1);
268 "int $c_name ($c_type $1, int b)"
269 } elsif ($sig eq "conn, int : int array") {
270 "int $c_name (virConnectPtr conn, int *ids, int maxids)"
271 } elsif ($sig eq "conn, int : string array") {
272 "int $c_name (virConnectPtr conn, char **const names, int maxnames)"
273 } elsif ($sig =~ /^(\w+), 0(U?) : string$/) {
274 my $c_type = short_name_to_c_type ($1);
275 my $unsigned = $2 eq "U" ? "unsigned " : "";
276 "char *$c_name ($c_type $1, $unsigned int flags)"
277 } elsif ($sig =~ /^(\w+), 0(U?) : unit$/) {
278 my $c_type = short_name_to_c_type ($1);
279 my $unsigned = $2 eq "U" ? "unsigned " : "";
280 "int $c_name ($c_type $1, $unsigned int flags)"
281 } elsif ($sig =~ /^(\w+) : unit$/) {
282 my $c_type = short_name_to_c_type ($1);
283 "int $c_name ($c_type $1)"
284 } elsif ($sig =~ /^(\w+), string : (\w+)$/) {
285 my $c_type = short_name_to_c_type ($1);
286 my $c_ret_type = short_name_to_c_type ($2);
287 "$c_ret_type $c_name ($c_type $1, const char *str)"
289 die "unknown signature $sig"
297 if ($sig =~ /^(\w+) : string$/) {
299 } elsif ($sig =~ /^(\w+) : static string$/) {
301 } elsif ($sig =~ /^(\w+) : int$/) {
303 } elsif ($sig =~ /^(\w+) : uuid$/) {
305 } elsif ($sig =~ /^(\w+) : uuid string$/) {
307 } elsif ($sig =~ /^(\w+) : bool$/) {
309 } elsif ($sig =~ /^(\w+), bool : unit$/) {
311 } elsif ($sig eq "conn, int : int array") {
313 } elsif ($sig eq "conn, int : string array") {
315 } elsif ($sig =~ /^(\w+), 0U? : string$/) {
317 } elsif ($sig =~ /^(\w+), 0U? : unit$/) {
319 } elsif ($sig =~ /^(\w+) : unit$/) {
321 } elsif ($sig =~ /^(\w+), string : (\w+)$/) {
324 die "unknown signature $sig"
333 "virConnectPtr conn = Connect_val (connv);"
334 } elsif ($_ eq "dom") {
335 "virDomainPtr dom = Domain_val (domv);\n".
336 " virConnectPtr conn = Connect_domv (domv);"
337 } elsif ($_ eq "net") {
338 "virNetworkPtr net = Network_val (netv);\n".
339 " virConnectPtr conn = Connect_netv (netv);"
340 } elsif ($_ eq "pool") {
341 "virStoragePoolPtr pool = Pool_val (poolv);\n".
342 " virConnectPtr conn = Connect_polv (poolv);"
343 } elsif ($_ eq "vol") {
344 "virStorageVolPtr vol = Volume_val (volv);\n".
345 " virConnectPtr conn = Connect_volv (volv);"
347 die "unknown short name $_"
356 "rv = Val_domain (r, connv);"
357 } elsif ($_ eq "net") {
358 "rv = Val_network (r, connv);"
359 } elsif ($_ eq "pool") {
360 "rv = Val_pool (r, connv);"
361 } elsif ($_ eq "vol") {
362 "rv = Val_volume (r, connv);"
371 if ($sig =~ /^(\w+) : string$/) {
374 " . gen_unpack_args ($1) . "
377 NONBLOCKING (r = $c_name ($1));
378 CHECK_ERROR (!r, conn, \"$c_name\");
380 rv = caml_copy_string (r);
384 } elsif ($sig =~ /^(\w+) : static string$/) {
387 " . gen_unpack_args ($1) . "
390 NONBLOCKING (r = $c_name ($1));
391 CHECK_ERROR (!r, conn, \"$c_name\");
393 rv = caml_copy_string (r);
396 } elsif ($sig =~ /^(\w+) : int$/) {
398 " . gen_unpack_args ($1) . "
401 NONBLOCKING (r = $c_name ($1));
402 CHECK_ERROR (r == -1, conn, \"$c_name\");
404 CAMLreturn (Val_int (r));
406 } elsif ($sig =~ /^(\w+) : uuid$/) {
409 " . gen_unpack_args ($1) . "
410 unsigned char uuid[VIR_UUID_BUFLEN];
413 NONBLOCKING (r = $c_name ($1, uuid));
414 CHECK_ERROR (r == -1, conn, \"$c_name\");
416 rv = caml_copy_string ((char *) uuid);
419 } elsif ($sig =~ /^(\w+) : uuid string$/) {
422 " . gen_unpack_args ($1) . "
423 char uuid[VIR_UUID_STRING_BUFLEN];
426 NONBLOCKING (r = $c_name ($1, uuid));
427 CHECK_ERROR (r == -1, conn, \"$c_name\");
429 rv = caml_copy_string (uuid);
432 } elsif ($sig =~ /^(\w+) : bool$/) {
434 " . gen_unpack_args ($1) . "
437 NONBLOCKING (r = $c_name ($1, &b));
438 CHECK_ERROR (r == -1, conn, \"$c_name\");
440 CAMLreturn (b ? Val_true : Val_false);
442 } elsif ($sig =~ /^(\w+), bool : unit$/) {
444 " . gen_unpack_args ($1) . "
447 b = bv == Val_true ? 1 : 0;
449 NONBLOCKING (r = $c_name ($1, b));
450 CHECK_ERROR (r == -1, conn, \"$c_name\");
452 CAMLreturn (Val_unit);
454 } elsif ($sig eq "conn, int : int array") {
457 virConnectPtr conn = Connect_val (connv);
458 int i = Int_val (iv);
461 NONBLOCKING (r = $c_name (conn, ids, i));
462 CHECK_ERROR (r == -1, conn, \"$c_name\");
464 rv = caml_alloc (r, 0);
465 for (i = 0; i < r; ++i)
466 Store_field (rv, i, Val_int (ids[i]));
470 } elsif ($sig eq "conn, int : string array") {
472 CAMLlocal2 (rv, strv);
473 virConnectPtr conn = Connect_val (connv);
474 int i = Int_val (iv);
478 NONBLOCKING (r = $c_name (conn, names, i));
479 CHECK_ERROR (r == -1, conn, \"$c_name\");
481 rv = caml_alloc (r, 0);
482 for (i = 0; i < r; ++i) {
483 strv = caml_copy_string (names[i]);
484 Store_field (rv, i, strv);
490 } elsif ($sig =~ /^(\w+), 0U? : string$/) {
493 " . gen_unpack_args ($1) . "
496 NONBLOCKING (r = $c_name ($1, 0));
497 CHECK_ERROR (!r, conn, \"$c_name\");
499 rv = caml_copy_string (r);
503 } elsif ($sig =~ /^(\w+) : unit$/) {
505 " . gen_unpack_args ($1) . "
508 NONBLOCKING (r = $c_name ($1));
509 CHECK_ERROR (r == -1, conn, \"$c_name\");
511 CAMLreturn (Val_unit);
513 } elsif ($sig =~ /^(\w+), 0U? : unit$/) {
515 " . gen_unpack_args ($1) . "
518 NONBLOCKING (r = $c_name ($1, 0));
519 CHECK_ERROR (r == -1, conn, \"$c_name\");
521 CAMLreturn (Val_unit);
523 } elsif ($sig =~ /^(\w+), string : (\w+)$/) {
524 my $c_ret_type = short_name_to_c_type ($2);
527 " . gen_unpack_args ($1) . "
528 char *str = String_val (strv);
531 NONBLOCKING (r = $c_name ($1, str));
532 CHECK_ERROR (!r, conn, \"$c_name\");
534 " . gen_pack_result ($2) . "
539 die "unknown signature $sig"
543 # Generate each function.
545 foreach my $function (@functions) {
546 my $c_name = $function->{name};
547 my $is_weak = $function->{weak};
548 my $sig = $function->{sig};
550 my $is_pool_func = $c_name =~ /^virStoragePool/;
551 my $is_vol_func = $c_name =~ /^virStorageVol/;
553 # Generate an equivalent C-external name for the function, unless
554 # one is defined already.
556 if (exists ($function->{c_external_name})) {
557 $c_external_name = $function->{c_external_name};
558 } elsif ($c_name =~ /^vir/) {
559 $c_external_name = substr $c_name, 3;
560 $c_external_name = camel_case_to_underscores ($c_external_name);
561 $c_external_name = "ocaml_libvirt_" . $c_external_name;
563 die "cannot convert c_name $c_name to c_external_name"
566 # Generate a full function prototype if the function is weak.
567 my $have_name = "HAVE_" . uc ($c_name);
569 my $c_sig = gen_c_signature ($sig, $c_name);
571 #ifdef HAVE_WEAK_SYMBOLS
573 extern $c_sig __attribute__((weak));
580 my @arg_names = gen_arg_names ($sig);
581 my $nr_arg_names = scalar @arg_names;
582 my $arg_names = join ", ", @arg_names;
583 my $arg_names_as_values = join (", ", map { "value $_" } @arg_names);
585 # Generate the start of the function, arguments.
588 $c_external_name ($arg_names_as_values)
590 CAMLparam$nr_arg_names ($arg_names);
593 # If weak, check the function exists at compile time or runtime.
597 /* Symbol $c_name not found at compile time. */
598 not_supported ("$c_name");
599 /* Suppresses a compiler warning. */
602 /* Check that the symbol $c_name
603 * is in runtime version of libvirt.
605 WEAK_SYMBOL_CHECK ($c_name);
609 # Generate the internals of the function.
610 print F (gen_c_code ($sig, $c_name));
612 # Finish off weak #ifdef.
619 # Finish off the function.
626 #----------------------------------------------------------------------
628 # Unimplemented functions.
630 printf "$0: warning: %d unimplemented functions\n", scalar (@unimplemented);
632 foreach my $c_external_name (@unimplemented) {
637 failwith ("$c_external_name is unimplemented");
643 #----------------------------------------------------------------------
645 # Write the epilogue.
648 #include "libvirt_c_epilogue.c"
654 print "$0: written $filename\n"