stdlib: Fix %branch -> %fedora-branch.
[goals.git] / src / cmdline.ml
1 (* Goalfile command line
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 also "let id" in [lexer.mll]. *)
25 let var_regexp =
26   Str.regexp "\\([a-zA-Z_][-a-zA-Z0-9_]*\\)[ \t]*=[ \t]*\\(.*\\)"
27
28 let usage =
29   "\
30 goals: Build software.
31
32  goals [-f Goalfile] ['var=value' ...] ['target' ...]
33
34 For detailed help see goals(1).
35
36 Options:"
37
38 let print_version () =
39   printf "%s %s\n" Config.package_name Config.package_version;
40   exit 0
41
42 (* Get stdlib directory. *)
43 let datadir =
44   try Sys.getenv "GOALS_DATADIR"
45   with Not_found -> Config.datadir // "goals"
46 let stdlibdir = datadir // "stdlib"
47 let prelude_gl_file = stdlibdir // "prelude.gl"
48 let prelude_sh_file = stdlibdir // "prelude.sh"
49
50 let debug_flag = ref false
51 let directory = ref "."
52 let input_file = ref "Goalfile"
53 let includes = ref [stdlibdir]
54 let add_include dir = includes := dir :: !includes
55 let nr_jobs = ref (nprocs ())
56 let silent = ref false
57 let use_prelude = ref true
58
59 let parse () =
60   if not (is_directory stdlibdir) || not (Sys.file_exists prelude_gl_file) then
61     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 ...’"
62       Sys.executable_name stdlibdir;
63
64   let jobshelp =
65     sprintf "jobs Set number of parallel jobs (default: %d)" !nr_jobs in
66   let argspec = [
67     "-C",          Arg.Set_string directory,
68                    "directory Change to directory before running";
69     "-d",          Arg.Set debug_flag,
70                    " Print debug information.";
71     "--directory", Arg.Set_string directory,
72                    "directory Change to directory before running";
73     "-f",          Arg.Set_string input_file,
74                    "filename Set name of Goalfile";
75     "--file",      Arg.Set_string input_file,
76                    "filename Set name of Goalfile";
77     "-I",          Arg.String add_include,
78                    "dir Add include directory";
79     "--include",   Arg.String add_include,
80                    "dir Add include directory";
81     "-j",          Arg.Set_int nr_jobs,
82                    jobshelp;
83     "--jobs",      Arg.Set_int nr_jobs,
84                    jobshelp;
85     "--no-prelude",Arg.Clear use_prelude,
86                    " Do not automatically use prelude.gl from stdlib";
87     "-s",          Arg.Set silent,
88                    " Silent operation";
89     "--silent",    Arg.Set silent,
90                    " Silent operation";
91     "--quiet",     Arg.Set silent,
92                    " Silent operation";
93     "-v",          Arg.Unit print_version,
94                    " Print version and exit";
95     "--version",   Arg.Unit print_version,
96                    " Print version and exit";
97   ] in
98   let argspec = Arg.align argspec in
99   let args = ref [] in
100   let anon_fun s = args := s :: !args in
101   Arg.parse argspec anon_fun usage;
102
103   (* Check various params are sensible. *)
104   if !nr_jobs < 1 then
105     failwithf "%s: -j must be >= 1" Sys.executable_name;
106
107   let args = List.rev !args in
108
109   (* Get the anon var assignments and targets. *)
110   let anon_vars, targets =
111     List.partition (
112       fun arg -> Str.string_match var_regexp arg 0
113     ) args in
114   let anon_vars =
115     List.map (
116       fun arg ->
117         ignore (Str.string_match var_regexp arg 0);
118         let name = Str.matched_group 1 arg in
119         let expr = Str.matched_group 2 arg in
120         (name, expr)
121     ) anon_vars in
122
123   anon_vars, targets
124
125 let debug_flag () = !debug_flag
126
127 (* Create the debug function. *)
128 let debug fs =
129   let display str = if debug_flag () then prerr_endline str in
130   ksprintf display fs
131
132 let directory () = !directory
133 let input_file () = !input_file
134
135 (* Don't reverse includes - we want newer -I options to take precedence. *)
136 let includes () = !includes
137
138 let nr_jobs () = !nr_jobs
139 let silent () = !silent
140 let use_prelude () = !use_prelude