50673fc200de4e54a0bf064d27431435b4df2021
[wrappi.git] / generator / wrappi_c_impl.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 Camlp4.PreCast
20
21 open Wrappi_utils
22 open Wrappi_types
23 open Wrappi_boilerplate
24 open Wrappi_pr
25
26 open Printf
27
28 let generate_implementation ep =
29   generate_header CStyle LGPLv2plus;
30
31   pr "\
32 #include <config.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include \"wrappi.h\"
38
39 #include \"internal.h\"
40
41 "
42
43   (* Depending on whether this is a local or remote function, include
44    * different definitions here.
45    *)
46   (*if ep.ep_local then ... *)
47
48 (* Make a unique, reproducible filename for each entry point. *)
49 let filename_of_ep ep =
50   let filename = Loc.file_name ep.ep_loc in
51   let filename = Filename.basename filename in
52   let filename =
53     try Filename.chop_extension filename
54     with Invalid_argument _ -> filename in
55   let filename = sprintf "%s-%s.c" filename ep.ep_name in
56   filename
57
58 let generate_lib_implementation_files_mk api =
59   generate_header HashStyle GPLv2plus;
60
61   let eps = StringMap.bindings api.api_entry_points in
62   let cmp (a, _) (b, _) = compare a b in
63   let eps = List.sort cmp eps in
64   let eps = List.map snd eps in
65
66   let rec loop = function
67     | [] -> ()
68     | [ep] -> pr "\t%s\n" (filename_of_ep ep)
69     | ep :: eps -> pr "\t%s \\\n" (filename_of_ep ep); loop eps
70   in
71
72   pr "local_implementation_files := \\\n";
73
74   loop (List.filter (fun ep -> ep.ep_local) eps);
75
76   pr "\n";
77   pr "remote_implementation_files := \\\n";
78
79   loop (List.filter (fun ep -> not ep.ep_local) eps)
80
81 let generate api =
82   let gitignores = ref [] in
83
84   iter_entry_points api (
85     fun ep ->
86       let filename = filename_of_ep ep in
87
88       gitignores := ("/" ^ filename) :: !gitignores;
89
90       output_to ("lib/" ^ filename) generate_implementation ep
91   );
92
93   let gitignores = List.rev !gitignores in
94   output_to "lib/.gitignore"
95     (fun () -> List.iter (pr "%s\n") gitignores) ();
96
97   output_to "lib/implementation_files.mk"
98     generate_lib_implementation_files_mk api