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 case phony: 587 auto cmd = Command(expandBuildDir(command)); 588 cmd.type = this.type; 589 return cmd; 590 default: 591 return this; 592 } 593 } 594 595 ///Replace $in, $out, $project with values 596 private static string expandCmd(in string cmd, in string projectPath, 597 in string[] outputs, in string[] inputs) @safe pure { 598 auto replaceIn = cmd.dup.replace("$in", inputs.join(" ")); 599 auto replaceOut = replaceIn.replace("$out", outputs.join(" ")); 600 return replaceOut.replace("$project", projectPath).replace(gBuilddir ~ dirSeparator, ""); 601 } 602 603 string rawCmdString(in string projectPath) @safe pure const { 604 if(getType != CommandType.shell) 605 throw new Exception("Command type 'code' not supported for ninja backend"); 606 return command.replace("$project", projectPath); 607 } 608 609 //@trusted because of replace 610 private string[] getParams(in string projectPath, in string key, 611 bool useIfNotFound, string[] ifNotFound = []) @safe pure const { 612 return params.get(key, ifNotFound).map!(a => a.replace("$project", projectPath)).array; 613 } 614 615 static string builtinTemplate(in CommandType type, 616 in Language language, 617 in Options options, 618 in Flag!"dependencies" deps = Yes.dependencies) @safe pure { 619 620 final switch(type) with(CommandType) { 621 case phony: 622 assert(0, "builtinTemplate cannot be phony"); 623 624 case shell: 625 assert(0, "builtinTemplate cannot be shell"); 626 627 case link: 628 final switch(language) with(Language) { 629 case D: 630 case unknown: 631 return options.dCompiler ~ " -of$out $flags $in"; 632 case Cplusplus: 633 return options.cppCompiler ~ " -o $out $flags $in"; 634 case C: 635 return options.cCompiler ~ " -o $out $flags $in"; 636 } 637 638 case code: 639 throw new Exception("Command type 'code' has no built-in template"); 640 641 case compile: 642 return compileTemplate(type, language, options, deps).replace("$out $in", "$out -c $in"); 643 644 case compileAndLink: 645 return compileTemplate(type, language, options, deps); 646 } 647 } 648 649 private static string compileTemplate(in CommandType type, 650 in Language language, 651 in Options options, 652 in Flag!"dependencies" deps = Yes.dependencies) @safe pure { 653 immutable ccParams = deps 654 ? " $flags $includes -MMD -MT $out -MF $out.dep -o $out $in" 655 : " $flags $includes -o $out $in"; 656 657 final switch(language) with(Language) { 658 case D: 659 return deps 660 ? ".reggae/dcompile --objFile=$out --depFile=$out.dep " ~ 661 options.dCompiler ~ " $flags $includes $stringImports $in" 662 : options.dCompiler ~ " $flags $includes $stringImports -of$out $in"; 663 case Cplusplus: 664 return options.cppCompiler ~ ccParams; 665 case C: 666 return options.cCompiler ~ ccParams; 667 case unknown: 668 throw new Exception("Unsupported language for compiling"); 669 } 670 } 671 672 string defaultCommand(in Options options, 673 in Language language, 674 in string[] outputs, 675 in string[] inputs, 676 Flag!"dependencies" deps = Yes.dependencies) @safe pure const { 677 678 import std.conv: text; 679 680 assert(isDefaultCommand, text("This command is not a default command: ", this)); 681 string cmd; 682 try 683 cmd = builtinTemplate(type, language, options, deps); 684 catch(Exception ex) { 685 throw new Exception(text(ex.msg, "\noutputs: ", outputs, "\ninputs: ", inputs)); 686 } 687 688 foreach(key; params.keys) { 689 immutable var = "$" ~ key; 690 immutable value = getParams(options.projectPath, key, []).join(" "); 691 cmd = cmd.replace(var, value); 692 } 693 return expandCmd(cmd, options.projectPath, outputs, inputs); 694 } 695 696 ///returns a command string to be run by the shell 697 string shellCommand(in Options options, 698 in Language language, 699 in string[] outputs, 700 in string[] inputs, 701 Flag!"dependencies" deps = Yes.dependencies) @safe pure const { 702 return isDefaultCommand 703 ? defaultCommand(options, language, outputs, inputs, deps) 704 : expandCmd(command, options.projectPath, outputs, inputs); 705 } 706 707 const(string)[] execute(in Options options, in Language language, 708 in string[] outputs, in string[] inputs) const @trusted { 709 import std.process; 710 711 final switch(type) with(CommandType) { 712 case shell: 713 case compile: 714 case link: 715 case compileAndLink: 716 case phony: 717 immutable cmd = shellCommand(options, language, outputs, inputs); 718 if(cmd == "") return outputs; 719 720 const string[string] env = null; 721 Config config = Config.none; 722 size_t maxOutput = size_t.max; 723 724 immutable res = executeShell(cmd, env, config, maxOutput, options.workingDir); 725 enforce(res.status == 0, "Could not execute phony " ~ cmd ~ ":\n" ~ res.output); 726 return [res.output]; 727 case code: 728 assert(function_ !is null || delegate_ !is null, 729 "Command of type code with null function"); 730 function_ !is null ? function_(inputs, outputs) : delegate_(inputs, outputs); 731 return ["code"]; 732 } 733 } 734 735 ubyte[] toBytes() @safe pure nothrow const { 736 final switch(type) { 737 738 case CommandType.shell: 739 return [cast(ubyte)type] ~ cast(ubyte[])command.dup; 740 741 case CommandType.compile: 742 case CommandType.compileAndLink: 743 case CommandType.link: 744 case CommandType.phony: 745 ubyte[] bytes; 746 bytes ~= cast(ubyte)type; 747 bytes ~= cast(ubyte)(params.keys.length >> 8); 748 bytes ~= (params.keys.length & 0xff); 749 foreach(key; params.keys) { 750 bytes ~= arrayToBytes(key); 751 bytes ~= cast(ubyte)(params[key].length >> 8); 752 bytes ~= (params[key].length & 0xff); 753 foreach(value; params[key]) 754 bytes ~= arrayToBytes(value); 755 } 756 return bytes; 757 758 case CommandType.code: 759 assert(0); 760 } 761 } 762 763 static Command fromBytes(ubyte[] bytes) @trusted pure { 764 immutable type = cast(CommandType)bytes[0]; 765 bytes = bytes[1..$]; 766 767 final switch(type) { 768 769 case CommandType.shell: 770 char[] chars; 771 foreach(b; bytes) chars ~= cast(char)b; 772 return Command(cast(string)chars); 773 774 case CommandType.compile: 775 case CommandType.compileAndLink: 776 case CommandType.link: 777 case CommandType.phony: 778 Params params; 779 780 immutable numKeys = getUshort(bytes); 781 foreach(i; 0..numKeys) { 782 immutable key = cast(string)bytesToArray!char(bytes); 783 immutable numValues = getUshort(bytes); 784 785 string[] values; 786 foreach(j; 0..numValues) { 787 values ~= bytesToArray!char(bytes); 788 } 789 params[key] = values; 790 } 791 return Command(type, params); 792 793 case CommandType.code: 794 throw new Exception("Cannot serialise Command of type code"); 795 } 796 } 797 798 string toString() const pure @safe { 799 final switch(type) with(CommandType) { 800 case shell: 801 case phony: 802 return `Command("` ~ command ~ `")`; 803 case compile: 804 case link: 805 case compileAndLink: 806 case code: 807 return `Command(` ~ type.to!string ~ 808 (params.keys.length ? ", " ~ text(params) : "") ~ 809 `)`; 810 } 811 } 812 } 813 814 815 private ubyte[] arrayToBytes(T)(in T[] arr) { 816 auto bytes = new ubyte[arr.length + 2]; 817 immutable length = cast(ushort)arr.length; 818 bytes[0] = length >> 8; 819 bytes[1] = length & 0xff; 820 foreach(i, c; arr) bytes[i + 2] = cast(ubyte)c; 821 return bytes; 822 } 823 824 825 private T[] bytesToArray(T)(ref ubyte[] bytes) { 826 T[] arr; 827 arr.length = getUshort(bytes); 828 foreach(i, b; bytes[0 .. arr.length]) arr[i] = cast(T)b; 829 bytes = bytes[arr.length .. $]; 830 return arr; 831 } 832 833 834 private ushort getUshort(ref ubyte[] bytes) @safe pure nothrow { 835 immutable length = (bytes[0] << 8) + bytes[1]; 836 bytes = bytes[2..$]; 837 return length; 838 } 839 840 private ubyte[] setUshort(in ushort length) @safe pure nothrow { 841 auto bytes = new ubyte[2]; 842 bytes[0] = length >> 8; 843 bytes[1] = length & 0xff; 844 return bytes; 845 } 846 847 848 string replaceConcreteCompilersWithVars(in string cmd, in Options options) @safe pure nothrow { 849 return cmd. 850 replace(options.dCompiler, "$(DC)"). 851 replace(options.cppCompiler, "$(CXX)"). 852 replace(options.cCompiler, "$(CC)"); 853 }