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         const leaves = () @trusted { return Leaves(this).array; }();
342         foreach(language; [Language.D, Language.Cplusplus, Language.C]) {
343             if(leaves.any!(a => a._outputs.length && reggae.rules.common.getLanguage(a._outputs[0]) == language))
344                 return language;
345         }
346 
347         return Language.unknown;
348     }
349 
350     ///Replace special variables and return a list of outputs thus modified
351     auto expandOutputs(in string projectPath) @safe pure const {
352 
353         string inProjectPath(in string path) {
354 
355             return path.startsWith(gProjdir)
356                 ? path
357                 : path.startsWith(gBuilddir)
358                     ? path.replace(gBuilddir ~ dirSeparator, "")
359                     : path[0] == '$'
360                         ? path
361                         : buildPath(projectPath, path);
362         }
363 
364         return _outputs.map!(a => isLeaf ? inProjectPath(a) : a).
365             map!(a => a.replace("$project", projectPath)).
366             map!(a => expandBuildDir(a)).
367             array;
368     }
369 
370     //@trusted because of replace
371     string rawCmdString(in string projectPath = "") @safe pure const {
372         return _command.rawCmdString(projectPath);
373     }
374 
375     ///returns a command string to be run by the shell
376     string shellCommand(in Options options,
377                         Flag!"dependencies" deps = Yes.dependencies) @safe pure const {
378         return _command.shellCommand(options, getLanguage(), _outputs, inputs(options.projectPath), deps);
379     }
380 
381     // not const because the code commands take inputs and outputs as non-const strings
382     const(string)[] execute(in Options options) @safe const {
383         return _command.execute(options, getLanguage(), _outputs, inputs(options.projectPath));
384     }
385 
386     bool hasDefaultCommand() @safe const pure {
387         return _command.isDefaultCommand;
388     }
389 
390     CommandType getCommandType() @safe pure const nothrow {
391         return _command.getType;
392     }
393 
394     string[] getCommandParams(in string projectPath, in string key, string[] ifNotFound) @safe pure const {
395         return _command.getParams(projectPath, key, ifNotFound);
396     }
397 
398     const(string)[] commandParamNames() @safe pure nothrow const {
399         return _command.paramNames;
400     }
401 
402     static Target phony(T...)(string name, string shellCommand, T args) {
403         return Target(name, Command.phony(shellCommand), args);
404     }
405 
406     string toString(in Options options) nothrow const {
407         try {
408             if(isLeaf) return _outputs[0];
409             immutable _outputs = _outputs.length == 1 ? `"` ~ _outputs[0] ~ `"` : text(_outputs);
410             immutable depsStr = _dependencies.length == 0 ? "" : text(_dependencies);
411             immutable impsStr = _implicits.length == 0 ? "" : text(_implicits);
412             auto parts = [text(_outputs), `"` ~ shellCommand(options) ~ `"`];
413             if(depsStr != "") parts ~= depsStr;
414             if(impsStr != "") parts ~= impsStr;
415             return text("Target(", parts.join(",\n"), ")");
416         } catch(Exception) {
417             assert(0);
418         }
419     }
420 
421     ubyte[] toBytes(in Options options) @safe pure const {
422         ubyte[] bytes;
423         bytes ~= setUshort(cast(ushort)_outputs.length);
424         foreach(output; _outputs) {
425             bytes ~= arrayToBytes(isLeaf ? inProjectPath(options.projectPath, output) : output);
426         }
427 
428         bytes ~= arrayToBytes(shellCommand(options));
429 
430         bytes ~= setUshort(cast(ushort)_dependencies.length);
431         foreach(dep; _dependencies) bytes ~= dep.toBytes(options);
432 
433         bytes ~= setUshort(cast(ushort)_implicits.length);
434         foreach(imp; _implicits) bytes ~= imp.toBytes(options);
435 
436         return bytes;
437     }
438 
439     static Target fromBytes(ref ubyte[] bytes) @trusted pure nothrow {
440         string[] outputs;
441         immutable numOutputs = getUshort(bytes);
442 
443         foreach(i; 0 .. numOutputs) {
444             outputs ~= cast(string)bytesToArray!char(bytes);
445         }
446 
447         auto command = Command(cast(string)bytesToArray!char(bytes));
448 
449         Target[] dependencies;
450         immutable numDeps = getUshort(bytes);
451         foreach(i; 0..numDeps) dependencies ~= Target.fromBytes(bytes);
452 
453         Target[] implicits;
454         immutable numImps = getUshort(bytes);
455         foreach(i; 0..numImps) implicits ~= Target.fromBytes(bytes);
456 
457         return Target(outputs, command, dependencies, implicits);
458     }
459 
460     bool opEquals()(auto ref const Target other) @safe pure const {
461 
462         bool sameSet(T)(const(T)[] fst, const(T)[] snd) {
463             if(fst.length != snd.length) return false;
464             return fst.all!(a => snd.any!(b => a == b));
465         }
466 
467         return
468             sameSet(_outputs, other._outputs) &&
469             _command == other._command &&
470             sameSet(_dependencies, other._dependencies) &&
471             sameSet(_implicits, other._implicits);
472     }
473 
474 private:
475 
476     string[] depsInProjectPath(in Target[] deps, in string projectPath) @safe pure const {
477         import reggae.range;
478         return deps.map!(a => a.expandOutputs(projectPath)).join;
479     }
480 
481     string[] inputs(in string projectPath) @safe pure nothrow const {
482         //functional didn't work here, I don't know why so sticking with loops for now
483         string[] inputs;
484         foreach(dep; _dependencies) {
485             foreach(output; dep._outputs) {
486                 //leaf objects are references to source files in the project path,
487                 //those need their path built. Any other dependencies are in the
488                 //build path, so they don't need the same treatment
489                 inputs ~= dep.isLeaf ? inProjectPath(projectPath, output) : output;
490             }
491         }
492         return inputs;
493     }
494 }
495 
496 string inProjectPath(in string projectPath, in string name) @safe pure nothrow {
497     if(name.startsWith(gBuilddir)) return name;
498     if(name[0] == '$') return name;
499     return buildPath(projectPath, name);
500 }
501 
502 
503 enum CommandType {
504     shell,
505     compile,
506     link,
507     compileAndLink,
508     code,
509     phony,
510 }
511 
512 alias CommandFunction = void function(in string[], in string[]);
513 alias CommandDelegate = void delegate(in string[], in string[]);
514 
515 /**
516  A command to be execute to produce a targets outputs from its inputs.
517  In general this will be a shell command, but the high-level rules
518  use commands with known semantics (compilation, linking, etc)
519 */
520 struct Command {
521     alias Params = AssocList!(string, string[]);
522 
523     private string command;
524     private CommandType type;
525     private Params params;
526     private CommandFunction function_;
527     private CommandDelegate delegate_;
528 
529     ///If constructed with a string, it's a shell command
530     this(string shellCommand) @safe pure nothrow {
531         command = shellCommand;
532         type = CommandType.shell;
533     }
534 
535     /**Explicitly request a command of this type with these parameters
536        In general to create one of the builtin high level rules*/
537     this(CommandType type, Params params = Params()) @safe pure {
538         if(type == CommandType.shell || type == CommandType.code)
539             throw new Exception("Command rule cannot be shell or code");
540         this.type = type;
541         this.params = params;
542     }
543 
544     ///A D function call command
545     this(CommandDelegate dg) @safe pure nothrow {
546         type = CommandType.code;
547         this.delegate_ = dg;
548     }
549 
550     ///A D function call command
551     this(CommandFunction func) @safe pure nothrow {
552         type = CommandType.code;
553         this.function_ = func;
554     }
555 
556     static Command phony(in string shellCommand) @safe pure nothrow {
557         Command cmd;
558         cmd.type = CommandType.phony;
559         cmd.command = shellCommand;
560         return cmd;
561     }
562 
563     const(string)[] paramNames() @safe pure nothrow const {
564         return params.keys;
565     }
566 
567     CommandType getType() @safe pure const nothrow {
568         return type;
569     }
570 
571     bool isDefaultCommand() @safe pure const {
572         return type == CommandType.compile || type == CommandType.link || type == CommandType.compileAndLink;
573     }
574 
575     string[] getParams(in string projectPath, in string key, string[] ifNotFound) @safe pure const {
576         return getParams(projectPath, key, true, ifNotFound);
577     }
578 
579     Command expandVariables() @safe pure {
580         switch(type) with(CommandType) {
581         case shell:
582             auto cmd = Command(expandBuildDir(command));
583             cmd.type = this.type;
584             return cmd;
585         default:
586             return this;
587         }
588     }
589 
590     ///Replace $in, $out, $project with values
591     private static string expandCmd(in string cmd, in string projectPath,
592                                     in string[] outputs, in string[] inputs) @safe pure {
593         auto replaceIn = cmd.dup.replace("$in", inputs.join(" "));
594         auto replaceOut = replaceIn.replace("$out", outputs.join(" "));
595         return replaceOut.replace("$project", projectPath).replace(gBuilddir ~ dirSeparator, "");
596     }
597 
598     string rawCmdString(in string projectPath) @safe pure const {
599         if(getType != CommandType.shell)
600             throw new Exception("Command type 'code' not supported for ninja backend");
601         return command.replace("$project", projectPath);
602     }
603 
604     //@trusted because of replace
605     private string[] getParams(in string projectPath, in string key,
606                                bool useIfNotFound, string[] ifNotFound = []) @safe pure const {
607         return params.get(key, ifNotFound).map!(a => a.replace("$project", projectPath)).array;
608     }
609 
610     static string builtinTemplate(in CommandType type,
611                                   in Language language,
612                                   in Options options,
613                                   in Flag!"dependencies" deps = Yes.dependencies) @safe pure {
614 
615         final switch(type) with(CommandType) {
616             case phony:
617                 assert(0, "builtinTemplate cannot be phony");
618 
619             case shell:
620                 assert(0, "builtinTemplate cannot be shell");
621 
622             case link:
623                 final switch(language) with(Language) {
624                     case D:
625                     case unknown:
626                         return options.dCompiler ~ " -of$out $flags $in";
627                     case Cplusplus:
628                         return options.cppCompiler ~ " -o $out $flags $in";
629                     case C:
630                         return options.cCompiler ~ " -o $out $flags $in";
631                 }
632 
633             case code:
634                 throw new Exception("Command type 'code' has no built-in template");
635 
636             case compile:
637                 return compileTemplate(type, language, options, deps).replace("$out $in", "$out -c $in");
638 
639             case compileAndLink:
640                 return compileTemplate(type, language, options, deps);
641         }
642     }
643 
644     private static string compileTemplate(in CommandType type,
645                                           in Language language,
646                                           in Options options,
647                                           in Flag!"dependencies" deps = Yes.dependencies) @safe pure {
648         immutable ccParams = deps
649             ? " $flags $includes -MMD -MT $out -MF $out.dep -o $out $in"
650             : " $flags $includes -o $out $in";
651 
652         final switch(language) with(Language) {
653             case D:
654                 return deps
655                     ? ".reggae/dcompile --objFile=$out --depFile=$out.dep " ~
656                     options.dCompiler ~ " $flags $includes $stringImports $in"
657                     : options.dCompiler ~ " $flags $includes $stringImports -of$out $in";
658             case Cplusplus:
659                 return options.cppCompiler ~ ccParams;
660             case C:
661                 return options.cCompiler ~ ccParams;
662             case unknown:
663                 throw new Exception("Unsupported language for compiling");
664         }
665     }
666 
667     string defaultCommand(in Options options,
668                           in Language language,
669                           in string[] outputs,
670                           in string[] inputs,
671                           Flag!"dependencies" deps = Yes.dependencies) @safe pure const {
672 
673         import std.conv: text;
674 
675         assert(isDefaultCommand, text("This command is not a default command: ", this));
676         string cmd;
677         try
678             cmd = builtinTemplate(type, language, options, deps);
679         catch(Exception ex) {
680             throw new Exception(text(ex.msg, "\noutputs: ", outputs, "\ninputs: ", inputs));
681         }
682 
683         foreach(key; params.keys) {
684             immutable var = "$" ~ key;
685             immutable value = getParams(options.projectPath, key, []).join(" ");
686             cmd = cmd.replace(var, value);
687         }
688         return expandCmd(cmd, options.projectPath, outputs, inputs);
689     }
690 
691     ///returns a command string to be run by the shell
692     string shellCommand(in Options options,
693                         in Language language,
694                         in string[] outputs,
695                         in string[] inputs,
696                         Flag!"dependencies" deps = Yes.dependencies) @safe pure const {
697         return isDefaultCommand
698             ? defaultCommand(options, language, outputs, inputs, deps)
699             : expandCmd(command, options.projectPath, outputs, inputs);
700     }
701 
702     const(string)[] execute(in Options options, in Language language,
703                             in string[] outputs, in string[] inputs) const @trusted {
704         import std.process;
705 
706         final switch(type) with(CommandType) {
707             case shell:
708             case compile:
709             case link:
710             case compileAndLink:
711             case phony:
712                 immutable cmd = shellCommand(options, language, outputs, inputs);
713                 if(cmd == "") return outputs;
714 
715                 const string[string] env = null;
716                 Config config = Config.none;
717                 size_t maxOutput = size_t.max;
718 
719                 immutable res = executeShell(cmd, env, config, maxOutput, options.workingDir);
720                 enforce(res.status == 0, "Could not execute phony " ~ cmd ~ ":\n" ~ res.output);
721                 return [res.output];
722             case code:
723                 assert(function_ !is null || delegate_ !is null,
724                        "Command of type code with null function");
725                 function_ !is null ? function_(inputs, outputs) : delegate_(inputs, outputs);
726                 return ["code"];
727         }
728     }
729 
730     ubyte[] toBytes() @safe pure nothrow const {
731         final switch(type) {
732 
733         case CommandType.shell:
734             return [cast(ubyte)type] ~ cast(ubyte[])command.dup;
735 
736         case CommandType.compile:
737         case CommandType.compileAndLink:
738         case CommandType.link:
739         case CommandType.phony:
740             ubyte[] bytes;
741             bytes ~= cast(ubyte)type;
742             bytes ~= cast(ubyte)(params.keys.length >> 8);
743             bytes ~= (params.keys.length & 0xff);
744             foreach(key; params.keys) {
745                 bytes ~= arrayToBytes(key);
746                 bytes ~= cast(ubyte)(params[key].length >> 8);
747                 bytes ~= (params[key].length & 0xff);
748                 foreach(value; params[key])
749                     bytes ~= arrayToBytes(value);
750             }
751             return bytes;
752 
753         case CommandType.code:
754             assert(0);
755         }
756     }
757 
758     static Command fromBytes(ubyte[] bytes) @trusted pure {
759         immutable type = cast(CommandType)bytes[0];
760         bytes = bytes[1..$];
761 
762         final switch(type) {
763 
764         case CommandType.shell:
765             char[] chars;
766             foreach(b; bytes) chars ~= cast(char)b;
767             return Command(cast(string)chars);
768 
769         case CommandType.compile:
770         case CommandType.compileAndLink:
771         case CommandType.link:
772         case CommandType.phony:
773             Params params;
774 
775             immutable numKeys = getUshort(bytes);
776             foreach(i; 0..numKeys) {
777                 immutable key = cast(string)bytesToArray!char(bytes);
778                 immutable numValues = getUshort(bytes);
779 
780                 string[] values;
781                 foreach(j; 0..numValues) {
782                     values ~= bytesToArray!char(bytes);
783                 }
784                 params[key] = values;
785             }
786             return Command(type, params);
787 
788         case CommandType.code:
789             throw new Exception("Cannot serialise Command of type code");
790         }
791     }
792 
793     string toString() const pure @safe {
794         final switch(type) with(CommandType) {
795             case shell:
796             case phony:
797                 return `Command("` ~ command ~ `")`;
798             case compile:
799             case link:
800             case compileAndLink:
801             case code:
802                 return `Command(` ~ type.to!string ~
803                     (params.keys.length ? ", " ~ text(params) : "") ~
804                     `)`;
805         }
806     }
807 }
808 
809 
810 private ubyte[] arrayToBytes(T)(in T[] arr) {
811     auto bytes = new ubyte[arr.length + 2];
812     immutable length = cast(ushort)arr.length;
813     bytes[0] = length >> 8;
814     bytes[1] = length & 0xff;
815     foreach(i, c; arr) bytes[i + 2] = cast(ubyte)c;
816     return bytes;
817 }
818 
819 
820 private T[] bytesToArray(T)(ref ubyte[] bytes) {
821     T[] arr;
822     arr.length = getUshort(bytes);
823     foreach(i, b; bytes[0 .. arr.length]) arr[i] = cast(T)b;
824     bytes = bytes[arr.length .. $];
825     return arr;
826 }
827 
828 
829 private ushort getUshort(ref ubyte[] bytes) @safe pure nothrow {
830     immutable length = (bytes[0] << 8) + bytes[1];
831     bytes = bytes[2..$];
832     return length;
833 }
834 
835 private ubyte[] setUshort(in ushort length) @safe pure nothrow {
836     auto bytes = new ubyte[2];
837     bytes[0] = length >> 8;
838     bytes[1] = length & 0xff;
839     return bytes;
840 }
841 
842 
843 string replaceConcreteCompilersWithVars(in string cmd, in Options options) @safe pure nothrow {
844     return cmd.
845         replace(options.dCompiler, "$(DC)").
846         replace(options.cppCompiler, "$(CXX)").
847         replace(options.cCompiler, "$(CC)");
848 }