1 (* Goalfile command line
2 * Copyright (C) 2019 Richard W.M. Jones
3 * Copyright (C) 2019 Red Hat Inc.
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.
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.
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.
24 (* See also "let id" in [lexer.mll]. *)
26 Str.regexp "\\([a-zA-Z_][-a-zA-Z0-9_]*\\)[ \t]*=[ \t]*\\(.*\\)"
30 goals: Build software.
32 goals [-f Goalfile] ['var=value' ...] ['target' ...]
34 For detailed help see goals(1).
38 let print_version () =
39 printf "%s %s\n" Config.package_name Config.package_version;
42 (* Get stdlib directory. *)
44 try Sys.getenv "GOALS_DATADIR" with Not_found -> Config.datadir
45 let stdlibdir = datadir // "stdlib"
46 let prelude_gl_file = stdlibdir // "prelude.gl"
47 let prelude_sh_file = stdlibdir // "prelude.sh"
49 if not (is_directory stdlibdir) || not (Sys.file_exists prelude_gl_file) then
50 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 ...’"
51 Sys.executable_name stdlibdir
54 debug_flag, directory, includes, nr_jobs, use_prelude, anon_vars, targets =
56 let debug_flag = ref false in
57 let directory = ref "." in
58 let input_file = ref "Goalfile" in
59 let includes = ref [stdlibdir] in
60 let add_include dir = includes := dir :: !includes in
61 let nr_jobs = ref 4 (* XXX use nproc *) in
62 let use_prelude = ref true in
65 "-C", Arg.Set_string directory,
66 "directory Change to directory before running";
67 "-d", Arg.Set debug_flag,
68 " Print debug information.";
69 "--directory", Arg.Set_string directory,
70 "directory Change to directory before running";
71 "-f", Arg.Set_string input_file,
72 "filename Set name of Goalfile";
73 "--file", Arg.Set_string input_file,
74 "filename Set name of Goalfile";
75 "-I", Arg.String add_include,
76 "dir Add include directory";
77 "--include", Arg.String add_include,
78 "dir Add include directory";
79 "-j", Arg.Set_int nr_jobs,
80 "jobs Set number of parallel jobs";
81 "--jobs", Arg.Set_int nr_jobs,
82 "jobs Set number of parallel jobs";
83 "--no-prelude",Arg.Clear use_prelude,
84 " Do not automatically use prelude.gl from stdlib";
85 "-v", Arg.Unit print_version,
86 " Print version and exit";
87 "--version", Arg.Unit print_version,
88 " Print version and exit";
90 let argspec = Arg.align argspec in
91 let anon_fun s = args := s :: !args in
92 Arg.parse argspec anon_fun usage;
94 let args = List.rev !args in
95 let debug_flag = !debug_flag in
96 let directory = !directory in
97 let input_file = !input_file in
98 (* Don't reverse includes - we want newer -I options to take precedence. *)
99 let includes = !includes in
100 let nr_jobs = !nr_jobs in
102 failwithf "%s: -j must be >= 1" Sys.executable_name;
103 let use_prelude = !use_prelude in
105 (* Get the anon var assignments and targets. *)
106 let anon_vars, targets =
108 fun arg -> Str.string_match var_regexp arg 0
113 ignore (Str.string_match var_regexp arg 0);
114 let name = Str.matched_group 1 arg in
115 let expr = Str.matched_group 2 arg in
120 debug_flag, directory, includes, nr_jobs, use_prelude, anon_vars, targets
122 (* Create the debug function. *)
124 let display str = if debug_flag then prerr_endline str in