Skip to content

Commit 0e8dc5e

Browse files
committed
Improve auto-macro injection
1 parent 16d04af commit 0e8dc5e

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

ddoc/source/preprocessor.d

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,41 @@ All unknown options are passed to the compiler.
6363
text = genChangelogVersion(inputFile, text);
6464
text = genSwitches(text);
6565

66-
if (inputFile.endsWith(".d"))
66+
// Phobos index.d should have been named index.dd
67+
if (inputFile.endsWith(".d") && !inputFile.endsWith("index.d"))
6768
text = assertWritelnModule(text);
6869

69-
// inject custom, "dynamic" macros
70-
if (inputFile.endsWith(".dd", "index.d"))
71-
text ~= "\nSRC_FILENAME=%s\n".format(inputFile.buildNormalizedPath);
72-
else if (inputFile.endsWith(".d"))
73-
text ~= "/**\nMacros:\n\nSRC_FILENAME=%s\n*/".format(inputFile.buildNormalizedPath);
74-
return compile(text, args);
70+
string[string] macros;
71+
macros["SRC_FILENAME"] = "%s\n".format(inputFile.buildNormalizedPath);
72+
return compile(text, args, macros);
7573
}
7674

77-
auto compile(R)(R buffer, string[] arguments)
75+
static auto createTmpFile(string buffer = null, string extension = ".d")
76+
{
77+
import std.uuid : randomUUID;
78+
auto name = tempDir.buildPath("ddoc_preprocessor_" ~ randomUUID.toString.replace("-", "") ~ extension);
79+
if (buffer !is null)
80+
std.file.write(name, buffer);
81+
return name;
82+
}
83+
84+
auto compile(R)(R buffer, string[] arguments, string[string] macros = null)
7885
{
7986
import core.time : usecs;
8087
import core.thread : Thread;
8188
import std.process : pipeProcess, Redirect, wait;
8289
auto args = [config.dmdBinPath] ~ arguments;
8390

84-
import std.file : write, tempDir;
85-
import std.uuid : randomUUID;
86-
auto fileName = tempDir.buildPath("ddoc_preprocessor_" ~ randomUUID.toString.replace("-", "") ~ ".d");
87-
std.file.write(fileName, buffer);
88-
scope(exit) fileName.remove;
89-
args = args.replace("-", fileName);
91+
string[] tmpFiles = [createTmpFile(buffer, ".d")];
92+
args = args.replace("-", tmpFiles[$ - 1]);
93+
scope(exit) tmpFiles.each!remove;
94+
95+
if (macros !is null)
96+
{
97+
auto macroString = macros.byPair.map!(a => "%s=%s".format(a[0], a[1])).join("\n");
98+
args ~= createTmpFile(macroString, ".ddoc");
99+
tmpFiles ~= args[$ - 1];
100+
}
90101

91102
foreach (arg; ["-c", "-o-"])
92103
{

0 commit comments

Comments
 (0)