aboutsummaryrefslogtreecommitdiffstats
path: root/src/redo.c
blob: 514c2355113df9469cda3b5392e1ffda0d58adf8 (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
/* redo.c
 *
 * Copyright (c) 2014 Tharre
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

#define _XOPEN_SOURCE 600
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <math.h>
#include <limits.h>
#include <unistd.h>

#include "build.h"
#include "util.h"
#include "dbg.h"
#include "filepath.h"


/* Returns the amount of digits a number n has in decimal. */
static inline int digits(unsigned n) {
  return (int) log10(n) + 1;
}

int main(int argc, char *argv[]) {
    /* create the dependency store if it doesn't already exist */
    mkdirp(".redo");
    mkdirp(".redo/deps");

    /* set REDO_ROOT */
    char *cwd = getcwd(NULL, 0);
    if (!cwd)
        fatal("redo: failed to obtain cwd");
    if (setenv("REDO_ROOT", cwd, 0))
        fatal("redo: failed to setenv REDO_ROOT to %s", cwd);
    free(cwd);

    /* set REDO_MAGIC */
    srand(time(NULL));
    char magic_str[digits(UINT_MAX) + 1];
    sprintf(magic_str, "%u", rand());

    debug("magic number: %s\n", magic_str);

    if (setenv("REDO_MAGIC", magic_str, 0))
        fatal("setenv()");

    if (argc < 2) {
        build_target("all");
    } else {
        int i;
        for (i = 1; i < argc; ++i) {
            build_target(argv[i]);
        }
    }
}