Newer
Older
dub_jkp / source / dub / recipe / packagerecipe.d
  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", wich in turn is a sub package of "packa".
  31. */
  32. string[] getSubPackagePath(string package_name)
  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)
  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)
  54. {
  55. return package_name.findSplit(":")[2];
  56. }
  57.  
  58. 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. deprecated("Use Package.dependencies or the dependencies of the individual BuildSettingsTemplates instead. Will be removed for version 1.0.0.")
  91. @property const(Dependency)[string] dependencies()
  92. const {
  93. Dependency[string] ret;
  94. foreach (n, d; this.buildSettings.dependencies)
  95. ret[n] = d;
  96. foreach (ref c; configurations)
  97. foreach (n, d; c.buildSettings.dependencies)
  98. ret[n] = d;
  99. return ret;
  100. }
  101.  
  102. inout(ConfigurationInfo) getConfiguration(string name)
  103. inout {
  104. foreach (c; configurations)
  105. if (c.name == name)
  106. return c;
  107. throw new Exception("Unknown configuration: "~name);
  108. }
  109. }
  110.  
  111. struct SubPackage
  112. {
  113. string path;
  114. PackageRecipe recipe;
  115. }
  116.  
  117.  
  118. /// Bundles information about a build configuration.
  119. struct ConfigurationInfo {
  120. string name;
  121. string[] platforms;
  122. BuildSettingsTemplate buildSettings;
  123.  
  124. this(string name, BuildSettingsTemplate build_settings)
  125. {
  126. enforce(!name.empty, "Configuration name is empty.");
  127. this.name = name;
  128. this.buildSettings = build_settings;
  129. }
  130.  
  131. bool matchesPlatform(in BuildPlatform platform)
  132. const {
  133. if( platforms.empty ) return true;
  134. foreach(p; platforms)
  135. if( platform.matchesSpecification("-"~p) )
  136. return true;
  137. return false;
  138. }
  139. }
  140.  
  141. /// This keeps general information about how to build a package.
  142. /// It contains functions to create a specific BuildSetting, targeted at
  143. /// a certain BuildPlatform.
  144. struct BuildSettingsTemplate {
  145. Dependency[string] dependencies;
  146. string systemDependencies;
  147. TargetType targetType = TargetType.autodetect;
  148. string targetPath;
  149. string targetName;
  150. string workingDirectory;
  151. string mainSourceFile;
  152. string[string] subConfigurations;
  153. string[][string] dflags;
  154. string[][string] lflags;
  155. string[][string] libs;
  156. string[][string] sourceFiles;
  157. string[][string] sourcePaths;
  158. string[][string] excludedSourceFiles;
  159. string[][string] copyFiles;
  160. string[][string] versions;
  161. string[][string] debugVersions;
  162. string[][string] importPaths;
  163. string[][string] stringImportPaths;
  164. string[][string] preGenerateCommands;
  165. string[][string] postGenerateCommands;
  166. string[][string] preBuildCommands;
  167. string[][string] postBuildCommands;
  168. BuildRequirements[string] buildRequirements;
  169. BuildOptions[string] buildOptions;
  170.  
  171.  
  172. /// Constructs a BuildSettings object from this template.
  173. void getPlatformSettings(ref BuildSettings dst, in BuildPlatform platform, Path base_path)
  174. const {
  175. dst.targetType = this.targetType;
  176. if (!this.targetPath.empty) dst.targetPath = this.targetPath;
  177. if (!this.targetName.empty) dst.targetName = this.targetName;
  178. if (!this.workingDirectory.empty) dst.workingDirectory = this.workingDirectory;
  179. if (!this.mainSourceFile.empty) {
  180. dst.mainSourceFile = this.mainSourceFile;
  181. dst.addSourceFiles(this.mainSourceFile);
  182. }
  183.  
  184. void collectFiles(string method)(in string[][string] paths_map, string pattern)
  185. {
  186. foreach (suffix, paths; paths_map) {
  187. if (!platform.matchesSpecification(suffix))
  188. continue;
  189.  
  190. foreach (spath; paths) {
  191. enforce(!spath.empty, "Paths must not be empty strings.");
  192. auto path = Path(spath);
  193. if (!path.absolute) path = base_path ~ path;
  194. if (!existsFile(path) || !isDir(path.toNativeString())) {
  195. logWarn("Invalid source/import path: %s", path.toNativeString());
  196. continue;
  197. }
  198.  
  199. foreach (d; dirEntries(path.toNativeString(), pattern, SpanMode.depth)) {
  200. import std.path : baseName;
  201. if (baseName(d.name)[0] == '.' || isDir(d.name)) continue;
  202. auto src = Path(d.name).relativeTo(base_path);
  203. __traits(getMember, dst, method)(src.toNativeString());
  204. }
  205. }
  206. }
  207. }
  208.  
  209. // collect files from all source/import folders
  210. collectFiles!"addSourceFiles"(sourcePaths, "*.d");
  211. collectFiles!"addImportFiles"(importPaths, "*.{d,di}");
  212. dst.removeImportFiles(dst.sourceFiles);
  213. collectFiles!"addStringImportFiles"(stringImportPaths, "*");
  214.  
  215. // ensure a deterministic order of files as passed to the compiler
  216. dst.sourceFiles.sort();
  217.  
  218. getPlatformSetting!("dflags", "addDFlags")(dst, platform);
  219. getPlatformSetting!("lflags", "addLFlags")(dst, platform);
  220. getPlatformSetting!("libs", "addLibs")(dst, platform);
  221. getPlatformSetting!("sourceFiles", "addSourceFiles")(dst, platform);
  222. getPlatformSetting!("excludedSourceFiles", "removeSourceFiles")(dst, platform);
  223. getPlatformSetting!("copyFiles", "addCopyFiles")(dst, platform);
  224. getPlatformSetting!("versions", "addVersions")(dst, platform);
  225. getPlatformSetting!("debugVersions", "addDebugVersions")(dst, platform);
  226. getPlatformSetting!("importPaths", "addImportPaths")(dst, platform);
  227. getPlatformSetting!("stringImportPaths", "addStringImportPaths")(dst, platform);
  228. getPlatformSetting!("preGenerateCommands", "addPreGenerateCommands")(dst, platform);
  229. getPlatformSetting!("postGenerateCommands", "addPostGenerateCommands")(dst, platform);
  230. getPlatformSetting!("preBuildCommands", "addPreBuildCommands")(dst, platform);
  231. getPlatformSetting!("postBuildCommands", "addPostBuildCommands")(dst, platform);
  232. getPlatformSetting!("buildRequirements", "addRequirements")(dst, platform);
  233. getPlatformSetting!("buildOptions", "addOptions")(dst, platform);
  234. }
  235.  
  236. void getPlatformSetting(string name, string addname)(ref BuildSettings dst, in BuildPlatform platform)
  237. const {
  238. foreach(suffix, values; __traits(getMember, this, name)){
  239. if( platform.matchesSpecification(suffix) )
  240. __traits(getMember, dst, addname)(values);
  241. }
  242. }
  243.  
  244. void warnOnSpecialCompilerFlags(string package_name, string config_name)
  245. {
  246. auto nodef = false;
  247. auto noprop = false;
  248. foreach (req; this.buildRequirements) {
  249. if (req & BuildRequirement.noDefaultFlags) nodef = true;
  250. if (req & BuildRequirement.relaxProperties) noprop = true;
  251. }
  252.  
  253. if (noprop) {
  254. 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.`);
  255. logWarn("");
  256. }
  257.  
  258. if (nodef) {
  259. logWarn("Warning: This package uses the \"noDefaultFlags\" build requirement. Please use only for development purposes and not for released packages.");
  260. logWarn("");
  261. } else {
  262. string[] all_dflags;
  263. BuildOptions all_options;
  264. foreach (flags; this.dflags) all_dflags ~= flags;
  265. foreach (options; this.buildOptions) all_options |= options;
  266. .warnOnSpecialCompilerFlags(all_dflags, all_options, package_name, config_name);
  267. }
  268. }
  269. }