summaryrefslogtreecommitdiff
path: root/make.c
blob: d8ea00eb43573db6fce1a508e24f90450aa9da88 (plain)
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
#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)
{
	fprintf(makefile, ".POSIX:\n\n");
	fprintf(makefile, "# This Makefile was generated by maje\n");
	fprintf(makefile, "# See https://gitlab.com/jkaivo/maje/ for more information\n");
	fprintf(makefile, "# Do not edit this Makefile by hand\n\n");

	fprintf(makefile, "default: all\n\n");

	fprintf(makefile, "CC=c99\n");
	fprintf(makefile, "LD=$(CC)\n");
	fprintf(makefile, "CFLAGS=-Wall -Wextra -Wpedantic -Werror -g\n");
	fprintf(makefile, "LDFLAGS=\n");
	fprintf(makefile, "\n");

	fprintf(makefile, "all: %s\n\n", target);

	fprintf(makefile, "clean:\n");
	fprintf(makefile, "\trm -f %s *.o\n\n", target);
}

static void add_object(FILE *makefile, const char *src, const char *target)
{
	char *fullobj = strdup(src);
	char *obj = basename(fullobj);
	obj[strlen(obj) - 1] = 'o';

	fprintf(makefile, "%s: %s\n", target, obj);
	fprintf(makefile, "%s: %s\n", obj, src);
	fprintf(makefile, "\t$(CC) $(CFLAGS) -c %s\n\n", src);

	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;
	}

	make_header(makefile, target);
	for (struct majefile *src = sources; src != NULL; src = src->next) {
		add_object(makefile, src->path, target);
	}

	fprintf(makefile, "%s:\n", target);
	fprintf(makefile, "\t$(LD) $(LDFLAGS) -o $@ *.o\n");

	fclose(makefile);
}