summaryrefslogtreecommitdiff
path: root/make.c
diff options
context:
space:
mode:
Diffstat (limited to 'make.c')
-rw-r--r--make.c68
1 files changed, 57 insertions, 11 deletions
diff --git a/make.c b/make.c
index 13bc7eb..80c2eba 100644
--- a/make.c
+++ b/make.c
@@ -6,7 +6,7 @@
#include "maje.h"
-static void make_header(FILE *makefile, const char *target, struct majeflag *flags)
+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");
@@ -43,26 +43,43 @@ static void make_header(FILE *makefile, const char *target, struct majeflag *fla
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: $(BINDIR)/%s\n\n", target);
+ 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);
- fprintf(makefile, "\tmkdir -p $(DESTDIR)/bin\n");
- fprintf(makefile, "\tcp $(BINDIR)/%s $(DESTDIR)/bin\n\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)
+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';
- fprintf(makefile, "$(BINDIR)/%s: $(OBJDIR)/%s\n", target, obj);
+ 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);
@@ -87,14 +104,43 @@ void make_makefile(const char *makepath, struct majefile *sources, const char *t
flags = add_flags(src, flags);
}
- make_header(makefile, target, 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) {
+ 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);
+ add_object(makefile, src, target, lib);
}
- fprintf(makefile, "$(BINDIR)/%s:\n", target);
- fprintf(makefile, "\t@mkdir -p $(@D)\n");
- fprintf(makefile, "\t$(LD) $(LDFLAGS) -o $@ $(OBJDIR)/*.o $(LDLIBS)\n");
+ 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);
}