Skip to content

Commit

Permalink
rewrite the GetOuputFileName(), add tests, update blurbs
Browse files Browse the repository at this point in the history
  • Loading branch information
Qining committed Feb 2, 2016
1 parent 9a07fe4 commit 5cfcd3b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 23 deletions.
89 changes: 67 additions & 22 deletions glslc/src/file_compiler.cc
Expand Up @@ -198,32 +198,77 @@ std::string GetBaseNameFromAbsolutePath(const std::string& path) {
} // anonymous namespace

std::string FileCompiler::GetOutputFileName(std::string input_filename) {
std::string output_file = "a.spv";
if (!needs_linking_) {
if (IsStageFile(input_filename)) {
output_file = input_filename + file_extension_;
std::string default_output_file = "a.spv";

if(needs_linking_) {
// When linking enabled, ignore -working-directory for output, And don't
// induce output file name from input file name.
if (output_file_name_.empty()) {
// no '-o' argument specified, return the default output file name.
// e.g. glslc shader.vert
// => a.spv in <curdir>/
return default_output_file;
} else {
output_file = input_filename.substr(0, input_filename.find_last_of('.')) +
file_extension_;
// If '-o' specified, use the argument as output file name, no matter
// relative path or absolute path it is.
// e.g. glslc shader.vert -o output.spv
// => output.spv in <curdir>/
return output_file_name_.str();
}
}
if (!output_file_name_.empty()) output_file = output_file_name_.str();
if (!needs_linking_ && !workdir_.empty() &&
// If the specified output file name is an absolute path, don't refer to
// working directory for the output.
!shaderc_util::IsAbsolutePath(output_file_name_.str())) {
// If the input file name is an absolute path, we will have output_file as
// an absolute path here. So we need to get the base name of it, and refer
// to the working directory for the output.
if (shaderc_util::IsAbsolutePath(output_file)) {
// Get the file name from the absolute path of the input file.
// This is because we need to prepend working directory to the output file
// name to get the output file path.
output_file = GetBaseNameFromAbsolutePath(output_file);
} else {
// When linking is disable, should refer to -working-directory when the
// '-o' argument is a relative path or unspecified. If the '-o' argument
// is an absolute path, ignore the -working-directory for output.
if (output_file_name_.empty()) {
// No '-o' specified, use the input file names to induce the output file
// names.
// e.g. shader.vert => shader.spv or shader.s
// shader.unknown => shader.spv or shader.s
// shader => shader.spv or shader.s
std::string induced_file_name;
if (IsStageFile(input_filename)) {
induced_file_name = input_filename + file_extension_;
} else {
induced_file_name =
input_filename.substr(0, input_filename.find_last_of('.')) +
file_extension_;
}

if (workdir_.empty()) {
// no -working-directory specified
// e.g. glslc shader.vert -c
// => shader.spv in <curdir>/
// glslc /usr/local/shader.vert -c
// => shader.spv in /usr/local/shader/
return induced_file_name;
} else {
// Case: -c/-S -working-directory=<some_dir> <input>
// e.g. glslc shader.vert -c -working-directory subdir
// => shader.spv in <curdir>/subdir/
//
// glslc /usr/local/shader.vert -c -working-directory subdir
// => shader.spv in <curdir>/subdir/
if (shaderc_util::IsAbsolutePath(induced_file_name)) {
induced_file_name = GetBaseNameFromAbsolutePath(induced_file_name);
}
return workdir_ + induced_file_name;
}
} else {
// Case: -c/-S <abspath input>
// '-o' specified. If the argument is an absolute path, use it, if not,
// refer to the working directory.
// e.g. glslc shader.vert -c -o output.x -working-dir subdir
// => output.x in <curdir>/subdir/
//
// glslc shadser.vert -c -o /usr/local/output.x -working-dir subdir
// => output.x in /usr/local/
if (shaderc_util::IsAbsolutePath(output_file_name_.str())) {
return output_file_name_.str();
} else {
return workdir_ + output_file_name_.str();
}
}
output_file = workdir_ + output_file;
}
return output_file;
}

} // namesapce glslc
23 changes: 22 additions & 1 deletion glslc/src/file_compiler.h
Expand Up @@ -99,7 +99,28 @@ class FileCompiler {
// compiler_.CompileGlslToSpv().
shaderc::CompileOptions options_;

// Returns the name of the output file, given the input_filename string.
// Returns the name of the output file, computed as follows.
//
// Let "target directory" be the user-specified working directory, if
// provided, and the current working directory otherwise.
// Let "result extension" be ".spv" when generating a SPIR-V binary, and
// ".s" when generating SPIR-V assembly text.
// Let "resolved input filename" be the input filename if it's absolute,
// or the input filename relative to the target directory otherwise.
//
// If the user specified an output filename, then:
// If the specified output filename is an absolute path, then return that.
// Otherwise, return the specified output filename relative to the target
// directory.
// If the user did not specify an output filename, then:
// If linking is performed, return "a.spv" relative to the target
// directory.
// Derive the output filename from the input filename as follows:
// If the input filename has a standard stage extension (e.g. .vert)
// then return the resolved input filename but add the result extension.
// Otherwise, return the resolved input filename but where its extension
// is replaced with the result extension. (If the resolved input filename
// does not have an extension, then append the result extension.)
std::string GetOutputFileName(std::string input_filename);

// Resolves relative paths against this working directory. Must always end
Expand Down

0 comments on commit 5cfcd3b

Please sign in to comment.