summaryrefslogtreecommitdiff
path: root/maje.c
blob: 99f3f04d44f65b5f1ca986b0e36508904e64bd0b (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
61
62
63
64
65
66
67
#define _XOPEN_SOURCE 700
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "maje.h"

static void usage(char *progname)
{
	printf("usage: %s [-n] [dir]\n", progname);
}

int main(int argc, char *argv[])
{
	int noexec = 0;
	char *srcdir = NULL;

	int c;
	while ((c = getopt(argc, argv, "n")) != -1) {
		switch (c) {
		case 'n':
			noexec = 1;
			break;

		default:
			usage(argv[0]);
			return 1;
		}
	}

	if (optind < argc - 1) {
		fprintf(stderr, "maje: extra operands\n");
		return 1;
	}

	srcdir = argv[optind];

	if (srcdir == NULL) {
		srcdir = ".";
	}

	struct majefile *sources = find_source_files(srcdir);
	char *mainname = find_main(sources);
	if (!mainname) {
		printf("libraries not yet supported\n");
		return 0;
	}
	char *target = strdup(mainname);
	target[strlen(target) - 2] = '\0';
	target = basename(target);

	FILE *makefile = fopen("Makefile", "w");
	if (makefile == NULL) {
		perror("fopen");
		return 1;
	}

	make_makefile("Makefile", sources, target);

	if (noexec) {
		return 0;
	}

	char *args[] = { "make", NULL };
	execvp(args[0], args);
}