-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwscript
161 lines (137 loc) · 4.55 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
159
160
161
#!/usr/bin/env python
from waflib.Build import BuildContext
from waflib.TaskGen import feature, extension, after, task_gen
from waflib.Tools import ccroot
APPNAME = 'scribble'
VERSION = '0.01'
top = '.'
out = 'out'
apps = ['lcdshield', 'nokia']
_DISTROOT = '/Applications/Arduino.app/Contents/Resources/Java'
_HARDWARE = _DISTROOT + '/hardware'
_LIBRARIES = _DISTROOT + '/libraries'
_AVR = _HARDWARE + '/tools/avr'
_AVR_BIN = _HARDWARE + '/tools/avr/bin/%s'
_CORE_LIB = _HARDWARE + '/arduino/cores/arduino/'
_PLATFORM_LIB = _HARDWARE + '/arduino/variants/standard/'
_DIALECT = ['-Wall', '-Wextra', '-ansi', '-Wno-uninitialized', '-Wno-unused-parameter']
_BAUD = '115200'
_PROGRAMMER = 'arduino'
def configure(conf):
conf.load('compiler_cxx')
conf.env['PORT'] = conf.options.port
conf.env['APP'] = conf.options.app
def options(opt):
opt.load('compiler_cxx')
opt.add_option('--device',
default = 'atmega328p',
action = 'store',
help = 'the type of device we\'re building for')
opt.add_option('--port',
default = None,
action = 'store',
help = 'which port to flash (typically /dev/tty.usbmodem...')
opt.add_option('--app',
default = apps[0],
action = 'store',
help = 'which app to flash')
opt.add_option('--clock',
default = '16000000',
action = 'store',
help = 'clock frequency of the device')
# Wrapper around a waf builder thingy that adds some useful utilities.
# That way the nastiness is encapsulated here and the build rule can
# be cleaner.
class BuildHelper(object):
_TOOLS = {
'CXX': 'avr-g++',
'LINK_CXX': 'avr-g++',
'OBJCOPY': 'avr-objcopy',
'AVRDUDE': 'avrdude'
}
def __init__(self, build):
self.build = build
# Defines a program; just passes the call on to the builder.
def program(self, **kwds):
self.build.program(**kwds)
# Overrides the default toolchain with the AVR one.
def configure_toolchain(self):
for name, tool in BuildHelper._TOOLS.items():
self.build.env[name] = _AVR_BIN % tool
# Pulls the eeprom part out of the source binary and stores the rest
# in a separate hex file.
def remove_eeprom(self, source, target):
self.build.new_task_gen(
source = source,
target = target,
rule = '${OBJCOPY} -O ihex -R .eeprom -R .fuse -R .lock ${SRC} ${TGT}'
)
# Pulls the eeprom part out of the source binary and stores it in a
# separate hex file.
def extract_eeprom(self, source, target):
flags = "-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0"
self.build.new_task_gen(
source = source,
target = target,
rule = '${OBJCOPY} %s ${SRC} ${TGT}' % flags
)
def flash(self, progmem, eeprom):
port = self.get_port()
if not port:
raise AssertionError("No --port specified.")
self.build.new_task_gen(
source = progmem,
depends = eeprom,
rule = '${AVRDUDE} -C%(avr)s/etc/avrdude.conf -v -v -v -v -p%(device)s -c%(programmer)s -P%(port)s -b%(baud)s -D -Uflash:w:${SRC}:i' % {
'avr': _AVR,
'device': self.get_device(),
'port': port,
'baud': _BAUD,
'programmer': _PROGRAMMER
},
always = True
)
def get_port(self):
options = self.build.options
if options.port:
return options.port
else:
return self.build.env['PORT']
def get_app(self):
options = self.build.options
if options.app:
return options.app
else:
return self.build.env['APP']
def get_clock(self):
return self.build.options.clock
def get_device(self):
return self.build.options.device
def build_sources(self):
self.configure_toolchain()
lib_sources = self.build.path.ant_glob('src/lib/**/*.(cc|cpp|c)')
for app in apps:
app_sources = ['src/%s.cc' % app]
includes = [_CORE_LIB, _PLATFORM_LIB]
elf_file = '%s.elf' % app
self.program(
source = lib_sources + app_sources,
target = elf_file,
includes = includes,
cxxflags = ['-Os', '-mmcu=%s' % self.get_device()] + _DIALECT,
linkflags = ['-Os', '-mmcu=%s' % self.get_device(), '-Wl,--gc-sections'],
defines = ['F_CPU=%sL' % self.get_clock()]
)
self.remove_eeprom(elf_file, '%s.hex' % app)
self.extract_eeprom(elf_file, '%s.eep' % app)
def build(bld):
helper = BuildHelper(bld)
helper.build_sources()
def flash(bld):
helper = BuildHelper(bld)
app = helper.get_app()
helper.build_sources()
helper.flash('%s.hex' % app, '%s.eep' % app)
class FlashContext(BuildContext):
cmd = 'flash'
fun = 'flash'