aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/cli.c
blob: c5649f0c936333aa26a1c3bff428aebd0b67e249 (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
68
69
70
71
72
73
74
75
76
77
78
#include "cli.h"
#include "common.h"
#include "version.h"

static void print_version() {
    printf("ducc v%s\n", DUCC_VERSION);
}

CliArgs* parse_cli_args(int argc, char** argv) {
    const char* output_filename = NULL;
    int positional_arguments_start = -1;
    bool only_compile = false;
    bool generate_deps = false;

    for (int i = 1; i < argc; ++i) {
        if (argv[i][0] != '-') {
            positional_arguments_start = i;
            break;
        }
        char c = argv[i][1];
        if (c == 'g') {
            // ignore
        } else if (c == 'O') {
            // ignore
        } else if (c == 'W') {
            // ignore
        } else if (c == 'M' && argv[i][2] == '\0') {
            // ignore -M
        } else if (c == 'o') {
            if (argc <= i + 1) {
                fatal_error("-o requires filename");
            }
            output_filename = argv[i + 1];
            ++i;
        } else if (c == 'c') {
            only_compile = true;
        } else if (strcmp(argv[i], "-MMD") == 0) {
            generate_deps = true;
        } else if (strcmp(argv[i], "--version") == 0) {
            print_version();
            exit(0);
        } else if (strcmp(argv[i], "--std=c23") == 0) {
            // ignore --std=c23
        } else {
            fatal_error("unknown option: %s", argv[i]);
        }
    }
    if (positional_arguments_start == -1) {
        fatal_error("usage: ducc <file>");
    }

    CliArgs* a = calloc(1, sizeof(CliArgs));
    a->input_filename = argv[positional_arguments_start];
    a->output_filename = output_filename;
    a->output_assembly = !output_filename || str_ends_with(output_filename, ".s");
    a->only_compile = only_compile;
    a->totally_deligate_to_gcc = false;
    a->gcc_command = NULL;
    a->generate_deps = generate_deps;

    if (!a->only_compile && str_ends_with(a->input_filename, ".o")) {
        a->totally_deligate_to_gcc = true;
        StrBuilder builder;
        strbuilder_init(&builder);
        strbuilder_append_string(&builder, "gcc ");
        for (int i = 1; i < argc; ++i) {
            strbuilder_append_char(&builder, '\'');
            strbuilder_append_string(&builder, argv[i]);
            strbuilder_append_char(&builder, '\'');
            if (i != argc - 1) {
                strbuilder_append_char(&builder, ' ');
            }
        }
        a->gcc_command = builder.buf;
    }

    return a;
}