build system work

This commit is contained in:
Your Name 2023-11-05 13:46:39 +03:00
parent e7a813d834
commit 34778ac3d1
5 changed files with 76 additions and 4 deletions

View File

@ -1,6 +1,6 @@
# micro-utils
[Compile]
cc builder.c -obuilder
cc builder.c -Ilibmu -obuilder
./builder
[or]

View File

@ -1,3 +1,3 @@
#!/bin/sh
cc builder.c -Wall -Wextra -Os -s -pedantic -obuilder
cc builder.c -Wall -Wextra -Os -s -pedantic -Ilibmu -obuilder
./builder && rm builder

View File

@ -1,7 +1,79 @@
#include <stdio.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#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;
}

View File

@ -9,6 +9,6 @@ const char *objects[] = {
"shell"
};
#define CFLAGS "-Wall -Wextra -pedantic -Os -s"
#define CFLAGS "-Wall -Wextra -pedantic -Os -s -I ../libmu"
#define CC "cc"
#endif

View File

@ -59,7 +59,7 @@ int main(const int argc, const char **argv) {
else if (!strcmp(argv[i], "-z"))
z_flag = 1;
else if (!strncmp(argv[i], "-n=", 3)
else if (!strncmp(argv[i], "-n=", 3))
n_loops = atoi(argv[i] + 3);
else if (!strcmp(argv[i], "--help")) {