aboutsummaryrefslogtreecommitdiff
path: root/make.c
blob: f909caae5a527691b4fc115f7e30d24260fe2701 (plain) (blame)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#if defined(__has_include) && __has_include(<features.h>)
#	include <features.h>
#endif

#ifdef __GLIBC__
#	define _POSIX_C_SOURCE 200809L
#endif

#include "cbs.h"
#include "src/compat.h"

#define CC "cc"
#define WARNINGS \
	"-Wall", "-Wextra", "-Wpedantic", "-Werror", "-Wno-parentheses", \
		"-Wno-pointer-sign", "-Wno-attributes"
#define CFLAGS_ALL WARNINGS, "-pipe", "-std=c2x"
#define CFLAGS_DBG CFLAGS_ALL, "-DGRAB_DEBUG", "-Og", "-g", "-ggdb3"
#ifdef __APPLE__
#	define CFLAGS_RLS CFLAGS_ALL, "-O3"
#else
#	define CFLAGS_RLS CFLAGS_ALL, "-O3", "-march=native", "-mtune=native"
#endif
#define PREFIX "/usr/local"

#define streq(a, b) (!strcmp(a, b))
#define CMDPRC(c) \
	do { \
		int ec; \
		cmdput(c); \
		if ((ec = cmdexec(c)) != EXIT_SUCCESS) \
			diex("%s terminated with exit-code %d", *c._argv, ec); \
		cmdclr(&c); \
	} while (0)

static char *mkoutpath(const char *);

static bool lflag, Pflag, rflag;

int
main(int argc, char **argv)
{
	int opt;
	cmd_t c = {0};
	struct option longopts[] = {
		{"lto",     no_argument, nullptr, 'l'},
		{"no-pcre", no_argument, nullptr, 'P'},
		{"release", no_argument, nullptr, 'r'},
		{nullptr,   0,           nullptr, 0  },
	};

	cbsinit(argc, argv);
	rebuild();

	while ((opt = getopt_long(argc, argv, "lPr", longopts, nullptr)) != -1) {
		switch (opt) {
		case 'l':
			lflag = true;
			break;
		case 'P':
			Pflag = true;
			break;
		case 'r':
			rflag = true;
			break;
		default:
			fputs("Usage: make [-lPd]\n", stderr);
			exit(EXIT_FAILURE);
		}
	}

	argc -= optind;
	argv += optind;

	if (argc > 0) {
		if (streq(*argv, "clean")) {
			cmdadd(&c, "find", ".", "-name", "grab", "-or", "-name", "git-grab",
			       "-or", "-name", "*.[ao]", "-delete");
			CMDPRC(c);
		} else if (streq(*argv, "install")) {
			char *bin, *man;
			bin = mkoutpath("/bin");
			man = mkoutpath("/share/man/man1");
			cmdadd(&c, "mkdir", "-p", bin, man);
			CMDPRC(c);
			cmdadd(&c, "cp", "grab", "git-grab", bin);
			CMDPRC(c);
			cmdadd(&c, "cp", "man/grab.1", "man/git-grab.1", man);
			CMDPRC(c);
		}
	} else {
		cmd_t c = {0};
		struct strv sv = {0};

		if (foutdated("vendor/librune/make", "vendor/librune/make.c")) {
			cmdadd(&c, CC, "-lpthread", "-o", "vendor/librune/make",
			       "vendor/librune/make.c");
			CMDPRC(c);
		}

		if (!fexists("vendor/librune/librune.a")) {
			cmdadd(&c, "vendor/librune/make");
			if (rflag)
				cmdadd(&c, "-r");
			if (lflag)
				cmdadd(&c, "-l");
			CMDPRC(c);
		}

		if (foutdated("./grab", "src/grab.c", "src/compat.h", "src/da.h",
		              "vendor/librune/librune.a"))
		{
			env_or_default(&sv, "CC", CC);
			if (rflag)
				env_or_default(&sv, "CFLAGS", CFLAGS_RLS);
			else
				env_or_default(&sv, "CFLAGS", CFLAGS_DBG);

			for (int i = 0; i < 2; i++) {
				char buf[] = "-DGIT_GRAB=X";
				buf[sizeof(buf) - 2] = i + '0';

				cmdaddv(&c, sv.buf, sv.len);
				if (lflag)
					cmdadd(&c, "-flto");
#ifdef __GLIBC__
				cmdadd(&c, "-D_POSIX_C_SOURCE=200809L");
#endif
				cmdadd(&c, "-Ivendor/librune/include", buf);
				if (!Pflag) {
					struct strv pc = {0};
					cmdadd(&c, "-DGRAB_DO_PCRE=1");
					if (pcquery(&pc, "libpcre2-posix", PKGC_CFLAGS | PKGC_LIBS))
						cmdaddv(&c, pc.buf, pc.len);
					else
						cmdadd(&c, "-lpcre2-posix");
					strvfree(&pc);
				}
				cmdadd(&c, "-o", i == 0 ? "grab" : "git-grab", "src/grab.c",
				       "vendor/librune/librune.a");
				CMDPRC(c);
			}

			strvfree(&sv);
		}
	}

	return EXIT_SUCCESS;
}

char *
mkoutpath(const char *s)
{
	char *p, *buf;

	buf = bufalloc(NULL, PATH_MAX, sizeof(char));
	buf[0] = 0;

	if (p = getenv("DESTDIR"), p && *p) {
		if (strlcat(buf, p, PATH_MAX) >= PATH_MAX)
			goto toolong;
	}

	p = getenv("PREFIX");
	if (strlcat(buf, p && *p ? p : PREFIX, PATH_MAX) >= PATH_MAX)
		goto toolong;
	if (strlcat(buf, s, PATH_MAX) >= PATH_MAX)
		goto toolong;

	return buf;

toolong:
	errno = ENAMETOOLONG;
	die(__func__);
}