Start generating C code.
[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_pr
23
24 let eps = Wrappi_globals.get_entry_points ()
25 let nr_eps = List.length eps
26
27 let dump_and_exit () =
28   printf "entry points (%d):\n" nr_eps;
29
30   List.iter (fun ep ->
31     printf "  %s\n" (Wrappi_types.string_of_entry_point ep)
32   ) eps;
33
34   exit 0
35
36 (* Parse command line arguments. *)
37 let () =
38   let display_version () =
39     printf "%s %s\n" Config.package_name Config.package_version;
40     exit 0
41   in
42
43   let argspec = Arg.align [
44     "--dump", Arg.Unit dump_and_exit, " Dump API data and exit";
45     "--version", Arg.Unit display_version, " Display version number and exit";
46   ] in
47   let anon_fun str = raise (Arg.Bad "generator: unknown parameter") in
48   let usage_msg = "
49 NAME
50   wrappi generator - generate a lot of code
51
52 SYNOPSIS
53
54 To run the generator normally (note it MUST be run from the top
55 level SOURCE directory):
56
57   ./generator/generator
58
59 Options are for debugging only:
60
61   ./generator/generator [--options]
62
63 OPTIONS" in
64   Arg.parse argspec anon_fun usage_msg
65
66 let perror msg = function
67   | Unix_error (err, _, _) ->
68       eprintf "%s: %s\n" msg (error_message err)
69   | exn ->
70       eprintf "%s: %s\n" msg (Printexc.to_string exn)
71
72 let () =
73   printf "generator, %d entry points\n" nr_eps;
74
75   (* Acquire a lock so parallel builds won't run the generator
76    * simultaneously.  It's assumed that ./configure.ac only exists in
77    * the top level source directory.  Note the lock is released
78    * implicitly when the program exits.
79    *)
80   let lock_fd =
81     try openfile "configure.ac" [O_RDWR] 0
82     with
83     | Unix_error (ENOENT, _, _) ->
84       eprintf "\
85 You are probably running this from the wrong directory.
86 Run it from the top source directory using the command
87   make -C generator stamp-generator
88 ";
89       exit 1
90     | exn ->
91       perror "open: configure.ac" exn;
92       exit 1 in
93
94   (try lockf lock_fd F_LOCK 1
95    with exn ->
96      perror "lock: configure.ac" exn;
97      exit 1);
98
99   (* Create a structure that we'll pass around to each generator function. *)
100   let api = {
101     Wrappi_types.api_entry_points = eps
102   } in
103
104   (* Generate code. *)
105   Wrappi_c.generate api;
106
107   printf "generated %d lines of code in %d files\n"
108     (get_lines_generated ()) (List.length (get_files_generated ()));
109
110   (* Create the stamp file last and unconditionally.  This is used
111    * by the Makefile to know when we must rerun the generator.
112    *)
113   let chan = open_out "generator/stamp-generator" in
114   fprintf chan "1\n";
115   close_out chan