File: main.ml

package info (click to toggle)
libguestfs 1%3A1.44.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,932 kB
  • sloc: ansic: 458,017; ml: 51,424; sh: 13,191; java: 9,578; makefile: 7,931; cs: 6,328; haskell: 5,674; python: 3,871; perl: 3,528; erlang: 2,446; xml: 1,347; ruby: 350; pascal: 257; javascript: 157; lex: 135; yacc: 128; cpp: 10
file content (376 lines) | stat: -rw-r--r-- 14,193 bytes parent folder | download
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
(* libguestfs
 * Copyright (C) 2009-2020 Red Hat Inc.
 *
 * 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *)

(* Please read generator/README first. *)

open Unix
open Printf

open Std_utils
open Pr
open Actions
open Structs
open Types

let perror msg = function
  | Unix_error (err, _, _) ->
      eprintf "%s: %s\n" msg (error_message err)
  | exn ->
      eprintf "%s: %s\n" msg (Printexc.to_string exn)

(* In some directories the actions are split across this many C
 * files.  You can increase this number in order to reduce the number
 * of lines in each file (hence making compilation faster), but you
 * also have to modify .../Makefile.am.
 *)
let nr_actions_files = 7
let actions_subsets =
  let h i { name } = i = Hashtbl.hash name mod nr_actions_files in
  Array.init nr_actions_files (fun i -> List.filter (h i) actions)
let output_to_subset fs f =
  for i = 0 to nr_actions_files-1 do
    ksprintf (fun filename -> output_to filename (f actions_subsets.(i))) fs i
  done

(* Main program. *)
let () =
  let lock_fd =
    try openfile "BUGS" [O_RDWR] 0
    with
    | Unix_error (ENOENT, _, _) ->
        eprintf "\
You are probably running this from the wrong directory.
Run it from the top source directory using the command
  make -C generator stamp-generator
";
        exit 1
    | exn ->
        perror "open: BUGS" exn;
        exit 1 in

  (* Acquire a lock so parallel builds won't try to run the generator
   * twice at the same time.  Subsequent builds will wait for the first
   * one to finish.  Note the lock is released implicitly when the
   * program exits.
   *)
  (try lockf lock_fd F_LOCK 1
   with exn ->
     perror "lock: BUGS" exn;
     exit 1);

  output_to "AUTHORS"
            Authors.generate_authors;

  output_to "common/errnostring/errnostring-gperf.gperf"
            Errnostring.generate_errnostring_gperf;
  output_to "common/errnostring/errnostring.c"
            Errnostring.generate_errnostring_c;
  output_to "common/errnostring/errnostring.h"
            Errnostring.generate_errnostring_h;
  output_to "common/protocol/guestfs_protocol.x"
            XDR.generate_xdr;
  output_to "common/structs/structs-cleanups.h"
            C.generate_client_structs_cleanups_h;
  output_to "common/structs/structs-cleanups.c"
            C.generate_client_structs_cleanups_c;
  output_to "common/structs/structs-print.c"
            C.generate_client_structs_print_c;
  output_to "common/structs/structs-print.h"
            C.generate_client_structs_print_h;
  output_to "lib/uefi.c"
            UEFI.generate_uefi_c;
  output_to "include/guestfs.h"
            C.generate_guestfs_h;
  output_to "lib/guestfs-internal-actions.h"
            C.generate_internal_actions_h;
  output_to "lib/bindtests.c"
            Bindtests.generate_bindtests;
  output_to "lib/guestfs-structs.pod"
            C.generate_structs_pod;
  output_to "lib/guestfs-actions.pod"
            C.generate_actions_pod;
  output_to "lib/guestfs-availability.pod"
            C.generate_availability_pod;
  output_to "lib/event-string.c"
            C.generate_event_string_c;
  output_to "lib/MAX_PROC_NR"
            C.generate_max_proc_nr;
  output_to "lib/libguestfs.syms"
            C.generate_linker_script;
  output_to "lib/structs-compare.c"
            C.generate_client_structs_compare;
  output_to "lib/structs-copy.c"
            C.generate_client_structs_copy;
  output_to "lib/structs-free.c"
            C.generate_client_structs_free;
  output_to "lib/actions-variants.c"
            C.generate_client_actions_variants;
  output_to_subset "lib/actions-%d.c"
                   C.generate_client_actions;
  output_to "tests/c-api/tests.c"
            Tests_c_api.generate_c_api_tests;

  output_to "daemon/actions.h"
            Daemon.generate_daemon_actions_h;
  output_to "daemon/stubs.h"
            Daemon.generate_daemon_stubs_h;
  output_to_subset "daemon/stubs-%d.c"
                   Daemon.generate_daemon_stubs;
  output_to "daemon/caml-stubs.c"
            Daemon.generate_daemon_caml_stubs;
  output_to "daemon/callbacks.ml"
            Daemon.generate_daemon_caml_callbacks_ml;
  output_to "daemon/dispatch.c"
            Daemon.generate_daemon_dispatch;
  output_to "daemon/names.c"
            Daemon.generate_daemon_names;
  output_to "daemon/optgroups.c"
            Daemon.generate_daemon_optgroups_c;
  output_to "daemon/optgroups.h"
            Daemon.generate_daemon_optgroups_h;
  output_to "daemon/optgroups.ml"
            Daemon.generate_daemon_optgroups_ml;
  output_to "daemon/optgroups.mli"
            Daemon.generate_daemon_optgroups_mli;
  output_to "daemon/lvm-tokenization.c"
            Daemon.generate_daemon_lvm_tokenization;
  output_to "daemon/structs-cleanups.c"
            Daemon.generate_daemon_structs_cleanups_c;
  output_to "daemon/structs-cleanups.h"
            Daemon.generate_daemon_structs_cleanups_h;
  let daemon_ocaml_interfaces =
    List.fold_left (
      fun set { impl } ->
        let ocaml_function =
          match impl with
          | OCaml f -> fst (String.split "." f)
          | C -> assert false in

        StringSet.add ocaml_function set
    ) StringSet.empty (actions |> impl_ocaml_functions) in
  StringSet.iter (
    fun modname ->
      let fn = Char.escaped (Char.lowercase_ascii (String.unsafe_get modname 0)) ^
               String.sub modname 1 (String.length modname - 1) in
      output_to (sprintf "daemon/%s.mli" fn)
                (Daemon.generate_daemon_caml_interface modname)
  ) daemon_ocaml_interfaces;

  output_to "fish/cmds-gperf.gperf"
            Fish.generate_fish_cmds_gperf;
  output_to "fish/cmds.c"
            Fish.generate_fish_cmds;
  output_to_subset "fish/entries-%d.c"
                   Fish.generate_fish_cmd_entries;
  output_to_subset "fish/run-%d.c"
                   Fish.generate_fish_run_cmds;
  output_to "fish/run.h"
            Fish.generate_fish_run_header;
  output_to "fish/completion.c"
            Fish.generate_fish_completion;
  output_to "fish/event-names.c"
            Fish.generate_fish_event_names;
  output_to "fish/fish-cmds.h"
            Fish.generate_fish_cmds_h;
  output_to "fish/guestfish-commands.pod"
            Fish.generate_fish_commands_pod;
  output_to "fish/guestfish-actions.pod"
            Fish.generate_fish_actions_pod;
  output_to "fish/prepopts.c"
            Fish.generate_fish_prep_options_c;
  output_to "fish/prepopts.h"
            Fish.generate_fish_prep_options_h;
  output_to "fish/guestfish-prepopts.pod"
            Fish.generate_fish_prep_options_pod;
  output_to ~perm:0o555 "fish/test-prep.sh"
            Fish.generate_fish_test_prep_sh;

  output_to "ocaml/guestfs.mli"
            OCaml.generate_ocaml_mli;
  output_to "ocaml/guestfs.ml"
            OCaml.generate_ocaml_ml;
  output_to "ocaml/guestfs-c-actions.c"
            OCaml.generate_ocaml_c;
  output_to "ocaml/guestfs-c-errnos.c"
            OCaml.generate_ocaml_c_errnos;
  output_to "daemon/structs.ml"
            OCaml.generate_ocaml_daemon_structs;
  output_to "daemon/structs.mli"
            OCaml.generate_ocaml_daemon_structs;
  output_to "ocaml/bindtests.ml"
            Bindtests.generate_ocaml_bindtests;

  output_to "perl/lib/Sys/Guestfs.xs"
            Perl.generate_perl_xs;
  output_to "perl/lib/Sys/Guestfs.pm"
            Perl.generate_perl_pm;
  output_to "perl/bindtests.pl"
            Bindtests.generate_perl_bindtests;

  output_to "python/actions.h"
            Python.generate_python_actions_h;
  output_to_subset "python/actions-%d.c"
                   Python.generate_python_actions;
  output_to "python/module.c"
            Python.generate_python_module;
  output_to "python/structs.c"
            Python.generate_python_structs;
  output_to "python/guestfs.py"
            Python.generate_python_py;
  output_to "python/bindtests.py"
            Bindtests.generate_python_bindtests;

  output_to "ruby/ext/guestfs/actions.h"
            Ruby.generate_ruby_h;
  output_to_subset "ruby/ext/guestfs/actions-%d.c"
                   Ruby.generate_ruby_c;
  output_to "ruby/ext/guestfs/module.c"
            Ruby.generate_ruby_module;
  output_to "ruby/bindtests.rb"
            Bindtests.generate_ruby_bindtests;

  output_to "java/com/redhat/et/libguestfs/GuestFS.java"
            Java.generate_java_java;
  List.iter (
    fun { s_name = typ; s_camel_name = jtyp } ->
      let cols = cols_of_struct typ in
      let filename = sprintf "java/com/redhat/et/libguestfs/%s.java" jtyp in
      output_to filename (Java.generate_java_struct jtyp cols)
  ) external_structs;
  delete_except_generated
    ~skip:["java/com/redhat/et/libguestfs/LibGuestFSException.java";
           "java/com/redhat/et/libguestfs/LibGuestFSOutOfMemory.java";
           "java/com/redhat/et/libguestfs/EventCallback.java"]
    "java/com/redhat/et/libguestfs/*.java";
  output_to "java/Makefile.inc"
            Java.generate_java_makefile_inc;
  output_to_subset "java/actions-%d.c"
                   Java.generate_java_c;
  output_to "java/com/redhat/et/libguestfs/.gitignore"
            Java.generate_java_gitignore;
  output_to "java/Bindtests.java"
            Bindtests.generate_java_bindtests;

  output_to "haskell/Guestfs.hs"
            Haskell.generate_haskell_hs;
  output_to "haskell/Bindtests.hs"
            Bindtests.generate_haskell_bindtests;

  output_to "csharp/Libguestfs.cs"
            Csharp.generate_csharp;

  output_to "php/extension/php_guestfs_php.h"
            Php.generate_php_h;
  output_to "php/extension/guestfs_php.c"
            Php.generate_php_c;
  output_to "php/extension/tests/guestfs_090_bindtests.phpt"
            Bindtests.generate_php_bindtests;

  output_to "erlang/guestfs.erl"
            Erlang.generate_erlang_erl;
  output_to "erlang/actions.h"
            Erlang.generate_erlang_actions_h;
  output_to_subset "erlang/actions-%d.c"
                   Erlang.generate_erlang_actions;
  output_to "erlang/dispatch.c"
            Erlang.generate_erlang_dispatch;
  output_to "erlang/structs.c"
            Erlang.generate_erlang_structs;
  output_to ~perm:0o555 "erlang/bindtests.erl"
            Bindtests.generate_erlang_bindtests;

  output_to "lua/lua-guestfs.c"
            Lua.generate_lua_c;
  output_to "lua/bindtests.lua"
            Bindtests.generate_lua_bindtests;

  output_to "golang/src/libguestfs.org/guestfs/guestfs.go"
            Golang.generate_golang_go;
  output_to "golang/bindtests.go"
            Bindtests.generate_golang_bindtests;

  output_to "gobject/bindtests.js"
            Bindtests.generate_gobject_js_bindtests;
  output_to "gobject/Makefile.inc"
            GObject.generate_gobject_makefile;
  output_to "gobject/include/guestfs-gobject.h"
            GObject.generate_gobject_header;
  List.iter (
    fun { s_name = typ; s_cols = cols } ->
      let short = sprintf "struct-%s" typ in
      let filename =
        sprintf "gobject/include/guestfs-gobject/%s.h" short in
      output_to filename
                (GObject.generate_gobject_struct_header short typ cols);
      let filename = sprintf "gobject/src/%s.c" short in
      output_to filename
                (GObject.generate_gobject_struct_source short typ)
  ) external_structs;
  delete_except_generated "gobject/include/guestfs-gobject/struct-*.h";
  delete_except_generated "gobject/src/struct-*.c";
  List.iter (
    function
    | ({ name; style = (_, _, (_::_ as optargs)) } as f) ->
      let short = sprintf "optargs-%s" name in
      let filename =
        sprintf "gobject/include/guestfs-gobject/%s.h" short in
      output_to filename
                (GObject.generate_gobject_optargs_header short name f);
      let filename = sprintf "gobject/src/%s.c" short in
      output_to filename
                (GObject.generate_gobject_optargs_source short name optargs f)
    | { style = _, _, [] } -> ()
  ) (actions |> external_functions |> sort);
  delete_except_generated "gobject/include/guestfs-gobject/optargs-*.h";
  delete_except_generated "gobject/src/optargs-*.c";
  output_to "gobject/include/guestfs-gobject/tristate.h"
            GObject.generate_gobject_tristate_header;
  output_to "gobject/src/tristate.c"
            GObject.generate_gobject_tristate_source;
  output_to "gobject/include/guestfs-gobject/session.h"
            GObject.generate_gobject_session_header;
  output_to "gobject/src/session.c"
            GObject.generate_gobject_session_source;

  (* mlv2v may not be shipped in this source. *)
  if is_regular_file "common/mlv2v/Makefile.am" then (
    output_to "common/mlv2v/uefi.ml"
              UEFI.generate_uefi_ml;
    output_to "common/mlv2v/uefi.mli"
              UEFI.generate_uefi_mli;
  );

  output_to "common/mlcustomize/customize_cmdline.mli"
            Customize.generate_customize_cmdline_mli;
  output_to "common/mlcustomize/customize_cmdline.ml"
            Customize.generate_customize_cmdline_ml;
  output_to "common/mlcustomize/customize-synopsis.pod"
            Customize.generate_customize_synopsis_pod;
  output_to "common/mlcustomize/customize-options.pod"
            Customize.generate_customize_options_pod;

  output_to "rust/src/guestfs.rs"
            Rust.generate_rust;
  output_to "rust/src/bin/bindtests.rs"
            Bindtests.generate_rust_bindtests;

  (* Generate the list of files generated -- last. *)
  printf "generated %d lines of code\n" (get_lines_generated ());
  let files = List.sort compare (get_files_generated ()) in
  output_to "generator/files-generated.txt"
    (fun () -> List.iter (pr "%s\n") files)