#include <filesystem>
#include <iostream>
#include "extract_source.h"
#include "source/opt/log.h"
#include "tools/io.h"
#include "tools/util/cli_consumer.h"
#include "tools/util/flags.h"
namespace {
constexpr auto kHelpTextFmt =
R"(%s - Dumps information from a SPIR-V binary.
Usage: %s [options] <filename>
one of the following switches must be given:
--source Extract source files obtained from debug symbols, output to stdout.
--entrypoint Extracts the entrypoint name of the module, output to stdout.
--compiler-cmd Extracts the command line used to compile this module, output to stdout.
General options:
-h, --help Print this help.
--version Display assembler version information.
-f,--force Allow output file overwrite.
Source dump options:
--list Do not extract source code, only print filenames to stdout.
--outdir Where shall the exrtacted HLSL/HLSL files be written to?
File written to stdout if '-' is given. Default is `-`.
)";
std::string fixPathForLLVM(std::string input) {
while (!input.empty() && input.back() == '/') input.resize(input.size() - 1);
return input;
}
bool OutputSourceFiles(
const std::unordered_map<std::string, std::string>& sources,
const std::string& outdirPath, bool overwrite) {
std::filesystem::path outdir(fixPathForLLVM(outdirPath));
if (!std::filesystem::is_directory(outdir)) {
if (!std::filesystem::create_directories(outdir)) {
std::cerr << "error: could not create output directory " << outdir
<< std::endl;
return false;
}
}
for (const auto & [ filepath, code ] : sources) {
if (code.empty()) {
std::cout << "Ignoring source for " << filepath
<< ": no code source in debug infos." << std::endl;
continue;
}
std::filesystem::path old_path(filepath);
std::filesystem::path new_path = outdir / old_path.filename();
if (!overwrite && std::filesystem::exists(new_path)) {
std::cerr << "file " << filepath
<< " already exists, aborting (use --overwrite to allow it)."
<< std::endl;
return false;
}
std::cout << "Exporting " << new_path << std::endl;
if (!WriteFile<char>(new_path.string().c_str(), "w", code.c_str(),
code.size())) {
return false;
}
}
return true;
}
}
FLAG_SHORT_bool( h, false, false);
FLAG_LONG_bool( help, false, false);
FLAG_LONG_bool( version, false, false);
FLAG_LONG_bool( source, false, false);
FLAG_LONG_bool( entrypoint, false, false);
FLAG_LONG_bool( compiler_cmd, false, false);
FLAG_SHORT_bool( f, false, false);
FLAG_LONG_bool( force, false, false);
FLAG_LONG_string( outdir, "-", false);
FLAG_LONG_bool( list, false, false);
int main(int, const char** argv) {
if (!flags::Parse(argv)) {
return 1;
}
if (flags::h.value() || flags::help.value()) {
printf(kHelpTextFmt, argv[0], argv[0]);
return 0;
}
if (flags::version.value()) {
printf("%s\n", spvSoftwareVersionDetailsString());
return 0;
}
if (flags::positional_arguments.size() != 1) {
std::cerr << "Expected exactly one input file." << std::endl;
return 1;
}
if (flags::entrypoint.value() || flags::compiler_cmd.value()) {
std::cerr << "Unimplemented flags." << std::endl;
return 1;
}
std::vector<uint32_t> binary;
if (!ReadBinaryFile(flags::positional_arguments[0].c_str(), &binary)) {
return 1;
}
if (flags::source.value()) {
std::unordered_map<std::string, std::string> sourceCode;
if (!ExtractSourceFromModule(binary, &sourceCode)) {
return 1;
}
if (flags::list.value()) {
for (const auto & [ filename, source ] : sourceCode) {
printf("%s\n", filename.c_str());
}
return 0;
}
const bool outputToConsole = flags::outdir.value() == "-";
if (outputToConsole) {
for (const auto & [ filename, source ] : sourceCode) {
std::cout << filename << ":" << std::endl
<< source << std::endl
<< std::endl;
}
return 0;
}
const std::filesystem::path outdirPath(flags::outdir.value());
if (!OutputSourceFiles(sourceCode, outdirPath.string(),
flags::force.value())) {
return 1;
}
}
return 0;
}