forked from yaoyuyang/CatchOneBus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wscript
158 lines (126 loc) · 5.15 KB
/
wscript
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
import json
import os
import re
from waflib.Configure import conf
top = '.'
out = 'build'
def options(ctx):
ctx.load('pebble_sdk')
def configure(ctx):
ctx.load('pebble_sdk')
if ctx.env.TARGET_PLATFORMS:
for platform in ctx.env.TARGET_PLATFORMS:
ctx.configure_platform(platform)
else:
ctx.configure_platform()
def build(ctx):
ctx.load('pebble_sdk')
binaries = []
js_target = ctx.concat_javascript(js_path='src/js')
if ctx.env.TARGET_PLATFORMS:
for platform in ctx.env.TARGET_PLATFORMS:
ctx.build_platform(platform, binaries=binaries)
ctx.pbl_bundle(binaries=binaries,
js=js_target)
else:
ctx.env.BUILD_DIR = 'aplite'
ctx.build_platform(binaries=binaries)
elfs = binaries[0]
ctx.pbl_bundle(elf=elfs['app_elf'],
worker_elf=elfs['worker_elf'] if 'worker_elf' in elfs else None,
js=js_target)
@conf
def configure_platform(ctx, platform=None):
if platform is not None:
ctx.setenv(platform, ctx.all_envs[platform])
cflags = ctx.env.CFLAGS
cflags = [ x for x in cflags if not x.startswith('-std=') ]
cflags.extend(['-std=c11',
'-fms-extensions',
'-Wno-address',
'-Wno-type-limits',
'-Wno-missing-field-initializers'])
ctx.env.CFLAGS = cflags
@conf
def build_platform(ctx, platform=None, binaries=None):
if platform is not None:
ctx.set_env(ctx.all_envs[platform])
build_worker = os.path.exists('worker_src')
app_elf='{}/pebble-app.elf'.format(ctx.env.BUILD_DIR)
ctx.pbl_program(source=ctx.path.ant_glob('src/**/*.c'),
target=app_elf)
if build_worker:
worker_elf='{}/pebble-worker.elf'.format(ctx.env.BUILD_DIR)
binaries.append({'platform': platform, 'app_elf': app_elf, 'worker_elf': worker_elf})
ctx.pbl_worker(source=ctx.path.ant_glob('worker_src/**/*.c'),
target=worker_elf)
else:
binaries.append({'platform': platform, 'app_elf': app_elf})
@conf
def concat_javascript(ctx, js_path=None):
js_nodes = (ctx.path.ant_glob(js_path + '/**/*.js') +
ctx.path.ant_glob(js_path + '/**/*.json') +
ctx.path.ant_glob(js_path + '/**/*.coffee'))
if not js_nodes:
return []
def concat_javascript_task(task):
LOADER_PATH = "loader.js"
LOADER_TEMPLATE = ("__loader.define({relpath}, {lineno}, " +
"function(exports, module, require) {{\n{body}\n}});")
JSON_TEMPLATE = "module.exports = {body};"
APPINFO_PATH = "appinfo.json"
def loader_translate(source, lineno):
return LOADER_TEMPLATE.format(
relpath=json.dumps(source['relpath']),
lineno=lineno,
body=source['body'])
def coffeescript_compile(relpath, body):
try:
import coffeescript
except ImportError:
ctx.fatal("""
CoffeeScript file '%s' found, but coffeescript module isn't installed.
You may try `pip install coffeescript` or `easy_install coffeescript`.
""" % (relpath))
body = coffeescript.compile(body)
# change ".coffee" or ".js.coffee" extensions to ".js"
relpath = re.sub('(\.js)?\.coffee$', '.js', relpath)
return relpath, body
sources = []
for node in task.inputs:
relpath = os.path.relpath(node.abspath(), js_path)
with open(node.abspath(), 'r') as f:
body = f.read()
if relpath.endswith('.json'):
body = JSON_TEMPLATE.format(body=body)
elif relpath.endswith('.coffee'):
relpath, body = coffeescript_compile(relpath, body)
compiled_js_path = os.path.join(out, js_path, relpath)
compiled_js_dir = os.path.dirname(compiled_js_path)
if not os.path.exists(compiled_js_dir):
os.makedirs(compiled_js_dir)
with open(compiled_js_path, 'w') as f:
f.write(body)
if relpath == LOADER_PATH:
sources.insert(0, body)
else:
sources.append({ 'relpath': relpath, 'body': body })
with open(APPINFO_PATH, 'r') as f:
body = JSON_TEMPLATE.format(body=f.read())
sources.append({ 'relpath': APPINFO_PATH, 'body': body })
sources.append('__loader.require("main");')
with open(task.outputs[0].abspath(), 'w') as f:
lineno = 1
for source in sources:
if type(source) is dict:
body = loader_translate(source, lineno)
else:
body = source
f.write(body + '\n')
lineno += body.count('\n') + 1
js_target = ctx.path.make_node('build/src/js/pebble-js-app.js')
ctx(rule=concat_javascript_task,
source=js_nodes,
target=js_target)
return js_target
# vim:filetype=python