1 module reggae.options;
2 
3 import reggae.types: Backend;
4 
5 import std.file: thisExePath;
6 import std.conv: ConvException;
7 import std.path: absolutePath, buildPath;
8 import std.file: exists;
9 
10 struct Options {
11     Backend backend;
12     string projectPath;
13     string dflags;
14     string reggaePath;
15     string[string] userVars;
16     string cCompiler;
17     string cppCompiler;
18     string dCompiler;
19     bool noFetch;
20     bool help;
21     bool perModule;
22     bool isDubProject;
23 
24     //finished setup
25     void finalize() @safe{
26         reggaePath = thisExePath();
27 
28         if(!cCompiler)   cCompiler   = "gcc";
29         if(!cppCompiler) cppCompiler = "g++";
30         if(!dCompiler)   dCompiler   = "dmd";
31 
32         isDubProject = _isDubProject;
33     }
34 
35     private bool _isDubProject() @safe nothrow {
36         return buildPath(projectPath, "dub.json").exists ||
37             buildPath(projectPath, "package.json").exists;
38     }
39 
40 }
41 
42 
43 //getopt is @system
44 Options getOptions(string[] args) @trusted {
45     import std.getopt;
46 
47     Options options;
48     try {
49         auto helpInfo = getopt(
50             args,
51             "backend|b", "Backend to use (ninja|make). Mandatory.", &options.backend,
52             "dflags", "D compiler flags.", &options.dflags,
53             "d", "User-defined variables (e.g. -d myvar=foo).", &options.userVars,
54             "dc", "D compiler to use (default dmd).", &options.dCompiler,
55             "cc", "C compiler to use (default gcc).", &options.cCompiler,
56             "cxx", "C++ compiler to use (default g++).", &options.cppCompiler,
57             "nofetch", "Assume dub packages are present (no dub fetch).", &options.noFetch,
58             "per_module", "Compile D files per module (default is per package)", &options.perModule,
59 
60             );
61 
62         if(helpInfo.helpWanted) {
63             defaultGetoptPrinter("Usage: reggae -b <ninja|make> </path/to/project>",
64                                  helpInfo.options);
65             options.help = true;
66         }
67 
68     } catch(ConvException ex) {
69         throw new Exception("Unsupported backend, -b must be make or ninja");
70     }
71 
72     if(args.length > 1) options.projectPath = args[1].absolutePath;
73     options.finalize();
74 
75     return options;
76 }