Add a --depfile option.
The output is similar to gcc -MMD and can be used by build systems like meson to automatically recompile shaders if an included header was changed.
This commit is contained in:
parent
60ce877de0
commit
f050209ce1
@ -40,6 +40,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
#include "./../glslang/Public/ShaderLang.h"
|
#include "./../glslang/Public/ShaderLang.h"
|
||||||
|
|
||||||
@ -84,12 +85,18 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual std::set<std::string> getIncludedFiles()
|
||||||
|
{
|
||||||
|
return includedFiles;
|
||||||
|
}
|
||||||
|
|
||||||
virtual ~DirStackFileIncluder() override { }
|
virtual ~DirStackFileIncluder() override { }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
typedef char tUserDataElement;
|
typedef char tUserDataElement;
|
||||||
std::vector<std::string> directoryStack;
|
std::vector<std::string> directoryStack;
|
||||||
int externalLocalDirectoryCount;
|
int externalLocalDirectoryCount;
|
||||||
|
std::set<std::string> includedFiles;
|
||||||
|
|
||||||
// Search for a valid "local" path based on combining the stack of include
|
// Search for a valid "local" path based on combining the stack of include
|
||||||
// directories and the nominal name of the header.
|
// directories and the nominal name of the header.
|
||||||
@ -108,6 +115,7 @@ protected:
|
|||||||
std::ifstream file(path, std::ios_base::binary | std::ios_base::ate);
|
std::ifstream file(path, std::ios_base::binary | std::ios_base::ate);
|
||||||
if (file) {
|
if (file) {
|
||||||
directoryStack.push_back(getDirectory(path));
|
directoryStack.push_back(getDirectory(path));
|
||||||
|
includedFiles.insert(path);
|
||||||
return newIncludeResult(path, file, (int)file.tellg());
|
return newIncludeResult(path, file, (int)file.tellg());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,6 +58,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
#include "../glslang/OSDependent/osinclude.h"
|
#include "../glslang/OSDependent/osinclude.h"
|
||||||
|
|
||||||
@ -165,6 +166,7 @@ int ReflectOptions = EShReflectionDefault;
|
|||||||
int Options = 0;
|
int Options = 0;
|
||||||
const char* ExecutableName = nullptr;
|
const char* ExecutableName = nullptr;
|
||||||
const char* binaryFileName = nullptr;
|
const char* binaryFileName = nullptr;
|
||||||
|
const char* depencyFileName = nullptr;
|
||||||
const char* entryPointName = nullptr;
|
const char* entryPointName = nullptr;
|
||||||
const char* sourceEntryPointName = nullptr;
|
const char* sourceEntryPointName = nullptr;
|
||||||
const char* shaderStageName = nullptr;
|
const char* shaderStageName = nullptr;
|
||||||
@ -798,6 +800,11 @@ void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItem
|
|||||||
break;
|
break;
|
||||||
} else if (lowerword == "quiet") {
|
} else if (lowerword == "quiet") {
|
||||||
beQuiet = true;
|
beQuiet = true;
|
||||||
|
} else if (lowerword == "depfile") {
|
||||||
|
if (argc <= 1)
|
||||||
|
Error("no <depfile-name> provided", lowerword.c_str());
|
||||||
|
depencyFileName = argv[1];
|
||||||
|
bumpArg();
|
||||||
} else if (lowerword == "version") {
|
} else if (lowerword == "version") {
|
||||||
Options |= EOptionDumpVersions;
|
Options |= EOptionDumpVersions;
|
||||||
} else if (lowerword == "help") {
|
} else if (lowerword == "help") {
|
||||||
@ -1135,6 +1142,23 @@ struct ShaderCompUnit {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Writes a depfile similar to gcc -MMD foo.c
|
||||||
|
bool writeDepFile(std::string depfile, std::vector<std::string>& binaryFiles, const std::vector<std::string>& sources)
|
||||||
|
{
|
||||||
|
std::ofstream file(depfile);
|
||||||
|
if (file.fail())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (auto it = binaryFiles.begin(); it != binaryFiles.end(); it++) {
|
||||||
|
file << *it << ":";
|
||||||
|
for (auto it = sources.begin(); it != sources.end(); it++) {
|
||||||
|
file << " " << *it;
|
||||||
|
}
|
||||||
|
file << std::endl;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// For linking mode: Will independently parse each compilation unit, but then put them
|
// For linking mode: Will independently parse each compilation unit, but then put them
|
||||||
// in the same program and link them together, making at most one linked module per
|
// in the same program and link them together, making at most one linked module per
|
||||||
@ -1151,6 +1175,12 @@ void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
|
|||||||
EShMessages messages = EShMsgDefault;
|
EShMessages messages = EShMsgDefault;
|
||||||
SetMessageOptions(messages);
|
SetMessageOptions(messages);
|
||||||
|
|
||||||
|
DirStackFileIncluder includer;
|
||||||
|
std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
|
||||||
|
includer.pushExternalLocalDirectory(dir); });
|
||||||
|
|
||||||
|
std::vector<std::string> sources;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Per-shader processing...
|
// Per-shader processing...
|
||||||
//
|
//
|
||||||
@ -1158,6 +1188,9 @@ void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
|
|||||||
glslang::TProgram& program = *new glslang::TProgram;
|
glslang::TProgram& program = *new glslang::TProgram;
|
||||||
for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
|
for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
|
||||||
const auto &compUnit = *it;
|
const auto &compUnit = *it;
|
||||||
|
for (int i = 0; i < compUnit.count; i++) {
|
||||||
|
sources.push_back(compUnit.fileNameList[i]);
|
||||||
|
}
|
||||||
glslang::TShader* shader = new glslang::TShader(compUnit.stage);
|
glslang::TShader* shader = new glslang::TShader(compUnit.stage);
|
||||||
shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
|
shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
|
||||||
if (entryPointName)
|
if (entryPointName)
|
||||||
@ -1252,9 +1285,6 @@ void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
|
|||||||
|
|
||||||
const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
|
const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
|
||||||
|
|
||||||
DirStackFileIncluder includer;
|
|
||||||
std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
|
|
||||||
includer.pushExternalLocalDirectory(dir); });
|
|
||||||
#ifndef GLSLANG_WEB
|
#ifndef GLSLANG_WEB
|
||||||
if (Options & EOptionOutputPreprocessed) {
|
if (Options & EOptionOutputPreprocessed) {
|
||||||
std::string str;
|
std::string str;
|
||||||
@ -1314,6 +1344,8 @@ void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
std::vector<std::string> outputFiles;
|
||||||
|
|
||||||
// Dump SPIR-V
|
// Dump SPIR-V
|
||||||
if (Options & EOptionSpv) {
|
if (Options & EOptionSpv) {
|
||||||
if (CompileFailed || LinkFailed)
|
if (CompileFailed || LinkFailed)
|
||||||
@ -1343,6 +1375,8 @@ void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
|
|||||||
} else {
|
} else {
|
||||||
glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
|
glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
outputFiles.push_back(GetBinaryName((EShLanguage)stage));
|
||||||
#ifndef GLSLANG_WEB
|
#ifndef GLSLANG_WEB
|
||||||
if (!SpvToolsDisassembler && (Options & EOptionHumanReadableSpv))
|
if (!SpvToolsDisassembler && (Options & EOptionHumanReadableSpv))
|
||||||
spv::Disassemble(std::cout, spirv);
|
spv::Disassemble(std::cout, spirv);
|
||||||
@ -1353,6 +1387,13 @@ void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (depencyFileName && !(CompileFailed || LinkFailed)) {
|
||||||
|
std::set<std::string> includedFiles = includer.getIncludedFiles();
|
||||||
|
sources.insert(sources.end(), includedFiles.begin(), includedFiles.end());
|
||||||
|
|
||||||
|
writeDepFile(depencyFileName, outputFiles, sources);
|
||||||
|
}
|
||||||
|
|
||||||
// Free everything up, program has to go before the shaders
|
// Free everything up, program has to go before the shaders
|
||||||
// because it might have merged stuff from the shaders, and
|
// because it might have merged stuff from the shaders, and
|
||||||
// the stuff from the shaders has to have its destructors called
|
// the stuff from the shaders has to have its destructors called
|
||||||
@ -1773,6 +1814,7 @@ void usage()
|
|||||||
" --auto-map-locations | --aml automatically locate input/output lacking\n"
|
" --auto-map-locations | --aml automatically locate input/output lacking\n"
|
||||||
" 'location' (fragile, not cross stage)\n"
|
" 'location' (fragile, not cross stage)\n"
|
||||||
" --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
|
" --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
|
||||||
|
" --depfile <file> writes depfile for build systems\n"
|
||||||
" --dump-builtin-symbols prints builtin symbol table prior each compile\n"
|
" --dump-builtin-symbols prints builtin symbol table prior each compile\n"
|
||||||
" -dumpfullversion | -dumpversion print bare major.minor.patchlevel\n"
|
" -dumpfullversion | -dumpversion print bare major.minor.patchlevel\n"
|
||||||
" --flatten-uniform-arrays | --fua flatten uniform texture/sampler arrays to\n"
|
" --flatten-uniform-arrays | --fua flatten uniform texture/sampler arrays to\n"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user