8f92cb17c6f1379dc1d37e13bd2ddefba9e447b5
[wrappi.git] / generator / wrappi_main.ml
1 (* wrappi
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *)
18
19 open Unix
20 open Printf
21
22 open Wrappi_utils
23 open Wrappi_types
24 open Wrappi_pr
25
26 let api = Wrappi_accumulator.get_api ()
27 let nr_tds = StringMap.cardinal api.api_typedefs
28 let nr_ens = StringMap.cardinal api.api_enums
29 let nr_sds = StringMap.cardinal api.api_structs
30 let nr_uns = StringMap.cardinal api.api_unions
31 let nr_eps = StringMap.cardinal api.api_entry_points
32
33 let dump_and_exit () =
34   printf "typedefs (%d):\n" nr_tds;
35   iter_typedefs api (fun td -> printf "  %s\n" (string_of_typedef td));
36
37   printf "enums (%d):\n" nr_ens;
38   iter_enums api (fun en -> printf "  %s\n" (string_of_enum en));
39
40   printf "structs (%d):\n" nr_sds;
41   iter_structs api (fun sd -> printf "  %s\n" (string_of_struct sd));
42
43   printf "unions (%d):\n" nr_uns;
44   iter_unions api (fun un -> printf "  %s\n" (string_of_union un));
45
46   printf "entry points (%d):\n" nr_eps;
47   iter_entry_points api (fun ep -> printf "  %s\n" (string_of_entry_point ep));
48
49   exit 0
50
51 (* Parse command line arguments. *)
52 let () =
53   let display_version () =
54     printf "%s %s\n" Config.package_name Config.package_version;
55     exit 0
56   in
57
58   let argspec = Arg.align [
59     "--dump", Arg.Unit dump_and_exit, " Dump API data and exit";
60     "--version", Arg.Unit display_version, " Display version number and exit";
61   ] in
62   let anon_fun str = raise (Arg.Bad "generator: unknown parameter") in
63   let usage_msg = "
64 NAME
65   wrappi generator - generate a lot of code
66
67 SYNOPSIS
68
69 To run the generator normally (note it MUST be run from the top
70 level SOURCE directory):
71
72   ./generator/generator
73
74 Options are for debugging only:
75
76   ./generator/generator [--options]
77
78 OPTIONS" in
79   Arg.parse argspec anon_fun usage_msg
80
81 let perror msg = function
82   | Unix_error (err, _, _) ->
83       eprintf "%s: %s\n" msg (error_message err)
84   | exn ->
85       eprintf "%s: %s\n" msg (Printexc.to_string exn)
86
87 let () =
88   printf "generator, %d typedefs, %d enums, %d structs, %d unions, %d entry points\n"
89     nr_tds nr_ens nr_sds nr_uns nr_eps;
90
91   (* Acquire a lock so parallel builds won't run the generator
92    * simultaneously.  It's assumed that ./configure.ac only exists in
93    * the top level source directory.  Note the lock is released
94    * implicitly when the program exits.
95    *)
96   let lock_fd =
97     try openfile "configure.ac" [O_RDWR] 0
98     with
99     | Unix_error (ENOENT, _, _) ->
100       eprintf "\
101 You are probably running this from the wrong directory.
102 Run it from the top source directory using the command
103   make -C generator stamp-generator
104 ";
105       exit 1
106     | exn ->
107       perror "open: configure.ac" exn;
108       exit 1 in
109
110   (try lockf lock_fd F_LOCK 1
111    with exn ->
112      perror "lock: configure.ac" exn;
113      exit 1);
114
115   (* Generate code. *)
116   Wrappi_c_impl.generate api;
117   Wrappi_c.generate api;
118
119   printf "generated %d lines of code in %d files\n"
120     (get_lines_generated ()) (List.length (get_files_generated ()));
121
122   (* Create the stamp file last and unconditionally.  This is used
123    * by the Makefile to know when we must rerun the generator.
124    *)
125   let chan = open_out "generator/stamp-generator" in
126   fprintf chan "1\n";
127   close_out chan