Skip to content

Commit

Permalink
Add --config flag
Browse files Browse the repository at this point in the history
* Added the flag `--config` for all commands, which will set the
configuration file
* Added the global flag `--print-config`, which will print the default
configuration file
  • Loading branch information
jacob-carlborg committed Jan 9, 2021
1 parent ed12453 commit ff01efe
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 55 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* Updated the D frontend DLP is using to DMD 2.095.0
* Added the flag `--target-architecture` for all commands, which specifies the
target architecture
* Added the flag `--config` for all commands, which will set the configuration
file
* Added the global flag `--print-config`, which will print the default
configuration file

#### Bugs Fixed

Expand Down
6 changes: 5 additions & 1 deletion source/dlp/commands/utility.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import dmd.target : Target;
mixin template StandardConfig()
{
import dmd.target : Target;
import dlp.core.optional : Optional, none;

string[] importPaths = [];
string[] stringImportPaths = [];
string[] versionIdentifiers = [];
Optional!string configFilename = none;
Target.Architecture architecture = Target.defaultArchitecture;
}

Expand Down Expand Up @@ -90,10 +92,12 @@ Module runParser(Config, Ast = ASTCodegen)(
import dmd.globals : global;
import dmd.target : is64bit;

import dlp.core.optional : or;

global.params.mscoff = config.architecture.is64bit;
initDMD(null, config.versionIdentifiers, ContractChecks(), config.architecture);

findImportPaths
findImportPaths(config.configFilename.or(""))
.chain(config.importPaths)
.each!addImport;

Expand Down
2 changes: 1 addition & 1 deletion source/dlp/core/optional.d
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ struct Optional(T)
assert(b.empty);
}

T front()
inout(T) front() inout
{
return get;
}
Expand Down
19 changes: 18 additions & 1 deletion source/dlp/driver/application.d
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ private:
@("frontend-version", "Print the version of the frontend DLP is using")
bool frontendVersion;

@("print-config", "Print the configuration file that will be used and exit.")
bool printConfig;

bool help;
Optional!string command;
string[] remainingArgs;
Expand Down Expand Up @@ -109,7 +112,7 @@ private:
import std.format : format;

if (auto invoker = command in commands)
return (*invoker)._run(args);
return (*invoker).start(args);
else
throw new CliException(format!`Unrecognized command "%s"`(command));
}
Expand Down Expand Up @@ -198,6 +201,12 @@ private:
return ExitStatus.stop;
}

if (arguments.printConfig)
{
printConfig();
return ExitStatus.stop;
}

if (arguments.command.empty)
{
stderr.writeln("No command specified");
Expand Down Expand Up @@ -226,6 +235,14 @@ private:
writeln(global.versionString);
}

void printConfig()
{
import std.stdio : writeln;
import dmd.frontend : findConfigFilename;

writeln(findConfigFilename());
}

void printHelp(const ref Arguments arguments)
{
import std.format : format;
Expand Down
57 changes: 19 additions & 38 deletions source/dlp/driver/command.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,36 @@ module dlp.driver.command;

import dlp.core.optional : Optional;

struct DefaultConfig
{
import dlp.commands.utility : StandardConfig;

mixin StandardConfig;
}

mixin template StandardArguments()
{
import dmd.target : Target;

import dlp.driver.command : DefaultConfig;

@("import-path|i", "Add <path> as an import path.")
string[] importPaths;

@("string-import-path", "Add <path> as a string import path.")
string[] stringImportPaths;

@("version", "Specify <version> version identifier.")
string[] versionIdentifiers;

@("target-architecture", "Set <arch> as the target architecture [default].")
Target.Architecture architecture = DefaultConfig().architecture;
}

abstract class BaseCommand
{
abstract string name() const;
abstract string shortHelp() const;
abstract string usageHeader() const;

// Not part of the public API.
abstract bool _run(string[] rawArgs) const;
bool start(string[] rawArgs)
{
beforeRun();
return _run(rawArgs);
}

protected void beforeRun()
{

}

// Not part of the public API.
protected abstract bool _run(string[] rawArgs);
}

abstract class Command(Arguments = void) : BaseCommand
{
static if (is(Arguments == void))
abstract void run(const string[] remainingArgs) const;
else
abstract void run(ref Arguments args, const string[] remainingArgs) const;
static if (!is(Arguments == void))
protected Arguments arguments;

abstract void run(const string[] remainingArgs) const;

override bool _run(string[] rawArgs) const
protected override bool _run(string[] rawArgs)
{
static if (is(Arguments == void))
run(rawArgs[1 .. $]);
Expand All @@ -54,11 +40,6 @@ abstract class Command(Arguments = void) : BaseCommand
import std.format : format;
import dlp.driver.cli : parseCommandLine, printHelp;

static if (is(Arguments : Object))
auto arguments = new Arguments;
else
Arguments arguments;

auto result = parseCommandLine(rawArgs, arguments);

if (result.helpWanted)
Expand All @@ -68,7 +49,7 @@ abstract class Command(Arguments = void) : BaseCommand
return true;
}

run(arguments, rawArgs[1 .. $]);
run(rawArgs[1 .. $]);
}

return true;
Expand Down
41 changes: 41 additions & 0 deletions source/dlp/driver/commands/dlp_command.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module dlp.driver.commands.dlp_command;

import dlp.driver.command : Command;

mixin template StandardArguments()
{
import dmd.target : Target;

@("import-path|i", "Add <path> as an import path.")
string[] importPaths;

@("string-import-path", "Add <path> as a string import path.")
string[] stringImportPaths;

@("version", "Specify <version> version identifier.")
string[] versionIdentifiers;

@("config", "Use <filename> as the configuration file.")
string configFilename;

@("target-architecture", "Set <arch> as the target architecture [default].")
Target.Architecture architecture;
}

abstract class DlpCommand(Arguments) : Command!Arguments
{
override void beforeRun()
{
import dmd.frontend : findConfigFilename;

static struct TempConfig
{
import dlp.commands.utility : StandardConfig;

mixin StandardConfig;
}

arguments.configFilename = findConfigFilename();
arguments.architecture = TempConfig().architecture;
}
}
13 changes: 6 additions & 7 deletions source/dlp/driver/commands/infer_attributes.d
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module dlp.driver.commands.infer_attributes;

import dlp.commands.infer_attributes : Config;
import dlp.driver.command : Command;
import dlp.driver.commands.dlp_command : DlpCommand;

private struct Arguments
{
import dlp.driver.command : StandardArguments;
import dlp.driver.commands.dlp_command : StandardArguments;

mixin StandardArguments;

Expand All @@ -19,7 +19,7 @@ private Config toConfig(const ref Arguments self) pure nothrow @nogc @safe
return self.toConfig!Config;
}

class InferAttributes : Command!(Arguments)
class InferAttributes : DlpCommand!(Arguments)
{
import dmd.func : FuncDeclaration;

Expand Down Expand Up @@ -48,7 +48,7 @@ class InferAttributes : Command!(Arguments)
"functions that are not already declared.");
}

override void run(ref Arguments args, const string[] remainingArgs) const
override void run(const string[] remainingArgs) const
{
import std.algorithm : each, map, sort;
import std.array : array, empty;
Expand All @@ -73,7 +73,7 @@ class InferAttributes : Command!(Arguments)

remainingArgs
.map!(e => tuple(e, readText(e)))
.flatMap!(e => inferAttributes(e.expand, args.toConfig).byKeyValue)
.flatMap!(e => inferAttributes(e.expand, arguments.toConfig).byKeyValue)
.map!(e => toInferredAttributes(e.key, e.value))
.array
.sort!sortByLocation
Expand Down Expand Up @@ -149,10 +149,9 @@ import dlp.core.test;
import std.exception : assertThrown;
import dlp.driver.utility : MissingArgumentException;

Arguments arguments;
string[] inputFiles = [];

assertThrown!MissingArgumentException(
new InferAttributes().run(arguments, inputFiles)
new InferAttributes().run(inputFiles)
);
}
13 changes: 6 additions & 7 deletions source/dlp/driver/commands/leaf_functions.d
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module dlp.driver.commands.leaf_functions;

import dlp.commands.leaf_functions : Config;
import dlp.driver.command : Command;
import dlp.driver.commands.dlp_command : DlpCommand;

private struct Arguments
{
import dlp.driver.command : StandardArguments;
import dlp.driver.commands.dlp_command : StandardArguments;

mixin StandardArguments;
}
Expand All @@ -16,7 +16,7 @@ private Config toConfig(const ref Arguments self) pure nothrow @nogc @safe
return self.toConfig!Config;
}

class LeafFunctions : Command!(Arguments)
class LeafFunctions : DlpCommand!(Arguments)
{
import dmd.func : FuncDeclaration;

Expand Down Expand Up @@ -44,7 +44,7 @@ class LeafFunctions : Command!(Arguments)
"have a body.");
}

override void run(ref Arguments args, const string[] remainingArgs) const
override void run(const string[] remainingArgs) const
{
import std.algorithm : each, map, sort;
import std.array : array, empty;
Expand All @@ -64,7 +64,7 @@ class LeafFunctions : Command!(Arguments)

remainingArgs
.map!(e => tuple(e, readText(e)))
.flatMap!(e => leafFunctions(e.expand, args.toConfig)[])
.flatMap!(e => leafFunctions(e.expand, arguments.toConfig)[])
.map!toLeafFunction
.array
.sort!sortByLine
Expand Down Expand Up @@ -112,10 +112,9 @@ import dlp.core.test;
import std.exception : assertThrown;
import dlp.driver.utility : MissingArgumentException;

Arguments arguments;
string[] inputFiles = [];

assertThrown!MissingArgumentException(
new LeafFunctions().run(arguments, inputFiles)
new LeafFunctions().run(inputFiles)
);
}

0 comments on commit ff01efe

Please sign in to comment.