From a5b0cde056af9e97824a9aaca6773bfd879a7934 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Mon, 21 Sep 2020 15:45:35 -0400 Subject: Allow #include of absolute paths (#1555) * #include an absolute path didn't work - because paths were taken to always be relative. * Improve comments. * Small comment improvement. --- source/slang/slang-include-system.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'source/slang/slang-include-system.cpp') diff --git a/source/slang/slang-include-system.cpp b/source/slang/slang-include-system.cpp index 66aeb3f67..891d376f6 100644 --- a/source/slang/slang-include-system.cpp +++ b/source/slang/slang-include-system.cpp @@ -9,15 +9,26 @@ namespace Slang SlangResult IncludeSystem::findFile(SlangPathType fromPathType, const String& fromPath, const String& path, PathInfo& outPathInfo) { - // Get relative path - ComPtr combinedPathBlob; - SLANG_RETURN_ON_FAIL(m_fileSystemExt->calcCombinedPath(fromPathType, fromPath.begin(), path.begin(), combinedPathBlob.writeRef())); - String combinedPath(StringUtil::getString(combinedPathBlob)); - if (combinedPath.getLength() <= 0) + String combinedPath; + + if (fromPath.getLength() == 0 || Path::isAbsolute(path)) { - return SLANG_FAIL; + // 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) @@ -55,6 +66,14 @@ SlangResult IncludeSystem::findFile(String const& pathToInclude, String const& p { 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); -- cgit v1.2.3