Skip to content

Commit a0d80e8

Browse files
committed
Move data part of dub.platform to dub.data
We want to separate the data part, which is exposed to the PackageRecipe, from the actual platform check code, which is used when building.
1 parent c7a21f4 commit a0d80e8

File tree

4 files changed

+131
-117
lines changed

4 files changed

+131
-117
lines changed

build-files.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ source/dub/compilers/dmd.d
66
source/dub/compilers/gdc.d
77
source/dub/compilers/ldc.d
88
source/dub/compilers/utils.d
9+
source/dub/data/platform.d
910
source/dub/data/settings.d
1011
source/dub/dependency.d
1112
source/dub/dependencyresolver.d

source/dub/compilers/compiler.d

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module dub.compilers.compiler;
99

1010
public import dub.compilers.buildsettings;
1111
deprecated("Please `import dub.dependency : Dependency` instead") public import dub.dependency : Dependency;
12-
public import dub.platform : BuildPlatform, matchesSpecification;
12+
public import dub.data.platform : BuildPlatform, matchesSpecification;
1313

1414
import dub.internal.vibecompat.inet.path;
1515
import dub.internal.vibecompat.core.file;

source/dub/data/platform.d

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
}

source/dub/platform.d

+1-116
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
module dub.platform;
1616

1717
import std.array;
18+
public import dub.data.platform;
1819

1920
// archCheck, compilerCheck, and platformCheck are used below and in
2021
// generatePlatformProbeFile, so they've been extracted into these strings
@@ -166,119 +167,3 @@ string determineCompiler()
166167
{
167168
mixin(compilerCheck);
168169
}
169-
170-
/** Matches a platform specification string against a build platform.
171-
172-
Specifications are build upon the following scheme, where each component
173-
is optional (indicated by []), but the order is obligatory:
174-
"[-platform][-architecture][-compiler]"
175-
176-
So the following strings are valid specifications: `"-windows-x86-dmd"`,
177-
`"-dmd"`, `"-arm"`, `"-arm-dmd"`, `"-windows-dmd"`
178-
179-
Params:
180-
platform = The build platform to match against the platform specification
181-
specification = The specification being matched. It must either be an
182-
empty string or start with a dash.
183-
184-
Returns:
185-
`true` if the given specification matches the build platform, `false`
186-
otherwise. Using an empty string as the platform specification will
187-
always result in a match.
188-
*/
189-
bool matchesSpecification(in BuildPlatform platform, const(char)[] specification)
190-
{
191-
import std.string : chompPrefix, format;
192-
import std.algorithm : canFind, splitter;
193-
import std.exception : enforce;
194-
195-
if (specification.empty) return true;
196-
if (platform == BuildPlatform.any) return true;
197-
198-
auto splitted = specification.chompPrefix("-").splitter('-');
199-
enforce(!splitted.empty, format("Platform specification, if present, must not be empty: \"%s\"", specification));
200-
201-
if (platform.platform.canFind(splitted.front)) {
202-
splitted.popFront();
203-
if (splitted.empty)
204-
return true;
205-
}
206-
if (platform.architecture.canFind(splitted.front)) {
207-
splitted.popFront();
208-
if (splitted.empty)
209-
return true;
210-
}
211-
if (platform.compiler == splitted.front) {
212-
splitted.popFront();
213-
enforce(splitted.empty, "No valid specification! The compiler has to be the last element: " ~ specification);
214-
return true;
215-
}
216-
return false;
217-
}
218-
219-
///
220-
unittest {
221-
auto platform = BuildPlatform(["posix", "linux"], ["x86_64"], "dmd");
222-
assert(platform.matchesSpecification(""));
223-
assert(platform.matchesSpecification("posix"));
224-
assert(platform.matchesSpecification("linux"));
225-
assert(platform.matchesSpecification("linux-dmd"));
226-
assert(platform.matchesSpecification("linux-x86_64-dmd"));
227-
assert(platform.matchesSpecification("x86_64"));
228-
assert(!platform.matchesSpecification("windows"));
229-
assert(!platform.matchesSpecification("ldc"));
230-
assert(!platform.matchesSpecification("windows-dmd"));
231-
232-
// Before PR#2279, a leading '-' was required
233-
assert(platform.matchesSpecification("-x86_64"));
234-
}
235-
236-
/// Represents a platform a package can be build upon.
237-
struct BuildPlatform {
238-
/// Special constant used to denote matching any build platform.
239-
enum any = BuildPlatform(null, null, null, null, -1);
240-
241-
/// Platform identifiers, e.g. ["posix", "windows"]
242-
string[] platform;
243-
/// CPU architecture identifiers, e.g. ["x86", "x86_64"]
244-
string[] architecture;
245-
/// Canonical compiler name e.g. "dmd"
246-
string compiler;
247-
/// Compiler binary name e.g. "ldmd2"
248-
string compilerBinary;
249-
/// Compiled frontend version (e.g. `2067` for frontend versions 2.067.x)
250-
int frontendVersion;
251-
/// Compiler version e.g. "1.11.0"
252-
string compilerVersion;
253-
/// Frontend version string from frontendVersion
254-
/// e.g: 2067 => "2.067"
255-
string frontendVersionString() const
256-
{
257-
import std.format : format;
258-
259-
const maj = frontendVersion / 1000;
260-
const min = frontendVersion % 1000;
261-
return format("%d.%03d", maj, min);
262-
}
263-
///
264-
unittest
265-
{
266-
BuildPlatform bp;
267-
bp.frontendVersion = 2067;
268-
assert(bp.frontendVersionString == "2.067");
269-
}
270-
271-
/// Checks to see if platform field contains windows
272-
bool isWindows() const {
273-
import std.algorithm : canFind;
274-
return this.platform.canFind("windows");
275-
}
276-
///
277-
unittest {
278-
BuildPlatform bp;
279-
bp.platform = ["windows"];
280-
assert(bp.isWindows);
281-
bp.platform = ["posix"];
282-
assert(!bp.isWindows);
283-
}
284-
}

0 commit comments

Comments
 (0)