diff --git a/minimake/Makefile b/Makefile similarity index 100% rename from minimake/Makefile rename to Makefile diff --git a/README.md b/README.md new file mode 100644 index 0000000..7cc185b --- /dev/null +++ b/README.md @@ -0,0 +1,73 @@ +# Minimake + +Minimake is a small project written in about a week in C99. Its goal was to reproduce the main features of the well known GNU Make utility. + +> **Note** This is a school project, therefore it probably won't interest you if you are looking for something useful. + +## Build + +```sh +make +``` +or even better +```sh +minimake +``` + +## How it works + +If you're not familiar with Make, what it does is that it reads a file named +`Makefile` in the current directory which contains instructions to build a +project in the form of recipes. It includes support for variables, dependencies +, implicit rules and more. + +Then depending on the user input it automatically executes a recipe with its dependencies. + + +Here is what a basic one can look like +```make +BENCH_FLAGS = --all + +test_and_bench: + bash ./runtests.sh + bash ./runbenchs.sh $(BENCH_FLAGS) +``` + +But it will more realisticly look like that + +```make +CC = gcc +CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla +LDFLAGS= + +DBG_CFLAGS = -fsanitize=address -g +DBG_LDFLAGS= -fsanitize=address + + +SRC_DIR = src +LIB_SRCS = lines/lines.c hash_maps/hash_maps.c lists/lists.c files/files.c +MAIN_SRCS = main.c minimake.c + +SRCS = $(MAIN_SRCS:%=$(SRC_DIR)/%) $(LIB_SRCS:%=$(SRC_DIR)/%) +OBJS = $(SRCS:.c=.o) + +TARGET= minimake +DBG_TARGET = minimake-dbg + +$(TARGET): $(OBJS) + $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS) + @echo $(OBJS) + +debug: CFLAGS += $(DBG_CFLAGS) +debug: LDFLAGS += $(DBG_LDFLAGS) +debug: $(OBJS) + $(CC) -o $(DBG_TARGET) $^ $(LDFLAGS) $(LDLIBS) + +check: + dash ./tests/run.sh + +clean: + $(RM) $(TARGET) + $(RM) $(OBJS) + +``` diff --git a/micromake/src/Microfile b/micromake/src/Microfile deleted file mode 100644 index 607b02a..0000000 --- a/micromake/src/Microfile +++ /dev/null @@ -1,3 +0,0 @@ -qwertyu: -cbejw: a bc d -dmwq :d wdwd diff --git a/micromake/src/micromake.c b/micromake/src/micromake.c deleted file mode 100644 index fda3fe2..0000000 --- a/micromake/src/micromake.c +++ /dev/null @@ -1,119 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#define BUFFER_SIZE 1024 -#define STRING_BUFFER_SIZE 32 - -#include -#include -#include -#include - -// Helps to match a string (excludes blanks and special characters) -static int is_char(char c) -{ - return c != '\0' && !isblank(c) && c != ':' && c != '=' && c != '#'; -} - -// Returns the next word from buf until buf_len, -// and the numbers of read characters in *read_chars -// WARNING allocates the result on the heap -static char *readWord(char *buf, size_t buf_len, size_t *read_chars) -{ - size_t i = 0; - size_t str_buf_size = STRING_BUFFER_SIZE; - char *str_buf = malloc(sizeof(char) * str_buf_size); - while (i < buf_len && is_char(buf[i])) - { - // Reallocate more space if necessary - if (i >= str_buf_size - 1) - { - str_buf_size += STRING_BUFFER_SIZE; - str_buf = realloc(str_buf, str_buf_size); - if (str_buf == NULL) - errx(2, "Could not realloc"); - } - - str_buf[i] = buf[i]; - i++; - } - - str_buf[i] = '\0'; - *read_chars = i; - - return str_buf; -} - -int skip_blanks(char *buf, size_t buf_len) -{ - size_t i = 0; - while (i < buf_len && isblank(buf[i])) - i++; - return i; -} - -int main(int argc, char **argv) -{ - if (argc < 2) - errx(2, "Not enough arguments"); - if (argc > 2) - errx(2, "Not enough arguments"); - - // Open file - FILE *stream = fopen(argv[1], "r"); - if (stream == 0) - errx(2, "Could not open file"); - - // Allocate buffer - size_t buf_size = BUFFER_SIZE; - char *buf = malloc(sizeof(char) * buf_size); - if (buf == NULL) - errx(2, "Could not allocate more memory"); - - // Read - ssize_t nread; - while ((nread = getline(&buf, &buf_size, stream)) != -1) - { - size_t u_nread = nread; - size_t i = 0; - // Skip blanks - i += skip_blanks(buf + i, nread - i); - - // Read target name - size_t skipped_chars = 0; - char *rule_name = readWord(buf + i, nread - i, &skipped_chars); - if (skipped_chars != 0) - printf("%s:", rule_name); - i += skipped_chars; - - // Skip until ':' - i += skip_blanks(buf + i, nread - i); - if (buf[i] != ':') - errx(2, "Unexpected character '%c' after rule declaration '%s'", - buf[i], rule_name); - i++; - - // Read deps - while (i < u_nread) - { - // Skip blanks - i += skip_blanks(buf + i, nread - i); - - // Read word - size_t skipped_chars = 0; - char *dep_name = readWord(buf + i, nread - i, &skipped_chars); - if (skipped_chars != 0) - printf(" %s", dep_name); - free(dep_name); - i += skipped_chars; - - if (!is_char(buf[i]) && !isblank(buf[i]) && buf[i] != '\0') - errx(2, "Unexpected character '%c'", buf[i]); - } - - i++; - free(rule_name); - } - - free(buf); - fclose(stream); - return 0; -} diff --git a/microshell/microshell.c b/microshell/microshell.c deleted file mode 100644 index 4a7b661..0000000 --- a/microshell/microshell.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include -#include -#include - -int main(int argc, char **argv) -{ - if (argc < 2) - err(1, "Not enough args"); - - int id = fork(); - if (id < 0) - err(1, "Cannot fork"); - - if (id == 0) - { - int res = 0; - wait(&res); - // printf("%d\n", res); - return res; - } - else - { - int res = execl("/bin/sh", "supershell", "-c", argv[1], NULL); - printf("process exit status: %d\n", res); - // exit(res); - return res; - } -} diff --git a/simple_ls/simple_ls.c b/simple_ls/simple_ls.c deleted file mode 100644 index 508c5df..0000000 --- a/simple_ls/simple_ls.c +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include - -static void simple_ls(char *path) -{ - if (path == NULL) - errx(1, "Internal error: Passed NULL path"); - - DIR *dir = opendir(path); - if (dir == NULL) - errx(1, "Internal error: cannot open directory"); - - struct dirent *element; - while ((element = readdir(dir))) - { - puts(element->d_name); - } - - int res = closedir(dir); - if (res == -1) - errx(1, "Could not close dir"); -} - -int main(int argc, char **argv) -{ - if (argc < 2) - simple_ls("."); - else - { - for (int i = 1; i < argc; i++) - { - simple_ls(argv[i]); - } - } - return 0; -} diff --git a/minimake/src/files/files.c b/src/files/files.c similarity index 100% rename from minimake/src/files/files.c rename to src/files/files.c diff --git a/minimake/src/files/files.h b/src/files/files.h similarity index 100% rename from minimake/src/files/files.h rename to src/files/files.h diff --git a/minimake/src/hash_maps/hash_maps.c b/src/hash_maps/hash_maps.c similarity index 100% rename from minimake/src/hash_maps/hash_maps.c rename to src/hash_maps/hash_maps.c diff --git a/minimake/src/hash_maps/hash_maps.h b/src/hash_maps/hash_maps.h similarity index 100% rename from minimake/src/hash_maps/hash_maps.h rename to src/hash_maps/hash_maps.h diff --git a/minimake/src/lines/lines.c b/src/lines/lines.c similarity index 100% rename from minimake/src/lines/lines.c rename to src/lines/lines.c diff --git a/minimake/src/lines/lines.h b/src/lines/lines.h similarity index 100% rename from minimake/src/lines/lines.h rename to src/lines/lines.h diff --git a/minimake/src/lists/lists.c b/src/lists/lists.c similarity index 100% rename from minimake/src/lists/lists.c rename to src/lists/lists.c diff --git a/minimake/src/lists/lists.h b/src/lists/lists.h similarity index 100% rename from minimake/src/lists/lists.h rename to src/lists/lists.h diff --git a/minimake/src/main.c b/src/main.c similarity index 100% rename from minimake/src/main.c rename to src/main.c diff --git a/minimake/src/minimake.c b/src/minimake.c similarity index 100% rename from minimake/src/minimake.c rename to src/minimake.c diff --git a/minimake/src/minimake.h b/src/minimake.h similarity index 100% rename from minimake/src/minimake.h rename to src/minimake.h diff --git a/minimake/tests/Makefile.empty b/tests/Makefile.empty similarity index 100% rename from minimake/tests/Makefile.empty rename to tests/Makefile.empty diff --git a/minimake/tests/Makefile.syntax-test b/tests/Makefile.syntax-test similarity index 100% rename from minimake/tests/Makefile.syntax-test rename to tests/Makefile.syntax-test diff --git a/minimake/tests/Makefile.test3 b/tests/Makefile.test3 similarity index 100% rename from minimake/tests/Makefile.test3 rename to tests/Makefile.test3 diff --git a/minimake/tests/Makefile.test4 b/tests/Makefile.test4 similarity index 100% rename from minimake/tests/Makefile.test4 rename to tests/Makefile.test4 diff --git a/minimake/tests/Makefile.vars b/tests/Makefile.vars similarity index 100% rename from minimake/tests/Makefile.vars rename to tests/Makefile.vars diff --git a/minimake/tests/Makefile2.syntax-test b/tests/Makefile2.syntax-test similarity index 100% rename from minimake/tests/Makefile2.syntax-test rename to tests/Makefile2.syntax-test diff --git a/minimake/tests/run.sh b/tests/run.sh similarity index 100% rename from minimake/tests/run.sh rename to tests/run.sh