-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-arduino-generic.rb
288 lines (251 loc) · 8.03 KB
/
build-arduino-generic.rb
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/ruby
# This program builds the Arduino project in the Current Working Directory.
# If the first argument is an existing directory, it is used instead of the CWD.
#SELF_FILE = File.expand_path __FILE__
require 'stringio'
# This will probably fail on non-Windows platforms.
# TODO: fix.
require 'win32/registry'
require './preprocess.rb'
require './localConfig.rb'
[
:ARDUINO_SDK_DIR,
:ARDUINO_LIB_DIR,
:ARDUINO_COM_PORT,
].each do |const|
if(!Module.const_defined?(const))
raise "localConfig.rb must define #{const}"
end
end
CExe = File.expand_path 'rules/cExe.rb'
if(ARGV[0] && Dir.exist?(ARGV[0]))
Dir.chdir(ARGV[0])
ARGV.delete_at(0)
end
NAME = File.basename(Dir.pwd)
require CExe
if(File.exist?('settings.rb'))
require './settings.rb'
end
def inoFileName
NAME+'.ino'
end
class ArduinoSourceTask < MemoryGeneratedFileTask
def initialize
@prerequisites = [DirTask.new('build/src')]
super('build/src/'+inoFileName+'.cpp') do
@buf = preprocess(IO.read(inoFileName), inoFileName)
end
end
end
# returns an array of FileTasks based on cwd.
def genArduinoSourceTasks
return [ArduinoSourceTask.new]
end
BASIC_ARDUINO_IDIRS = [
ARDUINO_SDK_DIR+'hardware/arduino/'+ARDUINO_ARCHITECTURE+'cores/arduino',
ARDUINO_SDK_DIR+'hardware/arduino/'+ARDUINO_ARCHITECTURE+'variants/standard',
]
# returns an array of strings.
def arduinoIncludeDirctories
patterns = {}
utils = []
idirs = BASIC_ARDUINO_IDIRS.clone
roots = [
ARDUINO_SDK_DIR+'hardware/arduino/'+ARDUINO_ARCHITECTURE+'libraries/',
ARDUINO_SDK_DIR+'libraries/',
ARDUINO_LIB_DIR,
]
addIdir = proc do |idir|
idirs << idir
# As a special addition, arduino libraries may include a "utility" subdirectory.
# If present, this directory will be added as an include directory for that library,
# and its source files will be compiled.
util = idir+'/utility'
if(Dir.exist?(util))
utils << util
if(!patterns[idir])
patterns[Regexp.new(Regexp.escape(idir))] = idirFlags([util])
end
end
end
# from project's settings.rb
if(defined?(LIBRARIES))
LIBRARIES.each do |lib|
found = false
roots.each do |root|
idir = root+lib
if(File.exist?(idir))
found = true
puts "Found library #{lib} in #{idir}"
addIdir.call(idir)
break
end
end
raise "Library #{lib} not found!" if(!found)
end
end
# find directories based on .ino #includes.
# each include must be found in only one of the idirs,
# or its noExtName be in only one of the roots and the include be found in that dir.
count = 0
IO.foreach(inoFileName) do |line|
count += 1
res = /#include *<(.+)>/.match(line)
if(line.include?('#include'))
#puts line
#puts res[1].inspect
end
if(res && res.length != 2)
raise "Error parsing line #{count}"
end
if(res)
include = res[1]
found = false
# search idirs.
idirs.each do |idir|
if(File.exist?(idir+'/'+include))
found = true
puts "Found <#{include}> in #{idir}"
break
end
end
# search roots.
if(!found)
roots.each do |root|
extensionlessName = File.basename(include, File.extname(include))
idir = root+extensionlessName
#puts "test #{idir}"
if(File.exist?(idir) && File.exist?(idir+'/'+include))
found = true
puts "Found <#{include}> in #{idir}"
addIdir.call(idir)
break
end
end
end
# searches failed.
if(!found)
raise "Include <#{include}> not found!"
end
end
end
return idirs, utils, patterns
end
module ArduinoCompilerModule
include GccCompilerModule
def gcc
ARDUINO_SDK_DIR+'hardware\tools\avr\bin\avr-gcc'
end
alias old_setCompilerVersion setCompilerVersion
def setCompilerVersion
default(:TARGET_PLATFORM, :arduino)
oldDir = Dir.pwd
# This is required to access the cygwin dll files in Arduino 1.5.
Dir.chdir(ARDUINO_SDK_DIR)
old_setCompilerVersion
Dir.chdir(oldDir)
end
def linkerName
ARDUINO_SDK_DIR+'hardware\tools\avr\bin\avr-gcc'
end
end
ENV['CYGWIN'] = 'nodosfilewarning'
def idirFlags(idirs)
idirs.reject {|dir| BASIC_ARDUINO_IDIRS.include?(dir)}.collect {|dir| " -I\""+File.expand_path_fix(dir)+'"'}.join
end
class ArduinoWork < ExeWork
def initialize
@TARGET_PLATFORM = :arduino
super(ArduinoCompilerModule) do
@SOURCE_TASKS = genArduinoSourceTasks
@NAME = NAME
idirs, utils, @PATTERN_CFLAGS = arduinoIncludeDirctories
#p @PATTERN_CFLAGS
@SPECIFIC_CFLAGS = {
NAME+'.ino.cpp' => idirFlags(idirs),
# Work around bugs in the Arduino libs.
# TODO: For Fun, disable all these flags and fix the bugs.
'HardwareSerial.cpp' => ' -Wno-sign-compare -Wno-shadow -Wno-unused -Wno-empty-body',
'HardwareSerial0.cpp' => ' -Wno-missing-declarations',
'Print.cpp' => ' -Wno-attributes',
'Tone.cpp' => ' -Wno-shadow -Wno-missing-declarations -Wno-error',
'WMath.cpp' => ' -Wno-missing-declarations -Wno-shadow',
'WString.cpp' => ' -Wno-missing-declarations -Wno-shadow',
'wiring.c' => ' -Wno-old-style-definition',
'wiring_digital.c' => ' -Wno-declaration-after-statement',
'Dns.cpp' => ' -Wno-shadow',
'WiFi.cpp' => ' -Wno-shadow -Wno-undef',
'WiFiClient.cpp' => ' -Wno-undef',
'WiFiServer.cpp' => ' -Wno-shadow -Wno-undef',
'WiFiUdp.cpp' => ' -Wno-shadow -Wno-undef',
'server_drv.cpp' => ' -Wno-undef',
'spi_drv.cpp' => ' -Wno-missing-declarations -Wno-undef -Wno-error',
'wifi_drv.cpp' => ' -Wno-type-limits -Wno-extra -Wno-undef',
'Stream.cpp' => ' -Wno-write-strings',
'hooks.c' => ' -Wno-strict-prototypes -Wno-old-style-definition',
'acilib.cpp' => ' -Wno-switch',
'lib_aci.cpp' => ' -Wno-missing-declarations',
'RBL_nRF8001.cpp' => ' -Wno-missing-declarations -Wno-switch',
}
if(@GCC_V4_SUB >= 8)
@SPECIFIC_CFLAGS['RBL_nRF8001.cpp'] += ' -Wno-missing-declarations -Wno-switch -Wno-suggest-attribute=noreturn'
else
@SPECIFIC_CFLAGS['RBL_nRF8001.cpp'] += ' -Wno-error'
@SPECIFIC_CFLAGS['hal_aci_tl.cpp'] = ' -Wno-error'
end
@SOURCES = idirs + utils
@EXTRA_INCLUDES = BASIC_ARDUINO_IDIRS + idirs
@EXTRA_LINKFLAGS = ' -Os -Wl,--gc-sections -mmcu=atmega328p'
end
# Once this object is properly constructed, we can create the ones that depend on it.
elf = self
ShellTask.new(@BUILDDIR+NAME+'.eep', [elf],
"\"#{ARDUINO_SDK_DIR}hardware/tools/avr/bin/avr-objcopy\" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load"+
" --no-change-warnings --change-section-lma .eeprom=0 \"#{elf}\" \"#{@BUILDDIR+NAME+'.eep'}\"")
@hexFile = ShellTask.new(@BUILDDIR+NAME+'.hex', [elf],
"\"#{ARDUINO_SDK_DIR}hardware/tools/avr/bin/avr-objcopy\" -O ihex -R .eeprom \"#{elf}\" \"#{@BUILDDIR+NAME+'.hex'}\"")
end
def hexFile
@hexFile
end
def targetName
return CCompileTask.genFilename(@COMMON_EXE ? @COMMON_BUILDDIR : @BUILDDIR, @NAME, '.elf')
end
end
work = ArduinoWork.new
def findDefaultComPort
found = nil
# This will definitely fail on non-Windows platforms.
# TODO: fix.
# This function is heuristical and may need modification in the future.
# Investigation has shows that Windows stores the numbers of all active serial ports in this Registry key.
# Arduino boards are usually connected by USB and appear to have a name starting with '\Device\USBSER'.
# It is, as yet, unknown how far this will hold true.
Win32::Registry::HKEY_LOCAL_MACHINE.open('HARDWARE\DEVICEMAP\SERIALCOMM') do |reg|
reg.each do |name, type, value|
if(name.start_with?('\Device\USBSER'))
raise "Multiple default COM ports found! Unplug all but one, or choose manually." if(found)
found = value
end
end
end
raise "No appropriate default COM port found! Plug in an Arduino unit, or choose manually." if(!found)
return found
end
def selectComPort
if(ARDUINO_COM_PORT == :default)
return findDefaultComPort
else
return "COM#{ARDUINO_COM_PORT}"
end
end
target :run do
sh "\"#{ARDUINO_SDK_DIR}hardware/tools/avr/bin/avrdude\" \"-C#{ARDUINO_SDK_DIR}hardware/tools/avr/etc/avrdude.conf\""+
" -V -patmega328p -carduino -P\\\\.\\#{selectComPort} -b115200 -D \"-Uflash:w:#{work.hexFile}:i\""
end
oldDir = Dir.pwd
# This is required to access the cygwin dll files in Arduino 1.5.
Dir.chdir(ARDUINO_SDK_DIR)
Works.run
Dir.chdir(oldDir)