blob: f8ddaf4963c8234ef1a88d085f439d7ee522d59c [file] [log] [blame]
Craig Tiller1a718112015-07-03 12:07:06 -07001#!/usr/bin/env python2.7
2
Craig Tiller6169d5f2016-03-31 07:46:18 -07003# Copyright 2015, Google Inc.
Craig Tiller1a718112015-07-03 12:07:06 -07004# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met:
9#
10# * Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# * Redistributions in binary form must reproduce the above
13# copyright notice, this list of conditions and the following disclaimer
14# in the documentation and/or other materials provided with the
15# distribution.
16# * Neither the name of Google Inc. nor the names of its
17# contributors may be used to endorse or promote products derived from
18# this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
Craig Tiller0fe5ee72015-12-22 12:50:36 -080032import argparse
Craig Tiller1a718112015-07-03 12:07:06 -070033import glob
34import os
Craig Tiller259e6272015-08-31 16:58:18 -070035import shutil
Craig Tiller1a718112015-07-03 12:07:06 -070036import sys
37import tempfile
David Garcia Quintas985f22e2015-10-04 23:14:37 -070038import multiprocessing
Jan Tattermusch5c79a312016-12-20 11:02:50 +010039sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '..', 'run_tests', 'python_utils'))
Craig Tiller1a718112015-07-03 12:07:06 -070040
41assert sys.argv[1:], 'run generate_projects.sh instead of this directly'
42
43import jobset
44
45os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '..', '..'))
Craig Tiller0fe5ee72015-12-22 12:50:36 -080046
47argp = argparse.ArgumentParser()
Craig Tiller560c9012016-02-24 16:34:38 -080048argp.add_argument('build_files', nargs='+', default=[])
Craig Tiller0fe5ee72015-12-22 12:50:36 -080049argp.add_argument('--templates', nargs='+', default=[])
Craig Tiller3ab2fe02016-04-11 20:11:18 -070050argp.add_argument('--output_merged', default=None, type=str)
Craig Tiller12971702016-01-21 13:38:35 -080051argp.add_argument('--jobs', '-j', default=multiprocessing.cpu_count(), type=int)
Craig Tiller0fe5ee72015-12-22 12:50:36 -080052args = argp.parse_args()
53
Craig Tiller560c9012016-02-24 16:34:38 -080054json = args.build_files
Craig Tiller1a718112015-07-03 12:07:06 -070055
56test = {} if 'TEST' in os.environ else None
57
58plugins = sorted(glob.glob('tools/buildgen/plugins/*.py'))
59
Craig Tiller0fe5ee72015-12-22 12:50:36 -080060templates = args.templates
61if not templates:
62 for root, dirs, files in os.walk('templates'):
63 for f in files:
64 templates.append(os.path.join(root, f))
65
Craig Tiller560c9012016-02-24 16:34:38 -080066pre_jobs = []
67base_cmd = ['python2.7', 'tools/buildgen/mako_renderer.py']
68cmd = base_cmd[:]
69for plugin in plugins:
70 cmd.append('-p')
71 cmd.append(plugin)
72for js in json:
73 cmd.append('-d')
74 cmd.append(js)
75cmd.append('-w')
76preprocessed_build = '.preprocessed_build'
77cmd.append(preprocessed_build)
Craig Tiller3ab2fe02016-04-11 20:11:18 -070078if args.output_merged is not None:
79 cmd.append('-M')
80 cmd.append(args.output_merged)
Craig Tiller560c9012016-02-24 16:34:38 -080081pre_jobs.append(jobset.JobSpec(cmd, shortname='preprocess', timeout_seconds=None))
82
Craig Tiller1a718112015-07-03 12:07:06 -070083jobs = []
Craig Tiller560c9012016-02-24 16:34:38 -080084for template in reversed(sorted(templates)):
Craig Tiller0fe5ee72015-12-22 12:50:36 -080085 root, f = os.path.split(template)
86 if os.path.splitext(f)[1] == '.template':
87 out_dir = '.' + root[len('templates'):]
88 out = out_dir + '/' + os.path.splitext(f)[0]
89 if not os.path.exists(out_dir):
90 os.makedirs(out_dir)
Craig Tiller560c9012016-02-24 16:34:38 -080091 cmd = base_cmd[:]
92 cmd.append('-P')
93 cmd.append(preprocessed_build)
Craig Tiller0fe5ee72015-12-22 12:50:36 -080094 cmd.append('-o')
95 if test is None:
96 cmd.append(out)
97 else:
98 tf = tempfile.mkstemp()
99 test[out] = tf[1]
100 os.close(tf[0])
101 cmd.append(test[out])
102 cmd.append(root + '/' + f)
Craig Tiller590105a2016-01-19 13:03:46 -0800103 jobs.append(jobset.JobSpec(cmd, shortname=out, timeout_seconds=None))
Craig Tiller1a718112015-07-03 12:07:06 -0700104
Craig Tiller560c9012016-02-24 16:34:38 -0800105jobset.run(pre_jobs, maxjobs=args.jobs)
Craig Tiller12971702016-01-21 13:38:35 -0800106jobset.run(jobs, maxjobs=args.jobs)
Craig Tiller1a718112015-07-03 12:07:06 -0700107
108if test is not None:
Nicolas "Pixel" Noble22232ae2015-07-11 22:40:22 +0200109 for s, g in test.iteritems():
Craig Tiller259e6272015-08-31 16:58:18 -0700110 if os.path.isfile(g):
Nicolas "Pixel" Noble9baaead2015-10-03 02:25:02 +0200111 assert 0 == os.system('diff %s %s' % (s, g)), s
Craig Tiller259e6272015-08-31 16:58:18 -0700112 os.unlink(g)
113 else:
Nicolas "Pixel" Noble9baaead2015-10-03 02:25:02 +0200114 assert 0 == os.system('diff -r %s %s' % (s, g)), s
Craig Tiller259e6272015-08-31 16:58:18 -0700115 shutil.rmtree(g, ignore_errors=True)