Newer
Older
dub_jkp / source / dub / recipe / packagerecipe.d
@Sebastian Wilzbach Sebastian Wilzbach on 19 Oct 2018 10 KB Revert "implement "minDubVersion" field"
  1. /**
  2. Abstract representation of a package description file.
  3.  
  4. Copyright: © 2012-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, Matthias Dondorff
  7. */
  8. module dub.recipe.packagerecipe;
  9.  
  10. import dub.compilers.compiler;
  11. import dub.compilers.utils : warnOnSpecialCompilerFlags;
  12. import dub.dependency;
  13.  
  14. import dub.internal.vibecompat.core.file;
  15. import dub.internal.vibecompat.core.log;
  16. import dub.internal.vibecompat.inet.url;
  17.  
  18. import std.algorithm : findSplit, sort;
  19. import std.array : join, split;
  20. import std.exception : enforce;
  21. import std.file;
  22. import std.range;
  23.  
  24.  
  25. /**
  26. Returns the individual parts of a qualified package name.
  27.  
  28. Sub qualified package names are lists of package names separated by ":". For
  29. example, "packa:packb:packc" references a package named "packc" that is a
  30. sub package of "packb", which in turn is a sub package of "packa".
  31. */
  32. string[] getSubPackagePath(string package_name) @safe pure
  33. {
  34. return package_name.split(":");
  35. }
  36.  
  37. /**
  38. Returns the name of the top level package for a given (sub) package name.
  39.  
  40. In case of a top level package, the qualified name is returned unmodified.
  41. */
  42. string getBasePackageName(string package_name) @safe pure
  43. {
  44. return package_name.findSplit(":")[0];
  45. }
  46.  
  47. /**
  48. Returns the qualified sub package part of the given package name.
  49.  
  50. This is the part of the package name excluding the base package
  51. name. See also $(D getBasePackageName).
  52. */
  53. string getSubPackageName(string package_name) @safe pure
  54. {
  55. return package_name.findSplit(":")[2];
  56. }
  57.  
  58. @safe unittest
  59. {
  60. assert(getSubPackagePath("packa:packb:packc") == ["packa", "packb", "packc"]);
  61. assert(getSubPackagePath("pack") == ["pack"]);
  62. assert(getBasePackageName("packa:packb:packc") == "packa");
  63. assert(getBasePackageName("pack") == "pack");
  64. assert(getSubPackageName("packa:packb:packc") == "packb:packc");
  65. assert(getSubPackageName("pack") == "");
  66. }
  67.  
  68. /**
  69. Represents the contents of a package recipe file (dub.json/dub.sdl) in an abstract way.
  70.  
  71. This structure is used to reason about package descriptions in isolation.
  72. For higher level package handling, see the $(D Package) class.
  73. */
  74. struct PackageRecipe {
  75. string name;
  76. string version_;
  77. string description;
  78. string homepage;
  79. string[] authors;
  80. string copyright;
  81. string license;
  82. string[] ddoxFilterArgs;
  83. string ddoxTool;
  84. BuildSettingsTemplate buildSettings;
  85. ConfigurationInfo[] configurations;
  86. BuildSettingsTemplate[string] buildTypes;
  87.  
  88. SubPackage[] subPackages;
  89.  
  90. inout(ConfigurationInfo) getConfiguration(string name)
  91. inout {
  92. foreach (c; configurations)
  93. if (c.name == name)
  94. return c;
  95. throw new Exception("Unknown configuration: "~name);
  96. }
  97.  
  98. /** Clones the package recipe recursively.
  99. */
  100. PackageRecipe clone() const { return .clone(this); }
  101. }
  102.  
  103. struct SubPackage
  104. {
  105. string path;
  106. PackageRecipe recipe;
  107. }
  108.  
  109.  
  110. /// Bundles information about a build configuration.
  111. struct ConfigurationInfo {
  112. string name;
  113. string[] platforms;
  114. BuildSettingsTemplate buildSettings;
  115.  
  116. this(string name, BuildSettingsTemplate build_settings)
  117. {
  118. enforce(!name.empty, "Configuration name is empty.");
  119. this.name = name;
  120. this.buildSettings = build_settings;
  121. }
  122.  
  123. bool matchesPlatform(in BuildPlatform platform)
  124. const {
  125. if( platforms.empty ) return true;
  126. foreach(p; platforms)
  127. if( platform.matchesSpecification("-"~p) )
  128. return true;
  129. return false;
  130. }
  131. }
  132.  
  133. /// This keeps general information about how to build a package.
  134. /// It contains functions to create a specific BuildSetting, targeted at
  135. /// a certain BuildPlatform.
  136. struct BuildSettingsTemplate {
  137. Dependency[string] dependencies;
  138. string systemDependencies;
  139. TargetType targetType = TargetType.autodetect;
  140. string targetPath;
  141. string targetName;
  142. string workingDirectory;
  143. string mainSourceFile;
  144. string[string] subConfigurations;
  145. string[][string] dflags;
  146. string[][string] lflags;
  147. string[][string] libs;
  148. string[][string] sourceFiles;
  149. string[][string] sourcePaths;
  150. string[][string] excludedSourceFiles;
  151. string[][string] copyFiles;
  152. string[][string] versions;
  153. string[][string] debugVersions;
  154. string[][string] importPaths;
  155. string[][string] stringImportPaths;
  156. string[][string] preGenerateCommands;
  157. string[][string] postGenerateCommands;
  158. string[][string] preBuildCommands;
  159. string[][string] postBuildCommands;
  160. BuildRequirements[string] buildRequirements;
  161. BuildOptions[string] buildOptions;
  162.  
  163.  
  164. /// Constructs a BuildSettings object from this template.
  165. void getPlatformSettings(ref BuildSettings dst, in BuildPlatform platform, NativePath base_path)
  166. const {
  167. dst.targetType = this.targetType;
  168. if (!this.targetPath.empty) dst.targetPath = this.targetPath;
  169. if (!this.targetName.empty) dst.targetName = this.targetName;
  170. if (!this.workingDirectory.empty) dst.workingDirectory = this.workingDirectory;
  171. if (!this.mainSourceFile.empty) {
  172. auto p = NativePath(this.mainSourceFile);
  173. p.normalize();
  174. dst.mainSourceFile = p.toNativeString();
  175. dst.addSourceFiles(dst.mainSourceFile);
  176. }
  177.  
  178. string[] collectFiles(in string[][string] paths_map, string pattern)
  179. {
  180. auto files = appender!(string[]);
  181.  
  182. foreach (suffix, paths; paths_map) {
  183. if (!platform.matchesSpecification(suffix))
  184. continue;
  185.  
  186. foreach (spath; paths) {
  187. enforce(!spath.empty, "Paths must not be empty strings.");
  188. auto path = NativePath(spath);
  189. if (!path.absolute) path = base_path ~ path;
  190. if (!existsFile(path) || !isDir(path.toNativeString())) {
  191. import dub.project : buildSettingsVars;
  192. import std.algorithm : any, find;
  193. const hasVar = buildSettingsVars.any!((string var) {
  194. return spath.find("$"~var).length > 0 || spath.find("${"~var~"}").length > 0;
  195. });
  196. if (!hasVar)
  197. logWarn("Invalid source/import path: %s", path.toNativeString());
  198. continue;
  199. }
  200.  
  201. foreach (d; dirEntries(path.toNativeString(), pattern, SpanMode.depth)) {
  202. import std.path : baseName;
  203. if (baseName(d.name)[0] == '.' || d.isDir) continue;
  204. auto src = NativePath(d.name).relativeTo(base_path);
  205. files ~= src.toNativeString();
  206. }
  207. }
  208. }
  209.  
  210. return files.data;
  211. }
  212.  
  213. // collect source files
  214. dst.addSourceFiles(collectFiles(sourcePaths, "*.d"));
  215. auto sourceFiles = dst.sourceFiles.sort();
  216.  
  217. // collect import files and remove sources
  218. import std.algorithm : copy, setDifference;
  219.  
  220. auto importFiles = collectFiles(importPaths, "*.{d,di}").sort();
  221. immutable nremoved = importFiles.setDifference(sourceFiles).copy(importFiles.release).length;
  222. importFiles = importFiles[0 .. $ - nremoved];
  223. dst.addImportFiles(importFiles.release);
  224.  
  225. dst.addStringImportFiles(collectFiles(stringImportPaths, "*"));
  226.  
  227. getPlatformSetting!("dflags", "addDFlags")(dst, platform);
  228. getPlatformSetting!("lflags", "addLFlags")(dst, platform);
  229. getPlatformSetting!("libs", "addLibs")(dst, platform);
  230. getPlatformSetting!("sourceFiles", "addSourceFiles")(dst, platform);
  231. getPlatformSetting!("excludedSourceFiles", "removeSourceFiles")(dst, platform);
  232. getPlatformSetting!("copyFiles", "addCopyFiles")(dst, platform);
  233. getPlatformSetting!("versions", "addVersions")(dst, platform);
  234. getPlatformSetting!("debugVersions", "addDebugVersions")(dst, platform);
  235. getPlatformSetting!("importPaths", "addImportPaths")(dst, platform);
  236. getPlatformSetting!("stringImportPaths", "addStringImportPaths")(dst, platform);
  237. getPlatformSetting!("preGenerateCommands", "addPreGenerateCommands")(dst, platform);
  238. getPlatformSetting!("postGenerateCommands", "addPostGenerateCommands")(dst, platform);
  239. getPlatformSetting!("preBuildCommands", "addPreBuildCommands")(dst, platform);
  240. getPlatformSetting!("postBuildCommands", "addPostBuildCommands")(dst, platform);
  241. getPlatformSetting!("buildRequirements", "addRequirements")(dst, platform);
  242. getPlatformSetting!("buildOptions", "addOptions")(dst, platform);
  243. }
  244.  
  245. void getPlatformSetting(string name, string addname)(ref BuildSettings dst, in BuildPlatform platform)
  246. const {
  247. foreach(suffix, values; __traits(getMember, this, name)){
  248. if( platform.matchesSpecification(suffix) )
  249. __traits(getMember, dst, addname)(values);
  250. }
  251. }
  252.  
  253. void warnOnSpecialCompilerFlags(string package_name, string config_name)
  254. {
  255. auto nodef = false;
  256. auto noprop = false;
  257. foreach (req; this.buildRequirements) {
  258. if (req & BuildRequirement.noDefaultFlags) nodef = true;
  259. if (req & BuildRequirement.relaxProperties) noprop = true;
  260. }
  261.  
  262. if (noprop) {
  263. logWarn(`Warning: "buildRequirements": ["relaxProperties"] is deprecated and is now the default behavior. Note that the -property switch will probably be removed in future versions of DMD.`);
  264. logWarn("");
  265. }
  266.  
  267. if (nodef) {
  268. logWarn("Warning: This package uses the \"noDefaultFlags\" build requirement. Please use only for development purposes and not for released packages.");
  269. logWarn("");
  270. } else {
  271. string[] all_dflags;
  272. BuildOptions all_options;
  273. foreach (flags; this.dflags) all_dflags ~= flags;
  274. foreach (options; this.buildOptions) all_options |= options;
  275. .warnOnSpecialCompilerFlags(all_dflags, all_options, package_name, config_name);
  276. }
  277. }
  278. }
  279.  
  280. private T clone(T)(ref const(T) val)
  281. {
  282. import std.traits : isSomeString, isDynamicArray, isAssociativeArray, isBasicType, ValueType;
  283.  
  284. static if (is(T == immutable)) return val;
  285. else static if (isBasicType!T) return val;
  286. else static if (isDynamicArray!T) {
  287. alias V = typeof(T.init[0]);
  288. static if (is(V == immutable)) return val;
  289. else {
  290. T ret = new V[val.length];
  291. foreach (i, ref f; val)
  292. ret[i] = clone!V(f);
  293. return ret;
  294. }
  295. } else static if (isAssociativeArray!T) {
  296. alias V = ValueType!T;
  297. T ret;
  298. foreach (k, ref f; val)
  299. ret[k] = clone!V(f);
  300. return ret;
  301. } else static if (is(T == struct)) {
  302. T ret;
  303. foreach (i, M; typeof(T.tupleof))
  304. ret.tupleof[i] = clone!M(val.tupleof[i]);
  305. return ret;
  306. } else static assert(false, "Unsupported type: "~T.stringof);
  307. }
  308.  
  309. unittest { // issue #1407 - duplicate main source file
  310. {
  311. BuildSettingsTemplate t;
  312. t.mainSourceFile = "./foo.d";
  313. t.sourceFiles[""] = ["foo.d"];
  314. BuildSettings bs;
  315. t.getPlatformSettings(bs, BuildPlatform.init, NativePath("/"));
  316. assert(bs.sourceFiles == ["foo.d"]);
  317. }
  318.  
  319. version (Windows) {{
  320. BuildSettingsTemplate t;
  321. t.mainSourceFile = "src/foo.d";
  322. t.sourceFiles[""] = ["src\\foo.d"];
  323. BuildSettings bs;
  324. t.getPlatformSettings(bs, BuildPlatform.init, NativePath("/"));
  325. assert(bs.sourceFiles == ["src\\foo.d"]);
  326. }}
  327. }