From e253e10f2d1c35bd685de8c4de993653f6a8330d Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 31 Dec 2011 19:56:22 +0000 Subject: [PATCH] Begin generating C implementation code. --- APIs/error.api | 4 ++ APIs/filesize.api | 2 +- APIs/mknod.api | 2 +- generator/.depend | 7 +++- generator/Makefile.am | 3 ++ generator/wrappi_c.mli | 2 + generator/wrappi_c_impl.ml | 98 +++++++++++++++++++++++++++++++++++++++++++++ generator/wrappi_c_impl.mli | 21 ++++++++++ generator/wrappi_main.ml | 1 + lib/.gitignore | 8 ++++ lib/Makefile.am | 6 ++- lib/implementation_files.mk | 32 +++++++++++++++ 12 files changed, 181 insertions(+), 5 deletions(-) create mode 100644 generator/wrappi_c_impl.ml create mode 100644 generator/wrappi_c_impl.mli create mode 100644 lib/.gitignore create mode 100644 lib/implementation_files.mk diff --git a/APIs/error.api b/APIs/error.api index 190af67..e315338 100644 --- a/APIs/error.api +++ b/APIs/error.api @@ -27,3 +27,7 @@ void clear_error () << w->error_flag = 0; >> + +(* +event error ??? +*) diff --git a/APIs/filesize.api b/APIs/filesize.api index e57df8e..89a4063 100644 --- a/APIs/filesize.api +++ b/APIs/filesize.api @@ -22,7 +22,7 @@ off_t filesize (pathname path) struct stat buf; if (stat (path, &buf) == -1) { - error_errno ("stat: %s", path); + set_error_errno ("stat: %s", path); return -1; } diff --git a/APIs/mknod.api b/APIs/mknod.api index 2e16a3c..85c6458 100644 --- a/APIs/mknod.api +++ b/APIs/mknod.api @@ -20,7 +20,7 @@ entry_point void mknod_char (pathname path, fileperm perm, uint64 major, uint64 minor) << if (mknod (path, S_IFCHR | perm, makedev (major, minor)) == -1) { - error_errno ("mknod: %s", path); + set_error_errno ("mknod: %s", path); return -1; } return 0; diff --git a/generator/.depend b/generator/.depend index 9fe3110..033feaf 100644 --- a/generator/.depend +++ b/generator/.depend @@ -4,8 +4,11 @@ wrappi_boilerplate.cmx: wrappi_pr.cmx wrappi_boilerplate.cmi wrappi_c.cmi: wrappi_c.cmo: wrappi_pr.cmi wrappi_boilerplate.cmi wrappi_c.cmi wrappi_c.cmx: wrappi_pr.cmx wrappi_boilerplate.cmx wrappi_c.cmi -wrappi_main.cmo: wrappi_pr.cmi wrappi_c.cmi -wrappi_main.cmx: wrappi_pr.cmx wrappi_c.cmx +wrappi_c_impl.cmi: +wrappi_c_impl.cmo: wrappi_pr.cmi wrappi_boilerplate.cmi wrappi_c_impl.cmi +wrappi_c_impl.cmx: wrappi_pr.cmx wrappi_boilerplate.cmx wrappi_c_impl.cmi +wrappi_main.cmo: wrappi_pr.cmi wrappi_c_impl.cmi wrappi_c.cmi +wrappi_main.cmx: wrappi_pr.cmx wrappi_c_impl.cmx wrappi_c.cmx wrappi_pr.cmi: wrappi_pr.cmo: wrappi_pr.cmi wrappi_pr.cmx: wrappi_pr.cmi diff --git a/generator/Makefile.am b/generator/Makefile.am index 1379f1a..872d000 100644 --- a/generator/Makefile.am +++ b/generator/Makefile.am @@ -26,6 +26,8 @@ OCAMLOPTFLAGS = $(OCAMLCFLAGS) SOURCES = \ wrappi_boilerplate.mli \ wrappi_boilerplate.ml \ + wrappi_c_impl.mli \ + wrappi_c_impl.ml \ wrappi_c.mli \ wrappi_c.ml \ wrappi_main.ml \ @@ -36,6 +38,7 @@ SOURCES = \ OBJECTS = \ wrappi_pr.cmo \ wrappi_boilerplate.cmo \ + wrappi_c_impl.cmo \ wrappi_c.cmo \ wrappi_main.cmo diff --git a/generator/wrappi_c.mli b/generator/wrappi_c.mli index 9c4991d..958942c 100644 --- a/generator/wrappi_c.mli +++ b/generator/wrappi_c.mli @@ -16,4 +16,6 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) +(** Generate the C bindings. *) + val generate : Wrappi_types.api -> unit diff --git a/generator/wrappi_c_impl.ml b/generator/wrappi_c_impl.ml new file mode 100644 index 0000000..50673fc --- /dev/null +++ b/generator/wrappi_c_impl.ml @@ -0,0 +1,98 @@ +(* 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 generate_implementation ep = + generate_header CStyle LGPLv2plus; + + pr "\ +#include + +#include +#include + +#include \"wrappi.h\" + +#include \"internal.h\" + +" + + (* Depending on whether this is a local or remote function, include + * different definitions here. + *) + (*if ep.ep_local then ... *) + +(* 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 diff --git a/generator/wrappi_c_impl.mli b/generator/wrappi_c_impl.mli new file mode 100644 index 0000000..eeaedb8 --- /dev/null +++ b/generator/wrappi_c_impl.mli @@ -0,0 +1,21 @@ +(* 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. + *) + +(** Generate the C implementation of the library. *) + +val generate : Wrappi_types.api -> unit diff --git a/generator/wrappi_main.ml b/generator/wrappi_main.ml index ffcef60..8f92cb1 100644 --- a/generator/wrappi_main.ml +++ b/generator/wrappi_main.ml @@ -113,6 +113,7 @@ Run it from the top source directory using the command exit 1); (* Generate code. *) + Wrappi_c_impl.generate api; Wrappi_c.generate api; printf "generated %d lines of code in %d files\n" diff --git a/lib/.gitignore b/lib/.gitignore new file mode 100644 index 0000000..51ea85c --- /dev/null +++ b/lib/.gitignore @@ -0,0 +1,8 @@ +/error-clear_error.c +/handle-connect.c +/error-error.c +/filesize-filesize.c +/mkdir-mkdir.c +/mknod-mknod_char.c +/handle-set_hostname.c +/handle-set_scheme.c diff --git a/lib/Makefile.am b/lib/Makefile.am index 937c29f..d634a20 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -15,6 +15,8 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +include implementation_files.mk + include_HEADERS = wrappi.h lib_LTLIBRARIES = libwrappi.la @@ -26,4 +28,6 @@ libwrappi_la_LDFLAGS = -version-info $(LIB_MINOR):0:$(LIB_MINOR) libwrappi_la_SOURCES = \ wrappi.h \ - wrappi.c + wrappi.c \ + $(local_implementation_files) \ + $(remote_implementation_files) diff --git a/lib/implementation_files.mk b/lib/implementation_files.mk new file mode 100644 index 0000000..8760929 --- /dev/null +++ b/lib/implementation_files.mk @@ -0,0 +1,32 @@ +# wrappi generated file +# WARNING: THIS FILE IS GENERATED FROM: +# generator/wrappi_*.ml +# ANY CHANGES YOU MAKE TO THIS FILE WILL BE LOST. +# +# 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. + +local_implementation_files := \ + error-clear_error.c \ + handle-connect.c \ + error-error.c \ + handle-set_hostname.c \ + handle-set_scheme.c + +remote_implementation_files := \ + filesize-filesize.c \ + mkdir-mkdir.c \ + mknod-mknod_char.c -- 1.8.3.1