(* wrappi * Copyright (C) 2011 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. *) open Camlp4.PreCast open Wrappi_utils open Wrappi_types open Wrappi_boilerplate open Wrappi_pr open Printf let c_of_ptype ~param = function | TBool -> "int" | TBuffer -> assert false (* XXX not implemented *) | TEnum name -> sprintf "enum wrap_%s" name | TFile -> if param then "const char *" else "char *" | THash t -> if param then "char * const *" else "char **" | TInt -> "int" (* XXX not int, correct type depends on preconditions *) | TInt32 -> "int32_t" | TInt64 -> "int64_t" | TList t -> assert false (* XXX not implemented *) | TNullable TString -> if param then "const char *" else "char *" | TNullable _ -> assert false (* XXX may be implemented in future *) | TString -> if param then "const char *" else "char *" | TStruct name -> sprintf "struct wrap_%s" name | TTypedef name -> assert false (* should never happen *) | TUInt32 -> "uint32_t" | TUInt64 -> "uint64_t" | TUnion name -> sprintf "union wrap_%s" name let c_of_rtype = function | RVoid -> "void" | Return t -> c_of_ptype ~param:false t let pr_decl ep = let ret, req, opt = ep.ep_ftype in pr "%s\n" (c_of_rtype ret); pr "wrap_%s (wrap_h *w" ep.ep_name; (* Required parameters. *) List.iter ( fun (name, t, _) -> let t = c_of_ptype ~param:true t in let sep = (* "const char *" - omit space after asterisk *) let len = String.length t in if isalnum t.[len-1] then " " else "" in pr ", %s%s%s" t sep name ) req; (* Optional parameters. *) if opt <> [] then pr ", ..."; pr ")\n" let generate_implementation ep = generate_header CStyle LGPLv2plus; pr "\ #include #include #include "; List.iter (pr "#include <%s>\n") ep.ep_includes; pr "\ #include \"wrappi.h\" #include \"internal.h\" /* Automatically generated implementation of '%s'. * This API was defined in '%s' at line %d. */ " ep.ep_name (Loc.file_name ep.ep_loc) (Loc.start_line ep.ep_loc); (* Depending on whether this is a local or remote function, include * different definitions here. *) (*if ep.ep_local then ... *) pr_decl ep; pr "\ { #line %d \"%s\" " (Loc.start_line ep.ep_loc) (Loc.file_name ep.ep_loc); (match ep.ep_code with | None -> () | Some code -> pr "%s" code ); pr "}\n" (* Make a unique, reproducible filename for each entry point. *) let filename_of_ep ep = let filename = Loc.file_name ep.ep_loc in let filename = Filename.basename filename in let filename = try Filename.chop_extension filename with Invalid_argument _ -> filename in let filename = sprintf "%s-%s.c" filename ep.ep_name in filename let generate_lib_implementation_files_mk api = generate_header HashStyle GPLv2plus; let eps = StringMap.bindings api.api_entry_points in let cmp (a, _) (b, _) = compare a b in let eps = List.sort cmp eps in let eps = List.map snd eps in let rec loop = function | [] -> () | [ep] -> pr "\t%s\n" (filename_of_ep ep) | ep :: eps -> pr "\t%s \\\n" (filename_of_ep ep); loop eps in pr "local_implementation_files := \\\n"; loop (List.filter (fun ep -> ep.ep_local) eps); pr "\n"; pr "remote_implementation_files := \\\n"; loop (List.filter (fun ep -> not ep.ep_local) eps) let generate api = let gitignores = ref [] in iter_entry_points api ( fun ep -> let filename = filename_of_ep ep in gitignores := ("/" ^ filename) :: !gitignores; output_to ("lib/" ^ filename) generate_implementation ep ); let gitignores = List.rev !gitignores in output_to "lib/.gitignore" (fun () -> List.iter (pr "%s\n") gitignores) (); output_to "lib/implementation_files.mk" generate_lib_implementation_files_mk api