Merged assemble.c into mojoshader-compiler.c ...
--- a/CMakeLists.txt Thu Feb 19 03:36:49 2009 -0500
+++ b/CMakeLists.txt Thu Feb 19 03:38:10 2009 -0500
@@ -89,10 +89,8 @@
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 ...
--- a/utils/assemble.c Thu Feb 19 03:36:49 2009 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -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 <stdio.h>
-#include <stdlib.h>
-#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 <inputfile> <outputfile>\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 ...
-
--- a/utils/mojoshader-compiler.c Thu Feb 19 03:36:49 2009 -0500
+++ b/utils/mojoshader-compiler.c Thu Feb 19 03:38:10 2009 -0500
@@ -111,15 +111,8 @@
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 @@
} // 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 @@
{
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 @@
} // 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);