Update readme
[clinton/Smoothieware.git] / Rakefile
CommitLineData
29089d0c
JM
1require 'rake'
2require 'pathname'
3require 'fileutils'
4
4c8f5447
JM
5verbose(ENV['verbose'] == '1')
6DEBUG = ENV['debug'] == '1'
93ea6adb 7TESTING = ENV['testing'] == '1'
4c8f5447 8
29089d0c
JM
9def pop_path(path)
10 Pathname(path).each_filename.to_a[1..-1]
11end
12
13def obj2src(fn, e)
14 File.join('src', pop_path(File.dirname(fn)), File.basename(fn).ext(e))
15end
16
17def is_windows?
18 (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
19end
20
21# Makefile .d file loader to be used with the import file loader.
22# this emulates the -include $(DEPFILES) in a Makefile for the generated .d files
23class DfileLoader
24 include Rake::DSL
25
26 SPACE_MARK = "\0"
27
28 # Load the makefile dependencies in +fn+.
29 def load(fn)
30 return if ! File.exists?(fn)
31 lines = File.read fn
32 lines.gsub!(/\\ /, SPACE_MARK)
33 lines.gsub!(/#[^\n]*\n/m, "")
34 lines.gsub!(/\\\n/, ' ')
35 lines.each_line do |line|
36 process_line(line)
37 end
38 end
39
40 private
41
42 # Process one logical line of makefile data.
43 def process_line(line)
44 file_tasks, args = line.split(':', 2)
45 return if args.nil?
46 dependents = args.split.map { |d| respace(d) }
47 file_tasks.scan(/\S+/) do |file_task|
48 file_task = respace(file_task)
49 file file_task => dependents
50 end
51 end
52
53 def respace(str)
54 str.tr SPACE_MARK, ' '
55 end
56end
57
58# Install the handler
59Rake.application.add_loader('d', DfileLoader.new)
60
61PROG = 'smoothie'
62
63DEVICE = 'LPC1768'
64ARCHITECTURE = 'armv7-m'
65
66MBED_DIR = './mbed/drop'
67
68TOOLSBIN = './gcc-arm-none-eabi/bin/arm-none-eabi-'
69CC = "#{TOOLSBIN}gcc"
70CCPP = "#{TOOLSBIN}g++"
71LD = "#{TOOLSBIN}g++"
72OBJCOPY = "#{TOOLSBIN}objcopy"
73SIZE = "#{TOOLSBIN}size"
74
75# include a defaults file if present
76load 'rakefile.defaults' if File.exists?('rakefile.defaults')
93ea6adb
JM
77if TESTING
78 BUILDTYPE= 'Testing'
29089d0c 79
93ea6adb 80elsif DEBUG
4c8f5447
JM
81 BUILDTYPE= 'Debug'
82 ENABLE_DEBUG_MONITOR= '0'
83end
84
88443c6b
JM
85# Set build type
86BUILDTYPE= ENV['BUILDTYPE'] || 'Checked' unless defined? BUILDTYPE
87puts "#{BUILDTYPE} build"
88
89ENABLE_DEBUG_MONITOR = ENV['ENABLE_DEBUG_MONITOR'] || '0' unless defined? ENABLE_DEBUG_MONITOR
90
91# set default baud rate
92DEFAULT_SERIAL_BAUD_RATE= ENV['BAUDRATE'] || '115200' unless defined? DEFAULT_SERIAL_BAUD_RATE
93
29089d0c 94# set to true to eliminate all the network code
93ea6adb
JM
95unless defined? NONETWORK
96 NONETWORK= false || TESTING
97end
29089d0c
JM
98
99# list of modules to exclude, include directory it is in
100EXCLUDE_MODULES= %w(tools/touchprobe) unless defined? EXCLUDE_MODULES
101
102# e.g for a CNC machine
103#EXCLUDE_MODULES = %w(tools/touchprobe tools/laser tools/temperaturecontrol tools/extruder)
104
105# generate regex of modules to exclude and defines
106exclude_defines, excludes = EXCLUDE_MODULES.collect { |e| [e.tr('/', '_').upcase, e.sub('/', '\/')] }.transpose
107
108# see if network is enabled
109if ENV['NONETWORK'] || NONETWORK
110 nonetwork= true
111 excludes << '\/libs\/Network\/'
112 puts "Excluding Network code"
113else
114 nonetwork= false
115end
116
93ea6adb
JM
117if TESTING
118 # add modules to be tested here
119 TESTMODULES= %w(tools/temperatureswitch)
120 puts "Modules under test: #{TESTMODULES}"
121 excludes << %w(Kernel.cpp main.cpp) # we replace these with mock versions in testframework
122 testmodules= FileList['src/testframework/*.{c,cpp}', 'src/testframework/easyunit/*.{c,cpp}', 'src/modules/communication/SerialConsole.cpp', 'src/modules/communication/utils/Gcode.cpp'].include(TESTMODULES.collect { |e| "src/modules/#{e}/**/*.{c,cpp}"}).include(TESTMODULES.collect { |e| "src/testframework/unittests/#{e}/*.{c,cpp}"})
123 SRC = FileList['src/libs/**/*.{c,cpp}'].exclude(/#{excludes.join('|')}/) + testmodules
124else
125 excludes << %w(testframework)
126 SRC = FileList['src/**/*.{c,cpp}'].exclude(/#{excludes.join('|')}/)
127 puts "WARNING Excluding modules: #{EXCLUDE_MODULES.join(' ')}" unless exclude_defines.empty?
128end
29089d0c
JM
129
130OBJDIR = 'OBJ'
131OBJ = SRC.collect { |fn| File.join(OBJDIR, pop_path(File.dirname(fn)), File.basename(fn).ext('o')) } +
132 %W(#{OBJDIR}/configdefault.o #{OBJDIR}/mbed_custom.o)
133
134# list of header dependency files generated by compiler
135DEPFILES = OBJ.collect { |fn| File.join(File.dirname(fn), File.basename(fn).ext('d')) }
136
137# create destination directories
138SRC.each do |s|
139 d= File.join(OBJDIR, pop_path(File.dirname(s)))
140 FileUtils.mkdir_p(d) unless Dir.exists?(d)
141end
142
143INCLUDE_DIRS = [Dir.glob(['./src/**/', './mri/**/'])].flatten
144MBED_INCLUDE_DIRS = %W(#{MBED_DIR}/ #{MBED_DIR}/LPC1768/)
145
146INCLUDE = (INCLUDE_DIRS+MBED_INCLUDE_DIRS).collect { |d| "-I#{d}" }.join(" ")
147
88443c6b
JM
148if ENABLE_DEBUG_MONITOR == '1'
149 # Can add MRI_UART_BAUD=115200 to next line if GDB fails to connect to MRI.
150 # Tends to happen on some Linux distros but not Windows and OS X.
151 MRI_UART = 'MRI_UART_0'
152 MRI_BREAK_ON_INIT = 1 unless defined? MRI_BREAK_ON_INIT
153else
154 MRI_UART = 'MRI_UART_0 MRI_UART_SHARE'
155 MRI_BREAK_ON_INIT = 0 unless defined? MRI_BREAK_ON_INIT
156end
157
158# Configure MRI variables based on BUILD_TYPE build type variable.
159case BUILDTYPE.downcase
160when 'release'
161 OPTIMIZATION = 2
162 MRI_ENABLE = 0
163 MRI_SEMIHOST_STDIO = 0 unless defined? MRI_SEMIHOST_STDIO
164when 'debug'
165 OPTIMIZATION = 0
166 MRI_ENABLE = 1
167 MRI_SEMIHOST_STDIO = 1 unless defined? MRI_SEMIHOST_STDIO
168when 'checked'
169 OPTIMIZATION = 2
170 MRI_ENABLE = 1
171 MRI_SEMIHOST_STDIO = 1 unless defined? MRI_SEMIHOST_STDIO
93ea6adb
JM
172when 'testing'
173 OPTIMIZATION = 0
174 MRI_ENABLE = 1
175 MRI_SEMIHOST_STDIO = 0 unless defined? MRI_SEMIHOST_STDIO
88443c6b
JM
176end
177
178MRI_ENABLE = 1 unless defined? MRI_ENABLE # set to 0 to disable MRI
179MRI_LIB = MRI_ENABLE == 1 ? './mri/mri.ar' : ''
29089d0c 180MBED_LIB = "#{MBED_DIR}/LPC1768/GCC_ARM/libmbed.a"
88443c6b 181
29089d0c
JM
182SYS_LIBS = '-lstdc++_s -lsupc++_s -lm -lgcc -lc_s -lgcc -lc_s -lnosys'
183LIBS = [MBED_LIB, SYS_LIBS, MRI_LIB].join(' ')
184
88443c6b 185MRI_DEFINES = %W(-DMRI_ENABLE=#{MRI_ENABLE} -DMRI_INIT_PARAMETERS='"#{MRI_UART}"' -DMRI_BREAK_ON_INIT=#{MRI_BREAK_ON_INIT} -DMRI_SEMIHOST_STDIO=#{MRI_SEMIHOST_STDIO})
29089d0c
JM
186
187defines = %w(-DCHECKSUM_USE_CPP -D__LPC17XX__ -DTARGET_LPC1768 -DWRITE_BUFFER_DISABLE=0 -DSTACK_SIZE=3072 -DCHECKSUM_USE_CPP)
188defines += exclude_defines.collect{|d| "-DNO_#{d}"}
189defines += MRI_DEFINES
88443c6b
JM
190defines << "-DDEFAULT_SERIAL_BAUD_RATE=#{DEFAULT_SERIAL_BAUD_RATE}"
191defines << '-DDEBUG' if OPTIMIZATION == 0
29089d0c
JM
192defines << '-DNONETWORK' if nonetwork
193
194DEFINES= defines.join(' ')
195
196# Compiler flags used to enable creation of header dependencies.
197DEPFLAGS = '-MMD '
93ea6adb
JM
198CFLAGS = DEPFLAGS + "-Wall -Wextra -Wno-unused-parameter -Wcast-align -Wpointer-arith -Wredundant-decls -Wcast-qual -Wcast-align -O#{OPTIMIZATION} -g3 -mcpu=cortex-m3 -mthumb -mthumb-interwork -ffunction-sections -fdata-sections -fno-delete-null-pointer-checks"
199CPPFLAGS = CFLAGS + ' -fno-rtti -std=gnu++11 -fno-exceptions'
200CXXFLAGS = CFLAGS + ' -fno-rtti -std=gnu++11 -fexceptions' # used for a .cxx file that needs to be compiled with exceptions
29089d0c 201
88443c6b 202MRI_WRAPS = MRI_ENABLE == 1 ? ',--wrap=_read,--wrap=_write,--wrap=semihost_connected' : ''
29089d0c
JM
203
204# Linker script to be used. Indicates what code should be placed where in memory.
205LSCRIPT = "#{MBED_DIR}/LPC1768/GCC_ARM/LPC1768.ld"
206LDFLAGS = "-mcpu=cortex-m3 -mthumb -specs=./build/startfile.spec" +
207 " -Wl,-Map=#{OBJDIR}/smoothie.map,--cref,--gc-sections,--wrap=_isatty,--wrap=malloc,--wrap=realloc,--wrap=free" +
208 MRI_WRAPS +
209 " -T#{LSCRIPT}" +
210 " -u _scanf_float -u _printf_float"
211
212HTTPD_FSDATA = './src/libs/Network/uip/webserver/httpd-fsdata2.h'
213
214# tasks
215
216# generate the header dependencies if they exist
217import(*DEPFILES)
218
219task :clean do
220 FileUtils.rm_rf(OBJDIR)
221end
222
223task :realclean => [:clean] do
224 sh "cd ./mbed;make clean"
225end
226
227desc "Build the built-in web pages"
228task :webui => [HTTPD_FSDATA]
229
230task :default => [:build]
231
232task :build => [MBED_LIB, :version, "#{PROG}.bin", :size]
233
234task :version do
235 if is_windows?
236 VERSION = ' -D__GITVERSIONSTRING__=\"place-holder\"'
237 else
238 v1= `git symbolic-ref HEAD 2> /dev/null`
239 v2= `git log --pretty=format:%h -1`
240 VERSION = ' -D__GITVERSIONSTRING__=\"' + "#{v1[11..-1].chomp}-#{v2}".chomp + '\"'
241 FileUtils.touch './src/version.cpp' # we want it compiled everytime
242 end
243end
244
245desc "Upload via DFU"
246task :upload do
247 sh "dfu-util -R -d 1d50:6015 -D #{OBJDIR}/#{PROG}.bin"
248end
249
250task :size do
251 sh "#{SIZE} #{OBJDIR}/#{PROG}.elf"
252end
253
254# build internal web page
255WEB_SOURCE_FILES= FileList['./src/libs/Network/uip/webserver/httpd-fs-src/**/*']
256WEBDIR = './src/libs/Network/uip/webserver/httpd-fs'
257file HTTPD_FSDATA => WEB_SOURCE_FILES do
258 FileUtils.rm_rf WEBDIR
259 FileUtils.mkdir WEBDIR
260 FileUtils.cp WEB_SOURCE_FILES, WEBDIR
261 # TODO minify some files
262 # sh 'java -jar yuicompressor-2.4.8.jar file -o file'
263 sh 'cd ./src/libs/Network/uip/webserver; perl makefsdata.pl'
264 sh 'cd ./src; make'
265end
266
267file MBED_LIB do
268 puts "Building Mbed Using Make"
269 sh "cd ./mbed;make all"
270end
271
272file "#{OBJDIR}/mbed_custom.o" => ['./build/mbed_custom.cpp'] do |t|
93ea6adb 273 puts "Compiling mbed_custom.cpp"
29089d0c
JM
274 sh "#{CCPP} #{CPPFLAGS} #{INCLUDE} #{DEFINES} -c -o #{t.name} #{t.prerequisites[0]}"
275end
276
277file "#{OBJDIR}/configdefault.o" => 'src/config.default' do |t|
278 sh "cd ./src; ../#{OBJCOPY} -I binary -O elf32-littlearm -B arm --readonly-text --rename-section .data=.rodata.configdefault config.default ../#{OBJDIR}/configdefault.o"
279end
280
281file "#{PROG}.bin" => ["#{PROG}.elf"] do
282 sh "#{OBJCOPY} -O binary #{OBJDIR}/#{PROG}.elf #{OBJDIR}/#{PROG}.bin"
283end
284
285file "#{PROG}.elf" => OBJ do |t|
286 puts "Linking #{t.source}"
287 sh "#{LD} #{LDFLAGS} #{OBJ} #{LIBS} -o #{OBJDIR}/#{t.name}"
288end
289
290#arm-none-eabi-objcopy -R .stack -O ihex ../LPC1768/main.elf ../LPC1768/main.hex
291#arm-none-eabi-objdump -d -f -M reg-names-std --demangle ../LPC1768/main.elf >../LPC1768/main.disasm
292
293rule '.o' => lambda{ |objfile| obj2src(objfile, 'cpp') } do |t|
294 puts "Compiling #{t.source}"
295 sh "#{CCPP} #{CPPFLAGS} #{INCLUDE} #{DEFINES} #{VERSION} -c -o #{t.name} #{t.source}"
296end
297
93ea6adb
JM
298rule '.o' => lambda{ |objfile| obj2src(objfile, 'cxx') } do |t|
299 puts "Compiling #{t.source}"
300 sh "#{CCPP} #{CXXFLAGS} #{INCLUDE} #{DEFINES} #{VERSION} -c -o #{t.name} #{t.source}"
301end
302
29089d0c
JM
303rule '.o' => lambda{ |objfile| obj2src(objfile, 'c') } do |t|
304 puts "Compiling #{t.source}"
305 sh "#{CC} #{CFLAGS} #{INCLUDE} #{DEFINES} #{VERSION} -c -o #{t.name} #{t.source}"
306end
307