summaryrefslogtreecommitdiff
path: root/src/evanix.c
blob: 01279d1ccc0a61dc881d958e16f6113f783e4a41 (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
#include <getopt.h>
#include <stdlib.h>
#include <string.h>

#include "build.h"
#include "evanix.h"
#include "queue.h"
#include "util.h"

static const char usage[] =
	"Usage: evanix [options] expr\n"
	"\n"
	"  -h, --help                      Show help message and quit.\n"
	"  -f, --flake                     Build a flake.\n"
	"  -d, --dry-run                   Show what derivations would be "
	"built.\n"
	"  -s, --system                    System to build for."
	"  -p, --pipelined         <bool>  Use evanix build pipeline.\n"
	"  -c, --close-stderr-exec <bool>  Close stderr on exec.\n"
	"\n";

struct evanix_opts_t evanix_opts = {
	.close_stderr_exec = true,
	.isflake = false,
	.ispipelined = true,
	.isdryrun = false,
	.system = NULL,
};

static int evanix(char *expr);

static int evanix(char *expr)
{
	struct queue_thread *queue_thread = NULL;
	struct build_thread *build_thread = NULL;
	FILE *stream = NULL;
	int ret = 0;

	ret = jobs_init(&stream, expr);
	if (ret < 0)
		goto out_free;

	ret = queue_thread_new(&queue_thread, stream);
	if (ret < 0) {
		free(stream);
		goto out_free;
	}

	ret = build_thread_new(&build_thread, queue_thread->queue);
	if (ret < 0) {
		free(stream);
		goto out_free;
	}

	ret = pthread_create(&queue_thread->tid, NULL, queue_thread_entry,
			     queue_thread);
	if (ret < 0) {
		print_err("%s", strerror(ret));
		goto out_free;
	}

	if (evanix_opts.ispipelined)
		ret = pthread_create(&build_thread->tid, NULL,
				     build_thread_entry, build_thread);
	else
		ret = pthread_join(queue_thread->tid, NULL);
	if (ret < 0) {
		print_err("%s", strerror(ret));
		goto out_free;
	}

	if (evanix_opts.ispipelined)
		ret = pthread_join(queue_thread->tid, NULL);
	else
		ret = pthread_create(&build_thread->tid, NULL,
				     build_thread_entry, build_thread);
	if (ret < 0) {
		print_err("%s", strerror(ret));
		goto out_free;
	}

	ret = pthread_join(build_thread->tid, NULL);
	if (ret < 0) {
		print_err("%s", strerror(ret));
		goto out_free;
	}

out_free:
	queue_thread_free(queue_thread);
	free(build_thread);
	return ret;
}

int main(int argc, char *argv[])
{
	extern int optind, opterr, optopt;
	extern char *optarg;
	int ret, longindex;

	static struct option longopts[] = {
		{"help", no_argument, NULL, 'h'},
		{"flake", no_argument, NULL, 'f'},
		{"dry-run", no_argument, NULL, 'd'},
		{"system", required_argument, NULL, 's'},
		{"pipelined", required_argument, NULL, 'p'},
		{"close-stderr-exec", required_argument, NULL, 'c'},
		{NULL, 0, NULL, 0},
	};

	while ((ret = getopt_long(argc, argv, "", longopts, &longindex)) !=
	       -1) {
		switch (ret) {
		case 'h':
			printf("%s", usage);
			exit(EXIT_SUCCESS);
			break;
		case 'f':
			evanix_opts.isflake = true;
			break;
		case 'd':
			evanix_opts.isdryrun = true;
			break;
		case 's':
			evanix_opts.system = optarg;
			break;
		case 'p':
			ret = atob(optarg);
			if (ret < 0) {
				fprintf(stderr,
					"option --%s requires a bool argument\n"
					"Try 'evanix --help' for more "
					"information.\n",
					longopts[longindex].name);
				exit(EXIT_FAILURE);
			}

			evanix_opts.ispipelined = ret;
			break;
		case 'c':
			ret = atob(optarg);
			if (ret < 0) {
				fprintf(stderr,
					"option --%s requires a bool argument\n"
					"Try 'evanix --help' for more "
					"information.\n",
					longopts[longindex].name);
				exit(EXIT_FAILURE);
			}

			evanix_opts.close_stderr_exec = ret;
			break;
		default:
			fprintf(stderr,
				"Try 'evanix --help' for more information.\n");
			exit(EXIT_FAILURE);
			break;
		}
	}
	if (optind != argc - 1) {
		fprintf(stderr, "evanix: invalid expr operand\n"
				"Try 'evanix --help' for more information.\n");
		exit(EXIT_FAILURE);
	}

	ret = evanix(argv[optind]);
	if (ret < 0)
		exit(EXIT_FAILURE);

	exit(EXIT_SUCCESS);
}