summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-emit-dependency-file.cpp
diff options
context:
space:
mode:
authorTheresa Foley <10618364+tangent-vector@users.noreply.github.com>2025-07-24 12:59:58 -0700
committerGitHub <noreply@github.com>2025-07-24 19:59:58 +0000
commit8ccd495d5eaa82cb831378c28dd190e657b6c999 (patch)
treec39dc364d95f78984fd4ba2776d259ff944d2596 /source/slang/slang-emit-dependency-file.cpp
parent2d23a962766a97cbb11bcee5483a66aec923da49 (diff)
Organize code better by splitting some big files (#7890)
* Organize code better by splitting some big files The basic change here is that the majority of the declarations in `slang-compiler.h` have been split out into a set of smaller and more focused files. As a result, the implement of those declarations have been moved from `slang-compiler.cpp` and `slang.cpp` over to those new files when the proper home for code is obvious. I have tried as much as possible to *not* make any edits to the code along the way, and just copy-paste declarations from one place to another as-is. The exceptions I am aware of are: * In some cases a function that used to be file-scope `static` was used by code that landed in two or more different `.cpp` files. In these cases, I changed the function to be non-`static` (removing the `_` prefix from its name, if it had one, per our naming conventions), and put a declaration for the function into the most appropriate header I could identify. * I added a few comments in places where I saw ugly or unfortunate things in the code I was moving, and wanted to tag them with `TODO`s so we can hopefully get to them in the fullness of time. * I added top-level comments to each of the new `.h` files that was introduced to try to explain the logic for what goes into that file. * In cases where one of the new header files mostly existed to declare a single type, I sometimes added more detail to the doc comment on that type, to better explain the type and its role in the compiler (this is text that otherwise might have gone into the comment at the top leve lof the file, but I figured that the doc comment would have higher discoverability). I expect that the most contentious choice here is that the `Session` class lands in `slang-global-session.h` while `slang-session.h` holds the `Linkage` class. The names used in this change are consistent with how the relevant concepts in the public Slang API are named, and are consistent with how we *intend* to rename the classes themselves in time. * format code * fixup --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
Diffstat (limited to 'source/slang/slang-emit-dependency-file.cpp')
-rw-r--r--source/slang/slang-emit-dependency-file.cpp125
1 files changed, 125 insertions, 0 deletions
diff --git a/source/slang/slang-emit-dependency-file.cpp b/source/slang/slang-emit-dependency-file.cpp
new file mode 100644
index 000000000..a482b08de
--- /dev/null
+++ b/source/slang/slang-emit-dependency-file.cpp
@@ -0,0 +1,125 @@
+// slang-emit-dependency-file.cpp
+#include "slang-emit-dependency-file.h"
+
+#include "slang-compiler.h"
+
+namespace Slang
+{
+
+static void _writeString(Stream& stream, const char* string)
+{
+ stream.write(string, strlen(string));
+}
+
+static void _escapeDependencyString(const char* string, StringBuilder& outBuilder)
+{
+ // make has unusual escaping rules, but we only care about characters that are acceptable in a
+ // path
+ for (const char* p = string; *p; ++p)
+ {
+ char c = *p;
+ switch (c)
+ {
+ case ' ':
+ case ':':
+ case '#':
+ case '[':
+ case ']':
+ case '\\':
+ outBuilder.appendChar('\\');
+ break;
+
+ case '$':
+ outBuilder.appendChar('$');
+ break;
+ }
+
+ outBuilder.appendChar(c);
+ }
+}
+
+// Writes a line to the file stream, formatted like this:
+// <output-file>: <dependency-file> <dependency-file...>
+static void _writeDependencyStatement(
+ Stream& stream,
+ EndToEndCompileRequest* compileRequest,
+ const String& outputPath)
+{
+ if (outputPath.getLength() == 0)
+ return;
+
+ StringBuilder builder;
+ _escapeDependencyString(outputPath.begin(), builder);
+ _writeString(stream, builder.begin());
+ _writeString(stream, ": ");
+
+ int dependencyCount = compileRequest->getDependencyFileCount();
+ for (int dependencyIndex = 0; dependencyIndex < dependencyCount; ++dependencyIndex)
+ {
+ builder.clear();
+ _escapeDependencyString(compileRequest->getDependencyFilePath(dependencyIndex), builder);
+ _writeString(stream, builder.begin());
+ _writeString(stream, (dependencyIndex + 1 < dependencyCount) ? " " : "\n");
+ }
+}
+
+// Writes a file with dependency info, with one line in the output file per compile product.
+SlangResult writeDependencyFile(EndToEndCompileRequest* compileRequest)
+{
+ if (compileRequest->m_dependencyOutputPath.getLength() == 0)
+ return SLANG_OK;
+
+ FileStream stream;
+ SLANG_RETURN_ON_FAIL(stream.init(
+ compileRequest->m_dependencyOutputPath,
+ FileMode::Create,
+ FileAccess::Write,
+ FileShare::ReadWrite));
+
+ auto linkage = compileRequest->getLinkage();
+ auto program = compileRequest->getSpecializedGlobalAndEntryPointsComponentType();
+
+ // Iterate over all the targets and their outputs
+ for (const auto& targetReq : linkage->targets)
+ {
+ if (compileRequest->getTargetOptionSet(targetReq).getBoolOption(
+ CompilerOptionName::GenerateWholeProgram))
+ {
+ RefPtr<EndToEndCompileRequest::TargetInfo> targetInfo;
+ if (compileRequest->m_targetInfos.tryGetValue(targetReq, targetInfo))
+ {
+ _writeDependencyStatement(
+ stream,
+ compileRequest,
+ targetInfo->wholeTargetOutputPath);
+ }
+ }
+ else
+ {
+ Index entryPointCount = program->getEntryPointCount();
+ for (Index entryPointIndex = 0; entryPointIndex < entryPointCount; ++entryPointIndex)
+ {
+ RefPtr<EndToEndCompileRequest::TargetInfo> targetInfo;
+ if (compileRequest->m_targetInfos.tryGetValue(targetReq, targetInfo))
+ {
+ String outputPath;
+ if (targetInfo->entryPointOutputPaths.tryGetValue(entryPointIndex, outputPath))
+ {
+ _writeDependencyStatement(stream, compileRequest, outputPath);
+ }
+ }
+ }
+ }
+ }
+
+ // When the output is a binary module, linkage->targets can be empty. So
+ // we need to do their dependencies separately.
+ if (compileRequest->m_containerFormat == ContainerFormat::SlangModule)
+ {
+ _writeDependencyStatement(stream, compileRequest, compileRequest->m_containerOutputPath);
+ }
+
+ return SLANG_OK;
+}
+
+} // namespace Slang