forked from lancaster-university/microbit-v2-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·171 lines (131 loc) · 6.8 KB
/
build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
# The MIT License (MIT)
# Copyright (c) 2017 Lancaster University.
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
import os
import sys
import optparse
import platform
import json
import shutil
import re
from utils.python.codal_utils import system, build, read_json, checkgit, read_config, update, revision, printstatus, status, get_next_version, lock, delete_build_folder, generate_docs
parser = optparse.OptionParser(usage="usage: %prog target-name-or-url [options]", description="This script manages the build system for a codal device. Passing a target-name generates a codal.json for that devices, to list all devices available specify the target-name as 'ls'.")
parser.add_option('-c', '--clean', dest='clean', action="store_true", help='Whether to clean before building. Applicable only to unix based builds.', default=False)
parser.add_option('-t', '--test-platforms', dest='test_platform', action="store_true", help='Specify whether the target platform is a test platform or not.', default=False)
parser.add_option('-l', '--lock', dest='lock_target', action="store_true", help='Create target-lock.json, updating patch version', default=False)
parser.add_option('-b', '--branch', dest='branch', action="store_true", help='With -l, use vX.X.X-BRANCH.Y', default=False)
parser.add_option('-m', '--minor', dest='update_minor', action="store_true", help='With -l, update minor version', default=False)
parser.add_option('-M', '--major', dest='update_major', action="store_true", help='With -l, update major version', default=False)
parser.add_option('-V', '--version', dest='version', metavar="VERSION", help='With -l, set the version; use "-V v0.0.1" to bootstrap', default=False)
parser.add_option('-v', '--verbose', dest='verbose', action="store_true", help='Increases verbosity)', default=False)
parser.add_option('-u', '--update', dest='update', action="store_true", help='git pull target and libraries, use with "-d/--dev" to update all libraries to their latest master' , default=False)
parser.add_option('-s', '--status', dest='status', action="store_true", help='git status target and libraries', default=False)
parser.add_option('-r', '--revision', dest='revision', action="store", help='Checkout a specific revision of the target', default=False)
parser.add_option('-d', '--dev', dest='dev', action="store_true", help='enable developer mode (does not use target-locked.json)', default=False)
parser.add_option('-g', '--generate-docs', dest='generate_docs', action="store_true", help='generate documentation for the current target', default=False)
parser.add_option('-j', '--parallelism', dest='parallelism', action="store", help='Set the number of parallel threads to build with, if supported', default=10)
parser.add_option('-n', '--lines', dest='detail_lines', action="store", help="Sets the number of detail lines to output (only relevant to --status)", default=3 )
(options, args) = parser.parse_args()
if not os.path.exists("build"):
os.mkdir("build")
if options.lock_target:
lock(options)
exit(0)
if options.update:
update(sync_dev = options.dev)
exit(0)
if options.status:
status(logLines=options.detail_lines, detail=options.verbose, libs=args)
exit(0)
if options.revision:
revision(options.revision)
exit(0)
# out of source build!
os.chdir("build")
test_json = read_json("../utils/targets.json")
# configure the target a user has specified:
if len(args) == 1:
target_name = args[0]
target_config = None
# list all targets
if target_name == "ls":
for json_obj in test_json:
s = "%s: %s" % (json_obj["name"], json_obj["info"])
if "device_url" in json_obj.keys():
s += "(%s)" % json_obj["device_url"]
print(s)
exit(0)
# cycle through out targets and check for a match
for json_obj in test_json:
if json_obj["name"] != target_name:
continue
del json_obj["device_url"]
del json_obj["info"]
target_config = json_obj
break
if target_config == None and target_name.startswith("http"):
target_config = {
"name": re.sub("^.*/", "", target_name),
"url": target_name,
"branch": "master",
"type": "git"
}
if target_config == None:
print("'" + target_name + "'" + " is not a valid target.")
exit(1)
# developer mode is for users who wish to contribute, it will clone and checkout commitable branches.
if options.dev:
target_config["dev"] = True
config = {
"target":target_config
}
with open("../codal.json", 'w') as codal_json:
json.dump(config, codal_json, indent=4)
# remove the build folder, a user could be swapping targets.
delete_build_folder()
elif len(args) > 1:
print("Too many arguments supplied, only one target can be specified.")
exit(1)
if not options.test_platform:
if not os.path.exists("../codal.json"):
print("No target specified in codal.json, does codal.json exist?")
exit(1)
if options.generate_docs:
generate_docs()
exit(0)
build(options.clean, verbose=options.verbose, parallelism=options.parallelism)
exit(0)
for json_obj in test_json:
# some platforms aren't supported by travis, ignore them when testing.
if "test_ignore" in json_obj:
print("ignoring: " + json_obj["name"])
continue
# ensure we have a clean build tree.
delete_build_folder()
# clean libs
if os.path.exists("../libraries"):
shutil.rmtree('../libraries')
# configure the target and tests...
config = {
"target":json_obj,
"output":".",
"application":"libraries/"+json_obj["name"]+"/tests/"
}
with open("../codal.json", 'w') as codal_json:
json.dump(config, codal_json, indent=4)
build(True, True, options.parallelism)