X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;ds=sidebyside;f=generator%2Fwrappi_main.ml;h=42b21406d5bfc9f2a8aa065adc212d2d4adf9d34;hb=68483fce4f959886866af28c1c3342c2a6fcb410;hp=aaf3719b3da188e42fdbd9025277ca23062b9193;hpb=bb1329db94910f6739c839bee52177f1b3c4e695;p=wrappi.git diff --git a/generator/wrappi_main.ml b/generator/wrappi_main.ml index aaf3719..42b2140 100644 --- a/generator/wrappi_main.ml +++ b/generator/wrappi_main.ml @@ -16,12 +16,16 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) +open Unix open Printf +open Wrappi_pr + let eps = Wrappi_globals.get_entry_points () +let nr_eps = List.length eps let dump_and_exit () = - printf "entry points:\n"; + printf "entry points (%d):\n" nr_eps; List.iter (fun ep -> printf " %s\n" (Wrappi_types.string_of_entry_point ep) @@ -37,11 +41,15 @@ let () = in let argspec = Arg.align [ - "--dump", Arg.Unit dump_and_exit, " Dump API data and exit."; - "--version", Arg.Unit display_version, " Display version number and exit."; + "--dump", Arg.Unit dump_and_exit, " Dump API data and exit"; + "--version", Arg.Unit display_version, " Display version number and exit"; ] in let anon_fun str = raise (Arg.Bad "generator: unknown parameter") in - let usage_msg = "wrappi generator: generates lots of code + let usage_msg = " +NAME + wrappi generator - generate a lot of code + +SYNOPSIS To run the generator normally (note it MUST be run from the top level SOURCE directory): @@ -55,5 +63,53 @@ Options are for debugging only: OPTIONS" in Arg.parse argspec anon_fun usage_msg +let perror msg = function + | Unix_error (err, _, _) -> + eprintf "%s: %s\n" msg (error_message err) + | exn -> + eprintf "%s: %s\n" msg (Printexc.to_string exn) + let () = - printf "generator, %d entry points\n" (List.length eps) + printf "generator, %d entry points\n" nr_eps; + + (* Acquire a lock so parallel builds won't run the generator + * simultaneously. It's assumed that ./configure.ac only exists in + * the top level source directory. Note the lock is released + * implicitly when the program exits. + *) + let lock_fd = + try openfile "configure.ac" [O_RDWR] 0 + with + | Unix_error (ENOENT, _, _) -> + eprintf "\ +You are probably running this from the wrong directory. +Run it from the top source directory using the command + make -C generator stamp-generator +"; + exit 1 + | exn -> + perror "open: configure.ac" exn; + exit 1 in + + (try lockf lock_fd F_LOCK 1 + with exn -> + perror "lock: configure.ac" exn; + exit 1); + + (* Create a structure that we'll pass around to each generator function. *) + let api = { + Wrappi_types.api_entry_points = eps + } in + + (* Generate code. *) + Wrappi_c.generate api; + + printf "generated %d lines of code in %d files\n" + (get_lines_generated ()) (List.length (get_files_generated ())); + + (* Create the stamp file last and unconditionally. This is used + * by the Makefile to know when we must rerun the generator. + *) + let chan = open_out "generator/stamp-generator" in + fprintf chan "1\n"; + close_out chan