From c27bed00233169d9a3d8c292d48a49e508b47720 Mon Sep 17 00:00:00 2001 From: Henri Verbeet Date: Thu, 13 Aug 2020 17:37:38 +0430 Subject: [PATCH] vkd3d-compiler: Support writing to standard output. Signed-off-by: Henri Verbeet Signed-off-by: Alexandre Julliard --- programs/vkd3d-compiler/main.c | 75 ++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 22 deletions(-) diff --git a/programs/vkd3d-compiler/main.c b/programs/vkd3d-compiler/main.c index ed4b0eed..00512772 100644 --- a/programs/vkd3d-compiler/main.c +++ b/programs/vkd3d-compiler/main.c @@ -17,6 +17,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ +#define _GNU_SOURCE #include #include #include @@ -108,21 +109,9 @@ static bool read_shader(struct vkd3d_shader_code *shader, const char *filename) return true; } -static bool write_shader(const struct vkd3d_shader_code *shader, const char *filename) +static bool write_shader(const struct vkd3d_shader_code *shader, FILE *f) { - FILE *fd; - - if (!(fd = fopen(filename, "wb"))) - { - fprintf(stderr, "Cannot open file for writing: '%s'.\n", filename); - return false; - } - - if (fwrite(shader->code, 1, shader->size, fd) != shader->size) - fprintf(stderr, "Could not write shader bytecode to file: '%s'.\n", filename); - - fclose(fd); - return true; + return fwrite(shader->code, 1, shader->size, f) == shader->size; } static void print_usage(const char *program_name) @@ -136,7 +125,9 @@ static void print_usage(const char *program_name) " --buffer-uav= Specify the buffer type to use for buffer UAV bindings.\n" " Valid values are 'buffer-texture' (default) and\n" " 'storage-buffer'.\n" - " -o, --output= Write the output to .\n" + " -o, --output= Write the output to . If is '-' or no\n" + " output file is specified, output will be written to\n" + " standard output.\n" " --print-source-types Display the supported source types and exit.\n" " --print-target-types Display the supported target types for the specified\n" " source type and exit.\n" @@ -379,12 +370,34 @@ static void print_target_types(enum vkd3d_shader_source_type source_type) } } +static FILE *open_output(const char *filename, bool *close) +{ + FILE *f; + + *close = false; + + if (!filename || !strcmp(filename, "-")) + return stdout; + + if (!(f = fopen(filename, "wb"))) + { + fprintf(stderr, "Unable to open '%s' for writing.\n", filename); + return NULL; + } + + *close = true; + return f; +} + int main(int argc, char **argv) { struct vkd3d_shader_compile_info info; struct vkd3d_shader_code spirv; + bool close_output = false; struct options options; char *messages; + FILE *output; + int fail = 1; int ret; if (!parse_command_line(argc, argv, &options)) @@ -413,6 +426,16 @@ int main(int argc, char **argv) return 0; } + if (!(output = open_output(options.output_filename, &close_output))) + goto done; + + if (!options.output_filename && isatty(fileno(output))) + { + fprintf(stderr, "Output is a tty and output format is binary, exiting.\n" + "If this is really what you intended, specify the output explicitly.\n"); + goto done; + } + info.type = VKD3D_SHADER_STRUCTURE_TYPE_COMPILE_INFO; info.next = NULL; info.source_type = options.source_type; @@ -424,8 +447,8 @@ int main(int argc, char **argv) if (!read_shader(&info.source, options.filename)) { - fprintf(stderr, "Failed to read DXBC shader.\n"); - return 1; + fprintf(stderr, "Failed to read input shader.\n"); + goto done; } ret = vkd3d_shader_compile(&info, &spirv, &messages); @@ -435,13 +458,21 @@ int main(int argc, char **argv) vkd3d_shader_free_shader_code(&info.source); if (ret < 0) { - fprintf(stderr, "Failed to compile DXBC shader, ret %d.\n", ret); - return 1; + fprintf(stderr, "Failed to compile shader, ret %d.\n", ret); + goto done; } - if (options.output_filename) - write_shader(&spirv, options.output_filename); + if (!write_shader(&spirv, output)) + { + fprintf(stderr, "Failed to write output shader.\n"); + vkd3d_shader_free_shader_code(&spirv); + goto done; + } + fail = 0; vkd3d_shader_free_shader_code(&spirv); - return 0; +done: + if (close_output) + fclose(output); + return fail; }