2023-11-04 22:06:27 +03:00
|
|
|
#include <stdio.h>
|
2023-11-05 13:46:39 +03:00
|
|
|
#include <errno.h>
|
2023-11-04 22:06:27 +03:00
|
|
|
#include <dirent.h>
|
2023-11-05 13:46:39 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "make_path.h"
|
2023-11-04 22:06:27 +03:00
|
|
|
#include "config.h"
|
|
|
|
|
2023-11-05 13:46:39 +03:00
|
|
|
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("..");
|
|
|
|
}
|
|
|
|
|
2023-11-04 22:06:27 +03:00
|
|
|
int main(void) {
|
2023-11-05 13:46:39 +03:00
|
|
|
for (size_t i = 0; i < sizeof(objects) / sizeof(char *); i++)
|
|
|
|
ListAndCompile(objects[i]);
|
|
|
|
|
2023-11-04 22:06:27 +03:00
|
|
|
return 0;
|
|
|
|
}
|