blob: 39b15cc8b21fffde46b2b79a4b0da55988469bf7 [file] [log] [blame]
Jan Tattermuschbe538a12016-01-28 14:58:15 -08001#!/usr/bin/env python
2# Copyright 2016, Google Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31"""Runs selected gRPC test/build tasks."""
32
33import argparse
34import atexit
35import jobset
36import multiprocessing
37import sys
38
39import artifact_targets
40import package_targets
41
42_TARGETS = []
43_TARGETS += artifact_targets.targets()
44_TARGETS += package_targets.targets()
45
46def _create_build_map():
47 """Maps task names and labels to list of tasks to be built."""
48 target_build_map = dict([(target.name, [target])
49 for target in _TARGETS])
50 if len(_TARGETS) > len(target_build_map.keys()):
51 raise Exception('Target names need to be unique')
52
53 label_build_map = {}
54 label_build_map['all'] = [t for t in _TARGETS] # to build all targets
55 for target in _TARGETS:
56 for label in target.labels:
57 if label in label_build_map:
58 label_build_map[label].append(target)
59 else:
60 label_build_map[label] = [target]
61
62 if set(target_build_map.keys()).intersection(label_build_map.keys()):
63 raise Exception('Target names need to be distinct from label names')
64 return dict( target_build_map.items() + label_build_map.items())
65
66
67_BUILD_MAP = _create_build_map()
68
69argp = argparse.ArgumentParser(description='Runs build/test targets.')
70argp.add_argument('-b', '--build',
71 choices=sorted(_BUILD_MAP.keys()),
72 nargs='+',
73 default=['all'],
74 help='Target name or target label to build.')
75argp.add_argument('-f', '--filter',
76 choices=sorted(_BUILD_MAP.keys()),
77 nargs='+',
78 default=[],
79 help='Filter targets to build with AND semantics.')
80argp.add_argument('-j', '--jobs', default=multiprocessing.cpu_count(), type=int)
81argp.add_argument('-t', '--travis',
82 default=False,
83 action='store_const',
84 const=True)
85
86args = argp.parse_args()
87
88# Figure out which targets to build
89targets = []
90for label in args.build:
91 targets += _BUILD_MAP[label]
92
93# Among targets selected by -b, filter out those that don't match the filter
94targets = [t for t in targets if all(f in t.labels for f in args.filter)]
95targets = sorted(set(targets))
96
97# Execute pre-build phase
98prebuild_jobs = []
99for target in targets:
100 prebuild_jobs += target.pre_build_jobspecs()
101if prebuild_jobs:
102 num_failures, _ = jobset.run(
103 prebuild_jobs, newline_on_success=True, maxjobs=args.jobs)
104 if num_failures != 0:
105 jobset.message('FAILED', 'Pre-build phase failed.', do_newline=True)
106 sys.exit(1)
107
108build_jobs = []
109for target in targets:
110 build_jobs.append(target.build_jobspec())
111if not build_jobs:
112 print 'Nothing to build.'
113 sys.exit(1)
114
115jobset.message('START', 'Building targets.', do_newline=True)
116num_failures, _ = jobset.run(
117 build_jobs, newline_on_success=True, maxjobs=args.jobs)
118if num_failures == 0:
119 jobset.message('SUCCESS', 'All targets built successfully.',
120 do_newline=True)
121else:
122 jobset.message('FAILED', 'Failed to build targets.',
123 do_newline=True)
124 sys.exit(1)