| 1 |
/** |
| 2 |
* Implement the `-deps` and `-makedeps` switches, which output dependencies of modules for build tools. |
| 3 |
* |
| 4 |
* The grammar of the `-deps` output is: |
| 5 |
* --- |
| 6 |
* ImportDeclaration |
| 7 |
* ::= BasicImportDeclaration [ " : " ImportBindList ] [ " -> " |
| 8 |
* ModuleAliasIdentifier ] "\n" |
| 9 |
* |
| 10 |
* BasicImportDeclaration |
| 11 |
* ::= ModuleFullyQualifiedName " (" FilePath ") : " Protection|"string" |
| 12 |
* " [ " static" ] : " ModuleFullyQualifiedName " (" FilePath ")" |
| 13 |
* |
| 14 |
* FilePath |
| 15 |
* - any string with '(', ')' and '\' escaped with the '\' character |
| 16 |
* --- |
| 17 |
* |
| 18 |
* Make dependencies as generated by `-makedeps` look like this: |
| 19 |
* --- |
| 20 |
* source/app.d: |
| 21 |
* source/importa.d \ |
| 22 |
* source/importb.d |
| 23 |
* --- |
| 24 |
* |
| 25 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 26 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 27 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/deps.d, makedeps.d) |
| 28 |
* Documentation: https://dlang.org/phobos/dmd_deps.html |
| 29 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/deps.d |
| 30 |
*/ |
| 31 |
module dmd.deps; |
| 32 |
|
| 33 |
import core.stdc.stdio : printf; |
| 34 |
import core.stdc.string : strcmp; |
| 35 |
import dmd.common.outbuffer; |
| 36 |
import dmd.dimport : Import; |
| 37 |
import dmd.dmodule : Module; |
| 38 |
import dmd.globals : Param, Output; |
| 39 |
import dmd.hdrgen : visibilityToBuffer; |
| 40 |
import dmd.id : Id; |
| 41 |
import dmd.utils : escapePath; |
| 42 |
|
| 43 |
/** |
| 44 |
* Add an import expression to module dependencies |
| 45 |
* Params: |
| 46 |
* moduleDeps = output settings for `-deps` |
| 47 |
* makeDeps = output settings for `-makedeps` |
| 48 |
* fileNameZ = 0-termminated string containing the import expression's resolved filename |
| 49 |
* importString = raw string passed to import exp |
| 50 |
* imod = module import exp is in |
| 51 |
*/ |
| 52 |
void addImportExpDep(ref Output moduleDeps, ref Output makeDeps, const(char)[] fileNameZ, const(char)[] importString, Module imod) |
| 53 |
{ |
| 54 |
if (moduleDeps.buffer !is null) |
| 55 |
{ |
| 56 |
OutBuffer* ob = moduleDeps.buffer; |
| 57 |
|
| 58 |
if (!moduleDeps.name) |
| 59 |
ob.writestring("depsFile "); |
| 60 |
ob.writestring(imod.toPrettyChars()); |
| 61 |
ob.writestring(" ("); |
| 62 |
escapePath(ob, imod.srcfile.toChars()); |
| 63 |
ob.writestring(") : "); |
| 64 |
if (moduleDeps.name) |
| 65 |
ob.writestring("string : "); |
| 66 |
ob.write(importString); |
| 67 |
ob.writestring(" ("); |
| 68 |
escapePath(ob, fileNameZ.ptr); |
| 69 |
ob.writestring(")"); |
| 70 |
ob.writenl(); |
| 71 |
} |
| 72 |
if (makeDeps.doOutput) |
| 73 |
{ |
| 74 |
makeDeps.files.push(fileNameZ.ptr); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Add an import statement to module dependencies |
| 80 |
* Params: |
| 81 |
* moduleDeps = output settings |
| 82 |
* imp = import to add |
| 83 |
* imod = module that the import is in |
| 84 |
*/ |
| 85 |
void addImportDep(ref Output moduleDeps, Import imp, Module imod) |
| 86 |
{ |
| 87 |
// object self-imports itself, so skip that |
| 88 |
// https://issues.dlang.org/show_bug.cgi?id=7547 |
| 89 |
// don't list pseudo modules __entrypoint.d, __main.d |
| 90 |
// https://issues.dlang.org/show_bug.cgi?id=11117 |
| 91 |
// https://issues.dlang.org/show_bug.cgi?id=11164 |
| 92 |
if (moduleDeps.buffer is null || (imp.id == Id.object && imod.ident == Id.object) || |
| 93 |
strcmp(imod.ident.toChars(), "__main") == 0) |
| 94 |
return; |
| 95 |
|
| 96 |
OutBuffer* ob = moduleDeps.buffer; |
| 97 |
if (!moduleDeps.name) |
| 98 |
ob.writestring("depsImport "); |
| 99 |
ob.writestring(imod.toPrettyChars()); |
| 100 |
ob.writestring(" ("); |
| 101 |
escapePath(ob, imod.srcfile.toChars()); |
| 102 |
ob.writestring(") : "); |
| 103 |
// use visibility instead of sc.visibility because it couldn't be |
| 104 |
// resolved yet, see the comment above |
| 105 |
visibilityToBuffer(*ob, imp.visibility); |
| 106 |
ob.writeByte(' '); |
| 107 |
if (imp.isstatic) |
| 108 |
{ |
| 109 |
ob.writestring("static "); |
| 110 |
} |
| 111 |
ob.writestring(": "); |
| 112 |
foreach (pid; imp.packages) |
| 113 |
{ |
| 114 |
ob.printf("%s.", pid.toChars()); |
| 115 |
} |
| 116 |
ob.writestring(imp.id.toString()); |
| 117 |
ob.writestring(" ("); |
| 118 |
if (imp.mod) |
| 119 |
escapePath(ob, imp.mod.srcfile.toChars()); |
| 120 |
else |
| 121 |
ob.writestring("???"); |
| 122 |
ob.writeByte(')'); |
| 123 |
foreach (i, name; imp.names) |
| 124 |
{ |
| 125 |
if (i == 0) |
| 126 |
ob.writeByte(':'); |
| 127 |
else |
| 128 |
ob.writeByte(','); |
| 129 |
auto _alias = imp.aliases[i]; |
| 130 |
if (!_alias) |
| 131 |
{ |
| 132 |
ob.printf("%s", name.toChars()); |
| 133 |
_alias = name; |
| 134 |
} |
| 135 |
else |
| 136 |
ob.printf("%s=%s", _alias.toChars(), name.toChars()); |
| 137 |
} |
| 138 |
if (imp.aliasId) |
| 139 |
ob.printf(" -> %s", imp.aliasId.toChars()); |
| 140 |
ob.writenl(); |
| 141 |
} |