summaryrefslogtreecommitdiffstats
path: root/src/ninja.cc
blob: d273bea56422cced0433d770487be4418f221115 (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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "ninja.h"

#include <getopt.h>
#include <limits.h>
#include <stdio.h>

#include "build.h"
#include "build_log.h"
#include "parsers.h"

#include "graphviz.h"

// Import browse.py as binary data.
asm(
".data\n"
"browse_data_begin:\n"
".incbin \"src/browse.py\"\n"
"browse_data_end:\n"
);
// Declare the symbols defined above.
extern const char browse_data_begin[];
extern const char browse_data_end[];

option options[] = {
  { "help", no_argument, NULL, 'h' },
  { }
};

void usage() {
  fprintf(stderr,
"usage: ninja [options] target\n"
"\n"
"options:\n"
"  -g       output graphviz dot file for targets and exit\n"
"  -i FILE  specify input build file [default=build.ninja]\n"
"  -n       dry run (don't run commands but pretend they succeeded)\n"
"  -v       show all command lines\n"
"  -q       show inputs/outputs of target (query mode)\n"
"  -b       browse dependency graph of target in a web browser\n"
          );
}

struct RealFileReader : public ManifestParser::FileReader {
  bool ReadFile(const string& path, string* content, string* err) {
    return ::ReadFile(path, content, err) == 0;
  }
};

int main(int argc, char** argv) {
  BuildConfig config;
  const char* input_file = "build.ninja";
  bool graph = false;
  bool query = false;
  bool browse = false;

  int opt;
  while ((opt = getopt_long(argc, argv, "bghi:nvq", options, NULL)) != -1) {
    switch (opt) {
      case 'g':
        graph = true;
        break;
      case 'i':
        input_file = optarg;
        break;
      case 'n':
        config.dry_run = true;
        break;
      case 'v':
        config.verbosity = BuildConfig::VERBOSE;
        break;
      case 'q':
        query = true;
        break;
      case 'b':
        browse = true;
        break;
      case 'h':
      default:
        usage();
        return 1;
    }
  }
  if (optind >= argc) {
    fprintf(stderr, "expected target to build\n");
    usage();
    return 1;
  }
  argv += optind;
  argc -= optind;

  char cwd[PATH_MAX];
  if (!getcwd(cwd, sizeof(cwd))) {
    perror("getcwd");
    return 1;
  }

  State state;
  RealFileReader file_reader;
  ManifestParser parser(&state, &file_reader);
  string err;
  if (!parser.Load(input_file, &err)) {
    fprintf(stderr, "error loading '%s': %s\n", input_file, err.c_str());
    return 1;
  }

  if (graph) {
    GraphViz graph;
    graph.Start();
    for (int i = 0; i < argc; ++i)
      graph.AddTarget(state.GetNode(argv[i]));
    graph.Finish();
    return 0;
  }

  if (query) {
    for (int i = 0; i < argc; ++i) {
      Node* node = state.GetNode(argv[i]);
      if (node) {
        printf("%s:\n", argv[i]);
        if (node->in_edge_) {
          printf("  input: %s\n", node->in_edge_->rule_->name_.c_str());
          for (vector<Node*>::iterator in = node->in_edge_->inputs_.begin();
               in != node->in_edge_->inputs_.end(); ++in) {
            printf("    %s\n", (*in)->file_->path_.c_str());
          }
        }
        for (vector<Edge*>::iterator edge = node->out_edges_.begin();
             edge != node->out_edges_.end(); ++edge) {
          printf("  output: %s\n", (*edge)->rule_->name_.c_str());
          for (vector<Node*>::iterator out = (*edge)->outputs_.begin();
               out != (*edge)->outputs_.end(); ++out) {
            printf("    %s\n", (*out)->file_->path_.c_str());
          }
        }
      } else {
        printf("%s unknown\n", argv[i]);
      }
    }
    return 0;
  }

  if (browse) {
    // Create a temporary file, dump the Python code into it, and
    // delete the file, keeping our open handle to it.
    char tmpl[] = "browsepy-XXXXXX";
    int fd = mkstemp(tmpl);
    unlink(tmpl);
    const int browse_data_len = browse_data_end - browse_data_begin;
    int len = write(fd, browse_data_begin, browse_data_len);
    if (len < browse_data_len) {
      perror("write");
      return 1;
    }

    // exec Python, telling it to use our script file.
    const char* command[] = {
      "python", "/proc/self/fd/3", argv[0], NULL
    };
    execvp(command[0], (char**)command);

    // If we get here, the exec failed.
    printf("ERROR: Failed to spawn python for graph browsing, aborting.\n");
    return 1;
  }

  const char* kLogPath = ".ninja_log";
  if (!state.build_log_->Load(kLogPath, &err)) {
    fprintf(stderr, "error loading build log: %s\n", err.c_str());
    return 1;
  }

  if (!config.dry_run && !state.build_log_->OpenForWrite(kLogPath, &err)) {
    fprintf(stderr, "error opening build log: %s\n", err.c_str());
    return 1;
  }

  Builder builder(&state, config);
  for (int i = 0; i < argc; ++i) {
    if (!builder.AddTarget(argv[i], &err)) {
      if (!err.empty()) {
        fprintf(stderr, "%s\n", err.c_str());
        return 1;
      } else {
        // Added a target that is already up-to-date; not really
        // an error.
      }
    }
  }

  bool success = builder.Build(&err);
  if (!err.empty()) {
    printf("build stopped: %s.\n", err.c_str());
  }

  return success ? 0 : 1;
}