|
| 1 | +/******************************************************************************* |
| 2 | +
|
| 3 | + Represent a target platform |
| 4 | +
|
| 5 | + Platform informations can be embedded in recipe, such that some settings |
| 6 | + only target a certain platform (e.g. sourceFiles, lflags, etc...). |
| 7 | + The struct in this module represent that information, structured. |
| 8 | +
|
| 9 | +*******************************************************************************/ |
| 10 | + |
| 11 | +module dub.data.platform; |
| 12 | + |
| 13 | +/// Represents a platform a package can be build upon. |
| 14 | +struct BuildPlatform { |
| 15 | + /// Special constant used to denote matching any build platform. |
| 16 | + enum any = BuildPlatform(null, null, null, null, -1); |
| 17 | + |
| 18 | + /// Platform identifiers, e.g. ["posix", "windows"] |
| 19 | + string[] platform; |
| 20 | + /// CPU architecture identifiers, e.g. ["x86", "x86_64"] |
| 21 | + string[] architecture; |
| 22 | + /// Canonical compiler name e.g. "dmd" |
| 23 | + string compiler; |
| 24 | + /// Compiler binary name e.g. "ldmd2" |
| 25 | + string compilerBinary; |
| 26 | + /// Compiled frontend version (e.g. `2067` for frontend versions 2.067.x) |
| 27 | + int frontendVersion; |
| 28 | + /// Compiler version e.g. "1.11.0" |
| 29 | + string compilerVersion; |
| 30 | + /// Frontend version string from frontendVersion |
| 31 | + /// e.g: 2067 => "2.067" |
| 32 | + string frontendVersionString() const |
| 33 | + { |
| 34 | + import std.format : format; |
| 35 | + |
| 36 | + const maj = frontendVersion / 1000; |
| 37 | + const min = frontendVersion % 1000; |
| 38 | + return format("%d.%03d", maj, min); |
| 39 | + } |
| 40 | + /// |
| 41 | + unittest |
| 42 | + { |
| 43 | + BuildPlatform bp; |
| 44 | + bp.frontendVersion = 2067; |
| 45 | + assert(bp.frontendVersionString == "2.067"); |
| 46 | + } |
| 47 | + |
| 48 | + /// Checks to see if platform field contains windows |
| 49 | + bool isWindows() const { |
| 50 | + import std.algorithm : canFind; |
| 51 | + return this.platform.canFind("windows"); |
| 52 | + } |
| 53 | + /// |
| 54 | + unittest { |
| 55 | + BuildPlatform bp; |
| 56 | + bp.platform = ["windows"]; |
| 57 | + assert(bp.isWindows); |
| 58 | + bp.platform = ["posix"]; |
| 59 | + assert(!bp.isWindows); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** Matches a platform specification string against a build platform. |
| 64 | +
|
| 65 | + Specifications are build upon the following scheme, where each component |
| 66 | + is optional (indicated by []), but the order is obligatory: |
| 67 | + "[-platform][-architecture][-compiler]" |
| 68 | +
|
| 69 | + So the following strings are valid specifications: `"-windows-x86-dmd"`, |
| 70 | + `"-dmd"`, `"-arm"`, `"-arm-dmd"`, `"-windows-dmd"` |
| 71 | +
|
| 72 | + Params: |
| 73 | + platform = The build platform to match against the platform specification |
| 74 | + specification = The specification being matched. It must either be an |
| 75 | + empty string or start with a dash. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + `true` if the given specification matches the build platform, `false` |
| 79 | + otherwise. Using an empty string as the platform specification will |
| 80 | + always result in a match. |
| 81 | +*/ |
| 82 | +bool matchesSpecification(in BuildPlatform platform, const(char)[] specification) |
| 83 | +{ |
| 84 | + import std.range : empty; |
| 85 | + import std.string : chompPrefix, format; |
| 86 | + import std.algorithm : canFind, splitter; |
| 87 | + import std.exception : enforce; |
| 88 | + |
| 89 | + if (specification.empty) return true; |
| 90 | + if (platform == BuildPlatform.any) return true; |
| 91 | + |
| 92 | + auto splitted = specification.chompPrefix("-").splitter('-'); |
| 93 | + enforce(!splitted.empty, format("Platform specification, if present, must not be empty: \"%s\"", specification)); |
| 94 | + |
| 95 | + if (platform.platform.canFind(splitted.front)) { |
| 96 | + splitted.popFront(); |
| 97 | + if (splitted.empty) |
| 98 | + return true; |
| 99 | + } |
| 100 | + if (platform.architecture.canFind(splitted.front)) { |
| 101 | + splitted.popFront(); |
| 102 | + if (splitted.empty) |
| 103 | + return true; |
| 104 | + } |
| 105 | + if (platform.compiler == splitted.front) { |
| 106 | + splitted.popFront(); |
| 107 | + enforce(splitted.empty, "No valid specification! The compiler has to be the last element: " ~ specification); |
| 108 | + return true; |
| 109 | + } |
| 110 | + return false; |
| 111 | +} |
| 112 | + |
| 113 | +/// |
| 114 | +unittest { |
| 115 | + auto platform = BuildPlatform(["posix", "linux"], ["x86_64"], "dmd"); |
| 116 | + assert(platform.matchesSpecification("")); |
| 117 | + assert(platform.matchesSpecification("posix")); |
| 118 | + assert(platform.matchesSpecification("linux")); |
| 119 | + assert(platform.matchesSpecification("linux-dmd")); |
| 120 | + assert(platform.matchesSpecification("linux-x86_64-dmd")); |
| 121 | + assert(platform.matchesSpecification("x86_64")); |
| 122 | + assert(!platform.matchesSpecification("windows")); |
| 123 | + assert(!platform.matchesSpecification("ldc")); |
| 124 | + assert(!platform.matchesSpecification("windows-dmd")); |
| 125 | + |
| 126 | + // Before PR#2279, a leading '-' was required |
| 127 | + assert(platform.matchesSpecification("-x86_64")); |
| 128 | +} |
0 commit comments