#include #include #include #include #include #include #include "make_path.h" #include "config.h" void remove_suffix(char *base, const char *suffix) { char *ptr = base + strlen(base) - strlen(suffix); if (!strcmp(ptr, suffix)) *ptr = '\0'; } char *MakePath(char *src) { char *dup = strdup(src); if (dup == NULL) { fprintf(stderr, "builder: strdup failed"); exit(1); } remove_suffix(dup, ".c"); char *new_path = mu_make_path("builder", "../bin", dup); if (new_path == NULL) { free(dup); exit(1); } free(dup); return new_path; } void Compile(char *src) { char *path = MakePath(src); size_t len = strlen(CC) + strlen(CFLAGS) + strlen(src) + strlen(path) + 7; char *arg = malloc(len + 1); if (arg == NULL) { free(path); fprintf(stderr, "builder: malloc failed"); exit(1); } snprintf(arg, len, "%s %s %s -o %s", CC, CFLAGS, src, path); system(arg); free(arg); free(path); } void ListAndCompile(const char *dir) { chdir(dir); DIR *dp = opendir("."); if (dp == NULL) { fprintf(stderr, "builder: %s\n", strerror(errno)); exit(1); } struct dirent *ep; while ((ep = readdir(dp)) != NULL) { if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..")) continue; printf("[INFO] Building %s\n", ep->d_name); Compile(ep->d_name); } closedir(dp); chdir(".."); } int main(void) { for (size_t i = 0; i < sizeof(objects) / sizeof(char *); i++) ListAndCompile(objects[i]); return 0; }