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         if(isDubProject && backend == Backend.tup) {
35             throw new Exception("dub integration not supported with the tup backend");
36         }
37     }
38 
39     private bool _isDubProject() @safe nothrow {
40         return buildPath(projectPath, "dub.json").exists ||
41             buildPath(projectPath, "package.json").exists;
42     }
43 
44 }
45 
46 
47 //getopt is @system
48 Options getOptions(string[] args) @trusted {
49     import std.getopt;
50 
51     Options options;
52     try {
53         auto helpInfo = getopt(
54             args,
55             "backend|b", "Backend to use (ninja|make). Mandatory.", &options.backend,
56             "dflags", "D compiler flags.", &options.dflags,
57             "d", "User-defined variables (e.g. -d myvar=foo).", &options.userVars,
58             "dc", "D compiler to use (default dmd).", &options.dCompiler,
59             "cc", "C compiler to use (default gcc).", &options.cCompiler,
60             "cxx", "C++ compiler to use (default g++).", &options.cppCompiler,
61             "nofetch", "Assume dub packages are present (no dub fetch).", &options.noFetch,
62             "per_module", "Compile D files per module (default is per package)", &options.perModule,
63 
64             );
65 
66         if(helpInfo.helpWanted) {
67             defaultGetoptPrinter("Usage: reggae -b <ninja|make> </path/to/project>",
68                                  helpInfo.options);
69             options.help = true;
70         }
71 
72     } catch(ConvException ex) {
73         throw new Exception("Unsupported backend, -b must be one of: make|ninja|tup|binary");
74     }
75 
76     if(args.length > 1) options.projectPath = args[1].absolutePath;
77     options.finalize();
78 
79     return options;
80 }
81 
82 string projectBuildFile(in Options options) @safe pure nothrow {
83     return buildPath(options.projectPath, "reggaefile.d");
84 }