Skip to content
Myr

build_matrix.c

This file

Path

tools/build_matrix.c

137 lines · 4.8 KiB · C

Last changed by

various tests and infrastructure fixesSean Barrettbefore adoption4a7a434

History of this file

Take it

1#define STB_DEFINE2#include "stb.h"34// true if no error5int run_command(char *batch_file, char *command)6{7   char buffer[4096];8   if (batch_file[0]) {9      sprintf(buffer, "%s && %s", batch_file, command);10      return system(buffer) == 0;11   } else {12      return system(command) == 0;13   }14}1516typedef struct17{18   char *compiler_name;19   char *batchfile;20   char *objdir;21   char *compiler;22   char *args;23   char *link;24} compiler_info;2526compiler_info *compilers;27char *shared_args;28char *shared_link;2930typedef struct31{32   char *filelist;33} project_info;3435project_info *projects;3637enum { NONE, IN_COMPILERS, IN_ARGS, IN_PROJECTS, IN_LINK };3839int main(int argc, char **argv)40{41   int state = NONE;42   int i,j,n;43   char **line;44   if (argc != 2) stb_fatal("Usage: stb_build_matrix {build-file}\n");45   line = stb_stringfile(argv[1], &n);46   if (line == 0) stb_fatal("Couldn't open file '%s'\n", argv[1]);4748   for (i=0; i < n; ++i) {49      char *p = stb_trimwhite(line[i]);50      if (p[0] == 0) continue;51      else if (p[0] == '#') continue;52      else if (0 == stricmp(p, "[compilers]")) { state = IN_COMPILERS;                     }53      else if (0 == stricmp(p, "[args]"     )) { state = IN_ARGS     ; shared_args = NULL; }54      else if (0 == stricmp(p, "[projects]" )) { state = IN_PROJECTS ;                     }55      else if (0 == stricmp(p, "[link]"     )) { state = IN_LINK     ; shared_link = NULL; }56      else {57         switch (state) {58            case NONE: stb_fatal("Invalid text outside section at line %d.", i+1);59            case IN_COMPILERS: {60               char buffer[4096];61               int count;62               compiler_info ci;63               char **tokens = stb_tokens_stripwhite(p, ",", &count), *batch;64               if (count > 3) stb_fatal("Expecting name and batch file name at line %d.", i+1);65               batch = (count==1 ? tokens[0] : tokens[1]);66               if (strlen(batch)) 67                  sprintf(buffer, "c:\\%s.bat", batch);68               else69                  strcpy(buffer, "");70               ci.compiler_name = strdup(tokens[0]);71               ci.batchfile     = strdup(buffer);72               ci.compiler      = count==3 ? strdup(tokens[2]) : "cl";73               if (0==strnicmp(batch, "vcvars_", 7))74                  ci.objdir     = strdup(stb_sprintf("vs_%s_%d", batch+7, i));75               else76                  ci.objdir     = strdup(stb_sprintf("%s_%d", batch, i));77               ci.args = shared_args;78               ci.link = shared_link;79               stb_arr_push(compilers, ci);80               break;81            }82            case IN_ARGS: {83               stb_arr_push(shared_args, ' ');84               for (j=0; p[j] != 0; ++j)85                  stb_arr_push(shared_args, p[j]);86               break;87            }88            case IN_LINK: {89               stb_arr_push(shared_link, ' ');90               for (j=0; p[j] != 0; ++j)91                  stb_arr_push(shared_link, p[j]);92               break;93            }94            case IN_PROJECTS: {95               project_info pi;96               pi.filelist = strdup(p);97               stb_arr_push(projects, pi);98               break;99            }100         }101      }102   }103104   _mkdir("obj");105   for (j=0; j < stb_arr_len(compilers); ++j) {106      char command[4096];107      for (i=0; i < stb_arr_len(projects); ++i) {108         int r;109         _mkdir(stb_sprintf("obj/%s", compilers[j].objdir));110         if (stb_suffix(compilers[j].compiler, "cl"))111            sprintf(command, "%s %.*s %s /link %.*s",112                                     compilers[j].compiler,113                                     stb_arr_len(compilers[j].args), compilers[j].args,114                                     projects[i].filelist,115                                     stb_arr_len(compilers[j].link), compilers[j].link);116         else117            sprintf(command, "%s %.*s %s %.*s",118                                     compilers[j].compiler,119                                     stb_arr_len(compilers[j].args), compilers[j].args,120                                     projects[i].filelist,121                                     stb_arr_len(compilers[j].link), compilers[j].link);122         r = run_command(compilers[j].batchfile, command);123         stbprint("{%c== Compiler %s == Building %s}\n", r ? '$' : '!', compilers[j].compiler_name, projects[i].filelist);124         stb_copyfile("a.exe", stb_sprintf("obj/%s/a.exe", compilers[j].objdir));125         //printf("Copy: %s to %s\n", "a.exe", stb_sprintf("obj/%s/a.exe", compilers[j].objdir));126         stb_copyfile("temp.exe", stb_sprintf("obj/%s/temp.exe", compilers[j].objdir));127         system("if EXIST a.exe del /q a.exe");128         system("if EXIST temp.exe del /q temp.exe");129         system("if EXIST *.obj del /q *.obj");130         system("if EXIST *.o del /q *.o");131         if (!r)132            return 1;133      }134   }135136   return 0;137}