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