blob: e5ecc4867b553d1a5e81e9f6844f1282638e2abb [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
Jan Tattermusch38b519a2016-01-27 18:32:42 -080040import distribtest_targets
Jan Tattermuschbe538a12016-01-28 14:58:15 -080041import package_targets
42
43_TARGETS = []
44_TARGETS += artifact_targets.targets()
Jan Tattermusch38b519a2016-01-27 18:32:42 -080045_TARGETS += distribtest_targets.targets()
Jan Tattermuschbe538a12016-01-28 14:58:15 -080046_TARGETS += package_targets.targets()
47
48def _create_build_map():
49 """Maps task names and labels to list of tasks to be built."""
50 target_build_map = dict([(target.name, [target])
51 for target in _TARGETS])
52 if len(_TARGETS) > len(target_build_map.keys()):
53 raise Exception('Target names need to be unique')
54
55 label_build_map = {}
56 label_build_map['all'] = [t for t in _TARGETS] # to build all targets
57 for target in _TARGETS:
58 for label in target.labels:
59 if label in label_build_map:
60 label_build_map[label].append(target)
61 else:
62 label_build_map[label] = [target]
63
64 if set(target_build_map.keys()).intersection(label_build_map.keys()):
65 raise Exception('Target names need to be distinct from label names')
66 return dict( target_build_map.items() + label_build_map.items())
67
68
69_BUILD_MAP = _create_build_map()
70
71argp = argparse.ArgumentParser(description='Runs build/test targets.')
72argp.add_argument('-b', '--build',
73 choices=sorted(_BUILD_MAP.keys()),
74 nargs='+',
75 default=['all'],
76 help='Target name or target label to build.')
77argp.add_argument('-f', '--filter',
78 choices=sorted(_BUILD_MAP.keys()),
79 nargs='+',
80 default=[],
81 help='Filter targets to build with AND semantics.')
82argp.add_argument('-j', '--jobs', default=multiprocessing.cpu_count(), type=int)
83argp.add_argument('-t', '--travis',
84 default=False,
85 action='store_const',
86 const=True)
87
88args = argp.parse_args()
89
90# Figure out which targets to build
91targets = []
92for label in args.build:
93 targets += _BUILD_MAP[label]
94
95# Among targets selected by -b, filter out those that don't match the filter
96targets = [t for t in targets if all(f in t.labels for f in args.filter)]
97targets = sorted(set(targets))
98
99# Execute pre-build phase
100prebuild_jobs = []
101for target in targets:
102 prebuild_jobs += target.pre_build_jobspecs()
103if prebuild_jobs:
104 num_failures, _ = jobset.run(
105 prebuild_jobs, newline_on_success=True, maxjobs=args.jobs)
106 if num_failures != 0:
107 jobset.message('FAILED', 'Pre-build phase failed.', do_newline=True)
108 sys.exit(1)
109
110build_jobs = []
111for target in targets:
112 build_jobs.append(target.build_jobspec())
113if not build_jobs:
114 print 'Nothing to build.'
115 sys.exit(1)
116
117jobset.message('START', 'Building targets.', do_newline=True)
118num_failures, _ = jobset.run(
119 build_jobs, newline_on_success=True, maxjobs=args.jobs)
120if num_failures == 0:
121 jobset.message('SUCCESS', 'All targets built successfully.',
122 do_newline=True)
123else:
124 jobset.message('FAILED', 'Failed to build targets.',
125 do_newline=True)
126 sys.exit(1)