summaryrefslogtreecommitdiffstats
path: root/source/slang/slang.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2024-06-12 09:45:50 -0700
committerGitHub <noreply@github.com>2024-06-12 09:45:50 -0700
commitccc26c2d22d471ae649bf16f37ed1cd6cfbddd1b (patch)
treef1b84f6c30d2f8aa5014587ef1b7995ae51996e4 /source/slang/slang.cpp
parent318adcc27b8d89ec1d47c445a93239dd81be0b31 (diff)
Extend the COM-based API to support whole program compilation. (#4355)
Diffstat (limited to 'source/slang/slang.cpp')
-rw-r--r--source/slang/slang.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp
index f820bf8cd..ede22ec4f 100644
--- a/source/slang/slang.cpp
+++ b/source/slang/slang.cpp
@@ -4661,6 +4661,57 @@ void ComponentType::enumerateIRModules(EnumerateIRModulesCallback callback, void
acceptVisitor(&visitor, nullptr);
}
+SLANG_NO_THROW SlangResult SLANG_MCALL ComponentType::getTargetCode(
+ Int targetIndex,
+ slang::IBlob** outCode,
+ slang::IBlob** outDiagnostics)
+{
+ auto linkage = getLinkage();
+ if (targetIndex < 0 || targetIndex >= linkage->targets.getCount())
+ return SLANG_E_INVALID_ARG;
+
+ // If the user hasn't specified any entry points, then we should
+ // discover all entrypoints that are defined in linked modules, and
+ // include all of them in the compile.
+ //
+ if (getEntryPointCount() == 0)
+ {
+ List<Module*> modules;
+ this->enumerateModules([&](Module* module)
+ {
+ modules.add(module);
+ });
+ List<RefPtr<ComponentType>> components;
+ components.add(this);
+ for (auto module : modules)
+ {
+ for (auto entryPoint : module->getEntryPoints())
+ {
+ components.add(entryPoint);
+ }
+ }
+ RefPtr<CompositeComponentType> composite = new CompositeComponentType(linkage, components);
+ ComPtr<IComponentType> linkedComponentType;
+ SLANG_RETURN_ON_FAIL(composite->link(linkedComponentType.writeRef(), outDiagnostics));
+ return linkedComponentType->getTargetCode(targetIndex, outCode, outDiagnostics);
+ }
+
+ auto target = linkage->targets[targetIndex];
+ auto targetProgram = getTargetProgram(target);
+
+ DiagnosticSink sink(linkage->getSourceManager(), Lexer::sourceLocationLexer);
+ applySettingsToDiagnosticSink(&sink, &sink, linkage->m_optionSet);
+ applySettingsToDiagnosticSink(&sink, &sink, m_optionSet);
+
+ IArtifact* artifact = targetProgram->getOrCreateWholeProgramResult(&sink);
+ sink.getBlobIfNeeded(outDiagnostics);
+
+ if (artifact == nullptr)
+ return SLANG_FAIL;
+
+ return artifact->loadBlob(ArtifactKeep::Yes, outCode);
+}
+
//
// CompositeComponentType
//