1 /**
2  This module contains the core data definitions that allow a build
3  to be expressed in. $(D Build) is a container struct for top-level
4  targets, $(D Target) is the heart of the system.
5  */
6 
7 module reggae.build;
8 
9 import reggae.ctaa;
10 import reggae.rules.common: Language, getLanguage;
11 import reggae.options;
12 
13 import std.string: replace;
14 import std.algorithm;
15 import std.path: buildPath, dirSeparator;
16 import std.typetuple: allSatisfy;
17 import std.traits: Unqual, isSomeFunction, ReturnType, arity;
18 import std.array: array, join;
19 import std.conv;
20 import std.exception;
21 import std.typecons;
22 import std.range;
23 import std.typecons;
24 
25 
26 /**
27  Contains the top-level targets.
28  */
29 struct Build {
30     static struct TopLevelTarget {
31         Target target;
32         bool optional;
33     }
34 
35     private TopLevelTarget[] _targets;
36 
37     this(Target[] targets) {
38         _targets = targets.map!createTopLevelTarget.array;
39     }
40 
41     this(R)(R targets) if(isInputRange!R && is(Unqual!(ElementType!R) == TopLevelTarget)) {
42         _targets = targets.array;
43     }
44 
45     this(T...)(T targets) {
46         foreach(t; targets) {
47             //the constructor needs to go from Target to TopLevelTarget
48             //and accepts functions that return a parameter as well as parameters themselves
49             //if a function, call it, if not, take the value
50             //if the value is Target, call createTopLevelTarget, if not, take it as is
51             static if(isSomeFunction!(typeof(t)) && is(ReturnType!(typeof(t))) == Target) {
52                 _targets ~= createTopLevelTarget(t());
53             } else static if(is(Unqual!(typeof(t)) == TopLevelTarget)) {
54                 _targets ~= t;
55             } else {
56                 _targets ~= createTopLevelTarget(t);
57             }
58         }
59     }
60 
61     auto targets() @trusted pure nothrow {
62         return _targets.map!(a => a.target);
63     }
64 
65     auto defaultTargets() @trusted pure nothrow {
66         return _targets.filter!(a => !a.optional).map!(a => a.target);
67     }
68 
69     string defaultTargetsString(in string projectPath) @trusted pure {
70         return defaultTargets.map!(a => a.expandOutputs(projectPath).join(" ")).join(" ");
71     }
72 
73     auto range() @safe pure {
74         import reggae.range;
75         return UniqueDepthFirst(this);
76     }
77 
78     ubyte[] toBytes(in Options options) @safe pure {
79         ubyte[] bytes;
80         bytes ~= setUshort(cast(ushort)targets.length);
81         foreach(t; targets) bytes ~= t.toBytes(options);
82         return bytes;
83     }
84 
85     static Build fromBytes(ubyte[] bytes) @trusted {
86         immutable length = getUshort(bytes);
87         auto build = Build();
88         foreach(_; 0 .. length) {
89             build._targets ~= TopLevelTarget(Target.fromBytes(bytes), false);
90         }
91         return build;
92     }
93 }
94 
95 
96 /**
97  Designate a target as optional so it won't be built by default.
98  "Compile-time" version that can be aliased
99  */
100 Build.TopLevelTarget optional(alias targetFunc)() {
101     return optional(targetFunc());
102 }
103 
104 /**
105  Designate a target as optional so it won't be built by default.
106  */
107 Build.TopLevelTarget optional(Target target) {
108     return createTopLevelTarget(target, true);
109 }
110 
111 Build.TopLevelTarget createTopLevelTarget(Target target, bool optional = false) {
112     return Build.TopLevelTarget(target.inTopLevelObjDirOf(topLevelDirName(target), Yes.topLevel),
113                                 optional);
114 }
115 
116 
117 immutable gBuilddir = "$builddir";
118 immutable gProjdir  = "$project";
119 
120 //a directory for each top-level target no avoid name clashes
121 //@trusted because of map -> buildPath -> array
122 Target inTopLevelObjDirOf(Target target, string dirName, Flag!"topLevel" isTopLevel = No.topLevel) @trusted {
123     //leaf targets only get the $builddir expansion, nothing else
124     //this is because leaf targets are by definition in the project path
125 
126     //every other non-top-level target gets its outputs placed in a directory
127     //specific to its top-level parent
128 
129     if(target._outputs.any!(a => a.startsWith(gBuilddir) || a.startsWith(gProjdir))) {
130          dirName = topLevelDirName(target);
131     }
132 
133     auto outputs = isTopLevel
134         ? target._outputs.map!(a => expandBuildDir(a)).array
135         : target._outputs.map!(a => realTargetPath(dirName, target, a)).array;
136 
137     return Target(outputs,
138                   target._command.expandVariables,
139                   target._dependencies.map!(a => a.inTopLevelObjDirOf(dirName)).array,
140                   target._implicits.map!(a => a.inTopLevelObjDirOf(dirName)).array);
141 }
142 
143 
144 string topLevelDirName(in Target target) @safe pure {
145     import std.path: isAbsolute, buildPath;
146     return target._outputs[0].isAbsolute
147         ? buildPath(target._outputs[0], targetObjsDir(target))
148         : buildPath(".reggae", "objs", targetObjsDir(target));
149 }
150 
151 string targetObjsDir(in Target target) @safe pure {
152     return target._outputs[0].expandBuildDir ~ ".objs";
153 }
154 
155 //targets that have outputs with $builddir or $project in them want to be placed
156 //in a specific place. Those don't get touched. Other targets get
157 //placed in their top-level parent's object directory
158 string realTargetPath(in string dirName, in Target target, in string output) @trusted pure {
159     return target.isLeaf
160         ? output
161         : realTargetPath(dirName, output);
162 }
163 
164 
165 //targets that have outputs with $builddir or $project in them want to be placed
166 //in a specific place. Those don't get touched. Other targets get
167 //placed in their top-level parent's object directory
168 string realTargetPath(in string dirName, in string output) @trusted pure {
169     import std.algorithm: canFind;
170 
171     if(output.startsWith(gProjdir)) return output;
172 
173     return output.canFind(gBuilddir)
174         ? output.expandBuildDir
175         : buildPath(dirName, output);
176 }
177 
178 //replace $builddir with the current directory
179 string expandBuildDir(in string output) @trusted pure {
180     import std.path: buildNormalizedPath;
181     import std.algorithm;
182     return output.
183         splitter.
184         map!(a => a.canFind(gBuilddir) ? a.replace(gBuilddir, ".").buildNormalizedPath : a).
185         join(" ");
186 }
187 
188  enum isTarget(alias T) =
189      is(Unqual!(typeof(T)) == Target) ||
190      is(Unqual!(typeof(T)) == Build.TopLevelTarget) ||
191      isSomeFunction!T && is(ReturnType!T == Target) ||
192      isSomeFunction!T && is(ReturnType!T == Build.TopLevelTarget);
193 
194 unittest {
195     auto  t1 = Target();
196     const t2 = Target();
197     static assert(isTarget!t1);
198     static assert(isTarget!t2);
199     const t3 = Build.TopLevelTarget(Target());
200     static assert(isTarget!t3);
201 }
202 
203 mixin template buildImpl(targets...) if(allSatisfy!(isTarget, targets)) {
204     Build buildFunc() {
205         return Build(targets);
206     }
207 }
208 
209 /**
210  Two variations on a template mixin. When reggae is used as a library,
211  this will essentially build reggae itself as part of the build description.
212 
213  When reggae is used as a command-line tool to generate builds, it simply
214  declares the build function that will be called at run-time. The tool
215  will then compile the user's reggaefile.d with the reggae libraries,
216  resulting in a buildgen executable.
217 
218  In either case, the compile-time parameters of $(D build) are the
219  build's top-level targets.
220  */
221 version(reggaelib) {
222     mixin template build(targets...) if(allSatisfy!(isTarget, targets)) {
223         mixin reggaeGen!(targets);
224     }
225 } else {
226     alias build = buildImpl;
227 }
228 
229 package template isBuildFunction(alias T) {
230     static if(!isSomeFunction!T) {
231         enum isBuildFunction = false;
232     } else {
233         enum isBuildFunction = is(ReturnType!T == Build) && arity!T == 0;
234     }
235 }
236 
237 unittest {
238     Build myBuildFunction() { return Build(); }
239     static assert(isBuildFunction!myBuildFunction);
240     float foo;
241     static assert(!isBuildFunction!foo);
242 }
243 
244 
245 private static auto arrayify(E, T)(T value) {
246     static if(isInputRange!T && is(Unqual!(ElementType!T) == E))
247         return value.array;
248     else static if(is(Unqual!T == E))
249         return [value];
250     else static if(is(Unqual!T == void[])) {
251         E[] nothing;
252         return nothing;
253     } else static if(is(Unqual!T == string))
254         return [E(value)];
255     else {
256         import std.conv: text;
257         static assert(false, text("Can not arraify value of type ", T.stringof));
258     }
259 }
260 
261 
262 /**
263  The core of reggae's D-based DSL for describing build systems.
264  Targets contain outputs, a command to generate those outputs,
265  explicit dependencies and implicit dependencies. All dependencies
266  are themselves $(D Target) structs.
267 
268  The command is given as a string. In this string, certain words
269  have special meaning: $(D $in), $(D $out), $(D $project) and $(D builddir).
270 
271  $(D $in) gets expanded to all explicit dependencies.
272  $(D $out) gets expanded to all outputs.
273  $(D $project) gets expanded to the project directory (i.e. the directory including
274  the source files to build that was given as a command-line argument). This can be
275  useful when build outputs are to be placed in the source directory, such as
276  automatically generated source files.
277  $(D $builddir) expands to the build directory (i.e. where reggae was run from).
278  */
279 struct Target {
280     private string[] _outputs;
281     private Command _command; ///see $(D Command) struct
282     private Target[] _dependencies;
283     private Target[] _implicits;
284 
285     enum Target[] noTargets = [];
286 
287     this(string output) @safe pure nothrow {
288         this(output, "", noTargets, noTargets);
289     }
290 
291     this(O, C)(O outputs, C command) {
292         this(outputs, command, noTargets, noTargets);
293     }
294 
295     this(O, C, D)(O outputs, C command, D dependencies) {
296         this(outputs, command, dependencies, noTargets);
297     }
298 
299     this(O, C, D, I)(O outputs, C command, D dependencies, I implicits) {
300 
301         this._outputs = arrayify!string(outputs);
302 
303         static if(is(C == Command))
304             this._command = command;
305         else
306             this._command = Command(command);
307 
308         this._dependencies = arrayify!Target(dependencies);
309         this._implicits = arrayify!Target(implicits);
310     }
311 
312     /**
313        The outputs without expanding special variables
314      */
315     @property inout(string)[] rawOutputs(in string projectPath = "") @safe pure inout {
316         return _outputs;
317     }
318 
319     @property inout(Target)[] dependencyTargets(in string projectPath = "") @safe pure nothrow inout {
320         return _dependencies;
321     }
322 
323     @property inout(Target)[] implicitTargets(in string projectPath = "") @safe pure nothrow inout {
324         return _implicits;
325     }
326 
327     @property string[] dependenciesInProjectPath(in string projectPath) @safe pure const {
328         return depsInProjectPath(_dependencies, projectPath);
329     }
330 
331     @property string[] implicitsInProjectPath(in string projectPath) @safe pure const {
332         return depsInProjectPath(_implicits, projectPath);
333     }
334 
335     bool isLeaf() @safe pure const nothrow {
336         return _dependencies is null && _implicits is null && getCommandType == CommandType.shell && _command.command == "";
337     }
338 
339     Language getLanguage() @safe pure const nothrow {
340         import reggae.range: Leaves;
341         import reggae.rules.common: getLanguage;
342         import std.algorithm: any;
343 
344         auto leaves = () @trusted { return Leaves(this).array; }();
345 
346         foreach(language; [Language.D, Language.Cplusplus, Language.C]) {
347             if(leaves.any!(a => a._outputs.length && .getLanguage(a._outputs[0]) == language))
348                 return language;
349         }
350 
351         return Language.unknown;
352     }
353 
354     ///Replace special variables and return a list of outputs thus modified
355     auto expandOutputs(in string projectPath) @safe pure const {
356 
357         string inProjectPath(in string path) {
358 
359             return path.startsWith(gProjdir)
360                 ? path
361                 : path.startsWith(gBuilddir)
362                     ? path.replace(gBuilddir ~ dirSeparator, "")
363                     : path[0] == '$'
364                         ? path
365                         : buildPath(projectPath, path);
366         }
367 
368         return _outputs.map!(a => isLeaf ? inProjectPath(a) : a).
369             map!(a => a.replace("$project", projectPath)).
370             map!(a => expandBuildDir(a)).
371             array;
372     }
373 
374     //@trusted because of replace
375     string rawCmdString(in string projectPath = "") @safe pure const {
376         return _command.rawCmdString(projectPath);
377     }
378 
379     ///returns a command string to be run by the shell
380     string shellCommand(in Options options,
381                         Flag!"dependencies" deps = Yes.dependencies) @safe pure const {
382         return _command.shellCommand(options, getLanguage(), _outputs, inputs(options.projectPath), deps);
383     }
384 
385     // not const because the code commands take inputs and outputs as non-const strings
386     const(string)[] execute(in Options options) @safe const {
387         return _command.execute(options, getLanguage(), _outputs, inputs(options.projectPath));
388     }
389 
390     bool hasDefaultCommand() @safe const pure {
391         return _command.isDefaultCommand;
392     }
393 
394     CommandType getCommandType() @safe pure const nothrow {
395         return _command.getType;
396     }
397 
398     string[] getCommandParams(in string projectPath, in string key, string[] ifNotFound) @safe pure const {
399         return _command.getParams(projectPath, key, ifNotFound);
400     }
401 
402     const(string)[] commandParamNames() @safe pure nothrow const {
403         return _command.paramNames;
404     }
405 
406     static Target phony(T...)(string name, string shellCommand, T args) {
407         return Target(name, Command.phony(shellCommand), args);
408     }
409 
410     string toString(in Options options) nothrow const {
411         try {
412             if(isLeaf) return _outputs[0];
413             immutable _outputs = _outputs.length == 1 ? `"` ~ _outputs[0] ~ `"` : text(_outputs);
414             immutable depsStr = _dependencies.length == 0 ? "" : text(_dependencies);
415             immutable impsStr = _implicits.length == 0 ? "" : text(_implicits);
416             auto parts = [text(_outputs), `"` ~ shellCommand(options) ~ `"`];
417             if(depsStr != "") parts ~= depsStr;
418             if(impsStr != "") parts ~= impsStr;
419             return text("Target(", parts.join(",\n"), ")");
420         } catch(Exception) {
421             assert(0);
422         }
423     }
424 
425     ubyte[] toBytes(in Options options) @safe pure const {
426         ubyte[] bytes;
427         bytes ~= setUshort(cast(ushort)_outputs.length);
428         foreach(output; _outputs) {
429             bytes ~= arrayToBytes(isLeaf ? inProjectPath(options.projectPath, output) : output);
430         }
431 
432         bytes ~= arrayToBytes(shellCommand(options));
433 
434         bytes ~= setUshort(cast(ushort)_dependencies.length);
435         foreach(dep; _dependencies) bytes ~= dep.toBytes(options);
436 
437         bytes ~= setUshort(cast(ushort)_implicits.length);
438         foreach(imp; _implicits) bytes ~= imp.toBytes(options);
439 
440         return bytes;
441     }
442 
443     static Target fromBytes(ref ubyte[] bytes) @trusted pure nothrow {
444         string[] outputs;
445         immutable numOutputs = getUshort(bytes);
446 
447         foreach(i; 0 .. numOutputs) {
448             outputs ~= cast(string)bytesToArray!char(bytes);
449         }
450 
451         auto command = Command(cast(string)bytesToArray!char(bytes));
452 
453         Target[] dependencies;
454         immutable numDeps = getUshort(bytes);
455         foreach(i; 0..numDeps) dependencies ~= Target.fromBytes(bytes);
456 
457         Target[] implicits;
458         immutable numImps = getUshort(bytes);
459         foreach(i; 0..numImps) implicits ~= Target.fromBytes(bytes);
460 
461         return Target(outputs, command, dependencies, implicits);
462     }
463 
464     bool opEquals()(auto ref const Target other) @safe pure const {
465 
466         bool sameSet(T)(const(T)[] fst, const(T)[] snd) {
467             if(fst.length != snd.length) return false;
468             return fst.all!(a => snd.any!(b => a == b));
469         }
470 
471         return
472             sameSet(_outputs, other._outputs) &&
473             _command == other._command &&
474             sameSet(_dependencies, other._dependencies) &&
475             sameSet(_implicits, other._implicits);
476     }
477 
478 private:
479 
480     string[] depsInProjectPath(in Target[] deps, in string projectPath) @safe pure const {
481         import reggae.range;
482         return deps.map!(a => a.expandOutputs(projectPath)).join;
483     }
484 
485     string[] inputs(in string projectPath) @safe pure nothrow const {
486         //functional didn't work here, I don't know why so sticking with loops for now
487         string[] inputs;
488         foreach(dep; _dependencies) {
489             foreach(output; dep._outputs) {
490                 //leaf objects are references to source files in the project path,
491                 //those need their path built. Any other dependencies are in the
492                 //build path, so they don't need the same treatment
493                 inputs ~= dep.isLeaf ? inProjectPath(projectPath, output) : output;
494             }
495         }
496         return inputs;
497     }
498 }
499 
500 string inProjectPath(in string projectPath, in string name) @safe pure nothrow {
501     if(name.startsWith(gBuilddir)) return name;
502     if(name[0] == '$') return name;
503     return buildPath(projectPath, name);
504 }
505 
506 
507 enum CommandType {
508     shell,
509     compile,
510     link,
511     compileAndLink,
512     code,
513     phony,
514 }
515 
516 alias CommandFunction = void function(in string[], in string[]);
517 alias CommandDelegate = void delegate(in string[], in string[]);
518 
519 /**
520  A command to be execute to produce a targets outputs from its inputs.
521  In general this will be a shell command, but the high-level rules
522  use commands with known semantics (compilation, linking, etc)
523 */
524 struct Command {
525     alias Params = AssocList!(string, string[]);
526 
527     private string command;
528     private CommandType type;
529     private Params params;
530     private CommandFunction function_;
531     private CommandDelegate delegate_;
532 
533     ///If constructed with a string, it's a shell command
534     this(string shellCommand) @safe pure nothrow {
535         command = shellCommand;
536         type = CommandType.shell;
537     }
538 
539     /**Explicitly request a command of this type with these parameters
540        In general to create one of the builtin high level rules*/
541     this(CommandType type, Params params = Params()) @safe pure {
542         if(type == CommandType.shell || type == CommandType.code)
543             throw new Exception("Command rule cannot be shell or code");
544         this.type = type;
545         this.params = params;
546     }
547 
548     ///A D function call command
549     this(CommandDelegate dg) @safe pure nothrow {
550         type = CommandType.code;
551         this.delegate_ = dg;
552     }
553 
554     ///A D function call command
555     this(CommandFunction func) @safe pure nothrow {
556         type = CommandType.code;
557         this.function_ = func;
558     }
559 
560     static Command phony(in string shellCommand) @safe pure nothrow {
561         Command cmd;
562         cmd.type = CommandType.phony;
563         cmd.command = shellCommand;
564         return cmd;
565     }
566 
567     const(string)[] paramNames() @safe pure nothrow const {
568         return params.keys;
569     }
570 
571     CommandType getType() @safe pure const nothrow {
572         return type;
573     }
574 
575     bool isDefaultCommand() @safe pure const {
576         return type == CommandType.compile || type == CommandType.link || type == CommandType.compileAndLink;
577     }
578 
579     string[] getParams(in string projectPath, in string key, string[] ifNotFound) @safe pure const {
580         return getParams(projectPath, key, true, ifNotFound);
581     }
582 
583     Command expandVariables() @safe pure {
584         switch(type) with(CommandType) {
585         case shell:
586             auto cmd = Command(expandBuildDir(command));
587             cmd.type = this.type;
588             return cmd;
589         default:
590             return this;
591         }
592     }
593 
594     ///Replace $in, $out, $project with values
595     private static string expandCmd(in string cmd, in string projectPath,
596                                     in string[] outputs, in string[] inputs) @safe pure {
597         auto replaceIn = cmd.dup.replace("$in", inputs.join(" "));
598         auto replaceOut = replaceIn.replace("$out", outputs.join(" "));
599         return replaceOut.replace("$project", projectPath).replace(gBuilddir ~ dirSeparator, "");
600     }
601 
602     string rawCmdString(in string projectPath) @safe pure const {
603         if(getType != CommandType.shell)
604             throw new Exception("Command type 'code' not supported for ninja backend");
605         return command.replace("$project", projectPath);
606     }
607 
608     //@trusted because of replace
609     private string[] getParams(in string projectPath, in string key,
610                                bool useIfNotFound, string[] ifNotFound = []) @safe pure const {
611         return params.get(key, ifNotFound).map!(a => a.replace("$project", projectPath)).array;
612     }
613 
614     static string builtinTemplate(in CommandType type,
615                                   in Language language,
616                                   in Options options,
617                                   in Flag!"dependencies" deps = Yes.dependencies) @safe pure {
618 
619         final switch(type) with(CommandType) {
620             case phony:
621                 assert(0, "builtinTemplate cannot be phony");
622 
623             case shell:
624                 assert(0, "builtinTemplate cannot be shell");
625 
626             case link:
627                 final switch(language) with(Language) {
628                     case D:
629                     case unknown:
630                         return options.dCompiler ~ " -of$out $flags $in";
631                     case Cplusplus:
632                         return options.cppCompiler ~ " -o $out $flags $in";
633                     case C:
634                         return options.cCompiler ~ " -o $out $flags $in";
635                 }
636 
637             case code:
638                 throw new Exception("Command type 'code' has no built-in template");
639 
640             case compile:
641                 return compileTemplate(type, language, options, deps).replace("$out $in", "$out -c $in");
642 
643             case compileAndLink:
644                 return compileTemplate(type, language, options, deps);
645         }
646     }
647 
648     private static string compileTemplate(in CommandType type,
649                                           in Language language,
650                                           in Options options,
651                                           in Flag!"dependencies" deps = Yes.dependencies) @safe pure {
652         immutable ccParams = deps
653             ? " $flags $includes -MMD -MT $out -MF $out.dep -o $out $in"
654             : " $flags $includes -o $out $in";
655 
656         final switch(language) with(Language) {
657             case D:
658                 return deps
659                     ? ".reggae/dcompile --objFile=$out --depFile=$out.dep " ~
660                     options.dCompiler ~ " $flags $includes $stringImports $in"
661                     : options.dCompiler ~ " $flags $includes $stringImports -of$out $in";
662             case Cplusplus:
663                 return options.cppCompiler ~ ccParams;
664             case C:
665                 return options.cCompiler ~ ccParams;
666             case unknown:
667                 throw new Exception("Unsupported language for compiling");
668         }
669     }
670 
671     string defaultCommand(in Options options,
672                           in Language language,
673                           in string[] outputs,
674                           in string[] inputs,
675                           Flag!"dependencies" deps = Yes.dependencies) @safe pure const {
676 
677         import std.conv: text;
678 
679         assert(isDefaultCommand, text("This command is not a default command: ", this));
680         string cmd;
681         try
682             cmd = builtinTemplate(type, language, options, deps);
683         catch(Exception ex) {
684             throw new Exception(text(ex.msg, "\noutputs: ", outputs, "\ninputs: ", inputs));
685         }
686 
687         foreach(key; params.keys) {
688             immutable var = "$" ~ key;
689             immutable value = getParams(options.projectPath, key, []).join(" ");
690             cmd = cmd.replace(var, value);
691         }
692         return expandCmd(cmd, options.projectPath, outputs, inputs);
693     }
694 
695     ///returns a command string to be run by the shell
696     string shellCommand(in Options options,
697                         in Language language,
698                         in string[] outputs,
699                         in string[] inputs,
700                         Flag!"dependencies" deps = Yes.dependencies) @safe pure const {
701         return isDefaultCommand
702             ? defaultCommand(options, language, outputs, inputs, deps)
703             : expandCmd(command, options.projectPath, outputs, inputs);
704     }
705 
706     const(string)[] execute(in Options options, in Language language,
707                             in string[] outputs, in string[] inputs) const @trusted {
708         import std.process;
709 
710         final switch(type) with(CommandType) {
711             case shell:
712             case compile:
713             case link:
714             case compileAndLink:
715             case phony:
716                 immutable cmd = shellCommand(options, language, outputs, inputs);
717                 if(cmd == "") return outputs;
718 
719                 const string[string] env = null;
720                 Config config = Config.none;
721                 size_t maxOutput = size_t.max;
722 
723                 immutable res = executeShell(cmd, env, config, maxOutput, options.workingDir);
724                 enforce(res.status == 0, "Could not execute phony " ~ cmd ~ ":\n" ~ res.output);
725                 return [res.output];
726             case code:
727                 assert(function_ !is null || delegate_ !is null,
728                        "Command of type code with null function");
729                 function_ !is null ? function_(inputs, outputs) : delegate_(inputs, outputs);
730                 return ["code"];
731         }
732     }
733 
734     ubyte[] toBytes() @safe pure nothrow const {
735         final switch(type) {
736 
737         case CommandType.shell:
738             return [cast(ubyte)type] ~ cast(ubyte[])command.dup;
739 
740         case CommandType.compile:
741         case CommandType.compileAndLink:
742         case CommandType.link:
743         case CommandType.phony:
744             ubyte[] bytes;
745             bytes ~= cast(ubyte)type;
746             bytes ~= cast(ubyte)(params.keys.length >> 8);
747             bytes ~= (params.keys.length & 0xff);
748             foreach(key; params.keys) {
749                 bytes ~= arrayToBytes(key);
750                 bytes ~= cast(ubyte)(params[key].length >> 8);
751                 bytes ~= (params[key].length & 0xff);
752                 foreach(value; params[key])
753                     bytes ~= arrayToBytes(value);
754             }
755             return bytes;
756 
757         case CommandType.code:
758             assert(0);
759         }
760     }
761 
762     static Command fromBytes(ubyte[] bytes) @trusted pure {
763         immutable type = cast(CommandType)bytes[0];
764         bytes = bytes[1..$];
765 
766         final switch(type) {
767 
768         case CommandType.shell:
769             char[] chars;
770             foreach(b; bytes) chars ~= cast(char)b;
771             return Command(cast(string)chars);
772 
773         case CommandType.compile:
774         case CommandType.compileAndLink:
775         case CommandType.link:
776         case CommandType.phony:
777             Params params;
778 
779             immutable numKeys = getUshort(bytes);
780             foreach(i; 0..numKeys) {
781                 immutable key = cast(string)bytesToArray!char(bytes);
782                 immutable numValues = getUshort(bytes);
783 
784                 string[] values;
785                 foreach(j; 0..numValues) {
786                     values ~= bytesToArray!char(bytes);
787                 }
788                 params[key] = values;
789             }
790             return Command(type, params);
791 
792         case CommandType.code:
793             throw new Exception("Cannot serialise Command of type code");
794         }
795     }
796 
797     string toString() const pure @safe {
798         final switch(type) with(CommandType) {
799             case shell:
800             case phony:
801                 return `Command("` ~ command ~ `")`;
802             case compile:
803             case link:
804             case compileAndLink:
805             case code:
806                 return `Command(` ~ type.to!string ~
807                     (params.keys.length ? ", " ~ text(params) : "") ~
808                     `)`;
809         }
810     }
811 }
812 
813 
814 private ubyte[] arrayToBytes(T)(in T[] arr) {
815     auto bytes = new ubyte[arr.length + 2];
816     immutable length = cast(ushort)arr.length;
817     bytes[0] = length >> 8;
818     bytes[1] = length & 0xff;
819     foreach(i, c; arr) bytes[i + 2] = cast(ubyte)c;
820     return bytes;
821 }
822 
823 
824 private T[] bytesToArray(T)(ref ubyte[] bytes) {
825     T[] arr;
826     arr.length = getUshort(bytes);
827     foreach(i, b; bytes[0 .. arr.length]) arr[i] = cast(T)b;
828     bytes = bytes[arr.length .. $];
829     return arr;
830 }
831 
832 
833 private ushort getUshort(ref ubyte[] bytes) @safe pure nothrow {
834     immutable length = (bytes[0] << 8) + bytes[1];
835     bytes = bytes[2..$];
836     return length;
837 }
838 
839 private ubyte[] setUshort(in ushort length) @safe pure nothrow {
840     auto bytes = new ubyte[2];
841     bytes[0] = length >> 8;
842     bytes[1] = length & 0xff;
843     return bytes;
844 }
845 
846 
847 string replaceConcreteCompilersWithVars(in string cmd, in Options options) @safe pure nothrow {
848     return cmd.
849         replace(options.dCompiler, "$(DC)").
850         replace(options.cppCompiler, "$(CXX)").
851         replace(options.cCompiler, "$(CC)");
852 }