Skip to content

Commit b1aba7f

Browse files
committed
Merge pull request #170 from bridadan/build_everything
Adding a "build everything" script
2 parents 2904e10 + f649a2a commit b1aba7f

File tree

3 files changed

+237
-3
lines changed

3 files changed

+237
-3
lines changed

tools/build_everything.py

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
#! /usr/bin/env python
2+
"""
3+
mbed SDK
4+
Copyright (c) 2011-2013 ARM Limited
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
"""
18+
import sys
19+
from time import time
20+
from os.path import join, abspath, dirname, normpath
21+
from optparse import OptionParser
22+
import json
23+
24+
# Be sure that the tools directory is in the search path
25+
ROOT = abspath(join(dirname(__file__), ".."))
26+
sys.path.insert(0, ROOT)
27+
28+
from tools.build_api import build_library
29+
from tools.build_api import write_build_report
30+
from tools.targets import TARGET_MAP, TARGET_NAMES
31+
from tools.toolchains import TOOLCHAINS
32+
from tools.test_exporters import ReportExporter, ResultExporterType
33+
from tools.test_api import find_tests, build_tests, test_spec_from_test_builds
34+
from tools.build_release import OFFICIAL_MBED_LIBRARY_BUILD
35+
36+
if __name__ == '__main__':
37+
try:
38+
parser = OptionParser()
39+
40+
parser.add_option("--source", dest="source_dir",
41+
default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append")
42+
43+
parser.add_option("--build", dest="build_dir",
44+
default=None, help="The build (output) directory")
45+
46+
parser.add_option('-c', '--clean',
47+
dest='clean',
48+
metavar=False,
49+
action="store_true",
50+
help='Clean the build directory')
51+
52+
parser.add_option('-a', '--all', dest="all", default=False, action="store_true",
53+
help="Build every target (including unofficial targets) and with each of the supported toolchains")
54+
55+
parser.add_option('-o', '--official', dest="official_only", default=False, action="store_true",
56+
help="Build using only the official toolchain for each target")
57+
58+
parser.add_option("-D", "",
59+
action="append",
60+
dest="macros",
61+
help="Add a macro definition")
62+
63+
parser.add_option("-j", "--jobs", type="int", dest="jobs",
64+
default=0, help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)")
65+
66+
parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
67+
default=False, help="Verbose diagnostic output")
68+
69+
parser.add_option("-t", "--toolchains", dest="toolchains", help="Use toolchains names separated by comma")
70+
71+
parser.add_option("-p", "--platforms", dest="platforms", default="", help="Build only for the platform namesseparated by comma")
72+
73+
parser.add_option("", "--config", action="store_true", dest="list_config",
74+
default=False, help="List the platforms and toolchains in the release in JSON")
75+
76+
parser.add_option("", "--test-spec", dest="test_spec",
77+
default=None, help="Destination path for a test spec file that can be used by the Greentea automated test tool")
78+
79+
parser.add_option("", "--build-report-junit", dest="build_report_junit", help="Output the build results to an junit xml file")
80+
81+
options, args = parser.parse_args()
82+
83+
# Get set of valid targets
84+
all_platforms = set(TARGET_NAMES)
85+
bad_platforms = set()
86+
platforms = set()
87+
if options.platforms != "":
88+
platforms = set(options.platforms.split(","))
89+
bad_platforms = platforms.difference(all_platforms)
90+
platforms = platforms.intersection(all_platforms)
91+
elif options.all:
92+
platforms = all_platforms
93+
else:
94+
platforms = set(x[0] for x in OFFICIAL_MBED_LIBRARY_BUILD)
95+
bad_platforms = platforms.difference(all_platforms)
96+
platforms = platforms.intersection(all_platforms)
97+
98+
for bad_platform in bad_platforms:
99+
print "Platform '%s' is not a valid platform. Skipping." % bad_platform
100+
101+
if options.platforms:
102+
print "Limiting build to the following platforms: %s" % ",".join(platforms)
103+
104+
# Get set of valid toolchains
105+
all_toolchains = set(TOOLCHAINS)
106+
bad_toolchains = set()
107+
toolchains = set()
108+
109+
if options.toolchains:
110+
toolchains = set(options.toolchains.split(","))
111+
bad_toolchains = toolchains.difference(all_toolchains)
112+
toolchains = toolchains.intersection(all_toolchains)
113+
else:
114+
toolchains = all_toolchains
115+
116+
for bad_toolchain in bad_toolchains:
117+
print "Toolchain '%s' is not a valid toolchain. Skipping." % bad_toolchain
118+
119+
if options.toolchains:
120+
print "Limiting build to the following toolchains: %s" % ",".join(toolchains)
121+
122+
build_config = {}
123+
124+
for platform in platforms:
125+
target = TARGET_MAP[platform]
126+
127+
if options.official_only:
128+
default_toolchain = getattr(target, 'default_toolchain', 'ARM')
129+
build_config[platform] = list(toolchains.intersection(set([default_toolchain])))
130+
else:
131+
build_config[platform] = list(toolchains.intersection(set(target.supported_toolchains)))
132+
133+
if options.list_config:
134+
print json.dumps(build_config, indent=4)
135+
sys.exit(0)
136+
137+
# Ensure build directory is set
138+
if not options.build_dir:
139+
print "[ERROR] You must specify a build path"
140+
sys.exit(1)
141+
142+
# Default base source path is the current directory
143+
base_source_paths = options.source_dir
144+
if not base_source_paths:
145+
base_source_paths = ['.']
146+
147+
all_tests = find_tests(base_source_paths[0])
148+
149+
start = time()
150+
build_report = {}
151+
build_properties = {}
152+
test_builds = {}
153+
total_build_success = True
154+
155+
for target_name, target_toolchains in build_config.iteritems():
156+
target = TARGET_MAP[target_name]
157+
158+
for target_toolchain in target_toolchains:
159+
library_build_success = True
160+
161+
try:
162+
build_directory = join(options.build_dir, target_name, target_toolchain)
163+
# Build sources
164+
build_library(base_source_paths, build_directory, target, target_toolchain,
165+
jobs=options.jobs,
166+
clean=options.clean,
167+
report=build_report,
168+
properties=build_properties,
169+
name="mbed-os",
170+
macros=options.macros,
171+
verbose=options.verbose,
172+
archive=False)
173+
except Exception, e:
174+
library_build_success = False
175+
print "Failed to build library"
176+
print e
177+
178+
if library_build_success:
179+
# Build all the tests
180+
test_build_success, test_build = build_tests(all_tests, [build_directory], build_directory, target, target_toolchain,
181+
clean=options.clean,
182+
report=build_report,
183+
properties=build_properties,
184+
macros=options.macros,
185+
verbose=options.verbose,
186+
jobs=options.jobs)
187+
188+
if not test_build_success:
189+
total_build_success = False
190+
print "Failed to build some tests, check build log for details"
191+
192+
test_builds.update(test_build)
193+
else:
194+
total_build_success = False
195+
196+
# If a path to a test spec is provided, write it to a file
197+
if options.test_spec:
198+
test_spec_data = test_spec_from_test_builds(test_builds)
199+
200+
# Create the target dir for the test spec if necessary
201+
# mkdir will not create the dir if it already exists
202+
test_spec_dir = dirname(options.test_spec)
203+
if test_spec_dir:
204+
mkdir(test_spec_dir)
205+
206+
try:
207+
with open(options.test_spec, 'w') as f:
208+
f.write(json.dumps(test_spec_data, indent=2))
209+
except IOError, e:
210+
print "[ERROR] Error writing test spec to file"
211+
print e
212+
213+
# If a path to a JUnit build report spec is provided, write it to a file
214+
if options.build_report_junit:
215+
report_exporter = ReportExporter(ResultExporterType.JUNIT)
216+
report_exporter.report_to_file(build_report, options.build_report_junit, test_suite_properties=build_properties)
217+
218+
print "\n\nCompleted in: (%.2f)s" % (time() - start)
219+
220+
print_report_exporter = ReportExporter(ResultExporterType.PRINT, package="build")
221+
status = print_report_exporter.report(build_report)
222+
223+
if status:
224+
sys.exit(0)
225+
else:
226+
sys.exit(1)
227+
228+
except KeyboardInterrupt, e:
229+
print "\n[CTRL+c] exit"
230+
except Exception,e:
231+
import traceback
232+
traceback.print_exc(file=sys.stdout)
233+
print "[ERROR] %s" % str(e)
234+
sys.exit(1)

tools/test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
2626
sys.path.insert(0, ROOT)
2727

28-
from tools.test_api import test_path_to_name, find_tests, print_tests, build_tests, test_spec_from_test_build
28+
from tools.test_api import test_path_to_name, find_tests, print_tests, build_tests, test_spec_from_test_builds
2929
from tools.options import get_default_options_parser
3030
from tools.build_api import build_project, build_library
3131
from tools.targets import TARGET_MAP
@@ -162,7 +162,7 @@
162162

163163
# If a path to a test spec is provided, write it to a file
164164
if options.test_spec:
165-
test_spec_data = test_spec_from_test_build(test_build)
165+
test_spec_data = test_spec_from_test_builds(test_build)
166166

167167
# Create the target dir for the test spec if necessary
168168
# mkdir will not create the dir if it already exists

tools/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2089,7 +2089,7 @@ def build_tests(tests, base_source_paths, build_path, target, toolchain_name,
20892089
return result, test_builds
20902090

20912091

2092-
def test_spec_from_test_build(test_builds):
2092+
def test_spec_from_test_builds(test_builds):
20932093
return {
20942094
"builds": test_builds
20952095
}

0 commit comments

Comments
 (0)