From d4316c88457a32f1169b2d7d82053ccbc05fa7ed Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Fri, 14 May 2021 17:50:00 -0400 Subject: FXC as DownstreamCompiler (#1844) * #include an absolute path didn't work - because paths were taken to always be relative. * WIP Fxc as downstream compiler. * First pass FXC downstream compiler working. * GCC compile fix. * Fix FXC parsing issue. * Special case filesystem access. * Use StringUtil getSlice. * Fix isses with not emitting source for FXC. * Small fixes for DXBC handling. --- source/compiler-core/slang-include-system.cpp | 158 ++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 source/compiler-core/slang-include-system.cpp (limited to 'source/compiler-core/slang-include-system.cpp') diff --git a/source/compiler-core/slang-include-system.cpp b/source/compiler-core/slang-include-system.cpp new file mode 100644 index 000000000..891d376f6 --- /dev/null +++ b/source/compiler-core/slang-include-system.cpp @@ -0,0 +1,158 @@ +// slang-include-system.cpp +#include "slang-include-system.h" + +#include "../core/slang-io.h" +#include "../core/slang-string-util.h" + +namespace Slang +{ + +SlangResult IncludeSystem::findFile(SlangPathType fromPathType, const String& fromPath, const String& path, PathInfo& outPathInfo) +{ + String combinedPath; + + if (fromPath.getLength() == 0 || Path::isAbsolute(path)) + { + // If the path is absolute or the fromPath is empty, the combined path is just the path + combinedPath = path; + } + else + { + // Get relative path + ComPtr combinedPathBlob; + SLANG_RETURN_ON_FAIL(m_fileSystemExt->calcCombinedPath(fromPathType, fromPath.begin(), path.begin(), combinedPathBlob.writeRef())); + combinedPath = StringUtil::getString(combinedPathBlob); + if (combinedPath.getLength() <= 0) + { + return SLANG_FAIL; + } + } + + // This checks the path exists + SlangPathType pathType; + SLANG_RETURN_ON_FAIL(m_fileSystemExt->getPathType(combinedPath.begin(), &pathType)); + if (pathType != SLANG_PATH_TYPE_FILE) + { + return SLANG_E_NOT_FOUND; + } + + // Get the uniqueIdentity + ComPtr uniqueIdentityBlob; + SLANG_RETURN_ON_FAIL(m_fileSystemExt->getFileUniqueIdentity(combinedPath.begin(), uniqueIdentityBlob.writeRef())); + + // If the rel path exists -> a uniqueIdentity MUST exists too + String uniqueIdentity(StringUtil::getString(uniqueIdentityBlob)); + if (uniqueIdentity.getLength() <= 0) + { + // Unique identity can't be empty + return SLANG_FAIL; + } + + outPathInfo = PathInfo::makeNormal(combinedPath, uniqueIdentity); + return SLANG_OK; +} + +String IncludeSystem::simplifyPath(const String& path) +{ + ComPtr simplifiedPath; + if (SLANG_FAILED(m_fileSystemExt->getSimplifiedPath(path.getBuffer(), simplifiedPath.writeRef()))) + { + return path; + } + return StringUtil::getString(simplifiedPath); +} + +SlangResult IncludeSystem::findFile(String const& pathToInclude, String const& pathIncludedFrom, PathInfo& outPathInfo) +{ + outPathInfo.type = PathInfo::Type::Unknown; + + // If it's absolute we only have to try and find if it's there - no need to look at search paths + if (Path::isAbsolute(pathToInclude)) + { + // We pass in "" as the from path, so ensure no from path is taken into account + // and to allow easy identification that this is in effect absolute + return findFile(SLANG_PATH_TYPE_DIRECTORY, UnownedStringSlice::fromLiteral(""), pathToInclude, outPathInfo); + } + + // Try just relative to current path + { + SlangResult res = findFile(SLANG_PATH_TYPE_FILE, pathIncludedFrom, pathToInclude, outPathInfo); + // It either succeeded or wasn't found, anything else is a failure passed back + if (SLANG_SUCCEEDED(res) || res != SLANG_E_NOT_FOUND) + { + return res; + } + } + + // Search all the searchDirectories + for (auto sd = m_searchDirectories; sd; sd = sd->parent) + { + for (auto& dir : sd->searchDirectories) + { + SlangResult res = findFile(SLANG_PATH_TYPE_DIRECTORY, dir.path, pathToInclude, outPathInfo); + if (SLANG_SUCCEEDED(res) || res != SLANG_E_NOT_FOUND) + { + return res; + } + } + } + + return SLANG_E_NOT_FOUND; +} + +SlangResult IncludeSystem::loadFile(const PathInfo& pathInfo, ComPtr& outBlob) +{ + if (m_sourceManager) + { + // See if this an already loaded source file + SourceFile* sourceFile = m_sourceManager->findSourceFileRecursively(pathInfo.uniqueIdentity); + + // If not create a new one, and add to the list of known source files + if (!sourceFile) + { + ComPtr foundSourceBlob; + if (SLANG_FAILED(m_fileSystemExt->loadFile(pathInfo.foundPath.getBuffer(), foundSourceBlob.writeRef()))) + { + return SLANG_E_CANNOT_OPEN; + } + + sourceFile = m_sourceManager->createSourceFileWithBlob(pathInfo, foundSourceBlob); + m_sourceManager->addSourceFile(pathInfo.uniqueIdentity, sourceFile); + + outBlob = foundSourceBlob; + return SLANG_OK; + } + else + { + if (sourceFile->getContentBlob()) + { + outBlob = sourceFile->getContentBlob(); + return SLANG_OK; + } + + ComPtr foundSourceBlob; + if (SLANG_FAILED(m_fileSystemExt->loadFile(pathInfo.foundPath.getBuffer(), foundSourceBlob.writeRef()))) + { + return SLANG_E_CANNOT_OPEN; + } + + sourceFile->setContents(foundSourceBlob); + outBlob = foundSourceBlob; + return SLANG_OK; + } + } + else + { + // If we don't have the source manager, just load + return m_fileSystemExt->loadFile(pathInfo.foundPath.getBuffer(), outBlob.writeRef()); + } +} + +SlangResult IncludeSystem::findAndLoadFile(const String& pathToInclude, const String& pathIncludedFrom, PathInfo& outPathInfo, ComPtr& outBlob) +{ + SLANG_RETURN_ON_FAIL(findFile(pathToInclude, pathIncludedFrom, outPathInfo)); + SLANG_RETURN_ON_FAIL(loadFile(outPathInfo, outBlob)); + return SLANG_OK; +} + +} // namespace Slang -- cgit v1.2.3