blob: 61bedf6f6daca588cef32fa5e96a56d786cccaf4 [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// Copyright 2015 Google Inc. All rights reserved
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Fumitoshi Ukai744bb2b2015-06-25 00:10:52 +090015// +build ignore
16
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090017#include <limits.h>
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090018#include <stdio.h>
19#include <string.h>
Shinichiro Hamaji087cecd2015-07-06 19:51:58 +090020#include <stdlib.h>
Shinichiro Hamajia4bb7742015-07-31 11:27:30 +090021#include <time.h>
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090022#include <unistd.h>
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090023
24#include "ast.h"
25#include "dep.h"
26#include "eval.h"
27#include "exec.h"
28#include "file.h"
29#include "file_cache.h"
30#include "fileutil.h"
Shinichiro Hamaji5f57a992015-06-30 19:39:39 +090031#include "find.h"
Shinichiro Hamaji08808d32015-06-26 08:02:45 +090032#include "flags.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090033#include "func.h"
34#include "log.h"
Shinichiro Hamaji5f86e1a2015-06-29 14:25:39 +090035#include "ninja.h"
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090036#include "parser.h"
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090037#include "stats.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090038#include "string_piece.h"
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090039#include "stringprintf.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090040#include "strutil.h"
Shinichiro Hamajie7992752015-06-29 18:38:35 +090041#include "symtab.h"
Shinichiro Hamajicbd34cd2015-07-05 02:53:04 +090042#include "timeutil.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090043#include "var.h"
44
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090045static void Init() {
46 InitSymtab();
47 InitFuncTable();
48 InitDepNodePool();
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090049 InitParser();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090050
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +090051 if (g_flags.makefile == NULL) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090052 if (Exists("GNUmakefile")) {
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +090053 g_flags.makefile = "GNUmakefile";
Shinichiro Hamaji03e03af2015-07-13 20:43:39 -070054#if !defined(__APPLE__)
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090055 } else if (Exists("makefile")) {
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +090056 g_flags.makefile = "makefile";
Shinichiro Hamaji03e03af2015-07-13 20:43:39 -070057#endif
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090058 } else if (Exists("Makefile")) {
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +090059 g_flags.makefile = "Makefile";
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090060 }
61 }
62}
63
64static void Quit() {
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090065 ReportAllStats();
66
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090067 QuitParser();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090068 QuitDepNodePool();
69 QuitFuncTable();
70 QuitSymtab();
71}
72
Shinichiro Hamajie7992752015-06-29 18:38:35 +090073static void ReadBootstrapMakefile(const vector<Symbol>& targets,
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090074 vector<AST*>* asts) {
75 string bootstrap = (
Shinichiro Hamaji91d12492015-09-04 09:54:48 +090076 "CC?=cc\n"
Shinichiro Hamajid749b932015-07-13 20:51:15 -070077#if defined(__APPLE__)
Shinichiro Hamaji91d12492015-09-04 09:54:48 +090078 "CXX?=c++\n"
Shinichiro Hamajid749b932015-07-13 20:51:15 -070079#else
Shinichiro Hamaji91d12492015-09-04 09:54:48 +090080 "CXX?=g++\n"
Shinichiro Hamajid749b932015-07-13 20:51:15 -070081#endif
Shinichiro Hamaji91d12492015-09-04 09:54:48 +090082 "AR?=ar\n"
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090083 // Pretend to be GNU make 3.81, for compatibility.
Shinichiro Hamaji91d12492015-09-04 09:54:48 +090084 "MAKE_VERSION?=3.81\n"
Shinichiro Hamajicdcd4192015-09-04 10:06:30 +090085 "KATI?=ckati\n"
Shinichiro Hamaji91d12492015-09-04 09:54:48 +090086 // Overwrite $SHELL environment variable.
87 "SHELL=/bin/sh\n"
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090088 // TODO: Add more builtin vars.
89
90 // http://www.gnu.org/software/make/manual/make.html#Catalogue-of-Rules
91 // The document above is actually not correct. See default.c:
92 // http://git.savannah.gnu.org/cgit/make.git/tree/default.c?id=4.1
93 ".c.o:\n"
94 "\t$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<\n"
95 ".cc.o:\n"
96 "\t$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<\n"
97 // TODO: Add more builtin rules.
98 );
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +090099 if (g_flags.generate_ninja) {
Shinichiro Hamajib6a04542015-09-09 17:37:41 +0900100 bootstrap += StringPrintf("MAKE?=make -j%d\n",
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900101 g_flags.num_jobs < 1 ? 1 : g_flags.num_jobs / 2);
Shinichiro Hamajib6a04542015-09-09 17:37:41 +0900102 } else {
103 bootstrap += StringPrintf("MAKE?=%s\n",
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900104 JoinStrings(g_flags.subkati_args, " ").c_str());
Shinichiro Hamajib6a04542015-09-09 17:37:41 +0900105 }
Shinichiro Hamajicdcd4192015-09-04 10:06:30 +0900106 bootstrap += StringPrintf("MAKECMDGOALS?=%s\n",
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900107 JoinSymbols(targets, " ").c_str());
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +0900108
109 char cwd[PATH_MAX];
110 if (!getcwd(cwd, PATH_MAX)) {
111 fprintf(stderr, "getcwd failed\n");
112 CHECK(false);
113 }
114 bootstrap += StringPrintf("CURDIR:=%s\n", cwd);
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900115 Parse(Intern(bootstrap).str(), Loc("*bootstrap*", 0), asts);
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +0900116}
117
Shinichiro Hamajif62e9a72015-06-26 04:18:21 +0900118static void SetVar(StringPiece l, VarOrigin origin, Vars* vars) {
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +0900119 size_t found = l.find('=');
120 CHECK(found != string::npos);
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900121 Symbol lhs = Intern(l.substr(0, found));
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +0900122 StringPiece rhs = l.substr(found + 1);
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900123 vars->Assign(lhs,
124 new RecursiveVar(NewLiteral(rhs.data()), origin, rhs.data()));
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +0900125}
126
127extern "C" char** environ;
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +0900128
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900129static int Run(const vector<Symbol>& targets,
Shinichiro Hamaji998ccf72015-07-18 12:37:02 +0900130 const vector<StringPiece>& cl_vars,
131 const string& orig_args) {
Shinichiro Hamaji90db5412015-08-13 15:03:52 +0900132 double start_time = GetTime();
133
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900134 if (g_flags.generate_ninja && (g_flags.regen || g_flags.dump_kati_stamp)) {
Shinichiro Hamajif9e15242015-07-31 12:50:06 +0900135 ScopedTimeReporter tr("regen check time");
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900136 if (!NeedsRegen(g_flags.ninja_suffix, g_flags.ninja_dir,
137 g_flags.regen_ignoring_kati_binary,
138 g_flags.dump_kati_stamp,
Dan Willemsen87417412015-08-24 17:57:12 -0700139 start_time, orig_args)) {
Shinichiro Hamajif6df3342015-08-11 14:59:36 +0900140 printf("No need to regenerate ninja file\n");
141 return 0;
142 }
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900143 if (g_flags.dump_kati_stamp) {
Shinichiro Hamajif6df3342015-08-11 14:59:36 +0900144 printf("Need to regenerate ninja file\n");
Shinichiro Hamajia4bb7742015-07-31 11:27:30 +0900145 return 0;
146 }
Shinichiro Hamajia09ed282015-07-31 12:21:54 +0900147 ClearGlobCache();
Shinichiro Hamajia4bb7742015-07-31 11:27:30 +0900148 }
149
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900150 MakefileCacheManager* cache_mgr = NewMakefileCacheManager();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900151
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900152 Vars* vars = new Vars();
Shinichiro Hamaji91d12492015-09-04 09:54:48 +0900153 for (char** p = environ; *p; p++) {
154 SetVar(*p, VarOrigin::ENVIRONMENT, vars);
155 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900156 Evaluator* ev = new Evaluator(vars);
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +0900157
158 vector<AST*> bootstrap_asts;
159 ReadBootstrapMakefile(targets, &bootstrap_asts);
160 ev->set_is_bootstrap(true);
161 for (AST* ast : bootstrap_asts) {
162 LOG("%s", ast->DebugString().c_str());
163 ast->Eval(ev);
164 }
165 ev->set_is_bootstrap(false);
166
Shinichiro Hamaji91d12492015-09-04 09:54:48 +0900167 for (StringPiece l : cl_vars) {
168 SetVar(l, VarOrigin::COMMAND_LINE, ev->mutable_vars());
169 }
170
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900171 vars->Assign(Intern("MAKEFILE_LIST"),
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900172 new SimpleVar(StringPrintf(" %s", g_flags.makefile),
Shinichiro Hamaji5081c712015-08-14 16:49:20 +0900173 VarOrigin::FILE));
Shinichiro Hamajib74b8902015-06-22 18:22:30 +0900174
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +0900175 {
176 ScopedTimeReporter tr("eval time");
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900177 Makefile* mk = cache_mgr->ReadMakefile(g_flags.makefile);
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +0900178 for (AST* ast : mk->asts()) {
179 LOG("%s", ast->DebugString().c_str());
180 ast->Eval(ev);
181 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900182 }
183
Shinichiro Hamajia06bee92015-08-05 17:13:05 +0900184 for (ParseErrorAST* err : GetParseErrors()) {
185 WARN("%s:%d: warning for parse error in an unevaluated line: %s",
186 LOCF(err->loc()), err->msg.c_str());
187 }
188
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900189 vector<DepNode*> nodes;
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +0900190 {
191 ScopedTimeReporter tr("make dep time");
192 MakeDep(ev, ev->rules(), ev->rule_vars(), targets, &nodes);
193 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900194
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900195 if (g_flags.is_syntax_check_only)
Shinichiro Hamajied883ef2015-07-31 13:15:04 +0900196 return 0;
197
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900198 if (g_flags.generate_ninja) {
Shinichiro Hamajied883ef2015-07-31 13:15:04 +0900199 ScopedTimeReporter tr("generate ninja time");
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900200 GenerateNinja(g_flags.ninja_suffix, g_flags.ninja_dir,
201 nodes, ev, !targets.empty(),
Shinichiro Hamajied883ef2015-07-31 13:15:04 +0900202 orig_args, start_time);
203 return 0;
204 }
205
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900206 for (const auto& p : ev->exports()) {
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900207 const Symbol name = p.first;
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900208 if (p.second) {
209 Var* v = ev->LookupVar(name);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900210 const string&& value = v->Eval(ev);
211 LOG("setenv(%s, %s)", name.c_str(), value.c_str());
212 setenv(name.c_str(), value.c_str(), 1);
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900213 } else {
214 LOG("unsetenv(%s)", name.c_str());
215 unsetenv(name.c_str());
216 }
217 }
218
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +0900219 {
220 ScopedTimeReporter tr("exec time");
221 Exec(nodes, ev);
222 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900223
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +0900224 for (AST* ast : bootstrap_asts)
225 delete ast;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900226 delete ev;
227 delete vars;
228 delete cache_mgr;
229
Shinichiro Hamaji64cd8062015-06-27 16:58:36 +0900230 return 0;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900231}
232
233int main(int argc, char* argv[]) {
234 Init();
Shinichiro Hamaji998ccf72015-07-18 12:37:02 +0900235 string orig_args;
236 for (int i = 0; i < argc; i++) {
237 if (i)
238 orig_args += ' ';
239 orig_args += argv[i];
240 }
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900241 g_flags.Parse(argc, argv);
242 if (g_flags.makefile == NULL)
Shinichiro Hamajie4ee8ea2015-08-15 01:22:23 +0900243 ERROR("*** No targets specified and no makefile found.");
Shinichiro Hamaji5f57a992015-06-30 19:39:39 +0900244 // This depends on command line flags.
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900245 if (g_flags.use_find_emulator)
Shinichiro Hamaji4e950e62015-07-02 02:59:55 +0900246 InitFindEmulator();
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900247 int r = Run(g_flags.targets, g_flags.cl_vars, orig_args);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900248 Quit();
249 return r;
250}