1 /**
2 	Build settings definitions.
3 
4 	Copyright: © 2013-2014 rejectedsoftware e.K.
5 	License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
6 	Authors: Sönke Ludwig
7 */
8 module dub.compilers.buildsettings;
9 
10 import dub.internal.vibecompat.inet.path;
11 
12 import std.array : array;
13 import std.algorithm : filter, any;
14 import std.path : globMatch;
15 import std.typecons : BitFlags;
16 
17 
18 /// BuildPlatform specific settings, like needed libraries or additional
19 /// include paths.
20 struct BuildSettings {
21 	import dub.internal.vibecompat.data.serialization : byName;
22 
23 	TargetType targetType;
24 	string targetPath;
25 	string targetName;
26 	string workingDirectory;
27 	string mainSourceFile;
28 	string[] dflags;
29 	string[] lflags;
30 	string[] libs;
31 	string[] linkerFiles;
32 	string[] sourceFiles;
33 	string[] copyFiles;
34 	string[] extraDependencyFiles;
35 	string[] versions;
36 	string[] debugVersions;
37 	string[] versionFilters;
38 	string[] debugVersionFilters;
39 	string[] importPaths;
40 	string[] stringImportPaths;
41 	string[] importFiles;
42 	string[] stringImportFiles;
43 	string[] preGenerateCommands;
44 	string[] postGenerateCommands;
45 	string[] preBuildCommands;
46 	string[] postBuildCommands;
47 	string[] preRunCommands;
48 	string[] postRunCommands;
49 	@byName BuildRequirements requirements;
50 	@byName BuildOptions options;
51 
52 	BuildSettings dup()
53 	const {
54 		BuildSettings ret;
55 		foreach (m; __traits(allMembers, BuildSettings)) {
56 			static if (is(typeof(__traits(getMember, ret, m) = __traits(getMember, this, m).dup)))
57 				__traits(getMember, ret, m) = __traits(getMember, this, m).dup;
58 			else static if (is(typeof(__traits(getMember, ret, m) = __traits(getMember, this, m))))
59 				__traits(getMember, ret, m) = __traits(getMember, this, m);
60 		}
61 		assert(ret.targetType == targetType);
62 		assert(ret.targetName == targetName);
63 		assert(ret.importPaths == importPaths);
64 		return ret;
65 	}
66 
67 	void add(in BuildSettings bs)
68 	{
69 		addDFlags(bs.dflags);
70 		addLFlags(bs.lflags);
71 		addLibs(bs.libs);
72 		addLinkerFiles(bs.linkerFiles);
73 		addSourceFiles(bs.sourceFiles);
74 		addCopyFiles(bs.copyFiles);
75 		addExtraDependencyFiles(bs.extraDependencyFiles);
76 		addVersions(bs.versions);
77 		addDebugVersions(bs.debugVersions);
78 		addVersionFilters(bs.versionFilters);
79 		addDebugVersionFilters(bs.debugVersionFilters);
80 		addImportPaths(bs.importPaths);
81 		addStringImportPaths(bs.stringImportPaths);
82 		addImportFiles(bs.importFiles);
83 		addStringImportFiles(bs.stringImportFiles);
84 		addPreGenerateCommands(bs.preGenerateCommands);
85 		addPostGenerateCommands(bs.postGenerateCommands);
86 		addPreBuildCommands(bs.preBuildCommands);
87 		addPostBuildCommands(bs.postBuildCommands);
88 		addPreRunCommands(bs.preRunCommands);
89 		addPostRunCommands(bs.postRunCommands);
90 	}
91 
92 	void addDFlags(in string[] value...) { dflags ~= value; }
93 	void prependDFlags(in string[] value...) { prepend(dflags, value); }
94 	void removeDFlags(in string[] value...) { remove(dflags, value); }
95 	void addLFlags(in string[] value...) { lflags ~= value; }
96 	void addLibs(in string[] value...) { add(libs, value); }
97 	void addLinkerFiles(in string[] value...) { add(linkerFiles, value); }
98 	void addSourceFiles(in string[] value...) { add(sourceFiles, value); }
99 	void prependSourceFiles(in string[] value...) { prepend(sourceFiles, value); }
100 	void removeSourceFiles(in string[] value...) { removePaths(sourceFiles, value); }
101 	void addCopyFiles(in string[] value...) { add(copyFiles, value); }
102 	void addExtraDependencyFiles(in string[] value...) { add(extraDependencyFiles, value); }
103 	void addVersions(in string[] value...) { add(versions, value); }
104 	void addDebugVersions(in string[] value...) { add(debugVersions, value); }
105 	void addVersionFilters(in string[] value...) { add(versionFilters, value); }
106 	void addDebugVersionFilters(in string[] value...) { add(debugVersionFilters, value); }
107 	void addImportPaths(in string[] value...) { add(importPaths, value); }
108 	void addStringImportPaths(in string[] value...) { add(stringImportPaths, value); }
109 	void prependStringImportPaths(in string[] value...) { prepend(stringImportPaths, value); }
110 	void addImportFiles(in string[] value...) { add(importFiles, value); }
111 	void addStringImportFiles(in string[] value...) { addSI(stringImportFiles, value); }
112 	void addPreGenerateCommands(in string[] value...) { add(preGenerateCommands, value, false); }
113 	void addPostGenerateCommands(in string[] value...) { add(postGenerateCommands, value, false); }
114 	void addPreBuildCommands(in string[] value...) { add(preBuildCommands, value, false); }
115 	void addPostBuildCommands(in string[] value...) { add(postBuildCommands, value, false); }
116 	void addPreRunCommands(in string[] value...) { add(preRunCommands, value, false); }
117 	void addPostRunCommands(in string[] value...) { add(postRunCommands, value, false); }
118 	void addRequirements(in BuildRequirement[] value...) { foreach (v; value) this.requirements |= v; }
119 	void addRequirements(in BuildRequirements value) { this.requirements |= value; }
120 	void addOptions(in BuildOption[] value...) { foreach (v; value) this.options |= v; }
121 	void addOptions(in BuildOptions value) { this.options |= value; }
122 	void removeOptions(in BuildOption[] value...) { foreach (v; value) this.options &= ~v; }
123 	void removeOptions(in BuildOptions value) { this.options &= ~value; }
124 
125 private:
126 	static auto filterDuplicates(T)(ref string[] arr, in T vals, bool noDuplicates = true)
127 	{
128 		return noDuplicates
129 			? vals.filter!(filtered => !arr.any!(item => item == filtered)).array
130 			: vals;
131 	}
132 
133 	// Append vals to arr without adding duplicates.
134 	static void add(ref string[] arr, in string[] vals, bool noDuplicates = true)
135 	{
136 		arr ~= filterDuplicates(arr, vals, noDuplicates);
137 	}
138 
139 	unittest
140 	{
141 		auto ary = ["-dip1000", "-vgc"];
142 		BuildSettings.add(ary, ["-dip1000", "-vgc"]);
143 		assert(ary == ["-dip1000", "-vgc"]);
144 		BuildSettings.add(ary, ["-dip1001", "-vgc"], false);
145 		assert(ary == ["-dip1000", "-vgc", "-dip1001", "-vgc"]);
146 	}
147 
148 	// Prepend arr by vals without adding duplicates.
149 	static void prepend(ref string[] arr, in string[] vals, bool noDuplicates = true)
150 	{
151 		arr = filterDuplicates(arr, vals, noDuplicates) ~ arr;
152 	}
153 
154 	unittest
155 	{
156 		auto ary = ["-dip1000", "-vgc"];
157 		BuildSettings.prepend(ary, ["-dip1000", "-vgc"]);
158 		assert(ary == ["-dip1000", "-vgc"]);
159 		BuildSettings.prepend(ary, ["-dip1001", "-vgc"], false);
160 		assert(ary == ["-dip1001", "-vgc", "-dip1000", "-vgc"]);
161 	}
162 
163 	// add string import files (avoids file name duplicates in addition to path duplicates)
164 	static void addSI(ref string[] arr, in string[] vals)
165 	{
166 		bool[string] existing;
167 		foreach (v; arr) existing[NativePath(v).head.name] = true;
168 		foreach (v; vals) {
169 			auto s = NativePath(v).head.name;
170 			if (s !in existing) {
171 				existing[s] = true;
172 				arr ~= v;
173 			}
174 		}
175 	}
176 
177 	unittest
178 	{
179 		auto ary = ["path/foo.txt"];
180 		BuildSettings.addSI(ary, ["path2/foo2.txt"]);
181 		assert(ary == ["path/foo.txt", "path2/foo2.txt"]);
182 		BuildSettings.addSI(ary, ["path2/foo.txt"]); // no duplicate basenames
183 		assert(ary == ["path/foo.txt", "path2/foo2.txt"]);
184 	}
185 
186 	static bool pathMatch(string path, string pattern)
187 	{
188 		import std.functional : memoize;
189 
190 		alias nativePath = memoize!((string stringPath) => NativePath(stringPath));
191 
192 		return nativePath(path) == nativePath(pattern) || globMatch(path, pattern);
193 	}
194 
195 	static void removeValuesFromArray(alias Match)(ref string[] arr, in string[] vals)
196 	{
197 		bool matches(string s)
198 		{
199 			return vals.any!(item => Match(s, item));
200 		}
201 		arr = arr.filter!(s => !matches(s)).array;
202 	}
203 
204 	static void removePaths(ref string[] arr, in string[] vals)
205 	{
206 		removeValuesFromArray!(pathMatch)(arr, vals);
207 	}
208 
209 	unittest
210 	{
211 		auto ary = ["path1", "root/path1", "root/path2", "root2/path1"];
212 		BuildSettings.removePaths(ary, ["path1"]);
213 		assert(ary == ["root/path1", "root/path2", "root2/path1"]);
214 		BuildSettings.removePaths(ary, ["*/path1"]);
215 		assert(ary == ["root/path2"]);
216 		BuildSettings.removePaths(ary, ["foo", "bar", "root/path2"]);
217 		assert(ary == []);
218 	}
219 
220 	static void remove(ref string[] arr, in string[] vals)
221 	{
222 		removeValuesFromArray!((a, b) => a == b)(arr, vals);
223 	}
224 
225 	unittest
226 	{
227 		import std.string : join;
228 
229 		auto ary = ["path1", "root/path1", "root/path2", "root2/path1"];
230 		BuildSettings.remove(ary, ["path1"]);
231 		assert(ary == ["root/path1", "root/path2", "root2/path1"]);
232 		BuildSettings.remove(ary, ["root/path*"]);
233 		assert(ary == ["root/path1", "root/path2", "root2/path1"]);
234 		BuildSettings.removePaths(ary, ["foo", "root/path2", "bar", "root2/path1"]);
235 		assert(ary == ["root/path1"]);
236 		BuildSettings.remove(ary, ["root/path1", "foo"]);
237 		assert(ary == []);
238 	}
239 }
240 
241 enum BuildSetting {
242 	dflags            = 1<<0,
243 	lflags            = 1<<1,
244 	libs              = 1<<2,
245 	sourceFiles       = 1<<3,
246 	copyFiles         = 1<<4,
247 	versions          = 1<<5,
248 	debugVersions     = 1<<6,
249 	importPaths       = 1<<7,
250 	stringImportPaths = 1<<8,
251 	options           = 1<<9,
252 	none = 0,
253 	commandLine = dflags|copyFiles,
254 	commandLineSeparate = commandLine|lflags,
255 	all = dflags|lflags|libs|sourceFiles|copyFiles|versions|debugVersions|importPaths|stringImportPaths|options,
256 	noOptions = all & ~options
257 }
258 
259 enum TargetType {
260 	autodetect,
261 	none,
262 	executable,
263 	library,
264 	sourceLibrary,
265 	dynamicLibrary,
266 	staticLibrary,
267 	object
268 }
269 
270 enum BuildRequirement {
271 	none = 0,                     /// No special requirements
272 	allowWarnings        = 1<<0,  /// Warnings do not abort compilation
273 	silenceWarnings      = 1<<1,  /// Don't show warnings
274 	disallowDeprecations = 1<<2,  /// Using deprecated features aborts compilation
275 	silenceDeprecations  = 1<<3,  /// Don't show deprecation warnings
276 	disallowInlining     = 1<<4,  /// Avoid function inlining, even in release builds
277 	disallowOptimization = 1<<5,  /// Avoid optimizations, even in release builds
278 	requireBoundsCheck   = 1<<6,  /// Always perform bounds checks
279 	requireContracts     = 1<<7,  /// Leave assertions and contracts enabled in release builds
280 	relaxProperties      = 1<<8,  /// DEPRECATED: Do not enforce strict property handling (-property)
281 	noDefaultFlags       = 1<<9,  /// Do not issue any of the default build flags (e.g. -debug, -w, -property etc.) - use only for development purposes
282 }
283 
284 	struct BuildRequirements {
285 		import dub.internal.vibecompat.data.serialization : ignore;
286 
287 		@ignore BitFlags!BuildRequirement values;
288 		this(BuildRequirement req) { values = req; }
289 
290 		alias values this;
291 	}
292 
293 enum BuildOption {
294 	none = 0,                     /// Use compiler defaults
295 	debugMode = 1<<0,             /// Compile in debug mode (enables contracts, -debug)
296 	releaseMode = 1<<1,           /// Compile in release mode (disables assertions and bounds checks, -release)
297 	coverage = 1<<2,              /// Enable code coverage analysis (-cov)
298 	debugInfo = 1<<3,             /// Enable symbolic debug information (-g)
299 	debugInfoC = 1<<4,            /// Enable symbolic debug information in C compatible form (-gc)
300 	alwaysStackFrame = 1<<5,      /// Always generate a stack frame (-gs)
301 	stackStomping = 1<<6,         /// Perform stack stomping (-gx)
302 	inline = 1<<7,                /// Perform function inlining (-inline)
303 	noBoundsCheck = 1<<8,         /// Disable all bounds checking (-noboundscheck)
304 	optimize = 1<<9,              /// Enable optimizations (-O)
305 	profile = 1<<10,              /// Emit profiling code (-profile)
306 	unittests = 1<<11,            /// Compile unit tests (-unittest)
307 	verbose = 1<<12,              /// Verbose compiler output (-v)
308 	ignoreUnknownPragmas = 1<<13, /// Ignores unknown pragmas during compilation (-ignore)
309 	syntaxOnly = 1<<14,           /// Don't generate object files (-o-)
310 	warnings = 1<<15,             /// Enable warnings (-wi)
311 	warningsAsErrors = 1<<16,     /// Treat warnings as errors (-w)
312 	ignoreDeprecations = 1<<17,   /// Do not warn about using deprecated features (-d)
313 	deprecationWarnings = 1<<18,  /// Warn about using deprecated features (-dw)
314 	deprecationErrors = 1<<19,    /// Stop compilation upon usage of deprecated features (-de)
315 	property = 1<<20,             /// DEPRECATED: Enforce property syntax (-property)
316 	profileGC = 1<<21,            /// Profile runtime allocations
317 	pic = 1<<22,                  /// Generate position independent code
318 	betterC = 1<<23,              /// Compile in betterC mode (-betterC)
319 
320 	// for internal usage
321 	_docs = 1<<24,                // Write ddoc to docs
322 	_ddox = 1<<25                 // Compile docs.json
323 }
324 
325 	struct BuildOptions {
326 		import dub.internal.vibecompat.data.serialization : ignore;
327 
328 		@ignore BitFlags!BuildOption values;
329 		this(BuildOption opt) { values = opt; }
330 		this(BitFlags!BuildOption v) { values = v; }
331 
332 		alias values this;
333 	}
334 
335 /**
336 	All build options that will be inherited upwards in the dependency graph
337 
338 	Build options in this category fulfill one of the following properties:
339 	$(UL
340 		$(LI The option affects the semantics of the generated code)
341 		$(LI The option affects if a certain piece of code is valid or not)
342 		$(LI The option enabled meta information in dependent projects that are useful for the dependee (e.g. debug information))
343 	)
344 */
345 enum BuildOptions inheritedBuildOptions = BuildOption.debugMode | BuildOption.releaseMode
346 	| BuildOption.coverage | BuildOption.debugInfo | BuildOption.debugInfoC
347 	| BuildOption.alwaysStackFrame | BuildOption.stackStomping | BuildOption.inline
348 	| BuildOption.noBoundsCheck | BuildOption.profile | BuildOption.ignoreUnknownPragmas
349 	| BuildOption.syntaxOnly | BuildOption.warnings	| BuildOption.warningsAsErrors
350 	| BuildOption.ignoreDeprecations | BuildOption.deprecationWarnings
351 	| BuildOption.deprecationErrors | BuildOption.property | BuildOption.profileGC
352 	| BuildOption.pic;