1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#define _XOPEN_SOURCE 700
#include <libgen.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "maje.h"
static void make_header(FILE *makefile, const char *target, struct majeflag *flags)
{
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, "DESTDIR=/usr/local\n");
fprintf(makefile, "\n");
fprintf(makefile, "all: $(BINDIR)/%s\n\n", target);
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);
}
static void add_object(FILE *makefile, const struct majefile *src, const char *target)
{
char *fullobj = strdup(src->path);
char *obj = basename(fullobj);
obj[strlen(obj) - 1] = 'o';
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);
}
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);
}
make_header(makefile, target, flags);
for (struct majefile *src = sources; src != NULL; src = src->next) {
add_object(makefile, src, target);
}
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);
}
|