From 349ba0ff3bded60e8035d5896a6d5f689a632ae7 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sun, 12 Jan 2020 17:54:08 +0000 Subject: [PATCH] utils: Implement -j properly. It now picks the default value from the number of cores. --- Goalfile.in | 10 ++++++++++ Makefile.in | 3 ++- TODO | 3 +++ configure.ac | 4 ++++ src/cmdline.ml | 8 +++++--- src/utils-c.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/utils.ml | 2 ++ src/utils.mli | 3 +++ 8 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 src/utils-c.c diff --git a/Goalfile.in b/Goalfile.in index 623a09d..d38e70f 100644 --- a/Goalfile.in +++ b/Goalfile.in @@ -51,11 +51,15 @@ goal maintainer-clean = : clean { #---------------------------------------------------------------------- # Build the goals tool itself. +let CC = "@CC@" +let OCAMLLIB = "@OCAMLLIB@" let MENHIR = "@MENHIR@" let OCAMLDEP = "@OCAMLDEP@" let OCAMLFIND = "@OCAMLFIND@" let OCAMLLEX = "@OCAMLLEX@" # XXX +let CFLAGS = [ "-g", "-O2", "-I%OCAMLLIB", "-I." ] +#let CFLAGS = "@CFLAGS@ -I%OCAMLLIB -I." let OCAMLFLAGS = [ "-g", "-safe-string", "-warn-error", "CDEFLMPSUVYZX+52-3" ] let OCAMLPACKAGES = [ "-package", "str,unix,threads", "-I", "src", "-thread" ] #let OCAMLFLAGS = "@OCAMLFLAGS@" @@ -64,6 +68,7 @@ let OCAMLPACKAGES = [ "-package", "str,unix,threads", "-I", "src", "-thread" ] let objects = [ # These must be in dependency order. "src/config.cmx", + "src/utils-c.o", "src/utils.cmx", "src/cmdline.cmx", "src/jobs.cmx", @@ -79,6 +84,11 @@ let objects = [ goal tool = : ocaml_link ("src/goals", objects) ; +# C code. +"src/utils-c.o" : "src/utils-c.c" { + %CC %CFLAGS -c %< -o %@ +} + # Parser. "src/parser.mli", "src/parser.ml" : "src/parser.mly" { %MENHIR --explain %< diff --git a/Makefile.in b/Makefile.in index 20d0a49..7fbf0de 100644 --- a/Makefile.in +++ b/Makefile.in @@ -38,7 +38,8 @@ src/goals: echo 'val lexer_read : (Lexing.lexbuf -> token) option ref' >> src/parser.mli echo 'val eval_substitute : (Ast.env -> Ast.loc -> Ast.substs -> string) option ref' >> src/parser.mli $(OCAMLLEX) src/lexer.mll - $(OCAMLFIND) opt $(OCAMLFLAGS) $(OCAMLPACKAGES) -I src \ + $(OCAMLFIND) opt $(OCAMLFLAGS) $(OCAMLPACKAGES) -I . -I src \ + src/utils-c.c \ $$($(OCAMLDEP) -sort src/*.mli src/*.ml) \ -linkpkg -o $@ diff --git a/TODO b/TODO index 51bd05a..992f8e3 100644 --- a/TODO +++ b/TODO @@ -24,6 +24,9 @@ Unclear if this would be helpful or not. Implement more make functions, see: https://www.gnu.org/software/make/manual/html_node/Functions.html#Functions +Split "flags" strings. eg. Currently there is no way to pass +$CFLAGS from autoconf into a goalfile. + Make re-execs itself if the Makefile (or any include) changes, and goals should do something similar. See: https://www.gnu.org/software/make/manual/html_node/Remaking-Makefiles.html diff --git a/configure.ac b/configure.ac index e8fd7e5..51ea721 100644 --- a/configure.ac +++ b/configure.ac @@ -29,6 +29,10 @@ AC_PROG_CPP AC_C_PROTOTYPES test "x$U" != "x" && AC_MSG_ERROR([Compiler not ANSI compliant]) +dnl C headers and functions. +AC_CHECK_HEADERS([sys/sysinfo.h]) +AC_CHECK_FUNCS([get_nprocs]) + dnl Check for basic OCaml environment and findlib. AC_PROG_OCAML if test "x$OCAMLC" = "xno"; then diff --git a/src/cmdline.ml b/src/cmdline.ml index 0ac1ad5..f83d5eb 100644 --- a/src/cmdline.ml +++ b/src/cmdline.ml @@ -51,7 +51,7 @@ let directory = ref "." let input_file = ref "Goalfile" let includes = ref [stdlibdir] let add_include dir = includes := dir :: !includes -let nr_jobs = ref 4 (* XXX use nproc *) +let nr_jobs = ref (nprocs ()) let use_prelude = ref true let parse () = @@ -59,6 +59,8 @@ let parse () = failwithf "%s: cannot find the standard library directory, expected %s. If the standard library directory is in a non-standard location then set GOALS_DATADIR. If you can trying to run goals from the build directory then use ‘./run goals ...’" Sys.executable_name stdlibdir; + let jobshelp = + sprintf "jobs Set number of parallel jobs (default: %d)" !nr_jobs in let argspec = [ "-C", Arg.Set_string directory, "directory Change to directory before running"; @@ -75,9 +77,9 @@ let parse () = "--include", Arg.String add_include, "dir Add include directory"; "-j", Arg.Set_int nr_jobs, - "jobs Set number of parallel jobs"; + jobshelp; "--jobs", Arg.Set_int nr_jobs, - "jobs Set number of parallel jobs"; + jobshelp; "--no-prelude",Arg.Clear use_prelude, " Do not automatically use prelude.gl from stdlib"; "-v", Arg.Unit print_version, diff --git a/src/utils-c.c b/src/utils-c.c new file mode 100644 index 0000000..3c6554f --- /dev/null +++ b/src/utils-c.c @@ -0,0 +1,45 @@ +/* Goalfile utilities + * Copyright (C) 2020 Richard W.M. Jones + * Copyright (C) 2020 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. + */ + +#include + +#include +#include + +#include +#include + +#ifdef HAVE_SYS_SYSINFO_H +#include +#endif + +value +nprocs (value unitv) +{ + CAMLparam1 (unitv); + int n; + +#ifdef HAVE_GET_NPROCS + n = get_nprocs (); +#else + n = 1; +#endif + + CAMLreturn (Val_int (n)); +} diff --git a/src/utils.ml b/src/utils.ml index 3d1b0c5..c4722a8 100644 --- a/src/utils.ml +++ b/src/utils.ml @@ -19,6 +19,8 @@ open Printf +external nprocs : unit -> int = "nprocs" + let failwithf fs = ksprintf failwith fs let (//) = Filename.concat diff --git a/src/utils.mli b/src/utils.mli index 1979a34..c63db29 100644 --- a/src/utils.mli +++ b/src/utils.mli @@ -17,6 +17,9 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) +val nprocs : unit -> int +(** Return the number of processors on the system. *) + val failwithf : ('a, unit, string, 'b) format4 -> 'a (** Like [failwith] but supports printf-like arguments. *) -- 1.8.3.1