From a63af9311e1a83c61a968e9ef36c98427c9cde3f Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Thu, 19 Feb 2009 03:38:10 -0500 Subject: [PATCH] Merged assemble.c into mojoshader-compiler.c ... --- CMakeLists.txt | 6 +- utils/assemble.c | 93 -------------------------- utils/mojoshader-compiler.c | 130 ++++++++++++++++++++++++++---------- 3 files changed, 98 insertions(+), 131 deletions(-) delete mode 100644 utils/assemble.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 524f4015..076b9357 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,10 +89,8 @@ ADD_EXECUTABLE(testoutput utils/testoutput.c) TARGET_LINK_LIBRARIES(testoutput mojoshader) ADD_EXECUTABLE(finderrors utils/finderrors.c) TARGET_LINK_LIBRARIES(finderrors mojoshader ${SDL_LIBRARY}) -ADD_EXECUTABLE(assemble utils/assemble.c) -TARGET_LINK_LIBRARIES(assemble mojoshader) -ADD_EXECUTABLE(preprocess utils/preprocess.c) -TARGET_LINK_LIBRARIES(preprocess mojoshader) +ADD_EXECUTABLE(mojoshader-compiler utils/mojoshader-compiler.c) +TARGET_LINK_LIBRARIES(mojoshader-compiler mojoshader) # End of CMakeLists.txt ... diff --git a/utils/assemble.c b/utils/assemble.c deleted file mode 100644 index 16fb21c5..00000000 --- a/utils/assemble.c +++ /dev/null @@ -1,93 +0,0 @@ -/** - * MojoShader; generate shader programs from bytecode of compiled - * Direct3D shaders. - * - * Please see the file LICENSE.txt in the source's root directory. - * - * This file written by Ryan C. Gordon. - */ - -#include -#include -#include "mojoshader.h" - -static int assemble(const char *fname, const char *buf, int len, - const char *outfile) -{ - FILE *io = fopen(outfile, "wb"); - if (io == NULL) - { - printf(" ... fopen('%s') failed.\n", outfile); - return 0; - } // if - - const MOJOSHADER_parseData *pd; - int retval = 0; - - pd = MOJOSHADER_assemble(fname, buf, len, NULL, 0, NULL, 0, - NULL, 0, NULL, NULL, NULL, NULL, NULL); - if (pd->error_count > 0) - { - int i; - for (i = 0; i < pd->error_count; i++) - { - printf("%s:%d: ERROR: %s\n", - pd->errors[i].filename ? pd->errors[i].filename : "???", - pd->errors[i].error_position, - pd->errors[i].error); - } // for - } // if - else - { - if (pd->output != NULL) - { - if (fwrite(pd->output, pd->output_len, 1, io) != 1) - printf(" ... fwrite('%s') failed.\n", outfile); - else if (fclose(io) == EOF) - printf(" ... fclose('%s') failed.\n", outfile); - else - retval = 1; - } // if - } // else - MOJOSHADER_freeParseData(pd); - - return retval; -} // assemble - - -int main(int argc, char **argv) -{ - int retval = 1; - - if (argc != 3) - printf("\n\nUSAGE: %s \n\n", argv[0]); - else - { - const char *infile = argv[1]; - const char *outfile = argv[2]; - FILE *io = fopen(infile, "rb"); - if (io == NULL) - printf(" ... fopen('%s') failed.\n", infile); - else - { - char *buf = (char *) malloc(1000000); - int rc = fread(buf, 1, 1000000, io); - fclose(io); - if (rc == EOF) - printf(" ... fread('%s') failed.\n", infile); - else - { - if (assemble(infile, buf, rc, outfile)) - retval = 0; - else - remove(outfile); - free(buf); - } // else - } // for - } // else - - return retval; -} // main - -// end of assemble.c ... - diff --git a/utils/mojoshader-compiler.c b/utils/mojoshader-compiler.c index b1d0db07..3a1b7de5 100644 --- a/utils/mojoshader-compiler.c +++ b/utils/mojoshader-compiler.c @@ -111,15 +111,8 @@ static void close_include(const char *data, MOJOSHADER_malloc m, static int preprocess(const char *fname, const char *buf, int len, const char *outfile, const MOJOSHADER_preprocessorDefine *defs, - unsigned int defcount) + unsigned int defcount, FILE *io) { - FILE *io = outfile ? fopen(outfile, "wb") : stdout; - if (io == NULL) - { - printf(" ... fopen('%s') failed.\n", outfile); - return 0; - } // if - const MOJOSHADER_preprocessData *pd; int retval = 0; @@ -155,8 +148,58 @@ static int preprocess(const char *fname, const char *buf, int len, } // preprocess +static int assemble(const char *fname, const char *buf, int len, + const char *outfile, + const MOJOSHADER_preprocessorDefine *defs, + unsigned int defcount, FILE *io) +{ + const MOJOSHADER_parseData *pd; + int retval = 0; + + pd = MOJOSHADER_assemble(fname, buf, len, NULL, 0, NULL, 0, + defs, defcount, open_include, close_include, + Malloc, Free, NULL); + + if (pd->error_count > 0) + { + int i; + for (i = 0; i < pd->error_count; i++) + { + printf("%s:%d: ERROR: %s\n", + pd->errors[i].filename ? pd->errors[i].filename : "???", + pd->errors[i].error_position, + pd->errors[i].error); + } // for + } // if + else + { + if (pd->output != NULL) + { + if (fwrite(pd->output, pd->output_len, 1, io) != 1) + printf(" ... fwrite('%s') failed.\n", outfile); + else if ((outfile != NULL) && (fclose(io) == EOF)) + printf(" ... fclose('%s') failed.\n", outfile); + else + retval = 1; + } // if + } // else + MOJOSHADER_freeParseData(pd); + + return retval; +} // assemble + + +typedef enum +{ + ACTION_UNKNOWN, + ACTION_PREPROCESS, + ACTION_ASSEMBLE, +} Action; + + int main(int argc, char **argv) { + Action action = ACTION_UNKNOWN; int retval = 1; const char *infile = NULL; const char *outfile = NULL; @@ -169,7 +212,21 @@ int main(int argc, char **argv) { const char *arg = argv[i]; - if (strcmp(arg, "-o") == 0) + if (strcmp(arg, "-P") == 0) + { + if (action != ACTION_UNKNOWN) + fail("Multiple actions specified"); + action = ACTION_PREPROCESS; + } // if + + else if (strcmp(arg, "-A") == 0) + { + if (action != ACTION_UNKNOWN) + fail("Multiple actions specified"); + action = ACTION_ASSEMBLE; + } // else if + + else if (strcmp(arg, "-o") == 0) { if (outfile != NULL) fail("multiple output files specified"); @@ -219,36 +276,41 @@ int main(int argc, char **argv) } // else } // for + if (action == ACTION_UNKNOWN) + action = ACTION_ASSEMBLE; + if (infile == NULL) fail("no input file specified."); FILE *io = fopen(infile, "rb"); if (io == NULL) - printf(" ... fopen('%s') failed.\n", infile); - else - { - fseek(io, 0, SEEK_END); - long fsize = ftell(io); - fseek(io, 0, SEEK_SET); - if (fsize == -1) - fsize = 1000000; - char *buf = (char *) malloc(fsize); - const int rc = fread(buf, 1, fsize, io); - fclose(io); - if (rc == EOF) - printf(" ... fread('%s') failed.\n", infile); - else - { - if (preprocess(infile, buf, rc, outfile, defs, defcount)) - retval = 0; - else - { - if (outfile != NULL) - remove(outfile); - } // else - free(buf); - } // else - } // else + fail("failed to open input file"); + + fseek(io, 0, SEEK_END); + long fsize = ftell(io); + fseek(io, 0, SEEK_SET); + if (fsize == -1) + fsize = 1000000; + char *buf = (char *) malloc(fsize); + const int rc = fread(buf, 1, fsize, io); + fclose(io); + if (rc == EOF) + fail("failed to read input file"); + + FILE *outio = outfile ? fopen(outfile, "wb") : stdout; + if (outio == NULL) + fail("failed to open output file"); + + + if (action == ACTION_PREPROCESS) + retval = (!preprocess(infile, buf, rc, outfile, defs, defcount, outio)); + else if (action == ACTION_ASSEMBLE) + retval = (!assemble(infile, buf, rc, outfile, defs, defcount, outio)); + + if ((retval != 0) && (outfile != NULL)) + remove(outfile); + + free(buf); for (i = 0; i < defcount; i++) free((void *) defs[i].identifier);