~bl4ckb0ne/boson

ref: 1eebe4d73a4484cd26d3b5569bb7737127c12bd0 boson/src/setup.c -rw-r--r-- 1.9 KiB
1eebe4d7Simon Zeni improve build process a month ago
                                                                                
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
#define _XOPEN_SOURCE 700

#include "setup.h"
#include "log.h"
#include "getopt_long.h"
#include "parser.h"
#include "interpreter.h"
#include "ninja.h"

#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <errno.h>

#ifndef PATH_MAX
#define PATH_MAX 4096
#endif

static int
setup_usage(void)
{
	printf("usage: boson setup [options] builddir [sourcedir]\n"
			"options:\n"
			"  -h, --help\t\tDisplay this message and exit\n"
			"\n"
			"builddir\t\tDirectory into which the file will be generated, "
			"required\n"
			"sourcedir\t\tDirectory, optional\n"
			"\tDefault to the current working directory\n");
	return 0;
}

int
setup(int argc, char **argv)
{
	if (argc < 2) {
		fprintf(stderr, "Must specify a build directory\n");
		return 1;
	}

	static const struct option options[] = {
		{"help", no_argument, 0, 'h'},
		{0, 0, 0, 0},
	};

	const char *build_dir = NULL;
	const char *source_dir = NULL;

	optind = 1;
	int opt;
	while ((opt = getopt_long(argc, argv, "-h", options, NULL)) != -1) {
		switch (opt) {
		case 'h':
			return setup_usage();
		case '?':
			return 1;
		case 1:
			if (strcmp(optarg, "setup") == 0) {
				break;
			}
			if (build_dir == NULL) {
				build_dir = optarg;
				break;
			}
			if (source_dir == NULL) {
				source_dir = optarg;
				break;
			}
			break;
		default:
			fprintf(stderr, "%s: unrecognized option: %c\n", argv[0], opt);
			return 1;
		}
	}

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

	char *cwd = calloc(PATH_MAX, sizeof(char));
	getcwd(cwd, PATH_MAX);

	char abs_source_dir[PATH_MAX] = {0}, abs_build_dir[PATH_MAX] = {0};
	realpath(source_dir, abs_source_dir);
	snprintf(abs_build_dir, PATH_MAX, "%s/%s", cwd, build_dir);
	free(cwd);

	info("Version: " VERSION);

	struct ast_root root = parse(abs_source_dir);

	struct context ctx = interpret_ast(&root);

	// TODO free ctx
	return emit_ninja(&ctx, abs_build_dir);
}