#define _XOPEN_SOURCE 700 #include #include #include #include #include #include #include "maje.h" #ifndef PATH_MAX #ifdef _XOPEN_PATH_MAX #define PATH_MAX _XOPEN_PATH_MAX #else #define PATH_MAX _POSIX_PATH_MAX #endif #endif static void make_header(FILE *makefile, const char *target, struct majeflag *flags, int lib) { fprintf(makefile, ".POSIX:\n\n"); fprintf(makefile, "# This Makefile was generated by maje\n"); fprintf(makefile, "# See https://src.kaivo.net/dev/maje/ for more information\n"); fprintf(makefile, "# Do not edit this Makefile by hand\n\n"); fprintf(makefile, "CC=c99\n"); fprintf(makefile, "LD=$(CC)\n"); fprintf(makefile, "CFLAGS=-Wall -Wextra -Wpedantic -Werror -g"); for (struct majeflag *flag = flags; flag != NULL; flag = flag->next) { if (flag->type == MAJE_CFLAG) { fprintf(makefile, " %s", flag->flag); } } fprintf(makefile, "\n"); fprintf(makefile, "LDFLAGS="); for (struct majeflag *flag = flags; flag != NULL; flag = flag->next) { if (flag->type == MAJE_LDFLAG) { fprintf(makefile, " %s", flag->flag); } } fprintf(makefile, "\n"); fprintf(makefile, "LDLIBS="); for (struct majeflag *flag = flags; flag != NULL; flag = flag->next) { if (flag->type == MAJE_LDLIB) { fprintf(makefile, " %s", flag->flag); } } fprintf(makefile, "\n"); fprintf(makefile, "SRCDIR=.\n"); fprintf(makefile, "OBJDIR=.\n"); fprintf(makefile, "BINDIR=$(OBJDIR)\n"); fprintf(makefile, "LIBDIR=$(OBJDIR)\n"); fprintf(makefile, "DESTDIR=/usr/local\n"); fprintf(makefile, "\n"); fprintf(makefile, "all: "); if (lib) { fprintf(makefile, "$(LIBDIR)/lib%s.a $(LIBDIR)/lib%s.so", target, target); } else { fprintf(makefile, "$(BINDIR)/%s", target); } fprintf(makefile, "\n\n"); fprintf(makefile, "clean:\n"); fprintf(makefile, "\trm -f $(BINDIR)/%s $(OBJDIR)/*.o\n\n", target); fprintf(makefile, "install: $(BINDIR)/%s\n", target); if (lib) { fprintf(makefile, "\tmkdir -p $(DESTDIR)/lib\n"); fprintf(makefile, "\tcp $(LIBDIR)/lib%s.{a,so} $(DESTDIR)/lib\n\n", target); } else { fprintf(makefile, "\tmkdir -p $(DESTDIR)/bin\n"); fprintf(makefile, "\tcp $(BINDIR)/%s $(DESTDIR)/bin\n\n", target); } } static void add_object(FILE *makefile, const struct majefile *src, const char *target, int lib) { char *fullobj = strdup(src->path); char *obj = basename(fullobj); obj[strlen(obj) - 1] = 'o'; if (lib) { fprintf(makefile, "$(LIBDIR)/lib%s.a $(LIBDIR)/lib%s.so: $(OBJDIR)/%s\n", target, target, obj); } else { fprintf(makefile, "$(BINDIR)/%s: $(OBJDIR)/%s\n", target, obj); } for (struct majefile *inc = find_includes(src); inc != NULL; inc = inc->next) { fprintf(makefile, "$(OBJDIR)/%s: $(SRCDIR)/%s\n", obj, inc->path); } fprintf(makefile, "$(OBJDIR)/%s: $(SRCDIR)/%s\n", obj, src->path); fprintf(makefile, "\t@mkdir -p $(@D)\n"); fprintf(makefile, "\t$(CC) $(CFLAGS) -o $@ -c $(SRCDIR)/%s\n\n", src->path); free(fullobj); } static char *cwd_basename(void) { static char path[PATH_MAX]; if (getcwd(path, sizeof(path)) == NULL) { return NULL; } return basename(path); } void make_makefile(const char *makepath, struct majefile *sources, const char *target) { FILE *makefile = fopen(makepath, "w"); if (makefile == NULL) { perror("fopen: Makefile"); return; } struct majeflag *flags = NULL; for (struct majefile *src = sources; src != NULL; src = src->next) { flags = add_flags(src, flags); } int lib = 0; if (target == NULL) { lib = 1; for (struct majeflag *flag = flags; flag != NULL; flag = flag->next) { if (flag->type == MAJE_LIB) { if (target) { fprintf(stderr, "maje: only one MAJE_LIB is supported\n"); exit(1); } target = flag->flag; } } } if (lib == 1 && target == NULL) { target = cwd_basename(); if (target == NULL) { fprintf(stderr, "maje: couldn't determine binary or library name\n"); exit(1); } } make_header(makefile, target, flags, lib); for (struct majefile *src = sources; src != NULL; src = src->next) { add_object(makefile, src, target, lib); } if (lib) { fprintf(makefile, "$(LIBDIR)/lib%s.a:\n", target); fprintf(makefile, "\t@mkdir -p $(@D)\n"); fprintf(makefile, "\t$(AR) $(ARFLAGS) $@ $(OBJDIR)/*.o\n\n"); fprintf(makefile, "$(LIBDIR)/lib%s.so:\n", target); fprintf(makefile, "\t@mkdir -p $(@D)\n"); fprintf(makefile, "\t$(LD) -shared $(LDFLAGS) -o $@ $(OBJDIR)/*.o $(LDLIBS)\n\n"); } else { fprintf(makefile, "$(BINDIR)/%s:\n", target); fprintf(makefile, "\t@mkdir -p $(@D)\n"); fprintf(makefile, "\t$(LD) $(LDFLAGS) -o $@ $(OBJDIR)/*.o $(LDLIBS)\n"); } fclose(makefile); }