yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
b118451e3
master
1 2#include "slang-test-tool-util.h" 3 4#include "slang-com-helper.h" 5#include "slang-io.h" 6#include "slang-string-util.h" 7 8namespace Slang 9{ 10 11/* static */ ToolReturnCode TestToolUtil ::getReturnCode (SlangResult res ) 12{ 13switch (res ) 14 { 15case SLANG_OK : 16return ToolReturnCode ::Success ; 17case SLANG_E_INTERNAL_FAIL : 18return ToolReturnCode ::CompilationFailed ; 19case SLANG_FAIL : 20return ToolReturnCode ::Failed ; 21case SLANG_E_NOT_AVAILABLE : 22return ToolReturnCode ::Ignored ; 23default : 24 { 25return (SLANG_SUCCEEDED (res )) ?ToolReturnCode ::Success :ToolReturnCode ::Failed ; 26 } 27 } 28} 29 30/* static */ ToolReturnCode TestToolUtil ::getReturnCodeFromInt (int code ) 31{ 32if (code >=int (ToolReturnCodeSpan ::First )&& code <=int (ToolReturnCodeSpan ::Last )) 33 { 34return ToolReturnCode (code ); 35 } 36else 37 { 38SLANG_ASSERT (!"Invalid integral code" ); 39return ToolReturnCode ::Failed ; 40 } 41} 42 43/* static */ bool TestToolUtil ::hasDeferredCoreModule (Index argc ,const char * const * argv ) 44{ 45for (Index i = 0 ;i < argc ;++ i ) 46 { 47UnownedStringSlice option (argv [i ]); 48if (option == "-load-core-module" || option == "-compile-core-module" ) 49 { 50return true; 51 } 52 } 53return false; 54} 55 56/* static */ SlangResult TestToolUtil ::getIncludePath ( 57const String & parentPath , 58const char * path , 59String & outIncludePath ) 60{ 61String includePath ; 62SLANG_RETURN_ON_FAIL (Path ::getCanonical (Path ::combine (parentPath ,path ),includePath )); 63 64// Use forward slashes, to avoid escaping the path 65includePath = StringUtil ::calcCharReplaced (includePath ,'\\' ,'/' ); 66 67// It must exist! 68if (!File ::exists (includePath )) 69 { 70return SLANG_FAIL ; 71 } 72 73outIncludePath = includePath ; 74return SLANG_OK ; 75} 76 77static SlangResult _addCPPPrelude (const String & rootPath , slang::IGlobalSession * session ) 78{ 79String includePath ; 80SlangResult res = SLANG_FAIL ; 81if (SLANG_FAILED (res )) 82res = TestToolUtil ::getIncludePath ( 83Path ::combine (rootPath ,"include" ), 84"slang-cpp-prelude.h" , 85includePath ); 86if (SLANG_FAILED (res )) 87res = TestToolUtil ::getIncludePath (rootPath ,"prelude/slang-cpp-prelude.h" ,includePath ); 88SLANG_RETURN_ON_FAIL (res ); 89StringBuilder prelude ; 90prelude <<"#include \"" <<includePath <<"\"\n\n" ; 91session -> setLanguagePrelude (SLANG_SOURCE_LANGUAGE_CPP ,prelude .getBuffer ()); 92return SLANG_OK ; 93} 94 95static SlangResult _addCUDAPrelude (const String & rootPath , slang::IGlobalSession * session ) 96{ 97String includePath ; 98SlangResult res = SLANG_FAIL ; 99if (SLANG_FAILED (res )) 100res = TestToolUtil ::getIncludePath ( 101Path ::combine (rootPath ,"include" ), 102"slang-cuda-prelude.h" , 103includePath ); 104if (SLANG_FAILED (res )) 105res = TestToolUtil ::getIncludePath (rootPath ,"prelude/slang-cuda-prelude.h" ,includePath ); 106SLANG_RETURN_ON_FAIL (res ); 107StringBuilder prelude ; 108prelude <<"#include \"" <<includePath <<"\"\n\n" ; 109session -> setLanguagePrelude (SLANG_SOURCE_LANGUAGE_CUDA ,prelude .getBuffer ()); 110return SLANG_OK ; 111} 112 113/* static */ SlangResult TestToolUtil ::getExeDirectoryPath ( 114const char * exePath , 115String & outExeDirectoryPath ) 116{ 117String canonicalPath ; 118SLANG_RETURN_ON_FAIL (Path ::getCanonical (exePath ,canonicalPath )); 119// Get the directory 120outExeDirectoryPath = Path ::getParentDirectory (canonicalPath ); 121return SLANG_OK ; 122} 123 124/* static */ SlangResult TestToolUtil ::getDllDirectoryPath ( 125const char * exePath , 126String & outDllDirectoryPath ) 127{ 128String canonicalPath ; 129SLANG_RETURN_ON_FAIL (Path ::getCanonical (exePath ,canonicalPath )); 130 131// Get the directory 132String binPath = Path ::getParentDirectory (canonicalPath ); 133 134// Windows puts the dlls in the same directory as the exe, while on other platforms they are in 135// a 'lib' directory 136#ifdef _WIN32 137outDllDirectoryPath = binPath ; 138#else 139String binaryRootPath = Path ::getParentDirectory (binPath ); 140outDllDirectoryPath = Path ::combine (binaryRootPath ,"lib" ); 141#endif 142return SLANG_OK ; 143} 144 145/* static */ SlangResult TestToolUtil ::getRootPath (const char * inExePath ,String & outExePath ) 146{ 147// Get the directory holding the exe 148String parentPath ; 149SLANG_RETURN_ON_FAIL (getExeDirectoryPath (inExePath ,parentPath )); 150 151// Work out the relative path to the root, we will search upwards until we 152// find a directory containing 'prelude/slang-cpp-prelude.h' 153String rootRelPath ; 154SLANG_RETURN_ON_FAIL (Path ::getCanonical (parentPath ,rootRelPath )); 155do 156 { 157if (File ::exists (Path ::combine (rootRelPath ,"include/slang-cpp-prelude.h" ))) 158break ; 159if (File ::exists (Path ::combine (rootRelPath ,"prelude/slang-cpp-prelude.h" ))) 160break ; 161 162rootRelPath = Path ::getParentDirectory (rootRelPath ); 163if (rootRelPath == "" ) 164return SLANG_E_NOT_AVAILABLE ; 165 }while (1 ); 166 167outExePath = std::move (rootRelPath ); 168return SLANG_OK ; 169} 170 171/* static */ SlangResult TestToolUtil ::setSessionDefaultPreludeFromExePath ( 172const char * inExePath , 173 slang::IGlobalSession * session ) 174{ 175String rootPath ; 176SLANG_RETURN_ON_FAIL (getRootPath (inExePath ,rootPath )); 177SLANG_RETURN_ON_FAIL (setSessionDefaultPreludeFromRootPath (rootPath ,session )); 178return SLANG_OK ; 179} 180 181/* static */ SlangResult TestToolUtil ::setSessionDefaultPreludeFromRootPath ( 182const String & rootPath , 183 slang::IGlobalSession * session ) 184{ 185// Set the prelude to a path 186 187if (SLANG_FAILED (_addCPPPrelude (rootPath ,session ))) 188 { 189SLANG_ASSERT (!"Couldn't find the C++ prelude relative to the executable" ); 190 } 191 192if (SLANG_FAILED (_addCUDAPrelude (rootPath ,session ))) 193 { 194SLANG_ASSERT (!"Couldn't find the CUDA prelude relative to the executable" ); 195 } 196 197return SLANG_OK ; 198} 199 200}// namespace Slang