blob: 2e4581090823fe62b7dadb9dc35da73013814ca9 [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
Shinichiro Hamaji1a444a82016-02-16 13:49:49 +090024#include "affinity.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090025#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 Hamaji8ef0ce52016-01-19 18:07:43 +090037#include "regen.h"
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090038#include "stats.h"
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090039#include "stmt.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090040#include "string_piece.h"
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090041#include "stringprintf.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090042#include "strutil.h"
Shinichiro Hamajie7992752015-06-29 18:38:35 +090043#include "symtab.h"
Shinichiro Hamajicbd34cd2015-07-05 02:53:04 +090044#include "timeutil.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090045#include "var.h"
46
Dan Willemsen834c6db2017-08-15 14:44:32 -070047// We know that there are leaks in Kati. Turn off LeakSanitizer by default.
48extern "C" const char* __asan_default_options() {
49 return "detect_leaks=0";
50}
51
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090052static void Init() {
53 InitSymtab();
54 InitFuncTable();
55 InitDepNodePool();
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090056 InitParser();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090057}
58
59static void Quit() {
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090060 ReportAllStats();
61
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090062 QuitParser();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090063 QuitDepNodePool();
64 QuitFuncTable();
65 QuitSymtab();
66}
67
Shinichiro Hamajie7992752015-06-29 18:38:35 +090068static void ReadBootstrapMakefile(const vector<Symbol>& targets,
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090069 vector<Stmt*>* stmts) {
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090070 string bootstrap = (
Shinichiro Hamaji91d12492015-09-04 09:54:48 +090071 "CC?=cc\n"
Shinichiro Hamajid749b932015-07-13 20:51:15 -070072#if defined(__APPLE__)
Shinichiro Hamaji91d12492015-09-04 09:54:48 +090073 "CXX?=c++\n"
Shinichiro Hamajid749b932015-07-13 20:51:15 -070074#else
Shinichiro Hamaji91d12492015-09-04 09:54:48 +090075 "CXX?=g++\n"
Shinichiro Hamajid749b932015-07-13 20:51:15 -070076#endif
Shinichiro Hamaji91d12492015-09-04 09:54:48 +090077 "AR?=ar\n"
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090078 // Pretend to be GNU make 3.81, for compatibility.
Shinichiro Hamaji91d12492015-09-04 09:54:48 +090079 "MAKE_VERSION?=3.81\n"
Shinichiro Hamajicdcd4192015-09-04 10:06:30 +090080 "KATI?=ckati\n"
Shinichiro Hamaji91d12492015-09-04 09:54:48 +090081 // Overwrite $SHELL environment variable.
82 "SHELL=/bin/sh\n"
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090083 // TODO: Add more builtin vars.
84
85 // http://www.gnu.org/software/make/manual/make.html#Catalogue-of-Rules
86 // The document above is actually not correct. See default.c:
87 // http://git.savannah.gnu.org/cgit/make.git/tree/default.c?id=4.1
88 ".c.o:\n"
89 "\t$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<\n"
90 ".cc.o:\n"
91 "\t$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<\n"
92 // TODO: Add more builtin rules.
93 );
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +090094 if (g_flags.generate_ninja) {
Shinichiro Hamajib6a04542015-09-09 17:37:41 +090095 bootstrap += StringPrintf("MAKE?=make -j%d\n",
Shinichiro Hamaji9dce4442015-12-05 11:05:00 +090096 g_flags.num_jobs <= 1 ? 1 : g_flags.num_jobs / 2);
Shinichiro Hamajib6a04542015-09-09 17:37:41 +090097 } else {
98 bootstrap += StringPrintf("MAKE?=%s\n",
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +090099 JoinStrings(g_flags.subkati_args, " ").c_str());
Shinichiro Hamajib6a04542015-09-09 17:37:41 +0900100 }
Shinichiro Hamajicdcd4192015-09-04 10:06:30 +0900101 bootstrap += StringPrintf("MAKECMDGOALS?=%s\n",
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900102 JoinSymbols(targets, " ").c_str());
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +0900103
104 char cwd[PATH_MAX];
105 if (!getcwd(cwd, PATH_MAX)) {
106 fprintf(stderr, "getcwd failed\n");
107 CHECK(false);
108 }
109 bootstrap += StringPrintf("CURDIR:=%s\n", cwd);
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900110 Parse(Intern(bootstrap).str(), Loc("*bootstrap*", 0), stmts);
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +0900111}
112
Shinichiro Hamajic9b9e5e2016-02-18 18:18:54 +0900113static void SetVar(StringPiece l, VarOrigin origin) {
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +0900114 size_t found = l.find('=');
115 CHECK(found != string::npos);
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900116 Symbol lhs = Intern(l.substr(0, found));
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +0900117 StringPiece rhs = l.substr(found + 1);
Shinichiro Hamajic9b9e5e2016-02-18 18:18:54 +0900118 lhs.SetGlobalVar(
119 new RecursiveVar(NewLiteral(rhs.data()), origin, rhs.data()));
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +0900120}
121
122extern "C" char** environ;
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +0900123
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900124static int Run(const vector<Symbol>& targets,
Shinichiro Hamaji998ccf72015-07-18 12:37:02 +0900125 const vector<StringPiece>& cl_vars,
126 const string& orig_args) {
Shinichiro Hamaji90db5412015-08-13 15:03:52 +0900127 double start_time = GetTime();
128
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900129 if (g_flags.generate_ninja && (g_flags.regen || g_flags.dump_kati_stamp)) {
Shinichiro Hamajif9e15242015-07-31 12:50:06 +0900130 ScopedTimeReporter tr("regen check time");
Shinichiro Hamajif9869fc2015-09-28 17:18:59 +0900131 if (!NeedsRegen(start_time, orig_args)) {
Shinichiro Hamajiac50ff42016-02-09 16:23:28 +0900132 fprintf(stderr, "No need to regenerate ninja file\n");
Shinichiro Hamajif6df3342015-08-11 14:59:36 +0900133 return 0;
134 }
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900135 if (g_flags.dump_kati_stamp) {
Shinichiro Hamajif6df3342015-08-11 14:59:36 +0900136 printf("Need to regenerate ninja file\n");
Shinichiro Hamajia4bb7742015-07-31 11:27:30 +0900137 return 0;
138 }
Shinichiro Hamajia09ed282015-07-31 12:21:54 +0900139 ClearGlobCache();
Shinichiro Hamajia4bb7742015-07-31 11:27:30 +0900140 }
141
Shinichiro Hamaji1a444a82016-02-16 13:49:49 +0900142 SetAffinityForSingleThread();
143
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900144 MakefileCacheManager* cache_mgr = NewMakefileCacheManager();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900145
Shinichiro Hamajic9b9e5e2016-02-18 18:18:54 +0900146 Intern("MAKEFILE_LIST").SetGlobalVar(
147 new SimpleVar(StringPrintf(" %s", g_flags.makefile), VarOrigin::FILE));
Shinichiro Hamaji91d12492015-09-04 09:54:48 +0900148 for (char** p = environ; *p; p++) {
Shinichiro Hamajic9b9e5e2016-02-18 18:18:54 +0900149 SetVar(*p, VarOrigin::ENVIRONMENT);
Shinichiro Hamaji91d12492015-09-04 09:54:48 +0900150 }
Shinichiro Hamajic9b9e5e2016-02-18 18:18:54 +0900151 Evaluator* ev = new Evaluator();
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +0900152
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900153 vector<Stmt*> bootstrap_asts;
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +0900154 ReadBootstrapMakefile(targets, &bootstrap_asts);
155 ev->set_is_bootstrap(true);
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900156 for (Stmt* stmt : bootstrap_asts) {
157 LOG("%s", stmt->DebugString().c_str());
158 stmt->Eval(ev);
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +0900159 }
160 ev->set_is_bootstrap(false);
161
Shinichiro Hamaji1c3a6952016-05-20 16:35:35 +0900162 ev->set_is_commandline(true);
Shinichiro Hamaji91d12492015-09-04 09:54:48 +0900163 for (StringPiece l : cl_vars) {
Shinichiro Hamaji1c3a6952016-05-20 16:35:35 +0900164 vector<Stmt*> asts;
165 Parse(Intern(l).str(), Loc("*bootstrap*", 0), &asts);
166 CHECK(asts.size() == 1);
167 asts[0]->Eval(ev);
Shinichiro Hamaji91d12492015-09-04 09:54:48 +0900168 }
Shinichiro Hamaji1c3a6952016-05-20 16:35:35 +0900169 ev->set_is_commandline(false);
Shinichiro Hamaji91d12492015-09-04 09:54:48 +0900170
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +0900171 {
172 ScopedTimeReporter tr("eval time");
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900173 Makefile* mk = cache_mgr->ReadMakefile(g_flags.makefile);
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900174 for (Stmt* stmt : mk->stmts()) {
175 LOG("%s", stmt->DebugString().c_str());
176 stmt->Eval(ev);
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +0900177 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900178 }
179
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900180 for (ParseErrorStmt* err : GetParseErrors()) {
Dan Willemsene41c7552017-02-22 14:31:16 -0800181 WARN_LOC(err->loc(), "warning for parse error in an unevaluated line: %s",
182 err->msg.c_str());
Shinichiro Hamajia06bee92015-08-05 17:13:05 +0900183 }
184
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900185 vector<DepNode*> nodes;
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +0900186 {
187 ScopedTimeReporter tr("make dep time");
188 MakeDep(ev, ev->rules(), ev->rule_vars(), targets, &nodes);
189 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900190
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900191 if (g_flags.is_syntax_check_only)
Shinichiro Hamajied883ef2015-07-31 13:15:04 +0900192 return 0;
193
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900194 if (g_flags.generate_ninja) {
Shinichiro Hamajied883ef2015-07-31 13:15:04 +0900195 ScopedTimeReporter tr("generate ninja time");
Shinichiro Hamaji9c5e60c2015-09-29 13:26:45 +0900196 GenerateNinja(nodes, ev, orig_args, start_time);
Shinichiro Hamajied883ef2015-07-31 13:15:04 +0900197 return 0;
198 }
199
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900200 for (const auto& p : ev->exports()) {
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900201 const Symbol name = p.first;
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900202 if (p.second) {
203 Var* v = ev->LookupVar(name);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900204 const string&& value = v->Eval(ev);
205 LOG("setenv(%s, %s)", name.c_str(), value.c_str());
206 setenv(name.c_str(), value.c_str(), 1);
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900207 } else {
208 LOG("unsetenv(%s)", name.c_str());
209 unsetenv(name.c_str());
210 }
211 }
212
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +0900213 {
214 ScopedTimeReporter tr("exec time");
215 Exec(nodes, ev);
216 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900217
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900218 for (Stmt* stmt : bootstrap_asts)
219 delete stmt;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900220 delete ev;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900221 delete cache_mgr;
222
Shinichiro Hamaji64cd8062015-06-27 16:58:36 +0900223 return 0;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900224}
225
Shinichiro Hamaji34556cc2015-10-05 15:01:46 +0900226static void FindFirstMakefie() {
227 if (g_flags.makefile != NULL)
228 return;
229 if (Exists("GNUmakefile")) {
230 g_flags.makefile = "GNUmakefile";
231#if !defined(__APPLE__)
232 } else if (Exists("makefile")) {
233 g_flags.makefile = "makefile";
234#endif
235 } else if (Exists("Makefile")) {
236 g_flags.makefile = "Makefile";
237 }
238}
239
Shinichiro Hamaji71cf60b2015-10-08 18:00:07 +0900240static void HandleRealpath(int argc, char** argv) {
241 char buf[PATH_MAX];
242 for (int i = 0; i < argc; i++) {
243 if (realpath(argv[i], buf))
244 printf("%s\n", buf);
245 }
246}
247
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900248int main(int argc, char* argv[]) {
Shinichiro Hamaji71cf60b2015-10-08 18:00:07 +0900249 if (argc >= 2 && !strcmp(argv[1], "--realpath")) {
250 HandleRealpath(argc - 2, argv + 2);
251 return 0;
252 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900253 Init();
Shinichiro Hamaji998ccf72015-07-18 12:37:02 +0900254 string orig_args;
255 for (int i = 0; i < argc; i++) {
256 if (i)
257 orig_args += ' ';
258 orig_args += argv[i];
259 }
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900260 g_flags.Parse(argc, argv);
Shinichiro Hamaji34556cc2015-10-05 15:01:46 +0900261 FindFirstMakefie();
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900262 if (g_flags.makefile == NULL)
Shinichiro Hamajie4ee8ea2015-08-15 01:22:23 +0900263 ERROR("*** No targets specified and no makefile found.");
Shinichiro Hamaji5f57a992015-06-30 19:39:39 +0900264 // This depends on command line flags.
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900265 if (g_flags.use_find_emulator)
Shinichiro Hamaji4e950e62015-07-02 02:59:55 +0900266 InitFindEmulator();
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900267 int r = Run(g_flags.targets, g_flags.cl_vars, orig_args);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900268 Quit();
269 return r;
270}