finderrors.c
branchtrunk
changeset 153 4410cc03dade
child 169 7e9d90724cf8
equal deleted inserted replaced
152:cc7c38dfe145 153:4410cc03dade
       
     1 #include <stdio.h>
       
     2 #include <stdlib.h>
       
     3 #include <string.h>
       
     4 #include <stdarg.h>
       
     5 #include <pthread.h>
       
     6 #include <sys/types.h>
       
     7 #include <dirent.h>
       
     8 #include <errno.h>
       
     9 
       
    10 #include "mojoshader.h"
       
    11 
       
    12 static const char *profile = NULL;
       
    13 static volatile int die_threads = 0;
       
    14 static pthread_mutex_t grab_mutex;
       
    15 static pthread_mutex_t report_mutex;
       
    16 
       
    17 typedef struct ShaderBytecode
       
    18 {
       
    19     char *name;
       
    20     struct ShaderBytecode *next;
       
    21 } ShaderBytecode;
       
    22 
       
    23 static volatile ShaderBytecode *gbytecode = NULL;
       
    24 
       
    25 static void report(const char *fmt, ...)
       
    26 {
       
    27     va_list ap;
       
    28     va_start(ap, fmt);
       
    29     pthread_mutex_lock(&report_mutex);
       
    30     vprintf(fmt, ap);
       
    31     pthread_mutex_unlock(&report_mutex);
       
    32     va_end(ap);
       
    33 } // report
       
    34 
       
    35 static void *worker(void *unused)
       
    36 {
       
    37     unsigned char buf[1024 * 256];
       
    38     while (!die_threads)
       
    39     {
       
    40         ShaderBytecode *my_bytecode = NULL;
       
    41         pthread_mutex_lock(&grab_mutex);
       
    42         if (gbytecode == NULL)
       
    43             die_threads = 1;
       
    44         else
       
    45         {
       
    46             my_bytecode = (ShaderBytecode *) gbytecode;
       
    47             gbytecode = gbytecode->next;
       
    48         } // else
       
    49         pthread_mutex_unlock(&grab_mutex);
       
    50 
       
    51         if (my_bytecode == NULL)
       
    52             break;
       
    53 
       
    54         const char *fname = my_bytecode->name;
       
    55         FILE *io = fopen(fname, "rb");
       
    56         if (io == NULL)
       
    57             report("FAIL: %s fopen() failed.\n", fname);
       
    58         else
       
    59         {
       
    60             int rc = fread(buf, 1, sizeof (buf), io);
       
    61             fclose(io);
       
    62             if (rc == -1)
       
    63                 report("FAIL: %s %s\n", fname, strerror(errno));
       
    64             else
       
    65             {
       
    66                 const MOJOSHADER_parseData *pd;
       
    67                 pd = MOJOSHADER_parse(profile, buf, rc, 0, 0, 0);
       
    68                 if (pd->error != NULL)
       
    69                     report("FAIL: %s %s\n", fname, pd->error);
       
    70                 else
       
    71                     report("PASS: %s\n", fname);
       
    72                 MOJOSHADER_freeParseData(pd);
       
    73             } // else
       
    74         } // else
       
    75 
       
    76         free(my_bytecode->name);
       
    77         free(my_bytecode);
       
    78     } // while
       
    79 
       
    80     return NULL;
       
    81 } // worker
       
    82 
       
    83 
       
    84 static int do_dir(const char *dname)
       
    85 {
       
    86     const int dirlen = strlen(dname) + 1;
       
    87     int total = 0;
       
    88     DIR *dirp = opendir(dname);
       
    89     if (dirp != NULL)
       
    90     {
       
    91         ShaderBytecode *bytecode = NULL;
       
    92         struct dirent *dent;
       
    93         while ((dent = readdir(dirp)) != NULL)
       
    94         {
       
    95             if (strstr(dent->d_name, ".bytecode") == NULL)
       
    96                 continue;
       
    97 
       
    98             total++;
       
    99             bytecode = (ShaderBytecode *) malloc(sizeof (ShaderBytecode));
       
   100             bytecode->name = (char *) malloc(strlen(dent->d_name) + dirlen);
       
   101             sprintf(bytecode->name, "%s/%s", dname, dent->d_name);
       
   102             bytecode->next = (ShaderBytecode *) gbytecode;
       
   103             gbytecode = bytecode;
       
   104         } // while
       
   105         closedir(dirp);
       
   106     } // if
       
   107 
       
   108     return total;
       
   109 } // do_dir
       
   110 
       
   111 
       
   112 int main(int argc, char **argv)
       
   113 {
       
   114     //printf("MojoShader finderrors\n");
       
   115     //printf("Compiled against version %d\n", MOJOSHADER_VERSION);
       
   116     //printf("Linked against version %d\n", MOJOSHADER_version());
       
   117     //printf("\n");
       
   118 
       
   119     if (argc <= 2)
       
   120         printf("\n\nUSAGE: %s <profile> [dir1] ... [dirN]\n\n", argv[0]);
       
   121     else
       
   122     {
       
   123         #define MAX_WORKERS 4
       
   124         pthread_t workers[MAX_WORKERS];
       
   125         int total = 0;
       
   126 
       
   127         pthread_mutex_init(&grab_mutex, NULL);
       
   128         pthread_mutex_init(&report_mutex, NULL);
       
   129 
       
   130         profile = argv[1];
       
   131         int i;
       
   132 
       
   133         for (i = 2; i < argc; i++)
       
   134             total += do_dir(argv[i]);
       
   135 
       
   136         printf("Saw %d bytecode files.\n", total);
       
   137 
       
   138         for (i = 0; i < MAX_WORKERS; i++)
       
   139             pthread_create(&workers[i], NULL, worker, NULL);
       
   140 
       
   141         for (i = 0; i < MAX_WORKERS; i++)
       
   142             pthread_join(workers[i], NULL);
       
   143 
       
   144         pthread_mutex_destroy(&report_mutex);
       
   145         pthread_mutex_destroy(&grab_mutex);
       
   146     } // else
       
   147 
       
   148     return 0;
       
   149 } // main
       
   150 
       
   151 // end of finderrors.c ...
       
   152