FileCheckerTest.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // FileCheckerTest.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides tests that are based on FileChecker. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef UNICODE
  12. #define UNICODE
  13. #endif
  14. #include <memory>
  15. #include <vector>
  16. #include <string>
  17. #include <cctype>
  18. #include <cassert>
  19. #include <algorithm>
  20. #include "dxc/Support/WinIncludes.h"
  21. #include "dxc/dxcapi.h"
  22. #ifdef _WIN32
  23. #include <atlfile.h>
  24. #endif
  25. #include "dxc/Test/HlslTestData.h"
  26. #include "dxc/Test/HlslTestUtils.h"
  27. #include "dxc/Test/DxcTestUtils.h"
  28. #include "llvm/Support/raw_os_ostream.h"
  29. #include "llvm/Support/MD5.h"
  30. #include "dxc/Support/Global.h"
  31. #include "dxc/Support/dxcapi.use.h"
  32. #include "dxc/dxctools.h"
  33. #include "dxc/Support/HLSLOptions.h"
  34. #include "dxc/Support/Unicode.h"
  35. #include "dxc/DxilContainer/DxilContainer.h"
  36. #include "dxc/Test/D3DReflectionDumper.h"
  37. #include "d3d12shader.h"
  38. using namespace std;
  39. using namespace hlsl_test;
  40. using namespace refl_dump;
  41. FileRunCommandPart::FileRunCommandPart(const std::string &command, const std::string &arguments, LPCWSTR commandFileName) :
  42. Command(command), Arguments(arguments), CommandFileName(commandFileName) { }
  43. FileRunCommandResult FileRunCommandPart::RunHashTests(dxc::DxcDllSupport &DllSupport) {
  44. if (0 == _stricmp(Command.c_str(), "%dxc")) {
  45. return RunDxcHashTest(DllSupport);
  46. }
  47. else {
  48. return FileRunCommandResult::Success();
  49. }
  50. }
  51. FileRunCommandResult FileRunCommandPart::Run(dxc::DxcDllSupport &DllSupport, const FileRunCommandResult *Prior,
  52. PluginToolsPaths *pPluginToolsPaths /*=nullptr*/) {
  53. bool isFileCheck =
  54. 0 == _stricmp(Command.c_str(), "FileCheck") ||
  55. 0 == _stricmp(Command.c_str(), "%FileCheck");
  56. bool isXFail = 0 == _stricmp(Command.c_str(), "xfail");
  57. bool consumeErrors = isFileCheck || isXFail;
  58. // Stop the pipeline if on errors unless the command can consume them.
  59. if (Prior != nullptr && Prior->ExitCode && !consumeErrors) {
  60. FileRunCommandResult result = *Prior;
  61. result.AbortPipeline = true;
  62. return result;
  63. }
  64. // We would add support for 'not' and 'llc' here.
  65. if (isFileCheck) {
  66. return RunFileChecker(Prior);
  67. }
  68. else if (isXFail) {
  69. return RunXFail(Prior);
  70. }
  71. else if (0 == _stricmp(Command.c_str(), "tee")) {
  72. return RunTee(Prior);
  73. }
  74. else if (0 == _stricmp(Command.c_str(), "fc")) {
  75. return RunFileCompareText(Prior);
  76. }
  77. else if (0 == _stricmp(Command.c_str(), "%dxilver")) {
  78. return RunDxilVer(DllSupport, Prior);
  79. }
  80. else if (0 == _stricmp(Command.c_str(), "%dxc")) {
  81. return RunDxc(DllSupport, Prior);
  82. }
  83. else if (0 == _stricmp(Command.c_str(), "%dxv")) {
  84. return RunDxv(DllSupport, Prior);
  85. }
  86. else if (0 == _stricmp(Command.c_str(), "%opt")) {
  87. return RunOpt(DllSupport, Prior);
  88. }
  89. else if (0 == _stricmp(Command.c_str(), "%D3DReflect")) {
  90. return RunD3DReflect(DllSupport, Prior);
  91. }
  92. else if (0 == _stricmp(Command.c_str(), "%dxr")) {
  93. return RunDxr(DllSupport, Prior);
  94. }
  95. else if (pPluginToolsPaths != nullptr) {
  96. auto it = pPluginToolsPaths->find(Command.c_str());
  97. if (it != pPluginToolsPaths->end()) {
  98. return RunFromPath(it->second, Prior);
  99. }
  100. }
  101. FileRunCommandResult result {};
  102. result.ExitCode = 1;
  103. result.StdErr = "Unrecognized command ";
  104. result.StdErr += Command;
  105. return result;
  106. }
  107. FileRunCommandResult FileRunCommandPart::RunFileChecker(const FileRunCommandResult *Prior) {
  108. if (!Prior) return FileRunCommandResult::Error("Prior command required to generate stdin");
  109. FileCheckForTest t;
  110. t.CheckFilename = CW2A(CommandFileName, CP_UTF8);
  111. t.InputForStdin = Prior->ExitCode ? Prior->StdErr : Prior->StdOut;
  112. // Parse command arguments
  113. static constexpr char checkPrefixStr[] = "-check-prefix=";
  114. static constexpr char checkPrefixesStr[] = "-check-prefixes=";
  115. bool hasInputFilename = false;
  116. for (const std::string& arg : strtok(Arguments)) {
  117. if (arg == "%s") hasInputFilename = true;
  118. else if (arg == "-input=stderr") t.InputForStdin = Prior->StdErr;
  119. else if (strstartswith(arg, checkPrefixStr))
  120. t.CheckPrefixes.emplace_back(arg.substr(sizeof(checkPrefixStr) - 1));
  121. else if (strstartswith(arg, checkPrefixesStr)) {
  122. auto prefixes = strtok(arg.substr(sizeof(checkPrefixesStr) - 1), ", ");
  123. for (auto &prefix : prefixes)
  124. t.CheckPrefixes.emplace_back(prefix);
  125. }
  126. else return FileRunCommandResult::Error("Invalid argument");
  127. }
  128. if (!hasInputFilename) return FileRunCommandResult::Error("Missing input filename");
  129. FileRunCommandResult result {};
  130. // Run
  131. result.ExitCode = t.Run();
  132. result.StdOut = t.test_outs;
  133. result.StdErr = t.test_errs;
  134. // Capture the input as well.
  135. if (result.ExitCode != 0 && Prior != nullptr) {
  136. result.StdErr += "\n<full input to FileCheck>\n";
  137. result.StdErr += t.InputForStdin;
  138. }
  139. return result;
  140. }
  141. FileRunCommandResult FileRunCommandPart::ReadOptsForDxc(
  142. hlsl::options::MainArgs &argStrings, hlsl::options::DxcOpts &Opts,
  143. unsigned flagsToInclude) {
  144. std::string args(strtrim(Arguments));
  145. const char *inputPos = strstr(args.c_str(), "%s");
  146. if (inputPos == nullptr)
  147. return FileRunCommandResult::Error("Only supported pattern includes input file as argument");
  148. args.erase(inputPos - args.c_str(), strlen("%s"));
  149. llvm::StringRef argsRef = args;
  150. llvm::SmallVector<llvm::StringRef, 8> splitArgs;
  151. argsRef.split(splitArgs, " ");
  152. argStrings = hlsl::options::MainArgs(splitArgs);
  153. std::string errorString;
  154. llvm::raw_string_ostream errorStream(errorString);
  155. int RunResult = ReadDxcOpts(hlsl::options::getHlslOptTable(), flagsToInclude,
  156. argStrings, Opts, errorStream);
  157. errorStream.flush();
  158. if (RunResult)
  159. return FileRunCommandResult::Error(RunResult, errorString);
  160. return FileRunCommandResult::Success("");
  161. }
  162. static HRESULT ReAssembleTo(dxc::DxcDllSupport &DllSupport, void *bitcode, UINT32 size, IDxcBlob **pBlob) {
  163. CComPtr<IDxcAssembler> pAssembler;
  164. CComPtr<IDxcLibrary> pLibrary;
  165. IFT(DllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  166. IFT(DllSupport.CreateInstance(CLSID_DxcAssembler, &pAssembler));
  167. CComPtr<IDxcBlobEncoding> pInBlob;
  168. IFT(pLibrary->CreateBlobWithEncodingFromPinned(bitcode, size, 0, &pInBlob));
  169. CComPtr<IDxcOperationResult> pResult;
  170. pAssembler->AssembleToContainer(pInBlob, &pResult);
  171. HRESULT Result = 0;
  172. IFT(pResult->GetStatus(&Result));
  173. IFT(Result);
  174. IFT(pResult->GetResult(pBlob));
  175. return S_OK;
  176. }
  177. static HRESULT GetDxilBitcode(dxc::DxcDllSupport &DllSupport, IDxcBlob *pCompiledBlob, IDxcBlob **pBitcodeBlob) {
  178. CComPtr<IDxcContainerReflection> pReflection;
  179. CComPtr<IDxcLibrary> pLibrary;
  180. IFT(DllSupport.CreateInstance(CLSID_DxcContainerReflection, &pReflection));
  181. IFT(DllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  182. IFT(pReflection->Load(pCompiledBlob));
  183. UINT32 uIndex = 0;
  184. IFT(pReflection->FindFirstPartKind(hlsl::DFCC_DXIL, &uIndex));
  185. CComPtr<IDxcBlob> pPart;
  186. IFT(pReflection->GetPartContent(uIndex, &pPart));
  187. auto header = (hlsl::DxilProgramHeader*)pPart->GetBufferPointer();
  188. void *bitcode = (char *)&header->BitcodeHeader + header->BitcodeHeader.BitcodeOffset;
  189. UINT32 bitcode_size = header->BitcodeHeader.BitcodeSize;
  190. CComPtr<IDxcBlobEncoding> pBlob;
  191. IFT(pLibrary->CreateBlobWithEncodingFromPinned(bitcode, bitcode_size, 0, &pBlob));
  192. *pBitcodeBlob = pBlob.Detach();
  193. return S_OK;
  194. }
  195. static HRESULT CompileForHash(hlsl::options::DxcOpts &opts, LPCWSTR CommandFileName, dxc::DxcDllSupport &DllSupport, std::vector<LPCWSTR> &flags, IDxcBlob **ppHashBlob, std::string &output) {
  196. CComPtr<IDxcLibrary> pLibrary;
  197. CComPtr<IDxcCompiler> pCompiler;
  198. CComPtr<IDxcCompiler2> pCompiler2;
  199. CComPtr<IDxcOperationResult> pResult;
  200. CComPtr<IDxcBlobEncoding> pSource;
  201. CComPtr<IDxcBlob> pCompiledBlob;
  202. CComPtr<IDxcBlob> pCompiledName;
  203. CComPtr<IDxcIncludeHandler> pIncludeHandler;
  204. WCHAR *pDebugName = nullptr;
  205. CComPtr<IDxcBlob> pPDBBlob;
  206. std::wstring entry =
  207. Unicode::UTF8ToUTF16StringOrThrow(opts.EntryPoint.str().c_str());
  208. std::wstring profile =
  209. Unicode::UTF8ToUTF16StringOrThrow(opts.TargetProfile.str().c_str());
  210. IFT(DllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  211. IFT(pLibrary->CreateBlobFromFile(CommandFileName, nullptr, &pSource));
  212. IFT(pLibrary->CreateIncludeHandler(&pIncludeHandler));
  213. IFT(DllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
  214. IFT(pCompiler.QueryInterface(&pCompiler2));
  215. IFT(pCompiler2->CompileWithDebug(pSource, CommandFileName, entry.c_str(), profile.c_str(),
  216. flags.data(), flags.size(), nullptr, 0, pIncludeHandler, &pResult, &pDebugName, &pPDBBlob));
  217. HRESULT resultStatus = 0;
  218. IFT(pResult->GetStatus(&resultStatus));
  219. if (SUCCEEDED(resultStatus)) {
  220. IFT(pResult->GetResult(&pCompiledBlob));
  221. CComPtr<IDxcContainerReflection> pReflection;
  222. IFT(DllSupport.CreateInstance(CLSID_DxcContainerReflection, &pReflection));
  223. // If failed to load here, it's likely some non-compile operation thing. Just fail the hash generation.
  224. if (FAILED(pReflection->Load(pCompiledBlob)))
  225. return E_FAIL;
  226. *ppHashBlob = nullptr;
  227. UINT32 uHashIdx = 0;
  228. if (SUCCEEDED(pReflection->FindFirstPartKind(hlsl::DFCC_ShaderHash, &uHashIdx))) {
  229. CComPtr<IDxcBlob> pHashBlob;
  230. IFT(pReflection->GetPartContent(uHashIdx, &pHashBlob));
  231. *ppHashBlob = pHashBlob.Detach();
  232. }
  233. // Test that PDB is generated correctly.
  234. // This test needs to be done elsewhere later, ideally a fully
  235. // customizable test on all our test set with different compile options.
  236. if (pPDBBlob) {
  237. IFT(pReflection->Load(pPDBBlob));
  238. UINT32 uDebugInfoIndex = 0;
  239. IFT(pReflection->FindFirstPartKind(hlsl::DFCC_ShaderDebugInfoDXIL, &uDebugInfoIndex));
  240. }
  241. return S_OK;
  242. }
  243. else {
  244. CComPtr<IDxcBlobEncoding> pErrors;
  245. IFT(pResult->GetErrorBuffer(&pErrors));
  246. const char *errors = (char *)pErrors->GetBufferPointer();
  247. output = errors;
  248. return resultStatus;
  249. }
  250. }
  251. FileRunCommandResult FileRunCommandPart::RunDxcHashTest(dxc::DxcDllSupport &DllSupport) {
  252. hlsl::options::MainArgs args;
  253. hlsl::options::DxcOpts opts;
  254. ReadOptsForDxc(args, opts);
  255. std::vector<std::wstring> argWStrings;
  256. CopyArgsToWStrings(opts.Args, hlsl::options::CoreOption, argWStrings);
  257. // Extract the vanilla flags for the test (i.e. no debug or ast-dump)
  258. std::vector<LPCWSTR> original_flags;
  259. bool skipNext = false;
  260. for (const std::wstring &a : argWStrings) {
  261. if (skipNext) {
  262. skipNext = false;
  263. continue;
  264. }
  265. if (a.find(L"ast-dump") != std::wstring::npos) continue;
  266. if (a.find(L"Zi") != std::wstring::npos) continue;
  267. std::wstring optValVer(L"validator-version");
  268. if (a.substr(1, optValVer.length()).compare(optValVer) == 0) {
  269. skipNext = a.length() == optValVer.length() + 1;
  270. continue;
  271. }
  272. original_flags.push_back(a.data());
  273. }
  274. std::string originalOutput;
  275. CComPtr<IDxcBlob> pOriginalHash;
  276. // If failed the original compilation, just pass the test. The original test was likely
  277. // testing for failure.
  278. if (FAILED(CompileForHash(opts, CommandFileName, DllSupport, original_flags, &pOriginalHash, originalOutput)))
  279. return FileRunCommandResult::Success();
  280. // Results of our compilations
  281. CComPtr<IDxcBlob> pHash1;
  282. std::string Output0;
  283. CComPtr<IDxcBlob> pHash0;
  284. std::string Output1;
  285. // Fail if -Qstrip_reflect failed the compilation
  286. std::vector<LPCWSTR> normal_flags = original_flags;
  287. normal_flags.push_back(L"-Qstrip_reflect");
  288. normal_flags.push_back(L"-Zsb");
  289. std::string StdErr;
  290. if (FAILED(CompileForHash(opts, CommandFileName, DllSupport, normal_flags, &pHash0, Output0))) {
  291. StdErr += "Adding Qstrip_reflect failed compilation.";
  292. StdErr += originalOutput;
  293. StdErr += Output0;
  294. return FileRunCommandResult::Error(StdErr);
  295. }
  296. // Fail if -Qstrip_reflect failed the compilation
  297. std::vector<LPCWSTR> dbg_flags = original_flags;
  298. dbg_flags.push_back(L"/Zi");
  299. dbg_flags.push_back(L"-Qstrip_reflect");
  300. dbg_flags.push_back(L"-Zsb");
  301. if (FAILED(CompileForHash(opts, CommandFileName, DllSupport, dbg_flags, &pHash1, Output1))) {
  302. return FileRunCommandResult::Error("Adding Qstrip_reflect and Zi failed compilation.");
  303. }
  304. if (pHash0->GetBufferSize() != pHash1->GetBufferSize() || 0 != memcmp(pHash0->GetBufferPointer(), pHash0->GetBufferPointer(), pHash1->GetBufferSize())) {
  305. StdErr = "Hashes do not match between normal and debug!!!\n";
  306. StdErr += Output0;
  307. StdErr += Output1;
  308. return FileRunCommandResult::Error(StdErr);
  309. }
  310. return FileRunCommandResult::Success();
  311. }
  312. static FileRunCommandResult CheckDxilVer(dxc::DxcDllSupport& DllSupport,
  313. unsigned RequiredDxilMajor,
  314. unsigned RequiredDxilMinor,
  315. bool bCheckValidator = true) {
  316. bool Supported = true;
  317. // If the following fails, we have Dxil 1.0 compiler
  318. unsigned DxilMajor = 1, DxilMinor = 0;
  319. GetVersion(DllSupport, CLSID_DxcCompiler, DxilMajor, DxilMinor);
  320. Supported &= hlsl::DXIL::CompareVersions(DxilMajor, DxilMinor, RequiredDxilMajor, RequiredDxilMinor) >= 0;
  321. if (bCheckValidator) {
  322. // If the following fails, we have validator 1.0
  323. unsigned ValMajor = 1, ValMinor = 0;
  324. GetVersion(DllSupport, CLSID_DxcValidator, ValMajor, ValMinor);
  325. Supported &= hlsl::DXIL::CompareVersions(ValMajor, ValMinor, RequiredDxilMajor, RequiredDxilMinor) >= 0;
  326. }
  327. if (!Supported) {
  328. FileRunCommandResult result {};
  329. result.StdErr = "Skipping test due to unsupported dxil version";
  330. result.ExitCode = 0; // Succeed the test
  331. result.AbortPipeline = true;
  332. return result;
  333. }
  334. return FileRunCommandResult::Success();
  335. }
  336. FileRunCommandResult FileRunCommandPart::RunDxc(dxc::DxcDllSupport &DllSupport, const FileRunCommandResult *Prior) {
  337. // Support piping stdin from prior if needed.
  338. UNREFERENCED_PARAMETER(Prior);
  339. hlsl::options::MainArgs args;
  340. hlsl::options::DxcOpts opts;
  341. FileRunCommandResult readOptsResult = ReadOptsForDxc(args, opts);
  342. if (readOptsResult.ExitCode) return readOptsResult;
  343. std::wstring entry =
  344. Unicode::UTF8ToUTF16StringOrThrow(opts.EntryPoint.str().c_str());
  345. std::wstring profile =
  346. Unicode::UTF8ToUTF16StringOrThrow(opts.TargetProfile.str().c_str());
  347. std::vector<LPCWSTR> flags;
  348. if (opts.CodeGenHighLevel) {
  349. flags.push_back(L"-fcgl");
  350. }
  351. // Skip targets that require a newer compiler or validator.
  352. // Some features may require newer compiler/validator than indicated by the
  353. // shader model, but these should use %dxilver explicitly.
  354. {
  355. unsigned RequiredDxilMajor = 1, RequiredDxilMinor = 0;
  356. llvm::StringRef stage;
  357. IFTBOOL(ParseTargetProfile(opts.TargetProfile, stage, RequiredDxilMajor, RequiredDxilMinor), E_INVALIDARG);
  358. if (RequiredDxilMinor != 0xF && stage.compare("rootsig") != 0) {
  359. // Convert stage to minimum dxil/validator version:
  360. RequiredDxilMajor = std::max(RequiredDxilMajor, (unsigned)6) - 5;
  361. FileRunCommandResult result = CheckDxilVer(DllSupport, RequiredDxilMajor, RequiredDxilMinor, !opts.DisableValidation);
  362. if (result.AbortPipeline) {
  363. return result;
  364. }
  365. }
  366. }
  367. // For now, too many tests are sensitive to stripping the refleciton info
  368. // from the main module, so use this flag to prevent this until tests
  369. // can be updated.
  370. // That is, unless the test explicitly requests -Qstrip_reflect_from_dxil or -Qstrip_reflect
  371. if (!opts.StripReflectionFromDxil && !opts.StripReflection) {
  372. flags.push_back(L"-Qkeep_reflect_in_dxil");
  373. }
  374. std::vector<std::wstring> argWStrings;
  375. CopyArgsToWStrings(opts.Args, hlsl::options::CoreOption, argWStrings);
  376. for (const std::wstring &a : argWStrings)
  377. flags.push_back(a.data());
  378. CComPtr<IDxcLibrary> pLibrary;
  379. CComPtr<IDxcCompiler> pCompiler;
  380. CComPtr<IDxcOperationResult> pResult;
  381. CComPtr<IDxcBlobEncoding> pSource;
  382. CComPtr<IDxcBlobEncoding> pDisassembly;
  383. CComPtr<IDxcBlob> pCompiledBlob;
  384. CComPtr<IDxcIncludeHandler> pIncludeHandler;
  385. HRESULT resultStatus;
  386. IFT(DllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  387. IFT(pLibrary->CreateBlobFromFile(CommandFileName, nullptr, &pSource));
  388. IFT(pLibrary->CreateIncludeHandler(&pIncludeHandler));
  389. IFT(DllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
  390. IFT(pCompiler->Compile(pSource, CommandFileName, entry.c_str(), profile.c_str(),
  391. flags.data(), flags.size(), nullptr, 0, pIncludeHandler, &pResult));
  392. IFT(pResult->GetStatus(&resultStatus));
  393. FileRunCommandResult result = {};
  394. if (SUCCEEDED(resultStatus)) {
  395. IFT(pResult->GetResult(&pCompiledBlob));
  396. if (!opts.AstDump) {
  397. IFT(pCompiler->Disassemble(pCompiledBlob, &pDisassembly));
  398. result.StdOut = BlobToUtf8(pDisassembly);
  399. } else {
  400. result.StdOut = BlobToUtf8(pCompiledBlob);
  401. }
  402. CComPtr<IDxcBlobEncoding> pStdErr;
  403. IFT(pResult->GetErrorBuffer(&pStdErr));
  404. result.StdErr = BlobToUtf8(pStdErr);
  405. result.ExitCode = 0;
  406. }
  407. else {
  408. IFT(pResult->GetErrorBuffer(&pDisassembly));
  409. result.StdErr = BlobToUtf8(pDisassembly);
  410. result.ExitCode = resultStatus;
  411. }
  412. result.OpResult = pResult;
  413. return result;
  414. }
  415. FileRunCommandResult FileRunCommandPart::RunDxv(dxc::DxcDllSupport &DllSupport, const FileRunCommandResult *Prior) {
  416. std::string args(strtrim(Arguments));
  417. const char *inputPos = strstr(args.c_str(), "%s");
  418. if (inputPos == nullptr) {
  419. return FileRunCommandResult::Error("Only supported pattern includes input file as argument");
  420. }
  421. args.erase(inputPos - args.c_str(), strlen("%s"));
  422. llvm::StringRef argsRef = args;
  423. llvm::SmallVector<llvm::StringRef, 8> splitArgs;
  424. argsRef.split(splitArgs, " ");
  425. IFTMSG(splitArgs.size()==1, "wrong arg num for dxv");
  426. CComPtr<IDxcLibrary> pLibrary;
  427. CComPtr<IDxcAssembler> pAssembler;
  428. CComPtr<IDxcValidator> pValidator;
  429. CComPtr<IDxcOperationResult> pResult;
  430. CComPtr<IDxcBlobEncoding> pSource;
  431. CComPtr<IDxcBlob> pContainerBlob;
  432. HRESULT resultStatus;
  433. IFT(DllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  434. IFT(pLibrary->CreateBlobFromFile(CommandFileName, nullptr, &pSource));
  435. IFT(DllSupport.CreateInstance(CLSID_DxcAssembler, &pAssembler));
  436. IFT(pAssembler->AssembleToContainer(pSource, &pResult));
  437. IFT(pResult->GetStatus(&resultStatus));
  438. if (FAILED(resultStatus)) {
  439. CComPtr<IDxcBlobEncoding> pAssembleBlob;
  440. IFT(pResult->GetErrorBuffer(&pAssembleBlob));
  441. return FileRunCommandResult::Error(resultStatus, BlobToUtf8(pAssembleBlob));
  442. }
  443. IFT(pResult->GetResult(&pContainerBlob));
  444. IFT(DllSupport.CreateInstance(CLSID_DxcValidator, &pValidator));
  445. CComPtr<IDxcOperationResult> pValidationResult;
  446. IFT(pValidator->Validate(pContainerBlob, DxcValidatorFlags_InPlaceEdit,
  447. &pValidationResult));
  448. IFT(pValidationResult->GetStatus(&resultStatus));
  449. if (FAILED(resultStatus)) {
  450. CComPtr<IDxcBlobEncoding> pValidateBlob;
  451. IFT(pValidationResult->GetErrorBuffer(&pValidateBlob));
  452. return FileRunCommandResult::Success(BlobToUtf8(pValidateBlob));
  453. }
  454. return FileRunCommandResult::Success("");
  455. }
  456. FileRunCommandResult FileRunCommandPart::RunOpt(dxc::DxcDllSupport &DllSupport, const FileRunCommandResult *Prior) {
  457. std::string args(strtrim(Arguments));
  458. const char *inputPos = strstr(args.c_str(), "%s");
  459. if (inputPos == nullptr && Prior == nullptr) {
  460. return FileRunCommandResult::Error("Only supported patterns are input file as argument or prior "
  461. "command with disassembly");
  462. }
  463. CComPtr<IDxcLibrary> pLibrary;
  464. CComPtr<IDxcOptimizer> pOptimizer;
  465. CComPtr<IDxcBlobEncoding> pSource;
  466. CComPtr<IDxcBlobEncoding> pOutputText;
  467. CComPtr<IDxcBlob> pOutputModule;
  468. IFT(DllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  469. IFT(DllSupport.CreateInstance(CLSID_DxcOptimizer, &pOptimizer));
  470. if (inputPos != nullptr) {
  471. args.erase(inputPos - args.c_str(), strlen("%s"));
  472. IFT(pLibrary->CreateBlobFromFile(CommandFileName, nullptr, &pSource));
  473. }
  474. else {
  475. assert(Prior != nullptr && "else early check should have returned");
  476. CComPtr<IDxcAssembler> pAssembler;
  477. IFT(DllSupport.CreateInstance(CLSID_DxcAssembler, &pAssembler));
  478. IFT(pLibrary->CreateBlobWithEncodingFromPinned(
  479. Prior->StdOut.c_str(), Prior->StdOut.size(), CP_UTF8,
  480. &pSource));
  481. }
  482. args = strtrim(args);
  483. llvm::StringRef argsRef = args;
  484. llvm::SmallVector<llvm::StringRef, 8> splitArgs;
  485. argsRef.split(splitArgs, " ");
  486. std::vector<LPCWSTR> options;
  487. std::vector<std::wstring> optionStrings;
  488. for (llvm::StringRef S : splitArgs) {
  489. optionStrings.push_back(
  490. Unicode::UTF8ToUTF16StringOrThrow(strtrim(S.str()).c_str()));
  491. }
  492. // Add the options outside the above loop in case the vector is resized.
  493. for (const std::wstring& str : optionStrings)
  494. options.push_back(str.c_str());
  495. IFT(pOptimizer->RunOptimizer(pSource, options.data(), options.size(),
  496. &pOutputModule, &pOutputText));
  497. return FileRunCommandResult::Success(BlobToUtf8(pOutputText));
  498. }
  499. FileRunCommandResult FileRunCommandPart::RunD3DReflect(dxc::DxcDllSupport &DllSupport, const FileRunCommandResult *Prior) {
  500. std::string args(strtrim(Arguments));
  501. if (args != "%s")
  502. return FileRunCommandResult::Error("Only supported pattern is a plain input file");
  503. if (!Prior)
  504. return FileRunCommandResult::Error("Prior command required to generate stdin");
  505. CComPtr<IDxcLibrary> pLibrary;
  506. CComPtr<IDxcBlobEncoding> pSource;
  507. CComPtr<IDxcAssembler> pAssembler;
  508. CComPtr<IDxcOperationResult> pResult;
  509. CComPtr<ID3D12ShaderReflection> pShaderReflection;
  510. CComPtr<ID3D12LibraryReflection> pLibraryReflection;
  511. CComPtr<IDxcContainerReflection> containerReflection;
  512. uint32_t partCount;
  513. CComPtr<IDxcBlob> pContainerBlob;
  514. HRESULT resultStatus;
  515. bool blobFound = false;
  516. std::ostringstream ss;
  517. D3DReflectionDumper dumper(ss);
  518. IFT(DllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  519. IFT(DllSupport.CreateInstance(CLSID_DxcAssembler, &pAssembler));
  520. IFT(pLibrary->CreateBlobWithEncodingFromPinned(
  521. (LPBYTE)Prior->StdOut.c_str(), Prior->StdOut.size(), CP_UTF8,
  522. &pSource));
  523. IFT(pAssembler->AssembleToContainer(pSource, &pResult));
  524. IFT(pResult->GetStatus(&resultStatus));
  525. if (FAILED(resultStatus)) {
  526. CComPtr<IDxcBlobEncoding> pAssembleBlob;
  527. IFT(pResult->GetErrorBuffer(&pAssembleBlob));
  528. return FileRunCommandResult::Error(resultStatus, BlobToUtf8(pAssembleBlob));
  529. }
  530. IFT(pResult->GetResult(&pContainerBlob));
  531. VERIFY_SUCCEEDED(DllSupport.CreateInstance(CLSID_DxcContainerReflection, &containerReflection));
  532. VERIFY_SUCCEEDED(containerReflection->Load(pContainerBlob));
  533. VERIFY_SUCCEEDED(containerReflection->GetPartCount(&partCount));
  534. for (uint32_t i = 0; i < partCount; ++i) {
  535. uint32_t kind;
  536. VERIFY_SUCCEEDED(containerReflection->GetPartKind(i, &kind));
  537. if (kind == (uint32_t)hlsl::DxilFourCC::DFCC_DXIL) {
  538. blobFound = true;
  539. CComPtr<IDxcBlob> pPart;
  540. IFT(containerReflection->GetPartContent(i, &pPart));
  541. const hlsl::DxilProgramHeader *pProgramHeader =
  542. reinterpret_cast<const hlsl::DxilProgramHeader*>(pPart->GetBufferPointer());
  543. VERIFY_IS_TRUE(IsValidDxilProgramHeader(pProgramHeader, (uint32_t)pPart->GetBufferSize()));
  544. hlsl::DXIL::ShaderKind SK = hlsl::GetVersionShaderType(pProgramHeader->ProgramVersion);
  545. if (SK == hlsl::DXIL::ShaderKind::Library)
  546. VERIFY_SUCCEEDED(containerReflection->GetPartReflection(i, IID_PPV_ARGS(&pLibraryReflection)));
  547. else
  548. VERIFY_SUCCEEDED(containerReflection->GetPartReflection(i, IID_PPV_ARGS(&pShaderReflection)));
  549. break;
  550. }
  551. }
  552. if (!blobFound) {
  553. return FileRunCommandResult::Error("Unable to find DXIL part");
  554. } else if (pShaderReflection) {
  555. dumper.Dump(pShaderReflection);
  556. } else if (pLibraryReflection) {
  557. dumper.Dump(pLibraryReflection);
  558. }
  559. ss.flush();
  560. return FileRunCommandResult::Success(ss.str());
  561. }
  562. FileRunCommandResult FileRunCommandPart::RunDxr(dxc::DxcDllSupport &DllSupport, const FileRunCommandResult *Prior) {
  563. // Support piping stdin from prior if needed.
  564. UNREFERENCED_PARAMETER(Prior);
  565. hlsl::options::MainArgs args;
  566. hlsl::options::DxcOpts opts;
  567. FileRunCommandResult readOptsResult = ReadOptsForDxc(args, opts,
  568. hlsl::options::HlslFlags::RewriteOption);
  569. if (readOptsResult.ExitCode) return readOptsResult;
  570. std::wstring entry =
  571. Unicode::UTF8ToUTF16StringOrThrow(opts.EntryPoint.str().c_str());
  572. std::vector<LPCWSTR> flags;
  573. std::vector<std::wstring> argWStrings;
  574. CopyArgsToWStrings(opts.Args, hlsl::options::RewriteOption, argWStrings);
  575. for (const std::wstring &a : argWStrings)
  576. flags.push_back(a.data());
  577. CComPtr<IDxcLibrary> pLibrary;
  578. CComPtr<IDxcRewriter2> pRewriter;
  579. CComPtr<IDxcOperationResult> pResult;
  580. CComPtr<IDxcBlobEncoding> pSource;
  581. CComPtr<IDxcBlob> pResultBlob;
  582. CComPtr<IDxcIncludeHandler> pIncludeHandler;
  583. IFT(DllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  584. IFT(pLibrary->CreateBlobFromFile(CommandFileName, nullptr, &pSource));
  585. IFT(pLibrary->CreateIncludeHandler(&pIncludeHandler));
  586. IFT(DllSupport.CreateInstance(CLSID_DxcRewriter, &pRewriter));
  587. IFT(pRewriter->RewriteWithOptions(pSource, CommandFileName,
  588. flags.data(), flags.size(), nullptr, 0,
  589. pIncludeHandler, &pResult));
  590. HRESULT resultStatus;
  591. IFT(pResult->GetStatus(&resultStatus));
  592. FileRunCommandResult result = {};
  593. CComPtr<IDxcBlobEncoding> pStdErr;
  594. IFT(pResult->GetErrorBuffer(&pStdErr));
  595. result.StdErr = BlobToUtf8(pStdErr);
  596. result.ExitCode = resultStatus;
  597. if (SUCCEEDED(resultStatus)) {
  598. IFT(pResult->GetResult(&pResultBlob));
  599. result.StdOut = BlobToUtf8(pResultBlob);
  600. }
  601. result.OpResult = pResult;
  602. return result;
  603. }
  604. FileRunCommandResult FileRunCommandPart::RunTee(const FileRunCommandResult *Prior) {
  605. if (Prior == nullptr) {
  606. return FileRunCommandResult::Error("tee requires a prior command");
  607. }
  608. // Ignore commands for now - simply log out through test framework.
  609. {
  610. CA2W outWide(Prior->StdOut.c_str(), CP_UTF8);
  611. WEX::Logging::Log::Comment(outWide.m_psz);
  612. }
  613. if (!Prior->StdErr.empty()) {
  614. CA2W errWide(Prior->StdErr.c_str(), CP_UTF8);
  615. WEX::Logging::Log::Comment(L"<stderr>");
  616. WEX::Logging::Log::Comment(errWide.m_psz);
  617. }
  618. return *Prior;
  619. }
  620. void FileRunCommandPart::SubstituteFilenameVars(std::string &args) {
  621. size_t pos;
  622. std::string baseFileName = CW2A(CommandFileName);
  623. if ((pos = baseFileName.find_last_of(".")) != std::string::npos) {
  624. baseFileName = baseFileName.substr(0, pos);
  625. }
  626. while ((pos = args.find("%t")) != std::string::npos) {
  627. args.replace(pos, 2, baseFileName.c_str());
  628. }
  629. while ((pos = args.find("%b")) != std::string::npos) {
  630. args.replace(pos, 2, baseFileName.c_str());
  631. }
  632. }
  633. #if _WIN32
  634. bool FileRunCommandPart::ReadFileContentToString(HANDLE hFile, std::string &str) {
  635. char buffer[1024];
  636. DWORD len;
  637. size_t size = ::GetFileSize(hFile, nullptr);
  638. if (size == INVALID_FILE_SIZE) {
  639. return false;
  640. }
  641. str.reserve(size);
  642. if (::SetFilePointer(hFile, 0, nullptr, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
  643. return false;
  644. }
  645. while (::ReadFile(hFile, buffer, sizeof(buffer), &len, nullptr) && len > 0) {
  646. str.append(buffer, len);
  647. }
  648. return true;
  649. }
  650. #endif
  651. FileRunCommandResult FileRunCommandPart::RunFileCompareText(const FileRunCommandResult *Prior) {
  652. if (Prior != nullptr) {
  653. return FileRunCommandResult::Error("prior command not supported");
  654. }
  655. FileRunCommandResult result;
  656. result.ExitCode = 1;
  657. // strip leading and trailing spaces and split
  658. std::string args(strtrim(Arguments));
  659. size_t pos;
  660. if ((pos = args.find_first_of(' ')) == std::string::npos) {
  661. return FileRunCommandResult::Error("RunFileCompareText expected 2 file arguments.");
  662. }
  663. std::string fileName1 = args.substr(0, pos);
  664. std::string fileName2 = strtrim(args.substr(pos + 1));
  665. // replace %t and %b with the command file name without extension
  666. SubstituteFilenameVars(fileName1);
  667. SubstituteFilenameVars(fileName2);
  668. // read file content and compare
  669. CA2W fileName1W(fileName1.c_str());
  670. CA2W fileName2W(fileName2.c_str());
  671. hlsl_test::LogCommentFmt(L"Comparing files %s and %s", fileName1W.m_psz, fileName2W.m_psz);
  672. std::ifstream ifs1(fileName1, std::ifstream::in);
  673. if (ifs1.fail()) {
  674. hlsl_test::LogCommentFmt(L"Failed to open %s", fileName1W.m_psz);
  675. return result;
  676. }
  677. std::string file1Content((std::istreambuf_iterator<char>(ifs1)), (std::istreambuf_iterator<char>()));
  678. std::ifstream ifs2(fileName2, std::ifstream::in);
  679. if (ifs2.fail()) {
  680. hlsl_test::LogCommentFmt(L"Failed to open %s", fileName2W.m_psz);
  681. return result;
  682. }
  683. std::string file2Content((std::istreambuf_iterator<char>(ifs2)), (std::istreambuf_iterator<char>()));
  684. if (file1Content.compare(file2Content) == 0) {
  685. hlsl_test::LogCommentFmt(L"No differences found.");
  686. result.ExitCode = 0;
  687. }
  688. else {
  689. hlsl_test::LogCommentFmt(L"Files are different!");
  690. }
  691. return result;
  692. }
  693. FileRunCommandResult FileRunCommandPart::RunXFail(const FileRunCommandResult *Prior) {
  694. if (Prior == nullptr)
  695. return FileRunCommandResult::Error("XFail requires a prior command");
  696. if (Prior->ExitCode == 0) {
  697. return FileRunCommandResult::Error("XFail expected a failure from previous command");
  698. } else {
  699. return FileRunCommandResult::Success("");
  700. }
  701. }
  702. FileRunCommandResult FileRunCommandPart::RunDxilVer(dxc::DxcDllSupport& DllSupport, const FileRunCommandResult* Prior) {
  703. Arguments = strtrim(Arguments);
  704. if (Arguments.size() != 3 || !std::isdigit(Arguments[0]) || Arguments[1] != '.' || !std::isdigit(Arguments[2])) {
  705. return FileRunCommandResult::Error("Invalid dxil version format");
  706. }
  707. unsigned RequiredDxilMajor = Arguments[0] - '0';
  708. unsigned RequiredDxilMinor = Arguments[2] - '0';
  709. return CheckDxilVer(DllSupport, RequiredDxilMajor, RequiredDxilMinor);
  710. }
  711. #ifndef _WIN32
  712. FileRunCommandResult FileRunCommandPart::RunFromPath(const std::string &toolPath, const FileRunCommandResult *Prior) {
  713. return FileRunCommandResult::Error("RunFromPath not supported");
  714. }
  715. #else //_WIN32
  716. FileRunCommandResult FileRunCommandPart::RunFromPath(const std::string &toolPath, const FileRunCommandResult *Prior) {
  717. if (Prior != nullptr) {
  718. return FileRunCommandResult::Error("prior command not supported");
  719. }
  720. std::string args = Arguments;
  721. // replace %s with command file name
  722. size_t pos;
  723. while ((pos = args.find("%s")) != std::string::npos) {
  724. args.replace(pos, 2, CW2A(CommandFileName));
  725. }
  726. // replace %t and %b with the command file name without extension
  727. SubstituteFilenameVars(args);
  728. // Run the tool via CreateProcess, redirect stdout and strerr to temporary files
  729. std::wstring stdOutFileName = std::wstring(CommandFileName) + L".tmp_stdout";
  730. std::wstring stdErrFileName = std::wstring(CommandFileName) + L".tmp_stderr";
  731. SECURITY_ATTRIBUTES sa;
  732. ZeroMemory(&sa, sizeof(sa));
  733. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  734. sa.bInheritHandle = true;
  735. HANDLE hStdOutFile = CreateFileW(stdOutFileName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, &sa,
  736. CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, nullptr);
  737. IFT(hStdOutFile != INVALID_HANDLE_VALUE);
  738. HANDLE hStdErrFile = CreateFileW(stdErrFileName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, &sa,
  739. CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, nullptr);
  740. IFT(hStdErrFile!= INVALID_HANDLE_VALUE);
  741. STARTUPINFOA si;
  742. ZeroMemory(&si, sizeof(si));
  743. si.cb = sizeof(si);
  744. si.hStdOutput = hStdOutFile;
  745. si.hStdError = hStdErrFile;
  746. si.dwFlags |= STARTF_USESTDHANDLES;
  747. PROCESS_INFORMATION pi;
  748. ZeroMemory(&pi, sizeof(pi));
  749. std::vector<char> args2(args.c_str(), args.c_str() + args.size() + 1); // args to CreateProcess cannot be const char *
  750. if (!CreateProcessA(toolPath.c_str(), args2.data(), nullptr, nullptr, true, 0, nullptr, nullptr, &si, &pi)) {
  751. return FileRunCommandResult::Error("CreateProcess failed.");
  752. }
  753. ::WaitForSingleObject(pi.hProcess, 10000); // 10s timeout
  754. // Get exit code of the process
  755. FileRunCommandResult result;
  756. DWORD exitCode;
  757. if (!::GetExitCodeProcess(pi.hProcess, &exitCode)) {
  758. result = FileRunCommandResult::Error("GetExitCodeProcess failed.");
  759. }
  760. else {
  761. result.ExitCode = exitCode;
  762. }
  763. // Close process and thread handles
  764. ::CloseHandle(pi.hProcess);
  765. ::CloseHandle(pi.hThread);
  766. // Read stdout and strerr output from temporary files
  767. if (!ReadFileContentToString(hStdOutFile, result.StdOut) ||
  768. !ReadFileContentToString(hStdErrFile, result.StdErr)) {
  769. result = FileRunCommandResult::Error("RunFromPaths failed.");
  770. }
  771. // Close temporary file handles - will delete the files
  772. IFT(::CloseHandle(hStdOutFile));
  773. IFT(::CloseHandle(hStdErrFile));
  774. return result;
  775. }
  776. #endif //_WIN32
  777. class FileRunTestResultImpl : public FileRunTestResult {
  778. dxc::DxcDllSupport &m_support;
  779. PluginToolsPaths *m_pPluginToolsPaths;
  780. void RunHashTestFromCommands(LPCSTR commands, LPCWSTR fileName) {
  781. std::vector<FileRunCommandPart> parts;
  782. ParseCommandParts(commands, fileName, parts);
  783. FileRunCommandResult result;
  784. bool ran = false;
  785. for (FileRunCommandPart & part : parts) {
  786. result = part.RunHashTests(m_support);
  787. ran = true;
  788. break;
  789. }
  790. if (ran) {
  791. this->RunResult = result.ExitCode;
  792. this->ErrorMessage = result.StdErr;
  793. }
  794. else {
  795. this->RunResult = 0;
  796. }
  797. }
  798. void RunFileCheckFromCommands(LPCSTR commands, LPCWSTR fileName) {
  799. std::vector<FileRunCommandPart> parts;
  800. ParseCommandParts(commands, fileName, parts);
  801. if (parts.empty()) {
  802. this->RunResult = 1;
  803. this->ErrorMessage = "FileCheck found no commands to run";
  804. return;
  805. }
  806. FileRunCommandResult result;
  807. FileRunCommandResult* previousResult = nullptr;
  808. for (FileRunCommandPart & part : parts) {
  809. result = part.Run(m_support, previousResult, m_pPluginToolsPaths);
  810. previousResult = &result;
  811. if (result.AbortPipeline) break;
  812. }
  813. this->RunResult = result.ExitCode;
  814. this->ErrorMessage = result.StdErr;
  815. }
  816. public:
  817. FileRunTestResultImpl(dxc::DxcDllSupport &support, PluginToolsPaths *pPluginToolsPaths = nullptr)
  818. : m_support(support), m_pPluginToolsPaths(pPluginToolsPaths) {}
  819. void RunFileCheckFromFileCommands(LPCWSTR fileName) {
  820. // Assume UTF-8 files.
  821. auto cmds = GetRunLines(fileName);
  822. // Iterate over all RUN lines
  823. for (auto &cmd : cmds) {
  824. RunFileCheckFromCommands(cmd.c_str(), fileName);
  825. // If any of the RUN cmd fails then skip executing remaining cmds
  826. // and report the error
  827. if (this->RunResult != 0) break;
  828. }
  829. }
  830. void RunHashTestFromFileCommands(LPCWSTR fileName) {
  831. // Assume UTF-8 files.
  832. std::string commands(GetFirstLine(fileName));
  833. return RunHashTestFromCommands(commands.c_str(), fileName);
  834. }
  835. };
  836. FileRunTestResult FileRunTestResult::RunHashTestFromFileCommands(LPCWSTR fileName) {
  837. dxc::DxcDllSupport dllSupport;
  838. IFT(dllSupport.Initialize());
  839. FileRunTestResultImpl result(dllSupport);
  840. result.RunHashTestFromFileCommands(fileName);
  841. return result;
  842. }
  843. FileRunTestResult FileRunTestResult::RunFromFileCommands(LPCWSTR fileName,
  844. PluginToolsPaths *pPluginToolsPaths /*=nullptr*/) {
  845. dxc::DxcDllSupport dllSupport;
  846. IFT(dllSupport.Initialize());
  847. FileRunTestResultImpl result(dllSupport, pPluginToolsPaths);
  848. result.RunFileCheckFromFileCommands(fileName);
  849. return result;
  850. }
  851. FileRunTestResult FileRunTestResult::RunFromFileCommands(LPCWSTR fileName, dxc::DxcDllSupport &dllSupport,
  852. PluginToolsPaths *pPluginToolsPaths /*=nullptr*/) {
  853. FileRunTestResultImpl result(dllSupport, pPluginToolsPaths);
  854. result.RunFileCheckFromFileCommands(fileName);
  855. return result;
  856. }
  857. void ParseCommandParts(LPCSTR commands, LPCWSTR fileName,
  858. std::vector<FileRunCommandPart> &parts) {
  859. // Barely enough parsing here.
  860. commands = strstr(commands, "RUN: ");
  861. if (!commands) {
  862. return;
  863. }
  864. commands += strlen("RUN: ");
  865. LPCSTR endCommands = strchr(commands, '\0');
  866. while (commands != endCommands) {
  867. LPCSTR nextStart;
  868. LPCSTR thisEnd = strchr(commands, '|');
  869. if (!thisEnd) {
  870. nextStart = thisEnd = endCommands;
  871. } else {
  872. nextStart = thisEnd + 2;
  873. }
  874. LPCSTR commandEnd = strchr(commands, ' ');
  875. if (!commandEnd)
  876. commandEnd = endCommands;
  877. parts.emplace_back(std::string(commands, commandEnd),
  878. std::string(commandEnd, thisEnd), fileName);
  879. commands = nextStart;
  880. }
  881. }
  882. void ParseCommandPartsFromFile(LPCWSTR fileName,
  883. std::vector<FileRunCommandPart> &parts) {
  884. // Assume UTF-8 files.
  885. std::string commands(GetFirstLine(fileName));
  886. ParseCommandParts(commands.c_str(), fileName, parts);
  887. }