stdlib/fedora: Use rpmdev-bumpspec -r flag for < Rawhide builds.
[goals.git] / src / main.ml
1 (* Goalfile parser
2  * Copyright (C) 2019 Richard W.M. Jones
3  * Copyright (C) 2019 Red Hat Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *)
19
20 open Printf
21
22 open Utils
23
24 (* See comment in parser.mly. *)
25 let () =
26   Parser.lexer_read := Some Lexer.read;
27   Parser.eval_substitute := Some Eval.substitute
28
29 let main () =
30   (* Handle the command line. *)
31   let anon_vars, targets = Cmdline.parse () in
32
33   (* Change directory (-C option). *)
34   Sys.chdir (Cmdline.directory ());
35
36   (* Create a temporary directory which is always cleaned up at exit. *)
37   let tmpdir =
38     let temp_dir = try Unix.getenv "TMPDIR" with Not_found -> "/var/tmp" in
39     let t = Filename.temp_file ~temp_dir "goals" ".d" in
40     Unix.unlink t;
41     Unix.mkdir t 0o700;
42     at_exit (
43       fun () ->
44         let cmd = sprintf "rm -rf %s" (Filename.quote t) in
45         ignore (Sys.command cmd)
46     );
47     t in
48
49   (* Create the initial environment, containing the system environment
50    * and a few other standard strings.
51    *)
52   let env =
53     Array.fold_left (
54       fun env environ ->
55         let k, v = split "=" environ in
56         Ast.Env.add k (Ast.EConstant (Ast.noloc, Ast.CString v)) env
57     ) Ast.Env.empty (Unix.environment ()) in
58   let env =
59     Ast.Env.add "tmpdir" (Ast.EConstant (Ast.noloc, Ast.CString tmpdir)) env in
60   let env =
61     Ast.Env.add "stdlib"
62       (Ast.EConstant (Ast.noloc, Ast.CString Cmdline.stdlibdir))
63       env in
64   (*let env =
65     if Cmdline.debug_flag then Ast.Env.add "debug" (Ast.EConstant (noloc, Ast.CBool true)) env else env in *)
66
67   (* Parse the prelude. *)
68   let env =
69     if Cmdline.use_prelude () then
70       Parse.parse_goalfile env Cmdline.prelude_gl_file
71     else env in
72
73   (* Parse the input file. *)
74   let env = Parse.parse_goalfile env (Cmdline.input_file ()) in
75
76   (* Parse the command line assignments. *)
77   let env =
78     List.fold_left (
79       fun env (name, expr) ->
80         let expr = Parse.parse_expr "commandline" expr in
81         Ast.Env.add name expr env
82     ) env anon_vars in
83
84   (* Parse the target expressions. *)
85   let targets = List.map (Parse.parse_expr "commandline") targets in
86
87   (* If no target was set on the command line, use "all ()". *)
88   let targets =
89     if targets <> [] then targets
90     else [Ast.ECall (Ast.noloc, "all", [])] in
91
92   if Cmdline.debug_flag () then
93     Ast.print_env stderr env;
94
95   (* Construct the dependency DAG with the root(s) being the targets. *)
96   let dag = Deps.create env targets in
97
98   (* Run the jobs. *)
99   let state = Deps.new_state dag Run.goal_runner Run.exists_runner in
100   let next_job () = Deps.next_job state in
101   let retire_job job = Deps.retire_job state job in
102   let fail_job job = Deps.fail_job state job in
103   let string_of_job job = Deps.string_of_job job in
104   Jobs.run next_job retire_job fail_job string_of_job
105
106 let () =
107   try main ()
108   with
109   | Failure msg | Sys_error msg ->
110      prerr_endline ("*** error: " ^ msg);
111      exit 1