blob: fd184f50091af2f2a0762aea227e67c5f9431b6b [file] [log] [blame]
Joe Onorato0578cbc2016-10-19 17:03:06 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "aapt.h"
18#include "adb.h"
19#include "make.h"
20#include "print.h"
21#include "util.h"
22
23#include <sstream>
24#include <string>
25#include <vector>
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
31
32#include <google/protobuf/stubs/common.h>
33
34using namespace std;
35
Joe Onorato6c97f492019-02-27 20:42:37 -050036#define NATIVE_TESTS "NATIVE_TESTS"
37
Joe Onorato0578cbc2016-10-19 17:03:06 -070038/**
39 * An entry from the command line for something that will be built, installed,
40 * and/or tested.
41 */
42struct Target {
43 bool build;
44 bool install;
45 bool test;
46 string pattern;
47 string name;
48 vector<string> actions;
49 Module module;
50
51 int testActionCount;
52
53 int testPassCount;
54 int testFailCount;
Makoto Onuki6fb2c972017-08-02 14:40:12 -070055 int unknownFailureCount; // unknown failure == "Process crashed", etc.
Joe Onorato0578cbc2016-10-19 17:03:06 -070056 bool actionsWithNoTests;
57
58 Target(bool b, bool i, bool t, const string& p);
59};
60
61Target::Target(bool b, bool i, bool t, const string& p)
62 :build(b),
63 install(i),
64 test(t),
65 pattern(p),
66 testActionCount(0),
67 testPassCount(0),
68 testFailCount(0),
Makoto Onuki6fb2c972017-08-02 14:40:12 -070069 unknownFailureCount(0),
Joe Onorato0578cbc2016-10-19 17:03:06 -070070 actionsWithNoTests(false)
71{
72}
73
74/**
75 * Command line options.
76 */
77struct Options {
78 // For help
79 bool runHelp;
80
Joe Onorato7acae742019-03-23 16:12:32 -070081 // For refreshing module-info.json
82 bool runRefresh;
83
Joe Onorato0578cbc2016-10-19 17:03:06 -070084 // For tab completion
85 bool runTab;
86 string tabPattern;
87
88 // For build/install/test
Joe Onorato6592c3c2016-11-12 16:34:25 -080089 bool noRestart;
Joe Onorato0578cbc2016-10-19 17:03:06 -070090 bool reboot;
91 vector<Target*> targets;
92
93 Options();
94 ~Options();
95};
96
97Options::Options()
98 :runHelp(false),
Joe Onorato7acae742019-03-23 16:12:32 -070099 runRefresh(false),
Joe Onorato0578cbc2016-10-19 17:03:06 -0700100 runTab(false),
Joe Onorato6592c3c2016-11-12 16:34:25 -0800101 noRestart(false),
Joe Onorato0578cbc2016-10-19 17:03:06 -0700102 reboot(false),
103 targets()
104{
105}
106
107Options::~Options()
108{
109}
110
111struct InstallApk
112{
113 TrackedFile file;
114 bool alwaysInstall;
115 bool installed;
116
117 InstallApk();
118 InstallApk(const InstallApk& that);
119 InstallApk(const string& filename, bool always);
120 ~InstallApk() {};
121};
122
123InstallApk::InstallApk()
124{
125}
126
127InstallApk::InstallApk(const InstallApk& that)
128 :file(that.file),
129 alwaysInstall(that.alwaysInstall),
130 installed(that.installed)
131{
132}
133
134InstallApk::InstallApk(const string& filename, bool always)
135 :file(filename),
136 alwaysInstall(always),
137 installed(false)
138{
139}
140
Joe Onorato6c97f492019-02-27 20:42:37 -0500141struct PushedFile
142{
143 TrackedFile file;
144 string dest;
145
146 PushedFile();
147 PushedFile(const PushedFile& that);
148 PushedFile(const string& filename, const string& dest);
149 ~PushedFile() {};
150};
151
152PushedFile::PushedFile()
153{
154}
155
156PushedFile::PushedFile(const PushedFile& that)
157 :file(that.file),
158 dest(that.dest)
159{
160}
161
162PushedFile::PushedFile(const string& f, const string& d)
163 :file(f),
164 dest(d)
165{
166}
Joe Onorato0578cbc2016-10-19 17:03:06 -0700167
168/**
169 * Record for an test that is going to be launched.
170 */
171struct TestAction {
172 TestAction();
173
174 // The package name from the apk
175 string packageName;
176
177 // The test runner class
178 string runner;
179
180 // The test class, or none if all tests should be run
181 string className;
182
183 // The original target that requested this action
184 Target* target;
185
186 // The number of tests that passed
187 int passCount;
188
189 // The number of tests that failed
190 int failCount;
191};
192
193TestAction::TestAction()
194 :passCount(0),
195 failCount(0)
196{
197}
198
199/**
200 * Record for an activity that is going to be launched.
201 */
202struct ActivityAction {
203 // The package name from the apk
204 string packageName;
205
206 // The test class, or none if all tests should be run
207 string className;
208};
209
210/**
211 * Callback class for the am instrument command.
212 */
213class TestResults: public InstrumentationCallbacks
214{
215public:
216 virtual void OnTestStatus(TestStatus& status);
217 virtual void OnSessionStatus(SessionStatus& status);
218
219 /**
220 * Set the TestAction that the tests are for.
221 * It will be updated with statistics as the tests run.
222 */
223 void SetCurrentAction(TestAction* action);
224
Makoto Onuki6fb2c972017-08-02 14:40:12 -0700225 bool IsSuccess();
226
227 string GetErrorMessage();
228
Joe Onorato0578cbc2016-10-19 17:03:06 -0700229private:
230 TestAction* m_currentAction;
Makoto Onuki6fb2c972017-08-02 14:40:12 -0700231 SessionStatus m_sessionStatus;
Joe Onorato0578cbc2016-10-19 17:03:06 -0700232};
233
234void
235TestResults::OnTestStatus(TestStatus& status)
236{
237 bool found;
238// printf("OnTestStatus\n");
239// status.PrintDebugString();
240 int32_t resultCode = status.has_results() ? status.result_code() : 0;
241
242 if (!status.has_results()) {
243 return;
244 }
245 const ResultsBundle &results = status.results();
246
247 int32_t currentTestNum = get_bundle_int(results, &found, "current", NULL);
248 if (!found) {
249 currentTestNum = -1;
250 }
251
252 int32_t testCount = get_bundle_int(results, &found, "numtests", NULL);
253 if (!found) {
254 testCount = -1;
255 }
256
257 string className = get_bundle_string(results, &found, "class", NULL);
258 if (!found) {
259 return;
260 }
261
262 string testName = get_bundle_string(results, &found, "test", NULL);
263 if (!found) {
264 return;
265 }
266
267 if (resultCode == 0) {
268 // test passed
269 m_currentAction->passCount++;
270 m_currentAction->target->testPassCount++;
271 } else if (resultCode == 1) {
272 // test starting
273 ostringstream line;
274 line << "Running";
275 if (currentTestNum > 0) {
276 line << ": " << currentTestNum;
277 if (testCount > 0) {
278 line << " of " << testCount;
279 }
280 }
281 line << ": " << m_currentAction->target->name << ':' << className << "\\#" << testName;
282 print_one_line("%s", line.str().c_str());
Makoto Onuki6fb2c972017-08-02 14:40:12 -0700283 } else if ((resultCode == -1) || (resultCode == -2)) {
Joe Onorato0578cbc2016-10-19 17:03:06 -0700284 // test failed
Makoto Onuki6fb2c972017-08-02 14:40:12 -0700285 // Note -2 means an assertion failure, and -1 means other exceptions. We just treat them
286 // all as "failures".
Joe Onorato0578cbc2016-10-19 17:03:06 -0700287 m_currentAction->failCount++;
288 m_currentAction->target->testFailCount++;
289 printf("%s\n%sFailed: %s:%s\\#%s%s\n", g_escapeClearLine, g_escapeRedBold,
290 m_currentAction->target->name.c_str(), className.c_str(),
291 testName.c_str(), g_escapeEndColor);
292
Joe Onorato50307d92019-05-01 12:51:05 -0700293 bool stackFound;
294 string stack = get_bundle_string(results, &stackFound, "stack", NULL);
295 if (status.has_logcat()) {
296 const string logcat = status.logcat();
297 if (logcat.length() > 0) {
298 printf("%s\n", logcat.c_str());
299 }
300 } else if (stackFound) {
Joe Onorato0578cbc2016-10-19 17:03:06 -0700301 printf("%s\n", stack.c_str());
302 }
303 }
304}
305
306void
Makoto Onuki6fb2c972017-08-02 14:40:12 -0700307TestResults::OnSessionStatus(SessionStatus& status)
Joe Onorato0578cbc2016-10-19 17:03:06 -0700308{
309 //status.PrintDebugString();
Makoto Onuki6fb2c972017-08-02 14:40:12 -0700310 m_sessionStatus = status;
311 if (m_currentAction && !IsSuccess()) {
312 m_currentAction->target->unknownFailureCount++;
313 }
Joe Onorato0578cbc2016-10-19 17:03:06 -0700314}
315
316void
317TestResults::SetCurrentAction(TestAction* action)
318{
319 m_currentAction = action;
320}
321
Makoto Onuki6fb2c972017-08-02 14:40:12 -0700322bool
323TestResults::IsSuccess()
324{
325 return m_sessionStatus.result_code() == -1; // Activity.RESULT_OK.
326}
327
328string
329TestResults::GetErrorMessage()
330{
331 bool found;
332 string shortMsg = get_bundle_string(m_sessionStatus.results(), &found, "shortMsg", NULL);
333 if (!found) {
334 return IsSuccess() ? "" : "Unknown failure";
335 }
336 return shortMsg;
337}
338
339
Joe Onorato0578cbc2016-10-19 17:03:06 -0700340/**
341 * Prints the usage statement / help text.
342 */
343static void
344print_usage(FILE* out) {
345 fprintf(out, "usage: bit OPTIONS PATTERN\n");
346 fprintf(out, "\n");
347 fprintf(out, " Build, sync and test android code.\n");
348 fprintf(out, "\n");
349 fprintf(out, " The -b -i and -t options allow you to specify which phases\n");
350 fprintf(out, " you want to run. If none of those options are given, then\n");
351 fprintf(out, " all phases are run. If any of these options are provided\n");
352 fprintf(out, " then only the listed phases are run.\n");
353 fprintf(out, "\n");
354 fprintf(out, " OPTIONS\n");
355 fprintf(out, " -b Run a build\n");
356 fprintf(out, " -i Install the targets\n");
357 fprintf(out, " -t Run the tests\n");
358 fprintf(out, "\n");
Joe Onorato6592c3c2016-11-12 16:34:25 -0800359 fprintf(out, " -n Don't reboot or restart\n");
Joe Onorato0578cbc2016-10-19 17:03:06 -0700360 fprintf(out, " -r If the runtime needs to be restarted, do a full reboot\n");
361 fprintf(out, " instead\n");
362 fprintf(out, "\n");
363 fprintf(out, " PATTERN\n");
364 fprintf(out, " One or more targets to build, install and test. The target\n");
365 fprintf(out, " names are the names that appear in the LOCAL_MODULE or\n");
366 fprintf(out, " LOCAL_PACKAGE_NAME variables in Android.mk or Android.bp files.\n");
367 fprintf(out, "\n");
368 fprintf(out, " Building and installing\n");
369 fprintf(out, " -----------------------\n");
370 fprintf(out, " The modules specified will be built and then installed. If the\n");
371 fprintf(out, " files are on the system partition, they will be synced and the\n");
372 fprintf(out, " attached device rebooted. If they are APKs that aren't on the\n");
373 fprintf(out, " system partition they are installed with adb install.\n");
374 fprintf(out, "\n");
375 fprintf(out, " For example:\n");
376 fprintf(out, " bit framework\n");
377 fprintf(out, " Builds framework.jar, syncs the system partition and reboots.\n");
378 fprintf(out, "\n");
379 fprintf(out, " bit SystemUI\n");
380 fprintf(out, " Builds SystemUI.apk, syncs the system partition and reboots.\n");
381 fprintf(out, "\n");
382 fprintf(out, " bit CtsProtoTestCases\n");
383 fprintf(out, " Builds this CTS apk, adb installs it, but does not run any\n");
384 fprintf(out, " tests.\n");
385 fprintf(out, "\n");
386 fprintf(out, " Running Unit Tests\n");
387 fprintf(out, " ------------------\n");
388 fprintf(out, " To run a unit test, list the test class names and optionally the\n");
389 fprintf(out, " test method after the module.\n");
390 fprintf(out, "\n");
391 fprintf(out, " For example:\n");
392 fprintf(out, " bit CtsProtoTestCases:*\n");
393 fprintf(out, " Builds this CTS apk, adb installs it, and runs all the tests\n");
394 fprintf(out, " contained in that apk.\n");
395 fprintf(out, "\n");
396 fprintf(out, " bit framework CtsProtoTestCases:*\n");
397 fprintf(out, " Builds the framework and the apk, syncs and reboots, then\n");
398 fprintf(out, " adb installs CtsProtoTestCases.apk, and runs all tests \n");
399 fprintf(out, " contained in that apk.\n");
400 fprintf(out, "\n");
401 fprintf(out, " bit CtsProtoTestCases:.ProtoOutputStreamBoolTest\n");
402 fprintf(out, " bit CtsProtoTestCases:android.util.proto.cts.ProtoOutputStreamBoolTest\n");
403 fprintf(out, " Builds and installs CtsProtoTestCases.apk, and runs all the\n");
404 fprintf(out, " tests in the ProtoOutputStreamBoolTest class.\n");
405 fprintf(out, "\n");
406 fprintf(out, " bit CtsProtoTestCases:.ProtoOutputStreamBoolTest\\#testWrite\n");
407 fprintf(out, " Builds and installs CtsProtoTestCases.apk, and runs the testWrite\n");
408 fprintf(out, " test method on that class.\n");
409 fprintf(out, "\n");
410 fprintf(out, " bit CtsProtoTestCases:.ProtoOutputStreamBoolTest\\#testWrite,.ProtoOutputStreamBoolTest\\#testRepeated\n");
411 fprintf(out, " Builds and installs CtsProtoTestCases.apk, and runs the testWrite\n");
412 fprintf(out, " and testRepeated test methods on that class.\n");
413 fprintf(out, "\n");
Makoto Onuki164e7962017-07-06 16:20:11 -0700414 fprintf(out, " bit CtsProtoTestCases:android.util.proto.cts.\n");
415 fprintf(out, " Builds and installs CtsProtoTestCases.apk, and runs the tests in the java package\n");
416 fprintf(out, " \"android.util.proto.cts\".\n");
417 fprintf(out, "\n");
Joe Onorato0578cbc2016-10-19 17:03:06 -0700418 fprintf(out, " Launching an Activity\n");
419 fprintf(out, " ---------------------\n");
420 fprintf(out, " To launch an activity, specify the activity class name after\n");
421 fprintf(out, " the module name.\n");
422 fprintf(out, "\n");
423 fprintf(out, " For example:\n");
424 fprintf(out, " bit StatusBarTest:NotificationBuilderTest\n");
425 fprintf(out, " bit StatusBarTest:.NotificationBuilderTest\n");
426 fprintf(out, " bit StatusBarTest:com.android.statusbartest.NotificationBuilderTest\n");
427 fprintf(out, " Builds and installs StatusBarTest.apk, launches the\n");
428 fprintf(out, " com.android.statusbartest/.NotificationBuilderTest activity.\n");
429 fprintf(out, "\n");
430 fprintf(out, "\n");
Joe Onorato7acae742019-03-23 16:12:32 -0700431 fprintf(out, "usage: bit --refresh\n");
432 fprintf(out, "\n");
433 fprintf(out, " Update module-info.json, the cache of make goals that can be built.\n");
434 fprintf(out, "\n");
Joe Onorato0578cbc2016-10-19 17:03:06 -0700435 fprintf(out, "usage: bit --tab ...\n");
436 fprintf(out, "\n");
437 fprintf(out, " Lists the targets in a format for tab completion. To get tab\n");
438 fprintf(out, " completion, add this to your bash environment:\n");
439 fprintf(out, "\n");
440 fprintf(out, " complete -C \"bit --tab\" bit\n");
441 fprintf(out, "\n");
442 fprintf(out, " Sourcing android's build/envsetup.sh will do this for you\n");
443 fprintf(out, " automatically.\n");
444 fprintf(out, "\n");
445 fprintf(out, "\n");
446 fprintf(out, "usage: bit --help\n");
447 fprintf(out, "usage: bit -h\n");
448 fprintf(out, "\n");
449 fprintf(out, " Print this help message\n");
450 fprintf(out, "\n");
451}
452
453
454/**
455 * Sets the appropriate flag* variables. If there is a problem with the
456 * commandline arguments, prints the help message and exits with an error.
457 */
458static void
459parse_args(Options* options, int argc, const char** argv)
460{
461 // Help
462 if (argc == 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) {
463 options->runHelp = true;
464 return;
465 }
466
Joe Onorato7acae742019-03-23 16:12:32 -0700467 // Refresh
468 if (argc == 2 && strcmp(argv[1], "--refresh") == 0) {
469 options->runRefresh = true;
470 return;
471 }
472
Joe Onorato0578cbc2016-10-19 17:03:06 -0700473 // Tab
474 if (argc >= 4 && strcmp(argv[1], "--tab") == 0) {
475 options->runTab = true;
476 options->tabPattern = argv[3];
477 return;
478 }
479
480 // Normal usage
481 bool anyPhases = false;
482 bool gotPattern = false;
483 bool flagBuild = false;
484 bool flagInstall = false;
485 bool flagTest = false;
486 for (int i=1; i < argc; i++) {
487 string arg(argv[i]);
488 if (arg[0] == '-') {
489 for (size_t j=1; j<arg.size(); j++) {
490 switch (arg[j]) {
491 case '-':
492 break;
493 case 'b':
494 if (gotPattern) {
495 gotPattern = false;
496 flagInstall = false;
497 flagTest = false;
498 }
499 flagBuild = true;
500 anyPhases = true;
501 break;
502 case 'i':
503 if (gotPattern) {
504 gotPattern = false;
505 flagBuild = false;
506 flagTest = false;
507 }
508 flagInstall = true;
509 anyPhases = true;
510 break;
511 case 't':
512 if (gotPattern) {
513 gotPattern = false;
514 flagBuild = false;
515 flagInstall = false;
516 }
517 flagTest = true;
518 anyPhases = true;
519 break;
Joe Onorato6592c3c2016-11-12 16:34:25 -0800520 case 'n':
521 options->noRestart = true;
522 break;
Joe Onorato0578cbc2016-10-19 17:03:06 -0700523 case 'r':
524 options->reboot = true;
525 break;
526 default:
527 fprintf(stderr, "Unrecognized option '%c'\n", arg[j]);
528 print_usage(stderr);
529 exit(1);
530 break;
531 }
532 }
533 } else {
534 Target* target = new Target(flagBuild || !anyPhases, flagInstall || !anyPhases,
535 flagTest || !anyPhases, arg);
536 size_t colonPos = arg.find(':');
537 if (colonPos == 0) {
538 fprintf(stderr, "Test / activity supplied without a module to build: %s\n",
539 arg.c_str());
540 print_usage(stderr);
Yunlian Jiang2cfa8492016-12-06 16:08:39 -0800541 delete target;
Joe Onorato0578cbc2016-10-19 17:03:06 -0700542 exit(1);
543 } else if (colonPos == string::npos) {
544 target->name = arg;
545 } else {
546 target->name.assign(arg, 0, colonPos);
547 size_t beginPos = colonPos+1;
548 size_t commaPos;
549 while (true) {
550 commaPos = arg.find(',', beginPos);
551 if (commaPos == string::npos) {
552 if (beginPos != arg.size()) {
553 target->actions.push_back(string(arg, beginPos, commaPos));
554 }
555 break;
556 } else {
557 if (commaPos != beginPos) {
558 target->actions.push_back(string(arg, beginPos, commaPos-beginPos));
559 }
560 beginPos = commaPos+1;
561 }
562 }
563 }
564 options->targets.push_back(target);
565 gotPattern = true;
566 }
567 }
568 // If no pattern was supplied, give an error
569 if (options->targets.size() == 0) {
570 fprintf(stderr, "No PATTERN supplied.\n\n");
571 print_usage(stderr);
572 exit(1);
573 }
574}
575
576/**
577 * Get an environment variable.
578 * Exits with an error if it is unset or the empty string.
579 */
580static string
581get_required_env(const char* name, bool quiet)
582{
583 const char* value = getenv(name);
584 if (value == NULL || value[0] == '\0') {
585 if (!quiet) {
586 fprintf(stderr, "%s not set. Did you source build/envsetup.sh,"
587 " run lunch and do a build?\n", name);
588 }
589 exit(1);
590 }
591 return string(value);
592}
593
594/**
595 * Get the out directory.
596 *
597 * This duplicates the logic in build/make/core/envsetup.mk (which hasn't changed since 2011)
598 * so that we don't have to wait for get_build_var make invocation.
599 */
600string
601get_out_dir()
602{
603 const char* out_dir = getenv("OUT_DIR");
604 if (out_dir == NULL || out_dir[0] == '\0') {
605 const char* common_base = getenv("OUT_DIR_COMMON_BASE");
606 if (common_base == NULL || common_base[0] == '\0') {
607 // We don't prefix with buildTop because we cd there and it
608 // makes all the filenames long when being pretty printed.
609 return "out";
610 } else {
Joe Onorato8a5bb632016-10-21 14:31:42 -0700611 char pwd[PATH_MAX];
612 if (getcwd(pwd, PATH_MAX) == NULL) {
613 fprintf(stderr, "Your pwd is too long.\n");
614 exit(1);
615 }
Joe Onorato0578cbc2016-10-19 17:03:06 -0700616 const char* slash = strrchr(pwd, '/');
617 if (slash == NULL) {
618 slash = "";
619 }
620 string result(common_base);
621 result += slash;
Joe Onorato0578cbc2016-10-19 17:03:06 -0700622 return result;
623 }
624 }
625 return string(out_dir);
626}
627
628/**
629 * Check that a system property on the device matches the expected value.
630 * Exits with an error if they don't.
631 */
632static void
633check_device_property(const string& property, const string& expected)
634{
635 int err;
636 string deviceValue = get_system_property(property, &err);
637 check_error(err);
638 if (deviceValue != expected) {
639 print_error("There is a mismatch between the build you just did and the device you");
640 print_error("are trying to sync it to in the %s system property", property.c_str());
641 print_error(" build: %s", expected.c_str());
642 print_error(" device: %s", deviceValue.c_str());
643 exit(1);
644 }
645}
646
Chih-Hung Hsiehc7edf072017-10-03 09:57:55 -0700647static void
648chdir_or_exit(const char *path) {
649 // TODO: print_command("cd", path);
650 if (0 != chdir(path)) {
651 print_error("Error: Could not chdir: %s", path);
652 exit(1);
653 }
654}
655
Joe Onorato0578cbc2016-10-19 17:03:06 -0700656/**
657 * Run the build, install, and test actions.
658 */
Makoto Onuki6fb2c972017-08-02 14:40:12 -0700659bool
Joe Onorato6592c3c2016-11-12 16:34:25 -0800660run_phases(vector<Target*> targets, const Options& options)
Joe Onorato0578cbc2016-10-19 17:03:06 -0700661{
662 int err = 0;
663
664 //
665 // Initialization
666 //
667
668 print_status("Initializing");
669
670 const string buildTop = get_required_env("ANDROID_BUILD_TOP", false);
671 const string buildProduct = get_required_env("TARGET_PRODUCT", false);
672 const string buildVariant = get_required_env("TARGET_BUILD_VARIANT", false);
673 const string buildType = get_required_env("TARGET_BUILD_TYPE", false);
Joe Onoratoce0bd062019-01-14 15:30:05 -0800674 const string buildOut = get_out_dir();
Chih-Hung Hsiehc7edf072017-10-03 09:57:55 -0700675 chdir_or_exit(buildTop.c_str());
Joe Onorato0578cbc2016-10-19 17:03:06 -0700676
Joe Onoratoce0bd062019-01-14 15:30:05 -0800677 BuildVars buildVars(buildOut, buildProduct, buildVariant, buildType);
678
679 const string buildDevice = buildVars.GetBuildVar("TARGET_DEVICE", false);
680 const string buildId = buildVars.GetBuildVar("BUILD_ID", false);
Dan Willemsena40118d2017-10-17 17:46:41 -0700681
Joe Onorato0578cbc2016-10-19 17:03:06 -0700682 // Get the modules for the targets
683 map<string,Module> modules;
684 read_modules(buildOut, buildDevice, &modules, false);
685 for (size_t i=0; i<targets.size(); i++) {
686 Target* target = targets[i];
687 map<string,Module>::iterator mod = modules.find(target->name);
688 if (mod != modules.end()) {
689 target->module = mod->second;
690 } else {
691 print_error("Error: Could not find module: %s", target->name.c_str());
Joe Onorato7acae742019-03-23 16:12:32 -0700692 fprintf(stderr, "Try running %sbit --refresh%s if you recently added %s%s%s.\n",
693 g_escapeBold, g_escapeEndColor,
694 g_escapeBold, target->name.c_str(), g_escapeEndColor);
Joe Onorato0578cbc2016-10-19 17:03:06 -0700695 err = 1;
696 }
697 }
698 if (err != 0) {
699 exit(1);
700 }
701
702 // Choose the goals
703 vector<string> goals;
704 for (size_t i=0; i<targets.size(); i++) {
705 Target* target = targets[i];
706 if (target->build) {
707 goals.push_back(target->name);
708 }
709 }
710
Joe Onoratob42201b2019-10-23 12:52:41 -0700711
Joe Onorato0578cbc2016-10-19 17:03:06 -0700712 // Figure out whether we need to sync the system and which apks to install
Joe Onorato6c97f492019-02-27 20:42:37 -0500713 string deviceTargetPath = buildOut + "/target/product/" + buildDevice;
714 string systemPath = deviceTargetPath + "/system/";
715 string dataPath = deviceTargetPath + "/data/";
Joe Onoratob42201b2019-10-23 12:52:41 -0700716 string testPath = deviceTargetPath + "/testcases/";
Joe Onorato0578cbc2016-10-19 17:03:06 -0700717 bool syncSystem = false;
718 bool alwaysSyncSystem = false;
Joe Onorato70edfa82018-12-14 15:46:27 -0800719 vector<string> systemFiles;
Joe Onorato0578cbc2016-10-19 17:03:06 -0700720 vector<InstallApk> installApks;
Joe Onorato6c97f492019-02-27 20:42:37 -0500721 vector<PushedFile> pushedFiles;
Joe Onorato0578cbc2016-10-19 17:03:06 -0700722 for (size_t i=0; i<targets.size(); i++) {
723 Target* target = targets[i];
724 if (target->install) {
725 for (size_t j=0; j<target->module.installed.size(); j++) {
726 const string& file = target->module.installed[j];
727 // System partition
728 if (starts_with(file, systemPath)) {
729 syncSystem = true;
Joe Onorato70edfa82018-12-14 15:46:27 -0800730 systemFiles.push_back(file);
Joe Onorato0578cbc2016-10-19 17:03:06 -0700731 if (!target->build) {
732 // If a system partition target didn't get built then
733 // it won't change we will always need to do adb sync
734 alwaysSyncSystem = true;
735 }
736 continue;
737 }
738 // Apk in the data partition
Joe Onoratob42201b2019-10-23 12:52:41 -0700739 if (ends_with(file, ".apk")
740 && (starts_with(file, dataPath) || starts_with(file, testPath))) {
Joe Onorato0578cbc2016-10-19 17:03:06 -0700741 // Always install it if we didn't build it because otherwise
742 // it will never have changed.
743 installApks.push_back(InstallApk(file, !target->build));
744 continue;
745 }
Joe Onorato6c97f492019-02-27 20:42:37 -0500746 // If it's a native test module, push it.
747 if (target->module.HasClass(NATIVE_TESTS) && starts_with(file, dataPath)) {
748 string installedPath(file.c_str() + deviceTargetPath.length());
749 pushedFiles.push_back(PushedFile(file, installedPath));
750 }
Joe Onorato0578cbc2016-10-19 17:03:06 -0700751 }
752 }
753 }
754 map<string,FileInfo> systemFilesBefore;
755 if (syncSystem && !alwaysSyncSystem) {
756 get_directory_contents(systemPath, &systemFilesBefore);
757 }
758
Joe Onorato70edfa82018-12-14 15:46:27 -0800759 if (systemFiles.size() > 0){
760 print_info("System files:");
761 for (size_t i=0; i<systemFiles.size(); i++) {
762 printf(" %s\n", systemFiles[i].c_str());
763 }
764 }
Joe Onorato6c97f492019-02-27 20:42:37 -0500765 if (pushedFiles.size() > 0){
766 print_info("Files to push:");
767 for (size_t i=0; i<pushedFiles.size(); i++) {
768 printf(" %s\n", pushedFiles[i].file.filename.c_str());
769 printf(" --> %s\n", pushedFiles[i].dest.c_str());
770 }
771 }
Joe Onorato70edfa82018-12-14 15:46:27 -0800772 if (installApks.size() > 0){
773 print_info("APKs to install:");
774 for (size_t i=0; i<installApks.size(); i++) {
775 printf(" %s\n", installApks[i].file.filename.c_str());
776 }
777 }
778
Joe Onorato0578cbc2016-10-19 17:03:06 -0700779 //
780 // Build
781 //
782
783 // Run the build
784 if (goals.size() > 0) {
785 print_status("Building");
786 err = build_goals(goals);
787 check_error(err);
788 }
789
790 //
791 // Install
792 //
793
794 // Sync the system partition and reboot
795 bool skipSync = false;
796 if (syncSystem) {
797 print_status("Syncing /system");
798
799 if (!alwaysSyncSystem) {
800 // If nothing changed and we weren't forced to sync, skip the reboot for speed.
801 map<string,FileInfo> systemFilesAfter;
802 get_directory_contents(systemPath, &systemFilesAfter);
803 skipSync = !directory_contents_differ(systemFilesBefore, systemFilesAfter);
804 }
805 if (skipSync) {
806 printf("Skipping sync because no files changed.\n");
807 } else {
808 // Do some sanity checks
809 check_device_property("ro.build.product", buildProduct);
810 check_device_property("ro.build.type", buildVariant);
811 check_device_property("ro.build.id", buildId);
812
813 // Stop & Sync
Joe Onorato6592c3c2016-11-12 16:34:25 -0800814 if (!options.noRestart) {
815 err = run_adb("shell", "stop", NULL);
816 check_error(err);
817 }
Joe Onorato0578cbc2016-10-19 17:03:06 -0700818 err = run_adb("remount", NULL);
819 check_error(err);
820 err = run_adb("sync", "system", NULL);
821 check_error(err);
822
Joe Onorato6592c3c2016-11-12 16:34:25 -0800823 if (!options.noRestart) {
824 if (options.reboot) {
825 print_status("Rebooting");
Joe Onorato0578cbc2016-10-19 17:03:06 -0700826
Joe Onorato6592c3c2016-11-12 16:34:25 -0800827 err = run_adb("reboot", NULL);
828 check_error(err);
829 err = run_adb("wait-for-device", NULL);
830 check_error(err);
831 } else {
832 print_status("Restarting the runtime");
Joe Onorato0578cbc2016-10-19 17:03:06 -0700833
Joe Onorato6592c3c2016-11-12 16:34:25 -0800834 err = run_adb("shell", "setprop", "sys.boot_completed", "0", NULL);
835 check_error(err);
836 err = run_adb("shell", "start", NULL);
837 check_error(err);
Joe Onorato0578cbc2016-10-19 17:03:06 -0700838 }
Joe Onorato6592c3c2016-11-12 16:34:25 -0800839
840 while (true) {
841 string completed = get_system_property("sys.boot_completed", &err);
842 check_error(err);
843 if (completed == "1") {
844 break;
845 }
846 sleep(2);
847 }
848 sleep(1);
849 err = run_adb("shell", "wm", "dismiss-keyguard", NULL);
850 check_error(err);
Joe Onorato0578cbc2016-10-19 17:03:06 -0700851 }
Joe Onorato0578cbc2016-10-19 17:03:06 -0700852 }
853 }
854
Joe Onorato6c97f492019-02-27 20:42:37 -0500855 // Push files
856 if (pushedFiles.size() > 0) {
857 print_status("Pushing files");
858 for (size_t i=0; i<pushedFiles.size(); i++) {
859 const PushedFile& pushed = pushedFiles[i];
860 string dir = dirname(pushed.dest);
861 if (dir.length() == 0 || dir == "/") {
862 // This isn't really a file inside the data directory. Just skip it.
863 continue;
864 }
865 // TODO: if (!apk.file.fileInfo.exists || apk.file.HasChanged())
866 err = run_adb("shell", "mkdir", "-p", dir.c_str(), NULL);
867 check_error(err);
Joe Onorato871cf3e2019-03-25 14:30:34 -0700868 err = run_adb("push", pushed.file.filename.c_str(), pushed.dest.c_str(), NULL);
Joe Onorato6c97f492019-02-27 20:42:37 -0500869 check_error(err);
870 // pushed.installed = true;
871 }
872 }
873
Joe Onorato0578cbc2016-10-19 17:03:06 -0700874 // Install APKs
875 if (installApks.size() > 0) {
876 print_status("Installing APKs");
877 for (size_t i=0; i<installApks.size(); i++) {
878 InstallApk& apk = installApks[i];
879 if (!apk.file.fileInfo.exists || apk.file.HasChanged()) {
880 // It didn't exist before or it changed, so int needs install
Jeff Sharkey5f9dc422017-07-06 12:13:42 -0600881 err = run_adb("install", "-r", "-g", apk.file.filename.c_str(), NULL);
Joe Onorato0578cbc2016-10-19 17:03:06 -0700882 check_error(err);
883 apk.installed = true;
884 } else {
885 printf("APK didn't change. Skipping install of %s\n", apk.file.filename.c_str());
886 }
887 }
888 }
889
890 //
891 // Actions
892 //
893
Joe Onorato6c97f492019-02-27 20:42:37 -0500894 // Whether there have been any tests run, so we can print a summary.
895 bool testsRun = false;
896
897 // Run the native tests.
898 // TODO: We don't have a good way of running these and capturing the output of
899 // them live. It'll take some work. On the other hand, if they're gtest tests,
900 // the output of gtest is not completely insane like the text output of the
901 // instrumentation tests. So for now, we'll just live with that.
902 for (size_t i=0; i<targets.size(); i++) {
903 Target* target = targets[i];
904 if (target->test && target->module.HasClass(NATIVE_TESTS)) {
905 // We don't have a clear signal from the build system which of the installed
906 // files is actually the test, so we guess by looking for one with the same
907 // leaf name as the module that is executable.
908 for (size_t j=0; j<target->module.installed.size(); j++) {
909 string filename = target->module.installed[j];
910 if (!starts_with(filename, dataPath)) {
911 // Native tests go into the data directory.
912 continue;
913 }
914 if (leafname(filename) != target->module.name) {
915 // This isn't the test executable.
916 continue;
917 }
918 if (!is_executable(filename)) {
919 continue;
920 }
921 string installedPath(filename.c_str() + deviceTargetPath.length());
922 printf("the magic one is: %s\n", filename.c_str());
923 printf(" and it's installed at: %s\n", installedPath.c_str());
924
925 // Convert bit-style actions to gtest test filter arguments
926 if (target->actions.size() > 0) {
927 testsRun = true;
928 target->testActionCount++;
929 bool runAll = false;
930 string filterArg("--gtest_filter=");
931 for (size_t k=0; k<target->actions.size(); k++) {
932 string actionString = target->actions[k];
933 if (actionString == "*") {
934 runAll = true;
935 } else {
936 filterArg += actionString;
937 if (k != target->actions.size()-1) {
938 // We would otherwise have to worry about this condition
939 // being true, and appending an extra ':', but we know that
940 // if the extra action is "*", then we'll just run all and
941 // won't use filterArg anyway, so just keep this condition
942 // simple.
943 filterArg += ':';
944 }
945 }
946 }
947 if (runAll) {
948 err = run_adb("shell", installedPath.c_str(), NULL);
949 } else {
950 err = run_adb("shell", installedPath.c_str(), filterArg.c_str(), NULL);
951 }
952 if (err == 0) {
953 target->testPassCount++;
954 } else {
955 target->testFailCount++;
956 }
957 }
958 }
959 }
960 }
961
Joe Onorato0578cbc2016-10-19 17:03:06 -0700962 // Inspect the apks, and figure out what is an activity and what needs a test runner
963 bool printedInspecting = false;
964 vector<TestAction> testActions;
965 vector<ActivityAction> activityActions;
966 for (size_t i=0; i<targets.size(); i++) {
967 Target* target = targets[i];
968 if (target->test) {
969 for (size_t j=0; j<target->module.installed.size(); j++) {
970 string filename = target->module.installed[j];
971
Joe Onoratob42201b2019-10-23 12:52:41 -0700972 // Skip of not apk in the data partition or test
973 if (!(ends_with(filename, ".apk")
974 && (starts_with(filename, dataPath) || starts_with(filename, testPath)))) {
Joe Onorato0578cbc2016-10-19 17:03:06 -0700975 continue;
976 }
977
978 if (!printedInspecting) {
979 printedInspecting = true;
980 print_status("Inspecting APKs");
981 }
982
983 Apk apk;
984 err = inspect_apk(&apk, filename);
985 check_error(err);
986
987 for (size_t k=0; k<target->actions.size(); k++) {
988 string actionString = target->actions[k];
989 if (actionString == "*") {
990 if (apk.runner.length() == 0) {
991 print_error("Error: Test requested for apk that doesn't"
992 " have an <instrumentation> tag: %s\n",
993 target->module.name.c_str());
994 exit(1);
995 }
996 TestAction action;
997 action.packageName = apk.package;
998 action.runner = apk.runner;
999 action.target = target;
1000 testActions.push_back(action);
1001 target->testActionCount++;
1002 } else if (apk.HasActivity(actionString)) {
1003 ActivityAction action;
1004 action.packageName = apk.package;
1005 action.className = full_class_name(apk.package, actionString);
1006 activityActions.push_back(action);
1007 } else {
1008 if (apk.runner.length() == 0) {
1009 print_error("Error: Test requested for apk that doesn't"
1010 " have an <instrumentation> tag: %s\n",
1011 target->module.name.c_str());
1012 exit(1);
1013 }
1014 TestAction action;
1015 action.packageName = apk.package;
1016 action.runner = apk.runner;
1017 action.className = full_class_name(apk.package, actionString);
1018 action.target = target;
1019 testActions.push_back(action);
1020 target->testActionCount++;
1021 }
1022 }
1023 }
1024 }
1025 }
1026
1027 // Run the instrumentation tests
1028 TestResults testResults;
1029 if (testActions.size() > 0) {
1030 print_status("Running tests");
Joe Onorato6c97f492019-02-27 20:42:37 -05001031 testsRun = true;
Joe Onorato0578cbc2016-10-19 17:03:06 -07001032 for (size_t i=0; i<testActions.size(); i++) {
1033 TestAction& action = testActions[i];
1034 testResults.SetCurrentAction(&action);
1035 err = run_instrumentation_test(action.packageName, action.runner, action.className,
1036 &testResults);
1037 check_error(err);
1038 if (action.passCount == 0 && action.failCount == 0) {
1039 action.target->actionsWithNoTests = true;
1040 }
1041 int total = action.passCount + action.failCount;
1042 printf("%sRan %d test%s for %s. ", g_escapeClearLine,
1043 total, total > 1 ? "s" : "", action.target->name.c_str());
1044 if (action.passCount == 0 && action.failCount == 0) {
1045 printf("%s%d passed, %d failed%s\n", g_escapeYellowBold, action.passCount,
1046 action.failCount, g_escapeEndColor);
1047 } else if (action.failCount > 0) {
1048 printf("%d passed, %s%d failed%s\n", action.passCount, g_escapeRedBold,
1049 action.failCount, g_escapeEndColor);
1050 } else {
1051 printf("%s%d passed%s, %d failed\n", g_escapeGreenBold, action.passCount,
1052 g_escapeEndColor, action.failCount);
1053 }
Makoto Onuki6fb2c972017-08-02 14:40:12 -07001054 if (!testResults.IsSuccess()) {
1055 printf("\n%sTest didn't finish successfully: %s%s\n", g_escapeRedBold,
1056 testResults.GetErrorMessage().c_str(), g_escapeEndColor);
1057 }
Joe Onorato0578cbc2016-10-19 17:03:06 -07001058 }
1059 }
1060
1061 // Launch the activity
1062 if (activityActions.size() > 0) {
1063 print_status("Starting activity");
1064
1065 if (activityActions.size() > 1) {
1066 print_warning("Multiple activities specified. Will only start the first one:");
1067 for (size_t i=0; i<activityActions.size(); i++) {
1068 ActivityAction& action = activityActions[i];
1069 print_warning(" %s",
1070 pretty_component_name(action.packageName, action.className).c_str());
1071 }
1072 }
1073
1074 const ActivityAction& action = activityActions[0];
1075 string componentName = action.packageName + "/" + action.className;
1076 err = run_adb("shell", "am", "start", componentName.c_str(), NULL);
1077 check_error(err);
1078 }
1079
1080 //
1081 // Print summary
1082 //
1083
1084 printf("\n%s--------------------------------------------%s\n", g_escapeBold, g_escapeEndColor);
1085
1086 // Build
1087 if (goals.size() > 0) {
1088 printf("%sBuilt:%s\n", g_escapeBold, g_escapeEndColor);
1089 for (size_t i=0; i<goals.size(); i++) {
1090 printf(" %s\n", goals[i].c_str());
1091 }
1092 }
1093
1094 // Install
1095 if (syncSystem) {
1096 if (skipSync) {
1097 printf("%sSkipped syncing /system partition%s\n", g_escapeBold, g_escapeEndColor);
1098 } else {
1099 printf("%sSynced /system partition%s\n", g_escapeBold, g_escapeEndColor);
1100 }
1101 }
1102 if (installApks.size() > 0) {
1103 bool printedTitle = false;
1104 for (size_t i=0; i<installApks.size(); i++) {
1105 const InstallApk& apk = installApks[i];
1106 if (apk.installed) {
1107 if (!printedTitle) {
1108 printf("%sInstalled:%s\n", g_escapeBold, g_escapeEndColor);
1109 printedTitle = true;
1110 }
1111 printf(" %s\n", apk.file.filename.c_str());
1112 }
1113 }
1114 printedTitle = false;
1115 for (size_t i=0; i<installApks.size(); i++) {
1116 const InstallApk& apk = installApks[i];
1117 if (!apk.installed) {
1118 if (!printedTitle) {
1119 printf("%sSkipped install:%s\n", g_escapeBold, g_escapeEndColor);
1120 printedTitle = true;
1121 }
1122 printf(" %s\n", apk.file.filename.c_str());
1123 }
1124 }
1125 }
1126
1127 // Tests
Makoto Onuki6fb2c972017-08-02 14:40:12 -07001128 bool hasErrors = false;
Joe Onorato6c97f492019-02-27 20:42:37 -05001129 if (testsRun) {
Joe Onorato0578cbc2016-10-19 17:03:06 -07001130 printf("%sRan tests:%s\n", g_escapeBold, g_escapeEndColor);
1131 size_t maxNameLength = 0;
1132 for (size_t i=0; i<targets.size(); i++) {
1133 Target* target = targets[i];
1134 if (target->test) {
1135 size_t len = target->name.length();
1136 if (len > maxNameLength) {
1137 maxNameLength = len;
1138 }
1139 }
1140 }
1141 string padding(maxNameLength, ' ');
1142 for (size_t i=0; i<targets.size(); i++) {
1143 Target* target = targets[i];
1144 if (target->testActionCount > 0) {
1145 printf(" %s%s", target->name.c_str(), padding.c_str() + target->name.length());
Makoto Onuki6fb2c972017-08-02 14:40:12 -07001146 if (target->unknownFailureCount > 0) {
1147 printf(" %sUnknown failure, see above message.%s\n",
1148 g_escapeRedBold, g_escapeEndColor);
1149 hasErrors = true;
1150 } else if (target->actionsWithNoTests) {
Joe Onorato0578cbc2016-10-19 17:03:06 -07001151 printf(" %s%d passed, %d failed%s\n", g_escapeYellowBold,
1152 target->testPassCount, target->testFailCount, g_escapeEndColor);
Makoto Onuki6fb2c972017-08-02 14:40:12 -07001153 hasErrors = true;
Joe Onorato0578cbc2016-10-19 17:03:06 -07001154 } else if (target->testFailCount > 0) {
1155 printf(" %d passed, %s%d failed%s\n", target->testPassCount,
1156 g_escapeRedBold, target->testFailCount, g_escapeEndColor);
Makoto Onuki6fb2c972017-08-02 14:40:12 -07001157 hasErrors = true;
Joe Onorato0578cbc2016-10-19 17:03:06 -07001158 } else {
1159 printf(" %s%d passed%s, %d failed\n", g_escapeGreenBold,
1160 target->testPassCount, g_escapeEndColor, target->testFailCount);
1161 }
1162 }
1163 }
1164 }
1165 if (activityActions.size() > 1) {
1166 printf("%sStarted Activity:%s\n", g_escapeBold, g_escapeEndColor);
1167 const ActivityAction& action = activityActions[0];
1168 printf(" %s\n", pretty_component_name(action.packageName, action.className).c_str());
1169 }
1170
1171 printf("%s--------------------------------------------%s\n", g_escapeBold, g_escapeEndColor);
Makoto Onuki6fb2c972017-08-02 14:40:12 -07001172 return !hasErrors;
Joe Onorato0578cbc2016-10-19 17:03:06 -07001173}
1174
1175/**
Joe Onorato7acae742019-03-23 16:12:32 -07001176 * Refresh module-info.
1177 */
1178void
1179run_refresh()
1180{
1181 int err;
1182
1183 print_status("Initializing");
1184 const string buildTop = get_required_env("ANDROID_BUILD_TOP", false);
1185 const string buildProduct = get_required_env("TARGET_PRODUCT", false);
1186 const string buildVariant = get_required_env("TARGET_BUILD_VARIANT", false);
1187 const string buildType = get_required_env("TARGET_BUILD_TYPE", false);
1188 const string buildOut = get_out_dir();
1189 chdir_or_exit(buildTop.c_str());
1190
1191 BuildVars buildVars(buildOut, buildProduct, buildVariant, buildType);
1192
1193 string buildDevice = buildVars.GetBuildVar("TARGET_DEVICE", false);
1194
1195 vector<string> goals;
1196 goals.push_back(buildOut + "/target/product/" + buildDevice + "/module-info.json");
1197
1198 print_status("Refreshing module-info.json");
1199 err = build_goals(goals);
1200 check_error(err);
1201}
1202
1203/**
Joe Onorato0578cbc2016-10-19 17:03:06 -07001204 * Implement tab completion of the target names from the all modules file.
1205 */
1206void
1207run_tab_completion(const string& word)
1208{
Joe Onoratoce0bd062019-01-14 15:30:05 -08001209 const string buildTop = get_required_env("ANDROID_BUILD_TOP", false);
Joe Onorato0578cbc2016-10-19 17:03:06 -07001210 const string buildProduct = get_required_env("TARGET_PRODUCT", false);
Joe Onoratoce0bd062019-01-14 15:30:05 -08001211 const string buildVariant = get_required_env("TARGET_BUILD_VARIANT", false);
1212 const string buildType = get_required_env("TARGET_BUILD_TYPE", false);
Joe Onorato0578cbc2016-10-19 17:03:06 -07001213 const string buildOut = get_out_dir();
Chih-Hung Hsiehc7edf072017-10-03 09:57:55 -07001214 chdir_or_exit(buildTop.c_str());
Joe Onorato0578cbc2016-10-19 17:03:06 -07001215
Joe Onoratoce0bd062019-01-14 15:30:05 -08001216 BuildVars buildVars(buildOut, buildProduct, buildVariant, buildType);
1217
1218 string buildDevice = buildVars.GetBuildVar("TARGET_DEVICE", false);
Joe Onorato0578cbc2016-10-19 17:03:06 -07001219
1220 map<string,Module> modules;
1221 read_modules(buildOut, buildDevice, &modules, true);
1222
1223 for (map<string,Module>::const_iterator it = modules.begin(); it != modules.end(); it++) {
1224 if (starts_with(it->first, word)) {
1225 printf("%s\n", it->first.c_str());
1226 }
1227 }
1228}
1229
1230/**
1231 * Main entry point.
1232 */
1233int
1234main(int argc, const char** argv)
1235{
1236 GOOGLE_PROTOBUF_VERIFY_VERSION;
1237 init_print();
1238
1239 Options options;
1240 parse_args(&options, argc, argv);
1241
1242 if (options.runHelp) {
1243 // Help
1244 print_usage(stdout);
1245 exit(0);
Joe Onorato7acae742019-03-23 16:12:32 -07001246 } else if (options.runRefresh) {
1247 run_refresh();
1248 exit(0);
Joe Onorato0578cbc2016-10-19 17:03:06 -07001249 } else if (options.runTab) {
1250 run_tab_completion(options.tabPattern);
1251 exit(0);
1252 } else {
1253 // Normal run
Makoto Onuki6fb2c972017-08-02 14:40:12 -07001254 exit(run_phases(options.targets, options) ? 0 : 1);
Joe Onorato0578cbc2016-10-19 17:03:06 -07001255 }
1256
1257 return 0;
1258}
1259