#include #include #include #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(const char *src, const char *output_dir) { 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", output_dir, dup); if (new_path == NULL) { free(dup); exit(1); } free(dup); return new_path; } void Compile(const char *src, const char *output_dir) { char *path = MakePath(src, output_dir); printf("[CC] Building %s -> %s\n", src, path); pid_t pid; if ((pid = fork()) == 0) { if (pid == 1) exit(1); execlp(CC, CC, CFLAGS, src, "-o", path, NULL); // kill(getppid(), 9); // Probably doesn't need it as the child is resulted with exit(1) /* If compiler return 1 */ exit(1); } else if (pid == -1) { fprintf(stderr, "%s", "builder: fork failed"); exit(1); } free(path); int status = 0; waitpid(pid, &status, 0); if (status) exit(1); } void ListAndCompile(const char *dir, const char *output_dir) { if (chdir(dir) < 0) { fprintf(stderr, "builder: %s: %s\n", dir, strerror(errno)); exit(1); } printf("[CHDIR] %s\n", dir); DIR *dp = opendir("."); struct dirent *ep; while ((ep = readdir(dp)) != NULL) { if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..")) continue; Compile(ep->d_name, output_dir); } closedir(dp); chdir(".."); } int main(void) { /* for (size_t i = 0; i < sizeof(libs) / sizeof(char *); i++) ListAndCompile(objects[i], "../obj"); */ for (size_t i = 0; i < sizeof(objects) / sizeof(char *); i++) ListAndCompile(objects[i], "../bin"); return 0; }